2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('dd-ddm-drop', function (Y, NAME) {
12 * Extends the dd-ddm Class to add support for the placement of Drop Target
13 * shims inside the viewport shim. It also handles all Drop Target related events and interactions.
15 * @submodule dd-ddm-drop
20 //TODO CSS class name for the bestMatch..
23 * This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing.
30 * Placeholder for all active shims on the page
32 * @property _activeShims
37 * This method checks the _activeShims Object to see if there is a shim active.
39 * @method _hasActiveShim
42 _hasActiveShim: function() {
46 return this._activeShims.length;
49 * Adds a Drop Target to the list of active shims
51 * @method _addActiveShim
52 * @param {Object} d The Drop instance to add to the list.
54 _addActiveShim: function(d) {
55 this._activeShims.push(d);
58 * Removes a Drop Target to the list of active shims
60 * @method _removeActiveShim
61 * @param {Object} d The Drop instance to remove from the list.
63 _removeActiveShim: function(d) {
65 Y.Array.each(this._activeShims, function(v) {
66 if (v._yuid !== d._yuid) {
71 this._activeShims = s;
74 * This method will sync the position of the shims on the Drop Targets that are currently active.
75 * @method syncActiveShims
76 * @param {Boolean} force Resize/sync all Targets.
78 syncActiveShims: function(force) {
79 Y.later(0, this, function(force) {
80 var drops = ((force) ? this.targets : this._lookup());
81 Y.Array.each(drops, function(v) {
87 * The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
94 * In point mode, a Drop is targeted by the cursor being over the Target
101 * In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
103 * @property INTERSECT
108 * In strict mode, a Drop is targeted by the "entire" drag node being over the Target
115 * Should we only check targets that are in the viewport on drags (for performance), default: true
121 * A reference to the active Drop Target
122 * @property activeDrop
127 * An array of the valid Drop Targets for this interaction.
128 * @property validDrops
131 //TODO Change array/object literals to be in sync..
134 * An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets)
135 * @property otherDrops
146 * Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation.
149 * @param {Object} drop
153 _addValid: function(drop) {
154 this.validDrops.push(drop);
158 * Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation.
160 * @method _removeValid
161 * @param {Object} drop
165 _removeValid: function(drop) {
167 Y.Array.each(this.validDrops, function(v) {
173 this.validDrops = drops;
177 * Check to see if the Drag element is over the target, method varies on current mode
178 * @method isOverTarget
179 * @param {Object} drop The drop to check against
182 isOverTarget: function(drop) {
183 if (this.activeDrag && drop) {
184 var xy = this.activeDrag.mouseXY, r, dMode = this.activeDrag.get('dragMode'),
185 aRegion, node = drop.shim;
186 if (xy && this.activeDrag) {
187 aRegion = this.activeDrag.region;
188 if (dMode === this.STRICT) {
189 return this.activeDrag.get('dragNode').inRegion(drop.region, true, aRegion);
191 if (drop && drop.shim) {
192 if ((dMode === this.INTERSECT) && this._noShim) {
193 r = aRegion || this.activeDrag.get('node');
194 return drop.get('node').intersect(r, drop.region).inRegion;
198 node = drop.get('node');
200 return node.intersect({
205 }, drop.region).inRegion;
212 * Clears the cache data used for this interaction.
215 clearCache: function() {
216 this.validDrops = [];
217 this.otherDrops = {};
218 this._activeShims = [];
221 * Clear the cache and activate the shims of all the targets
223 * @method _activateTargets
225 _activateTargets: function() {
228 Y.Array.each(this.targets, function(v) {
230 if (v.get('noShim') === true) {
231 this._noShim = false;
234 this._handleTargetOver();
238 * This method will gather the area for all potential targets and see which has the hightest covered area and return it.
239 * @method getBestMatch
240 * @param {Array} drops An Array of drops to scan for the best match.
241 * @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.
242 * @return {Object or Array}
244 getBestMatch: function(drops, all) {
245 var biggest = null, area = 0, out;
247 Y.Array.each(drops, function(v) {
248 var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
249 v.region.area = inter.area;
251 if (inter.inRegion) {
252 if (inter.area > area) {
260 //TODO Sort the others in numeric order by area covered..
261 Y.Array.each(drops, function(v) {
266 return [biggest, out];
271 * This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
273 * @method _deactivateTargets
275 _deactivateTargets: function() {
277 activeDrag = this.activeDrag,
278 activeDrop = this.activeDrop;
280 //TODO why is this check so hard??
281 if (activeDrag && activeDrop && this.otherDrops[activeDrop]) {
282 if (!activeDrag.get('dragMode')) {
283 //TODO otherDrops -- private..
284 other = this.otherDrops;
285 delete other[activeDrop];
287 tmp = this.getBestMatch(this.otherDrops, true);
291 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
293 activeDrop.fire('drop:hit', { drag: activeDrag, drop: activeDrop, others: other });
294 activeDrag.fire('drag:drophit', { drag: activeDrag, drop: activeDrop, others: other });
296 } else if (activeDrag && activeDrag.get('dragging')) {
297 activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
298 activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
301 this.activeDrop = null;
303 Y.Array.each(this.targets, function(v) {
304 v._deactivateShim([]);
308 * 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.Array.each(this.otherDrops, function(v) {
317 v._handleOut.apply(v, []);
322 * 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.Array.each(this.validDrops, function(v) {
334 if (v.shim && v.shim.inViewportRegion(false, v.region)) {
342 * This method execs _handleTargetOver on all valid Drop Targets
344 * @method _handleTargetOver
346 _handleTargetOver: function() {
347 var drops = this._lookup();
348 Y.Array.each(drops, function(v) {
349 v._handleTargetOver.call(v);
353 * 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.push(t);
362 * Remove the passed in Target from the targets collection
364 * @method _unregTarget
365 * @param {Object} drop The Target to remove from the targets collection
367 _unregTarget: function(drop) {
368 var targets = [], vdrops;
369 Y.Array.each(this.targets, function(v) {
374 this.targets = targets;
377 Y.Array.each(this.validDrops, function(v) {
383 this.validDrops = vdrops;
386 * 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) {
394 if (n instanceof Y.Node) {
395 Y.Array.each(this.targets, function(v) {
396 if (n.compareTo(v.get('node'))) {
408 }, '3.13.0', {"requires": ["dd-ddm"]});