3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('dd-ddm-drop', function(Y) {
11 * 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 * @submodule dd-ddm-drop
18 //TODO CSS class name for the bestMatch..
23 * @description This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing.
29 * @property _activeShims
30 * @description Placeholder for all active shims on the page
36 * @method _hasActiveShim
37 * @description This method checks the _activeShims Object to see if there is a shim active.
40 _hasActiveShim: function() {
44 return this._activeShims.length;
48 * @method _addActiveShim
49 * @description Adds a Drop Target to the list of active shims
50 * @param {Object} d The Drop instance to add to the list.
52 _addActiveShim: function(d) {
53 this._activeShims[this._activeShims.length] = d;
57 * @method _removeActiveShim
58 * @description Removes a Drop Target to the list of active shims
59 * @param {Object} d The Drop instance to remove from the list.
61 _removeActiveShim: function(d) {
63 Y.each(this._activeShims, function(v, k) {
64 if (v._yuid !== d._yuid) {
69 this._activeShims = s;
72 * @method syncActiveShims
73 * @description This method will sync the position of the shims on the Drop Targets that are currently active.
74 * @param {Boolean} force Resize/sync all Targets.
76 syncActiveShims: function(force) {
77 Y.later(0, this, function(force) {
78 var drops = ((force) ? this.targets : this._lookup());
79 Y.each(drops, function(v, k) {
87 * @description The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
94 * @description In point mode, a Drop is targeted by the cursor being over the Target
100 * @property INTERSECT
101 * @description In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
108 * @description In strict mode, a Drop is targeted by the "entire" drag node being over the Target
114 * @description Should we only check targets that are in the viewport on drags (for performance), default: true
119 * @property activeDrop
120 * @description A reference to the active Drop Target
125 * @property validDrops
126 * @description An array of the valid Drop Targets for this interaction.
129 //TODO Change array/object literals to be in sync..
132 * @property otherDrops
133 * @description An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets)
139 * @description All of the Targets
146 * @description Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation.
147 * @param {Object} drop
151 _addValid: function(drop) {
152 this.validDrops[this.validDrops.length] = drop;
157 * @method _removeValid
158 * @description Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation.
159 * @param {Object} drop
163 _removeValid: function(drop) {
165 Y.each(this.validDrops, function(v, k) {
167 drops[drops.length] = v;
171 this.validDrops = drops;
175 * @method isOverTarget
176 * @description Check to see if the Drag element is over the target, method varies on current mode
177 * @param {Object} drop The drop to check against
180 isOverTarget: function(drop) {
181 if (this.activeDrag && drop) {
182 var xy = this.activeDrag.mouseXY, r, dMode = this.activeDrag.get('dragMode'),
183 aRegion, node = drop.shim;
184 if (xy && this.activeDrag) {
185 aRegion = this.activeDrag.region;
186 if (dMode == this.STRICT) {
187 return this.activeDrag.get('dragNode').inRegion(drop.region, true, aRegion);
189 if (drop && drop.shim) {
190 if ((dMode == this.INTERSECT) && this._noShim) {
191 r = ((aRegion) ? aRegion : this.activeDrag.get('node'));
192 return drop.get('node').intersect(r, drop.region).inRegion;
195 node = drop.get('node');
197 return node.intersect({
202 }, drop.region).inRegion;
217 * @description Clears the cache data used for this interaction.
219 clearCache: function() {
220 this.validDrops = [];
221 this.otherDrops = {};
222 this._activeShims = [];
226 * @method _activateTargets
227 * @description Clear the cache and activate the shims of all the targets
229 _activateTargets: function() {
232 Y.each(this.targets, function(v, k) {
234 if (v.get('noShim') == true) {
235 this._noShim = false;
238 this._handleTargetOver();
242 * @method getBestMatch
243 * @description This method will gather the area for all potential targets and see which has the hightest covered area and return it.
244 * @param {Array} drops An Array of drops to scan for the best match.
245 * @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.
246 * @return {Object or Array}
248 getBestMatch: function(drops, all) {
249 var biggest = null, area = 0, out;
251 Y.each(drops, function(v, k) {
252 var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
253 v.region.area = inter.area;
255 if (inter.inRegion) {
256 if (inter.area > area) {
264 //TODO Sort the others in numeric order by area covered..
265 Y.each(drops, function(v, k) {
270 return [biggest, out];
277 * @method _deactivateTargets
278 * @description This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
280 _deactivateTargets: function() {
282 activeDrag = this.activeDrag,
283 activeDrop = this.activeDrop;
285 //TODO why is this check so hard??
286 if (activeDrag && activeDrop && this.otherDrops[activeDrop]) {
287 if (!activeDrag.get('dragMode')) {
288 //TODO otherDrops -- private..
289 other = this.otherDrops;
290 delete other[activeDrop];
292 tmp = this.getBestMatch(this.otherDrops, true);
296 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
298 activeDrop.fire('drop:hit', { drag: activeDrag, drop: activeDrop, others: other });
299 activeDrag.fire('drag:drophit', { drag: activeDrag, drop: activeDrop, others: other });
301 } else if (activeDrag && activeDrag.get('dragging')) {
302 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
303 activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
307 this.activeDrop = null;
309 Y.each(this.targets, function(v, k) {
310 v._deactivateShim([]);
316 * @description This method is called when the move method is called on the Drag Object.
318 _dropMove: function() {
319 if (this._hasActiveShim()) {
320 this._handleTargetOver();
322 Y.each(this.otherDrops, function(v, k) {
323 v._handleOut.apply(v, []);
330 * @description Filters the list of Drops down to those in the viewport.
331 * @return {Array} The valid Drop Targets that are in the viewport.
333 _lookup: function() {
334 if (!this.useHash || this._noShim) {
335 return this.validDrops;
338 //Only scan drop shims that are in the Viewport
339 Y.each(this.validDrops, function(v, k) {
340 if (v.shim && v.shim.inViewportRegion(false, v.region)) {
341 drops[drops.length] = v;
349 * @method _handleTargetOver
350 * @description This method execs _handleTargetOver on all valid Drop Targets
352 _handleTargetOver: function() {
353 var drops = this._lookup();
354 Y.each(drops, function(v, k) {
355 v._handleTargetOver.call(v);
361 * @description Add the passed in Target to the targets collection
362 * @param {Object} t The Target to add to the targets collection
364 _regTarget: function(t) {
365 this.targets[this.targets.length] = t;
369 * @method _unregTarget
370 * @description Remove the passed in Target from the targets collection
371 * @param {Object} drop The Target to remove from the targets collection
373 _unregTarget: function(drop) {
374 var targets = [], vdrops;
375 Y.each(this.targets, function(v, k) {
377 targets[targets.length] = v;
380 this.targets = targets;
383 Y.each(this.validDrops, function(v, k) {
385 vdrops[vdrops.length] = v;
389 this.validDrops = vdrops;
393 * @description Get a valid Drop instance back from a Node or a selector string, false otherwise
394 * @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object
397 getDrop: function(node) {
400 if (n instanceof Y.Node) {
401 Y.each(this.targets, function(v, k) {
402 if (n.compareTo(v.get('node'))) {
414 }, '3.5.0' ,{skinnable:false, requires:['dd-ddm']});