1 | /*! |
---|
2 | * jQuery UI Droppable 1.9.0 |
---|
3 | * http://jqueryui.com |
---|
4 | * |
---|
5 | * Copyright 2012 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 | $.widget("ui.droppable", { |
---|
20 | version: "1.9.0", |
---|
21 | widgetEventPrefix: "drop", |
---|
22 | options: { |
---|
23 | accept: '*', |
---|
24 | activeClass: false, |
---|
25 | addClasses: true, |
---|
26 | greedy: false, |
---|
27 | hoverClass: false, |
---|
28 | scope: 'default', |
---|
29 | tolerance: 'intersect' |
---|
30 | }, |
---|
31 | _create: function() { |
---|
32 | |
---|
33 | var o = this.options, accept = o.accept; |
---|
34 | this.isover = 0; this.isout = 1; |
---|
35 | |
---|
36 | this.accept = $.isFunction(accept) ? accept : function(d) { |
---|
37 | return d.is(accept); |
---|
38 | }; |
---|
39 | |
---|
40 | //Store the droppable's proportions |
---|
41 | this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight }; |
---|
42 | |
---|
43 | // Add the reference and positions to the manager |
---|
44 | $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || []; |
---|
45 | $.ui.ddmanager.droppables[o.scope].push(this); |
---|
46 | |
---|
47 | (o.addClasses && this.element.addClass("ui-droppable")); |
---|
48 | |
---|
49 | }, |
---|
50 | |
---|
51 | _destroy: function() { |
---|
52 | var drop = $.ui.ddmanager.droppables[this.options.scope]; |
---|
53 | for ( var i = 0; i < drop.length; i++ ) |
---|
54 | if ( drop[i] == this ) |
---|
55 | drop.splice(i, 1); |
---|
56 | |
---|
57 | this.element.removeClass("ui-droppable ui-droppable-disabled"); |
---|
58 | }, |
---|
59 | |
---|
60 | _setOption: function(key, value) { |
---|
61 | |
---|
62 | if(key == 'accept') { |
---|
63 | this.accept = $.isFunction(value) ? value : function(d) { |
---|
64 | return d.is(value); |
---|
65 | }; |
---|
66 | } |
---|
67 | $.Widget.prototype._setOption.apply(this, arguments); |
---|
68 | }, |
---|
69 | |
---|
70 | _activate: function(event) { |
---|
71 | var draggable = $.ui.ddmanager.current; |
---|
72 | if(this.options.activeClass) this.element.addClass(this.options.activeClass); |
---|
73 | (draggable && this._trigger('activate', event, this.ui(draggable))); |
---|
74 | }, |
---|
75 | |
---|
76 | _deactivate: function(event) { |
---|
77 | var draggable = $.ui.ddmanager.current; |
---|
78 | if(this.options.activeClass) this.element.removeClass(this.options.activeClass); |
---|
79 | (draggable && this._trigger('deactivate', event, this.ui(draggable))); |
---|
80 | }, |
---|
81 | |
---|
82 | _over: function(event) { |
---|
83 | |
---|
84 | var draggable = $.ui.ddmanager.current; |
---|
85 | if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element |
---|
86 | |
---|
87 | if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) { |
---|
88 | if(this.options.hoverClass) this.element.addClass(this.options.hoverClass); |
---|
89 | this._trigger('over', event, this.ui(draggable)); |
---|
90 | } |
---|
91 | |
---|
92 | }, |
---|
93 | |
---|
94 | _out: function(event) { |
---|
95 | |
---|
96 | var draggable = $.ui.ddmanager.current; |
---|
97 | if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element |
---|
98 | |
---|
99 | if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) { |
---|
100 | if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass); |
---|
101 | this._trigger('out', event, this.ui(draggable)); |
---|
102 | } |
---|
103 | |
---|
104 | }, |
---|
105 | |
---|
106 | _drop: function(event,custom) { |
---|
107 | |
---|
108 | var draggable = custom || $.ui.ddmanager.current; |
---|
109 | if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return false; // Bail if draggable and droppable are same element |
---|
110 | |
---|
111 | var childrenIntersection = false; |
---|
112 | this.element.find(":data(droppable)").not(".ui-draggable-dragging").each(function() { |
---|
113 | var inst = $.data(this, 'droppable'); |
---|
114 | if( |
---|
115 | inst.options.greedy |
---|
116 | && !inst.options.disabled |
---|
117 | && inst.options.scope == draggable.options.scope |
---|
118 | && inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element)) |
---|
119 | && $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance) |
---|
120 | ) { childrenIntersection = true; return false; } |
---|
121 | }); |
---|
122 | if(childrenIntersection) return false; |
---|
123 | |
---|
124 | if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) { |
---|
125 | if(this.options.activeClass) this.element.removeClass(this.options.activeClass); |
---|
126 | if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass); |
---|
127 | this._trigger('drop', event, this.ui(draggable)); |
---|
128 | return this.element; |
---|
129 | } |
---|
130 | |
---|
131 | return false; |
---|
132 | |
---|
133 | }, |
---|
134 | |
---|
135 | ui: function(c) { |
---|
136 | return { |
---|
137 | draggable: (c.currentItem || c.element), |
---|
138 | helper: c.helper, |
---|
139 | position: c.position, |
---|
140 | offset: c.positionAbs |
---|
141 | }; |
---|
142 | } |
---|
143 | |
---|
144 | }); |
---|
145 | |
---|
146 | $.ui.intersect = function(draggable, droppable, toleranceMode) { |
---|
147 | |
---|
148 | if (!droppable.offset) return false; |
---|
149 | |
---|
150 | var x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width, |
---|
151 | y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height; |
---|
152 | var l = droppable.offset.left, r = l + droppable.proportions.width, |
---|
153 | t = droppable.offset.top, b = t + droppable.proportions.height; |
---|
154 | |
---|
155 | switch (toleranceMode) { |
---|
156 | case 'fit': |
---|
157 | return (l <= x1 && x2 <= r |
---|
158 | && t <= y1 && y2 <= b); |
---|
159 | break; |
---|
160 | case 'intersect': |
---|
161 | return (l < x1 + (draggable.helperProportions.width / 2) // Right Half |
---|
162 | && x2 - (draggable.helperProportions.width / 2) < r // Left Half |
---|
163 | && t < y1 + (draggable.helperProportions.height / 2) // Bottom Half |
---|
164 | && y2 - (draggable.helperProportions.height / 2) < b ); // Top Half |
---|
165 | break; |
---|
166 | case 'pointer': |
---|
167 | var draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left), |
---|
168 | draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top), |
---|
169 | isOver = $.ui.isOver(draggableTop, draggableLeft, t, l, droppable.proportions.height, droppable.proportions.width); |
---|
170 | return isOver; |
---|
171 | break; |
---|
172 | case 'touch': |
---|
173 | return ( |
---|
174 | (y1 >= t && y1 <= b) || // Top edge touching |
---|
175 | (y2 >= t && y2 <= b) || // Bottom edge touching |
---|
176 | (y1 < t && y2 > b) // Surrounded vertically |
---|
177 | ) && ( |
---|
178 | (x1 >= l && x1 <= r) || // Left edge touching |
---|
179 | (x2 >= l && x2 <= r) || // Right edge touching |
---|
180 | (x1 < l && x2 > r) // Surrounded horizontally |
---|
181 | ); |
---|
182 | break; |
---|
183 | default: |
---|
184 | return false; |
---|
185 | break; |
---|
186 | } |
---|
187 | |
---|
188 | }; |
---|
189 | |
---|
190 | /* |
---|
191 | This manager tracks offsets of draggables and droppables |
---|
192 | */ |
---|
193 | $.ui.ddmanager = { |
---|
194 | current: null, |
---|
195 | droppables: { 'default': [] }, |
---|
196 | prepareOffsets: function(t, event) { |
---|
197 | |
---|
198 | var m = $.ui.ddmanager.droppables[t.options.scope] || []; |
---|
199 | var type = event ? event.type : null; // workaround for #2317 |
---|
200 | var list = (t.currentItem || t.element).find(":data(droppable)").andSelf(); |
---|
201 | |
---|
202 | droppablesLoop: for (var i = 0; i < m.length; i++) { |
---|
203 | |
---|
204 | if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) continue; //No disabled and non-accepted |
---|
205 | 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 |
---|
206 | m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue; //If the element is not visible, continue |
---|
207 | |
---|
208 | if(type == "mousedown") m[i]._activate.call(m[i], event); //Activate the droppable if used directly from draggables |
---|
209 | |
---|
210 | m[i].offset = m[i].element.offset(); |
---|
211 | m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight }; |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | }, |
---|
216 | drop: function(draggable, event) { |
---|
217 | |
---|
218 | var dropped = false; |
---|
219 | $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() { |
---|
220 | |
---|
221 | if(!this.options) return; |
---|
222 | if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) |
---|
223 | dropped = this._drop.call(this, event) || dropped; |
---|
224 | |
---|
225 | if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) { |
---|
226 | this.isout = 1; this.isover = 0; |
---|
227 | this._deactivate.call(this, event); |
---|
228 | } |
---|
229 | |
---|
230 | }); |
---|
231 | return dropped; |
---|
232 | |
---|
233 | }, |
---|
234 | dragStart: function( draggable, event ) { |
---|
235 | //Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003) |
---|
236 | draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() { |
---|
237 | if( !draggable.options.refreshPositions ) $.ui.ddmanager.prepareOffsets( draggable, event ); |
---|
238 | }); |
---|
239 | }, |
---|
240 | drag: function(draggable, event) { |
---|
241 | |
---|
242 | //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse. |
---|
243 | if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event); |
---|
244 | |
---|
245 | //Run through all droppables and check their positions based on specific tolerance options |
---|
246 | $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() { |
---|
247 | |
---|
248 | if(this.options.disabled || this.greedyChild || !this.visible) return; |
---|
249 | var intersects = $.ui.intersect(draggable, this, this.options.tolerance); |
---|
250 | |
---|
251 | var c = !intersects && this.isover == 1 ? 'isout' : (intersects && this.isover == 0 ? 'isover' : null); |
---|
252 | if(!c) return; |
---|
253 | |
---|
254 | var parentInstance; |
---|
255 | if (this.options.greedy) { |
---|
256 | // find droppable parents with same scope |
---|
257 | var scope = this.options.scope; |
---|
258 | var parent = this.element.parents(':data(droppable)').filter(function () { |
---|
259 | return $.data(this, 'droppable').options.scope === scope; |
---|
260 | }); |
---|
261 | |
---|
262 | if (parent.length) { |
---|
263 | parentInstance = $.data(parent[0], 'droppable'); |
---|
264 | parentInstance.greedyChild = (c == 'isover' ? 1 : 0); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | // we just moved into a greedy child |
---|
269 | if (parentInstance && c == 'isover') { |
---|
270 | parentInstance['isover'] = 0; |
---|
271 | parentInstance['isout'] = 1; |
---|
272 | parentInstance._out.call(parentInstance, event); |
---|
273 | } |
---|
274 | |
---|
275 | this[c] = 1; this[c == 'isout' ? 'isover' : 'isout'] = 0; |
---|
276 | this[c == "isover" ? "_over" : "_out"].call(this, event); |
---|
277 | |
---|
278 | // we just moved out of a greedy child |
---|
279 | if (parentInstance && c == 'isout') { |
---|
280 | parentInstance['isout'] = 0; |
---|
281 | parentInstance['isover'] = 1; |
---|
282 | parentInstance._over.call(parentInstance, event); |
---|
283 | } |
---|
284 | }); |
---|
285 | |
---|
286 | }, |
---|
287 | dragStop: function( draggable, event ) { |
---|
288 | draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" ); |
---|
289 | //Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003) |
---|
290 | if( !draggable.options.refreshPositions ) $.ui.ddmanager.prepareOffsets( draggable, event ); |
---|
291 | } |
---|
292 | }; |
---|
293 | |
---|
294 | })(jQuery); |
---|