dragndrop.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. var realX, realY, scale, shiftX, shiftY;
  10. realX = 1/size * svgWidth;
  11. realY = 1/size * svgHeight;
  12. if (sizeX < sizeY)
  13. {
  14. // console.log("Left and Right White Spaces. Do stretch the Width ");
  15. shiftY = 0;
  16. shiftX = 0;//(innerWidth - realX) / 2;
  17. }
  18. else
  19. {
  20. // console.log("Top and Bottom White Spaces. Do reorient, or adopt the Height.");
  21. shiftX = 0;
  22. shiftY = 0;//(innerHeight - realY) / 2;
  23. }
  24. console.log("width:" + realX +" height:" + realY);
  25. /*
  26. document.getElementById("GameChatEditor").setAttribute("x", 864 * realX / svgWidth + shiftX);
  27. document.getElementById("GameChatEditor").setAttribute("y", 504 * realY / svgHeight + shiftY);
  28. document.getElementById("GameChatEditor").setAttribute("width", 198 * realX / svgWidth);
  29. document.getElementById("GameChatEditor").setAttribute("height", 120 * realY / svgHeight);
  30. document.getElementById("OnlineChatEditor").setAttribute("x", 10 * realX / svgWidth + shiftX);
  31. document.getElementById("OnlineChatEditor").setAttribute("y", 504 * realY / svgHeight + shiftY);
  32. document.getElementById("OnlineChatEditor").setAttribute("width", 198 * realX / svgWidth);
  33. document.getElementById("OnlineChatEditor").setAttribute("height", 120 * realY / svgHeight);
  34. */
  35. }
  36. function Draggable(root, options)
  37. {
  38. options = options || {},
  39. this.$el = $(root),
  40. this.revert = options.revert,
  41. this.elements = {},
  42. this.proxies = [ "onDown", "onMove", "onUp" ], this.__super__.constructor.call(this),
  43. this.enable();
  44. }
  45. var sizeX,
  46. sizeY,
  47. size,
  48. moved;
  49. $svg = $("svg");
  50. calcShift();
  51. $(window).on("resize", calcShift);
  52. $(window).on("orientationchange", calcShift);
  53. $.inherit(Draggable, scope.Controller);
  54. $.extend(Draggable.prototype, {
  55. storeTrf: function() {
  56. var trf = this.$el.attr("transform");
  57. this.initTrf = trf ? trf.slice(10, -1).split(/\s+/) : [ 0, 0 ];
  58. },
  59. disable: function() {
  60. this.$el.off(document.createTouch ? "touchstart" : "mousedown", this.onDown),
  61. this.$el.css({cursor: "default"});
  62. },
  63. enable: function() {
  64. this.$el.on(document.createTouch ? "touchstart" : "mousedown", this.onDown),
  65. this.$el.attr("style", "cursor: -moz-grab; cursor: -webkit-grab; cursor: grab;");
  66. },
  67. onDown: function(e, silent) {
  68. e.preventDefault(), moved || (moved = !1, this.$el.attr("style", "cursor: -moz-grabbing; cursor: -webkit-grabbing; cursor: grabbing;"),
  69. this.$el[0].parentNode.appendChild(this.$el[0]), this.x = this.clientX(e), this.y = this.clientY(e),
  70. this.trf = this.$el.attr("transform"), this.trf && (this.trf = this.trf.slice(10, -1).split(/\s+/)),
  71. this.storeTrf(), silent || (document.createTouch ? (this.$el.on("touchmove", this.onMove),
  72. this.$el.on("touchend", this.onUp)) : ($("body").on("mousemove", this.onMove), $("body").on("mouseup", this.onUp)),
  73. this.$el.trigger("dragstart")));
  74. },
  75. onMove: function(e, silent) {
  76. e.preventDefault(), moved = !0, this.dx = ((this.curX = this.clientX(e)) - this.x) * size,
  77. this.dy = ((this.curY = this.clientY(e)) - this.y) * size, this.trf && (this.dx += 0 | this.trf[0],
  78. this.dy += 0 | this.trf[1]), this.$el.attr("transform", "translate(" + this.dx + " " + this.dy + ")"),
  79. silent || this.$el.trigger("dragmove", {
  80. detail: {
  81. x: this.curX,
  82. y: this.curY,
  83. event: e
  84. }
  85. });
  86. },
  87. onUp: function(e, silent) {
  88. if (e.preventDefault(), this.$el.attr("style", "cursor: -moz-grab; cursor: -webkit-grab; cursor: grab;"),
  89. moved) {
  90. for (var droped, item, dropList = scope.Droppable.list, i = 0, l = dropList.length; l > i; i++) if (item = dropList[i],
  91. item.intersect(this.curX, this.curY)) {
  92. droped = item.drop(this, this.curX, this.curY);
  93. break;
  94. }
  95. droped || (this.$el.transform({
  96. from: [ this.dx, this.dy ].join(" "),
  97. to: this.initTrf.join(" ")
  98. }), silent || this.$el.trigger("revert")), silent || this.$el.trigger("dragstop", {
  99. detail: {
  100. x: this.curX,
  101. y: this.curY,
  102. event: e
  103. }
  104. }), moved = !1;
  105. }
  106. document.createTouch ? (this.$el.off("touchmove", this.onMove), this.$el.off("touchend", this.onUp)) : ($("body").off("mousemove", this.onMove),
  107. $("body").off("mouseup", this.onUp));
  108. }
  109. });
  110. scope.Draggable = Draggable;
  111. $.mixin({ draggable: function() { return this.each(function(el) { new Draggable(el); }); } });
  112. }
  113. function DropScope(scope) {
  114. function Droppable(root, options)
  115. {
  116. options = options || {},
  117. this.$el = $(root),
  118. this.accept = options.accept || function() { return !0; },
  119. this.onDrop = options.drop || function() {};
  120. this.elements = {};
  121. this.proxies = [];
  122. this.__super__.constructor.call(this);
  123. this.activate();
  124. }
  125. Droppable.list = [];
  126. $.inherit(Droppable, scope.Controller), $.extend(Droppable.prototype, {
  127. drop: function(target, x, y) { return this.accept(target, x, y) ? (this.onDrop(target, x, y), !0) : !1; },
  128. activate: function() { Droppable.list.push(this); },
  129. release: function() { var i; ~(i = Droppable.list.indexOf(this)) && Droppable.list.splice(i, 1); }
  130. });
  131. scope.Droppable = Droppable,
  132. $.mixin({
  133. droppable: function(options) { return this.each(function(el) { new Droppable(el, options); }); }
  134. });
  135. }