|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('dd-ddm-drop', function(Y) { |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
/** |
|
|
12 |
* Extends the dd-ddm Class to add support for the placement of Drop Target shims inside the viewport shim. It also handles all Drop Target related events and interactions. |
|
|
13 |
* @module dd |
|
|
14 |
* @submodule dd-ddm-drop |
|
|
15 |
* @for DDM |
|
|
16 |
* @namespace DD |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
//TODO CSS class name for the bestMatch.. |
|
|
20 |
Y.mix(Y.DD.DDM, { |
|
|
21 |
/** |
|
|
22 |
* @private |
|
|
23 |
* @property _noShim |
|
|
24 |
* @description This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing. |
|
|
25 |
* @type {Boolean} |
|
|
26 |
*/ |
|
|
27 |
_noShim: false, |
|
|
28 |
/** |
|
|
29 |
* @private |
|
|
30 |
* @property _activeShims |
|
|
31 |
* @description Placeholder for all active shims on the page |
|
|
32 |
* @type {Array} |
|
|
33 |
*/ |
|
|
34 |
_activeShims: [], |
|
|
35 |
/** |
|
|
36 |
* @private |
|
|
37 |
* @method _hasActiveShim |
|
|
38 |
* @description This method checks the _activeShims Object to see if there is a shim active. |
|
|
39 |
* @return {Boolean} |
|
|
40 |
*/ |
|
|
41 |
_hasActiveShim: function() { |
|
|
42 |
if (this._noShim) { |
|
|
43 |
return true; |
|
|
44 |
} |
|
|
45 |
return this._activeShims.length; |
|
|
46 |
}, |
|
|
47 |
/** |
|
|
48 |
* @private |
|
|
49 |
* @method _addActiveShim |
|
|
50 |
* @description Adds a Drop Target to the list of active shims |
|
|
51 |
* @param {Object} d The Drop instance to add to the list. |
|
|
52 |
*/ |
|
|
53 |
_addActiveShim: function(d) { |
|
|
54 |
this._activeShims[this._activeShims.length] = d; |
|
|
55 |
}, |
|
|
56 |
/** |
|
|
57 |
* @private |
|
|
58 |
* @method _removeActiveShim |
|
|
59 |
* @description Removes a Drop Target to the list of active shims |
|
|
60 |
* @param {Object} d The Drop instance to remove from the list. |
|
|
61 |
*/ |
|
|
62 |
_removeActiveShim: function(d) { |
|
|
63 |
var s = []; |
|
|
64 |
Y.each(this._activeShims, function(v, k) { |
|
|
65 |
if (v._yuid !== d._yuid) { |
|
|
66 |
s[s.length] = v; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
}); |
|
|
70 |
this._activeShims = s; |
|
|
71 |
}, |
|
|
72 |
/** |
|
|
73 |
* @method syncActiveShims |
|
|
74 |
* @description This method will sync the position of the shims on the Drop Targets that are currently active. |
|
|
75 |
* @param {Boolean} force Resize/sync all Targets. |
|
|
76 |
*/ |
|
|
77 |
syncActiveShims: function(force) { |
|
|
78 |
Y.later(0, this, function(force) { |
|
|
79 |
var drops = ((force) ? this.targets : this._lookup()); |
|
|
80 |
Y.each(drops, function(v, k) { |
|
|
81 |
v.sizeShim.call(v); |
|
|
82 |
}, this); |
|
|
83 |
}, force); |
|
|
84 |
}, |
|
|
85 |
/** |
|
|
86 |
* @private |
|
|
87 |
* @property mode |
|
|
88 |
* @description The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict |
|
|
89 |
* @type Number |
|
|
90 |
*/ |
|
|
91 |
mode: 0, |
|
|
92 |
/** |
|
|
93 |
* @private |
|
|
94 |
* @property POINT |
|
|
95 |
* @description In point mode, a Drop is targeted by the cursor being over the Target |
|
|
96 |
* @type Number |
|
|
97 |
*/ |
|
|
98 |
POINT: 0, |
|
|
99 |
/** |
|
|
100 |
* @private |
|
|
101 |
* @property INTERSECT |
|
|
102 |
* @description In intersect mode, a Drop is targeted by "part" of the drag node being over the Target |
|
|
103 |
* @type Number |
|
|
104 |
*/ |
|
|
105 |
INTERSECT: 1, |
|
|
106 |
/** |
|
|
107 |
* @private |
|
|
108 |
* @property STRICT |
|
|
109 |
* @description In strict mode, a Drop is targeted by the "entire" drag node being over the Target |
|
|
110 |
* @type Number |
|
|
111 |
*/ |
|
|
112 |
STRICT: 2, |
|
|
113 |
/** |
|
|
114 |
* @property useHash |
|
|
115 |
* @description Should we only check targets that are in the viewport on drags (for performance), default: true |
|
|
116 |
* @type {Boolean} |
|
|
117 |
*/ |
|
|
118 |
useHash: true, |
|
|
119 |
/** |
|
|
120 |
* @property activeDrop |
|
|
121 |
* @description A reference to the active Drop Target |
|
|
122 |
* @type {Object} |
|
|
123 |
*/ |
|
|
124 |
activeDrop: null, |
|
|
125 |
/** |
|
|
126 |
* @property validDrops |
|
|
127 |
* @description An array of the valid Drop Targets for this interaction. |
|
|
128 |
* @type {Array} |
|
|
129 |
*/ |
|
|
130 |
//TODO Change array/object literals to be in sync.. |
|
|
131 |
validDrops: [], |
|
|
132 |
/** |
|
|
133 |
* @property otherDrops |
|
|
134 |
* @description An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets) |
|
|
135 |
* @type {Object} |
|
|
136 |
*/ |
|
|
137 |
otherDrops: {}, |
|
|
138 |
/** |
|
|
139 |
* @property targets |
|
|
140 |
* @description All of the Targets |
|
|
141 |
* @type {Array} |
|
|
142 |
*/ |
|
|
143 |
targets: [], |
|
|
144 |
/** |
|
|
145 |
* @private |
|
|
146 |
* @method _addValid |
|
|
147 |
* @description Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation. |
|
|
148 |
* @param {Object} drop |
|
|
149 |
* @return {Self} |
|
|
150 |
* @chainable |
|
|
151 |
*/ |
|
|
152 |
_addValid: function(drop) { |
|
|
153 |
this.validDrops[this.validDrops.length] = drop; |
|
|
154 |
return this; |
|
|
155 |
}, |
|
|
156 |
/** |
|
|
157 |
* @private |
|
|
158 |
* @method _removeValid |
|
|
159 |
* @description Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation. |
|
|
160 |
* @param {Object} drop |
|
|
161 |
* @return {Self} |
|
|
162 |
* @chainable |
|
|
163 |
*/ |
|
|
164 |
_removeValid: function(drop) { |
|
|
165 |
var drops = []; |
|
|
166 |
Y.each(this.validDrops, function(v, k) { |
|
|
167 |
if (v !== drop) { |
|
|
168 |
drops[drops.length] = v; |
|
|
169 |
} |
|
|
170 |
}); |
|
|
171 |
|
|
|
172 |
this.validDrops = drops; |
|
|
173 |
return this; |
|
|
174 |
}, |
|
|
175 |
/** |
|
|
176 |
* @method isOverTarget |
|
|
177 |
* @description Check to see if the Drag element is over the target, method varies on current mode |
|
|
178 |
* @param {Object} drop The drop to check against |
|
|
179 |
* @return {Boolean} |
|
|
180 |
*/ |
|
|
181 |
isOverTarget: function(drop) { |
|
|
182 |
if (this.activeDrag && drop) { |
|
|
183 |
var xy = this.activeDrag.mouseXY; |
|
|
184 |
if (xy) { |
|
|
185 |
if (this.activeDrag.get('dragMode') == this.STRICT) { |
|
|
186 |
return this.activeDrag.get('dragNode').inRegion(drop.region, true, this.activeDrag.region); |
|
|
187 |
} else { |
|
|
188 |
if (drop && drop.shim) { |
|
|
189 |
return drop.shim.intersect({ |
|
|
190 |
top: xy[1], |
|
|
191 |
bottom: xy[1], |
|
|
192 |
left: xy[0], |
|
|
193 |
right: xy[0] |
|
|
194 |
}, drop.region).inRegion; |
|
|
195 |
} else { |
|
|
196 |
return false; |
|
|
197 |
} |
|
|
198 |
} |
|
|
199 |
} else { |
|
|
200 |
return false; |
|
|
201 |
} |
|
|
202 |
} else { |
|
|
203 |
return false; |
|
|
204 |
} |
|
|
205 |
}, |
|
|
206 |
/** |
|
|
207 |
* @method clearCache |
|
|
208 |
* @description Clears the cache data used for this interaction. |
|
|
209 |
*/ |
|
|
210 |
clearCache: function() { |
|
|
211 |
this.validDrops = []; |
|
|
212 |
this.otherDrops = {}; |
|
|
213 |
this._activeShims = []; |
|
|
214 |
}, |
|
|
215 |
/** |
|
|
216 |
* @private |
|
|
217 |
* @method _activateTargets |
|
|
218 |
* @description Clear the cache and activate the shims of all the targets |
|
|
219 |
*/ |
|
|
220 |
_activateTargets: function() { |
|
|
221 |
this.clearCache(); |
|
|
222 |
Y.each(this.targets, function(v, k) { |
|
|
223 |
v._activateShim.apply(v, []); |
|
|
224 |
}, this); |
|
|
225 |
this._handleTargetOver(); |
|
|
226 |
|
|
|
227 |
}, |
|
|
228 |
/** |
|
|
229 |
* @method getBestMatch |
|
|
230 |
* @description This method will gather the area for all potential targets and see which has the hightest covered area and return it. |
|
|
231 |
* @param {Array} drops An Array of drops to scan for the best match. |
|
|
232 |
* @param {Boolean} all If present, it returns an Array. First item is best match, second is an Array of the other items in the original Array. |
|
|
233 |
* @return {Object or Array} |
|
|
234 |
*/ |
|
|
235 |
getBestMatch: function(drops, all) { |
|
|
236 |
var biggest = null, area = 0, out; |
|
|
237 |
|
|
|
238 |
Y.each(drops, function(v, k) { |
|
|
239 |
var inter = this.activeDrag.get('dragNode').intersect(v.get('node')); |
|
|
240 |
v.region.area = inter.area; |
|
|
241 |
|
|
|
242 |
if (inter.inRegion) { |
|
|
243 |
if (inter.area > area) { |
|
|
244 |
area = inter.area; |
|
|
245 |
biggest = v; |
|
|
246 |
} |
|
|
247 |
} |
|
|
248 |
}, this); |
|
|
249 |
if (all) { |
|
|
250 |
out = []; |
|
|
251 |
//TODO Sort the others in numeric order by area covered.. |
|
|
252 |
Y.each(drops, function(v, k) { |
|
|
253 |
if (v !== biggest) { |
|
|
254 |
out[out.length] = v; |
|
|
255 |
} |
|
|
256 |
}, this); |
|
|
257 |
return [biggest, out]; |
|
|
258 |
} else { |
|
|
259 |
return biggest; |
|
|
260 |
} |
|
|
261 |
}, |
|
|
262 |
/** |
|
|
263 |
* @private |
|
|
264 |
* @method _deactivateTargets |
|
|
265 |
* @description This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims.. |
|
|
266 |
*/ |
|
|
267 |
_deactivateTargets: function() { |
|
|
268 |
var other = [], tmp, |
|
|
269 |
activeDrag = this.activeDrag, |
|
|
270 |
activeDrop = this.activeDrop; |
|
|
271 |
|
|
|
272 |
//TODO why is this check so hard?? |
|
|
273 |
if (activeDrag && activeDrop && this.otherDrops[activeDrop]) { |
|
|
274 |
if (!activeDrag.get('dragMode')) { |
|
|
275 |
//TODO otherDrops -- private.. |
|
|
276 |
other = this.otherDrops; |
|
|
277 |
delete other[activeDrop]; |
|
|
278 |
} else { |
|
|
279 |
tmp = this.getBestMatch(this.otherDrops, true); |
|
|
280 |
activeDrop = tmp[0]; |
|
|
281 |
other = tmp[1]; |
|
|
282 |
} |
|
|
283 |
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over'); |
|
|
284 |
if (activeDrop) { |
|
|
285 |
activeDrop.fire('drop:hit', { drag: activeDrag, drop: activeDrop, others: other }); |
|
|
286 |
activeDrag.fire('drag:drophit', { drag: activeDrag, drop: activeDrop, others: other }); |
|
|
287 |
} |
|
|
288 |
} else if (activeDrag) { |
|
|
289 |
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over'); |
|
|
290 |
activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] }); |
|
|
291 |
} else { |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
this.activeDrop = null; |
|
|
295 |
|
|
|
296 |
Y.each(this.targets, function(v, k) { |
|
|
297 |
v._deactivateShim.apply(v, []); |
|
|
298 |
}, this); |
|
|
299 |
}, |
|
|
300 |
/** |
|
|
301 |
* @private |
|
|
302 |
* @method _dropMove |
|
|
303 |
* @description This method is called when the move method is called on the Drag Object. |
|
|
304 |
*/ |
|
|
305 |
_dropMove: function() { |
|
|
306 |
if (this._hasActiveShim()) { |
|
|
307 |
this._handleTargetOver(); |
|
|
308 |
} else { |
|
|
309 |
Y.each(this.otherDrops, function(v, k) { |
|
|
310 |
v._handleOut.apply(v, []); |
|
|
311 |
}); |
|
|
312 |
} |
|
|
313 |
}, |
|
|
314 |
/** |
|
|
315 |
* @private |
|
|
316 |
* @method _lookup |
|
|
317 |
* @description Filters the list of Drops down to those in the viewport. |
|
|
318 |
* @return {Array} The valid Drop Targets that are in the viewport. |
|
|
319 |
*/ |
|
|
320 |
_lookup: function() { |
|
|
321 |
if (!this.useHash || this._noShim) { |
|
|
322 |
return this.validDrops; |
|
|
323 |
} |
|
|
324 |
var drops = []; |
|
|
325 |
//Only scan drop shims that are in the Viewport |
|
|
326 |
Y.each(this.validDrops, function(v, k) { |
|
|
327 |
if (v.shim && v.shim.inViewportRegion(false, v.region)) { |
|
|
328 |
drops[drops.length] = v; |
|
|
329 |
} |
|
|
330 |
}); |
|
|
331 |
return drops; |
|
|
332 |
|
|
|
333 |
}, |
|
|
334 |
/** |
|
|
335 |
* @private |
|
|
336 |
* @method _handleTargetOver |
|
|
337 |
* @description This method execs _handleTargetOver on all valid Drop Targets |
|
|
338 |
*/ |
|
|
339 |
_handleTargetOver: function() { |
|
|
340 |
var drops = this._lookup(); |
|
|
341 |
Y.each(drops, function(v, k) { |
|
|
342 |
v._handleTargetOver.call(v); |
|
|
343 |
}, this); |
|
|
344 |
}, |
|
|
345 |
/** |
|
|
346 |
* @private |
|
|
347 |
* @method _regTarget |
|
|
348 |
* @description Add the passed in Target to the targets collection |
|
|
349 |
* @param {Object} t The Target to add to the targets collection |
|
|
350 |
*/ |
|
|
351 |
_regTarget: function(t) { |
|
|
352 |
this.targets[this.targets.length] = t; |
|
|
353 |
}, |
|
|
354 |
/** |
|
|
355 |
* @private |
|
|
356 |
* @method _unregTarget |
|
|
357 |
* @description Remove the passed in Target from the targets collection |
|
|
358 |
* @param {Object} drop The Target to remove from the targets collection |
|
|
359 |
*/ |
|
|
360 |
_unregTarget: function(drop) { |
|
|
361 |
var targets = [], vdrops; |
|
|
362 |
Y.each(this.targets, function(v, k) { |
|
|
363 |
if (v != drop) { |
|
|
364 |
targets[targets.length] = v; |
|
|
365 |
} |
|
|
366 |
}, this); |
|
|
367 |
this.targets = targets; |
|
|
368 |
|
|
|
369 |
vdrops = []; |
|
|
370 |
Y.each(this.validDrops, function(v, k) { |
|
|
371 |
if (v !== drop) { |
|
|
372 |
vdrops[vdrops.length] = v; |
|
|
373 |
} |
|
|
374 |
}); |
|
|
375 |
|
|
|
376 |
this.validDrops = vdrops; |
|
|
377 |
}, |
|
|
378 |
/** |
|
|
379 |
* @method getDrop |
|
|
380 |
* @description Get a valid Drop instance back from a Node or a selector string, false otherwise |
|
|
381 |
* @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object |
|
|
382 |
* @return {Object} |
|
|
383 |
*/ |
|
|
384 |
getDrop: function(node) { |
|
|
385 |
var drop = false, |
|
|
386 |
n = Y.Node.get(node); |
|
|
387 |
if (n instanceof Y.Node) { |
|
|
388 |
Y.each(this.targets, function(v, k) { |
|
|
389 |
if (n.compareTo(v.get('node'))) { |
|
|
390 |
drop = v; |
|
|
391 |
} |
|
|
392 |
}); |
|
|
393 |
} |
|
|
394 |
return drop; |
|
|
395 |
} |
|
|
396 |
}, true); |
|
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
|
|
|
402 |
|
|
|
403 |
|
|
|
404 |
}, '3.0.0b1' ,{requires:['dd-ddm'], skinnable:false}); |