dragndrop.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function DragScope(scope) {
  2. function calcShift()
  3. {
  4. sizeX = $svg[0].viewBox.baseVal.width / innerWidth;
  5. sizeY = $svg[0].viewBox.baseVal.height / innerHeight;
  6. $page = $("#Page-1");
  7. sizeX = $page[0].width / innerWidth;
  8. sizeY = $page[0].height / innerHeight;
  9. size = Math.max(sizeX, sizeY) || 1;
  10. }
  11. function Draggable(root, options)
  12. {
  13. options = options || {},
  14. this.$el = $(root),
  15. this.revert = options.revert,
  16. this.elements = {},
  17. this.proxies = [ "onDown", "onMove", "onUp" ], this.__super__.constructor.call(this),
  18. this.enable();
  19. }
  20. var sizeX,
  21. sizeY,
  22. size,
  23. moved;
  24. $svg = $("svg");
  25. calcShift();
  26. $(window).on("resize", calcShift);
  27. $(window).on("orientationchange", calcShift);
  28. $.inherit(Draggable, scope.Controller);
  29. $.extend(Draggable.prototype, {
  30. storeTrf: function() {
  31. var trf = this.$el.attr("transform");
  32. this.initTrf = trf ? trf.slice(10, -1).split(/\s+/) : [ 0, 0 ];
  33. },
  34. disable: function() {
  35. this.$el.off(document.createTouch ? "touchstart" : "mousedown", this.onDown),
  36. this.$el.css({cursor: "default"});
  37. },
  38. enable: function() {
  39. this.$el.on(document.createTouch ? "touchstart" : "mousedown", this.onDown),
  40. this.$el.attr("style", "cursor: -moz-grab; cursor: -webkit-grab; cursor: grab;");
  41. },
  42. onDown: function(e, silent) {
  43. e.preventDefault(), moved || (moved = !1, this.$el.attr("style", "cursor: -moz-grabbing; cursor: -webkit-grabbing; cursor: grabbing;"),
  44. this.$el[0].parentNode.appendChild(this.$el[0]), this.x = this.clientX(e), this.y = this.clientY(e),
  45. this.trf = this.$el.attr("transform"), this.trf && (this.trf = this.trf.slice(10, -1).split(/\s+/)),
  46. this.storeTrf(), silent || (document.createTouch ? (this.$el.on("touchmove", this.onMove),
  47. this.$el.on("touchend", this.onUp)) : ($("body").on("mousemove", this.onMove), $("body").on("mouseup", this.onUp)),
  48. this.$el.trigger("dragstart")));
  49. },
  50. onMove: function(e, silent) {
  51. e.preventDefault(), moved = !0, this.dx = ((this.curX = this.clientX(e)) - this.x) * size,
  52. this.dy = ((this.curY = this.clientY(e)) - this.y) * size, this.trf && (this.dx += 0 | this.trf[0],
  53. this.dy += 0 | this.trf[1]), this.$el.attr("transform", "translate(" + this.dx + " " + this.dy + ")"),
  54. silent || this.$el.trigger("dragmove", {
  55. detail: {
  56. x: this.curX,
  57. y: this.curY,
  58. event: e
  59. }
  60. });
  61. },
  62. onUp: function(e, silent) {
  63. if (e.preventDefault(), this.$el.attr("style", "cursor: -moz-grab; cursor: -webkit-grab; cursor: grab;"),
  64. moved) {
  65. for (var droped, item, dropList = scope.Droppable.list, i = 0, l = dropList.length; l > i; i++) if (item = dropList[i],
  66. item.intersect(this.curX, this.curY)) {
  67. droped = item.drop(this, this.curX, this.curY);
  68. break;
  69. }
  70. droped || (this.$el.transform({
  71. from: [ this.dx, this.dy ].join(" "),
  72. to: this.initTrf.join(" ")
  73. }), silent || this.$el.trigger("revert")), silent || this.$el.trigger("dragstop", {
  74. detail: {
  75. x: this.curX,
  76. y: this.curY,
  77. event: e
  78. }
  79. }), moved = !1;
  80. }
  81. document.createTouch ? (this.$el.off("touchmove", this.onMove), this.$el.off("touchend", this.onUp)) : ($("body").off("mousemove", this.onMove),
  82. $("body").off("mouseup", this.onUp));
  83. }
  84. });
  85. scope.Draggable = Draggable;
  86. $.mixin({ draggable: function() { return this.each(function(el) { new Draggable(el); }); } });
  87. }
  88. function DropScope(scope) {
  89. function Droppable(root, options)
  90. {
  91. options = options || {},
  92. this.$el = $(root),
  93. this.accept = options.accept || function() { return !0; },
  94. this.onDrop = options.drop || function() {};
  95. this.elements = {};
  96. this.proxies = [];
  97. this.__super__.constructor.call(this);
  98. this.activate();
  99. }
  100. Droppable.list = [];
  101. $.inherit(Droppable, scope.Controller), $.extend(Droppable.prototype, {
  102. drop: function(target, x, y) { return this.accept(target, x, y) ? (this.onDrop(target, x, y), !0) : !1; },
  103. activate: function() { Droppable.list.push(this); },
  104. release: function() { var i; ~(i = Droppable.list.indexOf(this)) && Droppable.list.splice(i, 1); }
  105. });
  106. scope.Droppable = Droppable,
  107. $.mixin({
  108. droppable: function(options) { return this.each(function(el) { new Droppable(el, options); }); }
  109. });
  110. }