source: main/trunk/greenstone3/web/interfaces/oran/js/jquery-ui-1.8rc1/ui/jquery.ui.droppable.js@ 24245

Last change on this file since 24245 was 24245, checked in by sjb48, 13 years ago

Oran code for supporting format changes to document.

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