2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('dd-ddm-drop', function(Y) {
10
* 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.
12
* @submodule dd-ddm-drop
16
//TODO CSS class name for the bestMatch..
21
* @description This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing.
27
* @property _activeShims
28
* @description Placeholder for all active shims on the page
34
* @method _hasActiveShim
35
* @description This method checks the _activeShims Object to see if there is a shim active.
38
_hasActiveShim: function() {
42
return this._activeShims.length;
46
* @method _addActiveShim
47
* @description Adds a Drop Target to the list of active shims
48
* @param {Object} d The Drop instance to add to the list.
50
_addActiveShim: function(d) {
51
this._activeShims[this._activeShims.length] = d;
55
* @method _removeActiveShim
56
* @description Removes a Drop Target to the list of active shims
57
* @param {Object} d The Drop instance to remove from the list.
59
_removeActiveShim: function(d) {
61
Y.each(this._activeShims, function(v, k) {
62
if (v._yuid !== d._yuid) {
67
this._activeShims = s;
70
* @method syncActiveShims
71
* @description This method will sync the position of the shims on the Drop Targets that are currently active.
72
* @param {Boolean} force Resize/sync all Targets.
73
* @return {Array} drops The list of Drop Targets that was just synced.
75
syncActiveShims: function(force) {
76
var drops = ((force) ? this.targets : this._lookup());
77
Y.each(drops, function(v, k) {
86
* @description The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
93
* @description In point mode, a Drop is targeted by the cursor being over the Target
100
* @description In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
107
* @description In strict mode, a Drop is targeted by the "entire" drag node being over the Target
113
* @description Should we only check targets that are in the viewport on drags (for performance), default: true
118
* @property activeDrop
119
* @description A reference to the active Drop Target
124
* @property validDrops
125
* @description An array of the valid Drop Targets for this interaction.
128
//TODO Change array/object literals to be in sync..
131
* @property otherDrops
132
* @description An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets)
138
* @description All of the Targets
145
* @description Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation.
146
* @param {Object} drop
150
_addValid: function(drop) {
151
this.validDrops[this.validDrops.length] = drop;
156
* @method _removeValid
157
* @description Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation.
158
* @param {Object} drop
162
_removeValid: function(drop) {
164
Y.each(this.validDrops, function(v, k) {
166
drops[drops.length] = v
170
this.validDrops = drops;
174
* @method isOverTarget
175
* @description Check to see if the Drag element is over the target, method varies on current mode
176
* @param {Object} drop The drop to check against
179
isOverTarget: function(drop) {
180
//if (Y.Lang.isObject(this.activeDrag) && drop) { //TODO, check this check..
181
if (this.activeDrag && drop) {
182
var xy = this.activeDrag.mouseXY;
184
if (this.activeDrag.get('dragMode') == this.STRICT) {
185
return this.activeDrag.get('dragNode').inRegion(drop.region, true, this.activeDrag.region);
187
if (drop && drop.shim) {
188
return drop.shim.intersect({
193
}, drop.region).inRegion;
207
* @description Clears the cache data used for this interaction.
209
clearCache: function() {
210
this.validDrops = [];
211
this.otherDrops = {};
212
this._activeShims = [];
216
* @method _activateTargets
217
* @description Clear the cache and activate the shims of all the targets
219
_activateTargets: function() {
221
Y.each(this.targets, function(v, k) {
222
v._activateShim.apply(v, []);
224
this._handleTargetOver();
228
* @method getBestMatch
229
* @description This method will gather the area for all potential targets and see which has the hightest covered area and return it.
230
* @param {Array} drops An Array of drops to scan for the best match.
231
* @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.
232
* @return {Object or Array}
234
getBestMatch: function(drops, all) {
235
var biggest = null, area = 0;
237
Y.each(drops, function(v, k) {
238
var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
239
v.region.area = inter.area;
241
if (inter.inRegion) {
242
if (inter.area > area) {
250
//TODO Sort the others in numeric order by area covered..
251
Y.each(drops, function(v, k) {
256
return [biggest, out];
263
* @method _deactivateTargets
264
* @description This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
266
_deactivateTargets: function() {
268
activeDrag = this.activeDrag;
270
//TODO why is this check so hard??
271
if (activeDrag && !Y.Lang.isNull(this.activeDrop) && this.otherDrops[this.activeDrop]) {
272
if (!activeDrag.get('dragMode')) {
273
//TODO otherDrops -- private..
274
other = this.otherDrops;
275
delete other[this.activeDrop];
277
var tmp = this.getBestMatch(this.otherDrops, true);
278
this.activeDrop = tmp[0];
281
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over')
282
this.activeDrop.fire('drop:hit', { drag: activeDrag, drop: this.activeDrop, others: other });
283
activeDrag.fire('drag:drophit', { drag: activeDrag, drop: this.activeDrop, others: other });
284
} else if (this.activeDrag) {
285
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over')
286
activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
288
Y.log('No Active Drag', 'warn', 'dd-ddm');
291
this.activeDrop = null;
293
Y.each(this.targets, function(v, k) {
294
v._deactivateShim.apply(v, []);
300
* @description This method is called when the move method is called on the Drag Object.
302
_dropMove: function() {
303
if (this._hasActiveShim()) {
304
//Y.log('We have an active shim, check targets', 'info', 'dd-ddm');
305
this._handleTargetOver();
307
Y.each(this.otherDrops, function(v, k) {
308
v._handleOut.apply(v, []);
315
* @description Filters the list of Drops down to those in the viewport.
316
* @return {Array} The valid Drop Targets that are in the viewport.
318
_lookup: function() {
320
return this.validDrops;
323
//Only scan drop shims that are in the Viewport
324
Y.each(this.validDrops, function(v, k) {
325
if (v.shim && v.shim.inViewportRegion(false, v.region)) {
326
drops[drops.length] = v;
334
* @method _handleTargetOver
335
* @description This method execs _handleTargetOver on all valid Drop Targets
336
* @param {Boolean} force Force it to run the first time.
338
_handleTargetOver: function() {
339
var drops = this._lookup();
340
Y.each(drops, function(v, k) {
341
v._handleTargetOver.call(v);
347
* @description Add the passed in Target to the targets collection
348
* @param {Object} t The Target to add to the targets collection
350
_regTarget: function(t) {
351
this.targets[this.targets.length] = t;
355
* @method _unregTarget
356
* @description Remove the passed in Target from the targets collection
357
* @param {Object} drop The Target to remove from the targets collection
359
_unregTarget: function(drop) {
361
Y.each(this.targets, function(v, k) {
363
targets[targets.length] = v;
366
this.targets = targets;
369
Y.each(this.validDrops, function(v, k) {
371
vdrops[vdrops.length] = v
375
this.validDrops = vdrops;
379
* @description Get a valid Drop instance back from a Node or a selector string, false otherwise
380
* @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object
383
getDrop: function(node) {
385
n = Y.Node.get(node);
386
if (n instanceof Y.Node) {
387
Y.each(this.targets, function(v, k) {
388
if (n.compareTo(v.get('node'))) {
401
}, '3.0.0pr1' ,{requires:['dd-ddm'], skinnable:false});