7
|
1 /*!
|
|
2 * jQuery UI Droppable 1.10.4
|
|
3 * http://jqueryui.com
|
|
4 *
|
|
5 * Copyright 2014 jQuery Foundation and other contributors
|
|
6 * Released under the MIT license.
|
|
7 * http://jquery.org/license
|
|
8 *
|
|
9 * http://api.jqueryui.com/droppable/
|
|
10 *
|
|
11 * Depends:
|
|
12 * jquery.ui.core.js
|
|
13 * jquery.ui.widget.js
|
|
14 * jquery.ui.mouse.js
|
|
15 * jquery.ui.draggable.js
|
|
16 */
|
|
17 (function( $, undefined ) {
|
|
18
|
|
19 function isOverAxis( x, reference, size ) {
|
|
20 return ( x > reference ) && ( x < ( reference + size ) );
|
|
21 }
|
|
22
|
|
23 $.widget("ui.droppable", {
|
|
24 version: "1.10.4",
|
|
25 widgetEventPrefix: "drop",
|
|
26 options: {
|
|
27 accept: "*",
|
|
28 activeClass: false,
|
|
29 addClasses: true,
|
|
30 greedy: false,
|
|
31 hoverClass: false,
|
|
32 scope: "default",
|
|
33 tolerance: "intersect",
|
|
34
|
|
35 // callbacks
|
|
36 activate: null,
|
|
37 deactivate: null,
|
|
38 drop: null,
|
|
39 out: null,
|
|
40 over: null
|
|
41 },
|
|
42 _create: function() {
|
|
43
|
|
44 var proportions,
|
|
45 o = this.options,
|
|
46 accept = o.accept;
|
|
47
|
|
48 this.isover = false;
|
|
49 this.isout = true;
|
|
50
|
|
51 this.accept = $.isFunction(accept) ? accept : function(d) {
|
|
52 return d.is(accept);
|
|
53 };
|
|
54
|
|
55 this.proportions = function( /* valueToWrite */ ) {
|
|
56 if ( arguments.length ) {
|
|
57 // Store the droppable's proportions
|
|
58 proportions = arguments[ 0 ];
|
|
59 } else {
|
|
60 // Retrieve or derive the droppable's proportions
|
|
61 return proportions ?
|
|
62 proportions :
|
|
63 proportions = {
|
|
64 width: this.element[ 0 ].offsetWidth,
|
|
65 height: this.element[ 0 ].offsetHeight
|
|
66 };
|
|
67 }
|
|
68 };
|
|
69
|
|
70 // Add the reference and positions to the manager
|
|
71 $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
|
|
72 $.ui.ddmanager.droppables[o.scope].push(this);
|
|
73
|
|
74 (o.addClasses && this.element.addClass("ui-droppable"));
|
|
75
|
|
76 },
|
|
77
|
|
78 _destroy: function() {
|
|
79 var i = 0,
|
|
80 drop = $.ui.ddmanager.droppables[this.options.scope];
|
|
81
|
|
82 for ( ; i < drop.length; i++ ) {
|
|
83 if ( drop[i] === this ) {
|
|
84 drop.splice(i, 1);
|
|
85 }
|
|
86 }
|
|
87
|
|
88 this.element.removeClass("ui-droppable ui-droppable-disabled");
|
|
89 },
|
|
90
|
|
91 _setOption: function(key, value) {
|
|
92
|
|
93 if(key === "accept") {
|
|
94 this.accept = $.isFunction(value) ? value : function(d) {
|
|
95 return d.is(value);
|
|
96 };
|
|
97 }
|
|
98 $.Widget.prototype._setOption.apply(this, arguments);
|
|
99 },
|
|
100
|
|
101 _activate: function(event) {
|
|
102 var draggable = $.ui.ddmanager.current;
|
|
103 if(this.options.activeClass) {
|
|
104 this.element.addClass(this.options.activeClass);
|
|
105 }
|
|
106 if(draggable){
|
|
107 this._trigger("activate", event, this.ui(draggable));
|
|
108 }
|
|
109 },
|
|
110
|
|
111 _deactivate: function(event) {
|
|
112 var draggable = $.ui.ddmanager.current;
|
|
113 if(this.options.activeClass) {
|
|
114 this.element.removeClass(this.options.activeClass);
|
|
115 }
|
|
116 if(draggable){
|
|
117 this._trigger("deactivate", event, this.ui(draggable));
|
|
118 }
|
|
119 },
|
|
120
|
|
121 _over: function(event) {
|
|
122
|
|
123 var draggable = $.ui.ddmanager.current;
|
|
124
|
|
125 // Bail if draggable and droppable are same element
|
|
126 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
|
|
127 return;
|
|
128 }
|
|
129
|
|
130 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
|
|
131 if(this.options.hoverClass) {
|
|
132 this.element.addClass(this.options.hoverClass);
|
|
133 }
|
|
134 this._trigger("over", event, this.ui(draggable));
|
|
135 }
|
|
136
|
|
137 },
|
|
138
|
|
139 _out: function(event) {
|
|
140
|
|
141 var draggable = $.ui.ddmanager.current;
|
|
142
|
|
143 // Bail if draggable and droppable are same element
|
|
144 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
|
|
145 return;
|
|
146 }
|
|
147
|
|
148 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
|
|
149 if(this.options.hoverClass) {
|
|
150 this.element.removeClass(this.options.hoverClass);
|
|
151 }
|
|
152 this._trigger("out", event, this.ui(draggable));
|
|
153 }
|
|
154
|
|
155 },
|
|
156
|
|
157 _drop: function(event,custom) {
|
|
158
|
|
159 var draggable = custom || $.ui.ddmanager.current,
|
|
160 childrenIntersection = false;
|
|
161
|
|
162 // Bail if draggable and droppable are same element
|
|
163 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
|
|
164 return false;
|
|
165 }
|
|
166
|
|
167 this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() {
|
|
168 var inst = $.data(this, "ui-droppable");
|
|
169 if(
|
|
170 inst.options.greedy &&
|
|
171 !inst.options.disabled &&
|
|
172 inst.options.scope === draggable.options.scope &&
|
|
173 inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element)) &&
|
|
174 $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)
|
|
175 ) { childrenIntersection = true; return false; }
|
|
176 });
|
|
177 if(childrenIntersection) {
|
|
178 return false;
|
|
179 }
|
|
180
|
|
181 if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
|
|
182 if(this.options.activeClass) {
|
|
183 this.element.removeClass(this.options.activeClass);
|
|
184 }
|
|
185 if(this.options.hoverClass) {
|
|
186 this.element.removeClass(this.options.hoverClass);
|
|
187 }
|
|
188 this._trigger("drop", event, this.ui(draggable));
|
|
189 return this.element;
|
|
190 }
|
|
191
|
|
192 return false;
|
|
193
|
|
194 },
|
|
195
|
|
196 ui: function(c) {
|
|
197 return {
|
|
198 draggable: (c.currentItem || c.element),
|
|
199 helper: c.helper,
|
|
200 position: c.position,
|
|
201 offset: c.positionAbs
|
|
202 };
|
|
203 }
|
|
204
|
|
205 });
|
|
206
|
|
207 $.ui.intersect = function(draggable, droppable, toleranceMode) {
|
|
208
|
|
209 if (!droppable.offset) {
|
|
210 return false;
|
|
211 }
|
|
212
|
|
213 var draggableLeft, draggableTop,
|
|
214 x1 = (draggable.positionAbs || draggable.position.absolute).left,
|
|
215 y1 = (draggable.positionAbs || draggable.position.absolute).top,
|
|
216 x2 = x1 + draggable.helperProportions.width,
|
|
217 y2 = y1 + draggable.helperProportions.height,
|
|
218 l = droppable.offset.left,
|
|
219 t = droppable.offset.top,
|
|
220 r = l + droppable.proportions().width,
|
|
221 b = t + droppable.proportions().height;
|
|
222
|
|
223 switch (toleranceMode) {
|
|
224 case "fit":
|
|
225 return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
|
|
226 case "intersect":
|
|
227 return (l < x1 + (draggable.helperProportions.width / 2) && // Right Half
|
|
228 x2 - (draggable.helperProportions.width / 2) < r && // Left Half
|
|
229 t < y1 + (draggable.helperProportions.height / 2) && // Bottom Half
|
|
230 y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
|
|
231 case "pointer":
|
|
232 draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left);
|
|
233 draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top);
|
|
234 return isOverAxis( draggableTop, t, droppable.proportions().height ) && isOverAxis( draggableLeft, l, droppable.proportions().width );
|
|
235 case "touch":
|
|
236 return (
|
|
237 (y1 >= t && y1 <= b) || // Top edge touching
|
|
238 (y2 >= t && y2 <= b) || // Bottom edge touching
|
|
239 (y1 < t && y2 > b) // Surrounded vertically
|
|
240 ) && (
|
|
241 (x1 >= l && x1 <= r) || // Left edge touching
|
|
242 (x2 >= l && x2 <= r) || // Right edge touching
|
|
243 (x1 < l && x2 > r) // Surrounded horizontally
|
|
244 );
|
|
245 default:
|
|
246 return false;
|
|
247 }
|
|
248
|
|
249 };
|
|
250
|
|
251 /*
|
|
252 This manager tracks offsets of draggables and droppables
|
|
253 */
|
|
254 $.ui.ddmanager = {
|
|
255 current: null,
|
|
256 droppables: { "default": [] },
|
|
257 prepareOffsets: function(t, event) {
|
|
258
|
|
259 var i, j,
|
|
260 m = $.ui.ddmanager.droppables[t.options.scope] || [],
|
|
261 type = event ? event.type : null, // workaround for #2317
|
|
262 list = (t.currentItem || t.element).find(":data(ui-droppable)").addBack();
|
|
263
|
|
264 droppablesLoop: for (i = 0; i < m.length; i++) {
|
|
265
|
|
266 //No disabled and non-accepted
|
|
267 if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) {
|
|
268 continue;
|
|
269 }
|
|
270
|
|
271 // Filter out elements in the current dragged item
|
|
272 for (j=0; j < list.length; j++) {
|
|
273 if(list[j] === m[i].element[0]) {
|
|
274 m[i].proportions().height = 0;
|
|
275 continue droppablesLoop;
|
|
276 }
|
|
277 }
|
|
278
|
|
279 m[i].visible = m[i].element.css("display") !== "none";
|
|
280 if(!m[i].visible) {
|
|
281 continue;
|
|
282 }
|
|
283
|
|
284 //Activate the droppable if used directly from draggables
|
|
285 if(type === "mousedown") {
|
|
286 m[i]._activate.call(m[i], event);
|
|
287 }
|
|
288
|
|
289 m[ i ].offset = m[ i ].element.offset();
|
|
290 m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
|
|
291
|
|
292 }
|
|
293
|
|
294 },
|
|
295 drop: function(draggable, event) {
|
|
296
|
|
297 var dropped = false;
|
|
298 // Create a copy of the droppables in case the list changes during the drop (#9116)
|
|
299 $.each(($.ui.ddmanager.droppables[draggable.options.scope] || []).slice(), function() {
|
|
300
|
|
301 if(!this.options) {
|
|
302 return;
|
|
303 }
|
|
304 if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) {
|
|
305 dropped = this._drop.call(this, event) || dropped;
|
|
306 }
|
|
307
|
|
308 if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
|
|
309 this.isout = true;
|
|
310 this.isover = false;
|
|
311 this._deactivate.call(this, event);
|
|
312 }
|
|
313
|
|
314 });
|
|
315 return dropped;
|
|
316
|
|
317 },
|
|
318 dragStart: function( draggable, event ) {
|
|
319 //Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
|
|
320 draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
|
|
321 if( !draggable.options.refreshPositions ) {
|
|
322 $.ui.ddmanager.prepareOffsets( draggable, event );
|
|
323 }
|
|
324 });
|
|
325 },
|
|
326 drag: function(draggable, event) {
|
|
327
|
|
328 //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
|
|
329 if(draggable.options.refreshPositions) {
|
|
330 $.ui.ddmanager.prepareOffsets(draggable, event);
|
|
331 }
|
|
332
|
|
333 //Run through all droppables and check their positions based on specific tolerance options
|
|
334 $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
|
|
335
|
|
336 if(this.options.disabled || this.greedyChild || !this.visible) {
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 var parentInstance, scope, parent,
|
|
341 intersects = $.ui.intersect(draggable, this, this.options.tolerance),
|
|
342 c = !intersects && this.isover ? "isout" : (intersects && !this.isover ? "isover" : null);
|
|
343 if(!c) {
|
|
344 return;
|
|
345 }
|
|
346
|
|
347 if (this.options.greedy) {
|
|
348 // find droppable parents with same scope
|
|
349 scope = this.options.scope;
|
|
350 parent = this.element.parents(":data(ui-droppable)").filter(function () {
|
|
351 return $.data(this, "ui-droppable").options.scope === scope;
|
|
352 });
|
|
353
|
|
354 if (parent.length) {
|
|
355 parentInstance = $.data(parent[0], "ui-droppable");
|
|
356 parentInstance.greedyChild = (c === "isover");
|
|
357 }
|
|
358 }
|
|
359
|
|
360 // we just moved into a greedy child
|
|
361 if (parentInstance && c === "isover") {
|
|
362 parentInstance.isover = false;
|
|
363 parentInstance.isout = true;
|
|
364 parentInstance._out.call(parentInstance, event);
|
|
365 }
|
|
366
|
|
367 this[c] = true;
|
|
368 this[c === "isout" ? "isover" : "isout"] = false;
|
|
369 this[c === "isover" ? "_over" : "_out"].call(this, event);
|
|
370
|
|
371 // we just moved out of a greedy child
|
|
372 if (parentInstance && c === "isout") {
|
|
373 parentInstance.isout = false;
|
|
374 parentInstance.isover = true;
|
|
375 parentInstance._over.call(parentInstance, event);
|
|
376 }
|
|
377 });
|
|
378
|
|
379 },
|
|
380 dragStop: function( draggable, event ) {
|
|
381 draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
|
|
382 //Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
|
|
383 if( !draggable.options.refreshPositions ) {
|
|
384 $.ui.ddmanager.prepareOffsets( draggable, event );
|
|
385 }
|
|
386 }
|
|
387 };
|
|
388
|
|
389 })(jQuery);
|