dragndrop.js 4.8 KB

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