2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('dd-ddm-drop', function(Y) {
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.
14 * @submodule dd-ddm-drop
19 //TODO CSS class name for the bestMatch..
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.
30 * @property _activeShims
31 * @description Placeholder for all active shims on the page
37 * @method _hasActiveShim
38 * @description This method checks the _activeShims Object to see if there is a shim active.
41 _hasActiveShim: function() {
45 return this._activeShims.length;
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.
53 _addActiveShim: function(d) {
54 this._activeShims[this._activeShims.length] = d;
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.
62 _removeActiveShim: function(d) {
64 Y.each(this._activeShims, function(v, k) {
65 if (v._yuid !== d._yuid) {
70 this._activeShims = s;
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.
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) {
88 * @description The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
95 * @description In point mode, a Drop is targeted by the cursor being over the Target
101 * @property INTERSECT
102 * @description In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
109 * @description In strict mode, a Drop is targeted by the "entire" drag node being over the Target
115 * @description Should we only check targets that are in the viewport on drags (for performance), default: true
120 * @property activeDrop
121 * @description A reference to the active Drop Target
126 * @property validDrops
127 * @description An array of the valid Drop Targets for this interaction.
130 //TODO Change array/object literals to be in sync..
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)
140 * @description All of the Targets
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
152 _addValid: function(drop) {
153 this.validDrops[this.validDrops.length] = drop;
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
164 _removeValid: function(drop) {
166 Y.each(this.validDrops, function(v, k) {
168 drops[drops.length] = v;
172 this.validDrops = drops;
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
181 isOverTarget: function(drop) {
182 if (this.activeDrag && drop) {
183 var xy = this.activeDrag.mouseXY, r, dMode = this.activeDrag.get('dragMode'),
185 if (xy && this.activeDrag) {
186 aRegion = this.activeDrag.region;
187 if (dMode == this.STRICT) {
188 return this.activeDrag.get('dragNode').inRegion(drop.region, true, aRegion);
190 if (drop && drop.shim) {
191 if ((dMode == this.INTERSECT) && this._noShim) {
192 r = ((aRegion) ? aRegion : this.activeDrag.get('node'));
193 return drop.get('node').intersect(r).inRegion;
195 return drop.shim.intersect({
200 }, drop.region).inRegion;
215 * @description Clears the cache data used for this interaction.
217 clearCache: function() {
218 this.validDrops = [];
219 this.otherDrops = {};
220 this._activeShims = [];
224 * @method _activateTargets
225 * @description Clear the cache and activate the shims of all the targets
227 _activateTargets: function() {
229 Y.each(this.targets, function(v, k) {
230 v._activateShim.apply(v, []);
232 this._handleTargetOver();
236 * @method getBestMatch
237 * @description This method will gather the area for all potential targets and see which has the hightest covered area and return it.
238 * @param {Array} drops An Array of drops to scan for the best match.
239 * @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.
240 * @return {Object or Array}
242 getBestMatch: function(drops, all) {
243 var biggest = null, area = 0, out;
245 Y.each(drops, function(v, k) {
246 var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
247 v.region.area = inter.area;
249 if (inter.inRegion) {
250 if (inter.area > area) {
258 //TODO Sort the others in numeric order by area covered..
259 Y.each(drops, function(v, k) {
264 return [biggest, out];
271 * @method _deactivateTargets
272 * @description This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
274 _deactivateTargets: function() {
276 activeDrag = this.activeDrag,
277 activeDrop = this.activeDrop;
279 //TODO why is this check so hard??
280 if (activeDrag && activeDrop && this.otherDrops[activeDrop]) {
281 if (!activeDrag.get('dragMode')) {
282 //TODO otherDrops -- private..
283 other = this.otherDrops;
284 delete other[activeDrop];
286 tmp = this.getBestMatch(this.otherDrops, true);
290 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
292 activeDrop.fire('drop:hit', { drag: activeDrag, drop: activeDrop, others: other });
293 activeDrag.fire('drag:drophit', { drag: activeDrag, drop: activeDrop, others: other });
295 } else if (activeDrag) {
296 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
297 activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
301 this.activeDrop = null;
303 Y.each(this.targets, function(v, k) {
304 v._deactivateShim.apply(v, []);
310 * @description This method is called when the move method is called on the Drag Object.
312 _dropMove: function() {
313 if (this._hasActiveShim()) {
314 this._handleTargetOver();
316 Y.each(this.otherDrops, function(v, k) {
317 v._handleOut.apply(v, []);
324 * @description Filters the list of Drops down to those in the viewport.
325 * @return {Array} The valid Drop Targets that are in the viewport.
327 _lookup: function() {
328 if (!this.useHash || this._noShim) {
329 return this.validDrops;
332 //Only scan drop shims that are in the Viewport
333 Y.each(this.validDrops, function(v, k) {
334 if (v.shim && v.shim.inViewportRegion(false, v.region)) {
335 drops[drops.length] = v;
343 * @method _handleTargetOver
344 * @description This method execs _handleTargetOver on all valid Drop Targets
346 _handleTargetOver: function() {
347 var drops = this._lookup();
348 Y.each(drops, function(v, k) {
349 v._handleTargetOver.call(v);
355 * @description Add the passed in Target to the targets collection
356 * @param {Object} t The Target to add to the targets collection
358 _regTarget: function(t) {
359 this.targets[this.targets.length] = t;
363 * @method _unregTarget
364 * @description Remove the passed in Target from the targets collection
365 * @param {Object} drop The Target to remove from the targets collection
367 _unregTarget: function(drop) {
368 var targets = [], vdrops;
369 Y.each(this.targets, function(v, k) {
371 targets[targets.length] = v;
374 this.targets = targets;
377 Y.each(this.validDrops, function(v, k) {
379 vdrops[vdrops.length] = v;
383 this.validDrops = vdrops;
387 * @description Get a valid Drop instance back from a Node or a selector string, false otherwise
388 * @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object
391 getDrop: function(node) {
393 n = Y.Node.get(node);
394 if (n instanceof Y.Node) {
395 Y.each(this.targets, function(v, k) {
396 if (n.compareTo(v.get('node'))) {
411 }, '3.0.0' ,{requires:['dd-ddm'], skinnable:false});