25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

286 satır
9.2 KiB

  1. /*
  2. * jQuery UI Droppable 1.8
  3. *
  4. * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * http://docs.jquery.com/UI/Droppables
  9. *
  10. * Depends:
  11. * jquery.ui.core.js
  12. * jquery.ui.widget.js
  13. * jquery.ui.mouse.js
  14. * jquery.ui.draggable.js
  15. */
  16. (function($) {
  17. $.widget("ui.droppable", {
  18. widgetEventPrefix: "drop",
  19. options: {
  20. accept: '*',
  21. activeClass: false,
  22. addClasses: true,
  23. greedy: false,
  24. hoverClass: false,
  25. scope: 'default',
  26. tolerance: 'intersect'
  27. },
  28. _create: function() {
  29. var o = this.options, accept = o.accept;
  30. this.isover = 0; this.isout = 1;
  31. this.accept = $.isFunction(accept) ? accept : function(d) {
  32. return d.is(accept);
  33. };
  34. //Store the droppable's proportions
  35. this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
  36. // Add the reference and positions to the manager
  37. $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
  38. $.ui.ddmanager.droppables[o.scope].push(this);
  39. (o.addClasses && this.element.addClass("ui-droppable"));
  40. },
  41. destroy: function() {
  42. var drop = $.ui.ddmanager.droppables[this.options.scope];
  43. for ( var i = 0; i < drop.length; i++ )
  44. if ( drop[i] == this )
  45. drop.splice(i, 1);
  46. this.element
  47. .removeClass("ui-droppable ui-droppable-disabled")
  48. .removeData("droppable")
  49. .unbind(".droppable");
  50. return this;
  51. },
  52. _setOption: function(key, value) {
  53. if(key == 'accept') {
  54. this.accept = $.isFunction(value) ? value : function(d) {
  55. return d.is(value);
  56. };
  57. }
  58. $.Widget.prototype._setOption.apply(this, arguments);
  59. },
  60. _activate: function(event) {
  61. var draggable = $.ui.ddmanager.current;
  62. if(this.options.activeClass) this.element.addClass(this.options.activeClass);
  63. (draggable && this._trigger('activate', event, this.ui(draggable)));
  64. },
  65. _deactivate: function(event) {
  66. var draggable = $.ui.ddmanager.current;
  67. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  68. (draggable && this._trigger('deactivate', event, this.ui(draggable)));
  69. },
  70. _over: function(event) {
  71. var draggable = $.ui.ddmanager.current;
  72. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  73. if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  74. if(this.options.hoverClass) this.element.addClass(this.options.hoverClass);
  75. this._trigger('over', event, this.ui(draggable));
  76. }
  77. },
  78. _out: function(event) {
  79. var draggable = $.ui.ddmanager.current;
  80. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  81. if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  82. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  83. this._trigger('out', event, this.ui(draggable));
  84. }
  85. },
  86. _drop: function(event,custom) {
  87. var draggable = custom || $.ui.ddmanager.current;
  88. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return false; // Bail if draggable and droppable are same element
  89. var childrenIntersection = false;
  90. this.element.find(":data(droppable)").not(".ui-draggable-dragging").each(function() {
  91. var inst = $.data(this, 'droppable');
  92. if(
  93. inst.options.greedy
  94. && !inst.options.disabled
  95. && inst.options.scope == draggable.options.scope
  96. && inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element))
  97. && $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)
  98. ) { childrenIntersection = true; return false; }
  99. });
  100. if(childrenIntersection) return false;
  101. if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  102. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  103. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  104. this._trigger('drop', event, this.ui(draggable));
  105. return this.element;
  106. }
  107. return false;
  108. },
  109. ui: function(c) {
  110. return {
  111. draggable: (c.currentItem || c.element),
  112. helper: c.helper,
  113. position: c.position,
  114. offset: c.positionAbs
  115. };
  116. }
  117. });
  118. $.extend($.ui.droppable, {
  119. version: "1.8"
  120. });
  121. $.ui.intersect = function(draggable, droppable, toleranceMode) {
  122. if (!droppable.offset) return false;
  123. var x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width,
  124. y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height;
  125. var l = droppable.offset.left, r = l + droppable.proportions.width,
  126. t = droppable.offset.top, b = t + droppable.proportions.height;
  127. switch (toleranceMode) {
  128. case 'fit':
  129. return (l < x1 && x2 < r
  130. && t < y1 && y2 < b);
  131. break;
  132. case 'intersect':
  133. return (l < x1 + (draggable.helperProportions.width / 2) // Right Half
  134. && x2 - (draggable.helperProportions.width / 2) < r // Left Half
  135. && t < y1 + (draggable.helperProportions.height / 2) // Bottom Half
  136. && y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
  137. break;
  138. case 'pointer':
  139. var draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left),
  140. draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top),
  141. isOver = $.ui.isOver(draggableTop, draggableLeft, t, l, droppable.proportions.height, droppable.proportions.width);
  142. return isOver;
  143. break;
  144. case 'touch':
  145. return (
  146. (y1 >= t && y1 <= b) || // Top edge touching
  147. (y2 >= t && y2 <= b) || // Bottom edge touching
  148. (y1 < t && y2 > b) // Surrounded vertically
  149. ) && (
  150. (x1 >= l && x1 <= r) || // Left edge touching
  151. (x2 >= l && x2 <= r) || // Right edge touching
  152. (x1 < l && x2 > r) // Surrounded horizontally
  153. );
  154. break;
  155. default:
  156. return false;
  157. break;
  158. }
  159. };
  160. /*
  161. This manager tracks offsets of draggables and droppables
  162. */
  163. $.ui.ddmanager = {
  164. current: null,
  165. droppables: { 'default': [] },
  166. prepareOffsets: function(t, event) {
  167. var m = $.ui.ddmanager.droppables[t.options.scope] || [];
  168. var type = event ? event.type : null; // workaround for #2317
  169. var list = (t.currentItem || t.element).find(":data(droppable)").andSelf();
  170. droppablesLoop: for (var i = 0; i < m.length; i++) {
  171. if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) continue; //No disabled and non-accepted
  172. for (var j=0; j < list.length; j++) { if(list[j] == m[i].element[0]) { m[i].proportions.height = 0; continue droppablesLoop; } }; //Filter out elements in the current dragged item
  173. m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue; //If the element is not visible, continue
  174. m[i].offset = m[i].element.offset();
  175. m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
  176. if(type == "mousedown") m[i]._activate.call(m[i], event); //Activate the droppable if used directly from draggables
  177. }
  178. },
  179. drop: function(draggable, event) {
  180. var dropped = false;
  181. $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
  182. if(!this.options) return;
  183. if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance))
  184. dropped = dropped || this._drop.call(this, event);
  185. if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  186. this.isout = 1; this.isover = 0;
  187. this._deactivate.call(this, event);
  188. }
  189. });
  190. return dropped;
  191. },
  192. drag: function(draggable, event) {
  193. //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
  194. if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
  195. //Run through all droppables and check their positions based on specific tolerance options
  196. $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
  197. if(this.options.disabled || this.greedyChild || !this.visible) return;
  198. var intersects = $.ui.intersect(draggable, this, this.options.tolerance);
  199. var c = !intersects && this.isover == 1 ? 'isout' : (intersects && this.isover == 0 ? 'isover' : null);
  200. if(!c) return;
  201. var parentInstance;
  202. if (this.options.greedy) {
  203. var parent = this.element.parents(':data(droppable):eq(0)');
  204. if (parent.length) {
  205. parentInstance = $.data(parent[0], 'droppable');
  206. parentInstance.greedyChild = (c == 'isover' ? 1 : 0);
  207. }
  208. }
  209. // we just moved into a greedy child
  210. if (parentInstance && c == 'isover') {
  211. parentInstance['isover'] = 0;
  212. parentInstance['isout'] = 1;
  213. parentInstance._out.call(parentInstance, event);
  214. }
  215. this[c] = 1; this[c == 'isout' ? 'isover' : 'isout'] = 0;
  216. this[c == "isover" ? "_over" : "_out"].call(this, event);
  217. // we just moved out of a greedy child
  218. if (parentInstance && c == 'isout') {
  219. parentInstance['isout'] = 0;
  220. parentInstance['isover'] = 1;
  221. parentInstance._over.call(parentInstance, event);
  222. }
  223. });
  224. }
  225. };
  226. })(jQuery);