dragndrop.js 4.9 KB

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