1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('dd-ddm-drop', function (Y, NAME) {
6
* Extends the dd-ddm Class to add support for the placement of Drop Target
7
* shims inside the viewport shim. It also handles all Drop Target related events and interactions.
9
* @submodule dd-ddm-drop
14
//TODO CSS class name for the bestMatch..
17
* This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing.
24
* Placeholder for all active shims on the page
26
* @property _activeShims
31
* This method checks the _activeShims Object to see if there is a shim active.
33
* @method _hasActiveShim
36
_hasActiveShim: function() {
40
return this._activeShims.length;
43
* Adds a Drop Target to the list of active shims
45
* @method _addActiveShim
46
* @param {Object} d The Drop instance to add to the list.
48
_addActiveShim: function(d) {
49
this._activeShims.push(d);
52
* Removes a Drop Target to the list of active shims
54
* @method _removeActiveShim
55
* @param {Object} d The Drop instance to remove from the list.
57
_removeActiveShim: function(d) {
59
Y.Array.each(this._activeShims, function(v) {
60
if (v._yuid !== d._yuid) {
65
this._activeShims = s;
68
* This method will sync the position of the shims on the Drop Targets that are currently active.
69
* @method syncActiveShims
70
* @param {Boolean} force Resize/sync all Targets.
72
syncActiveShims: function(force) {
73
Y.later(0, this, function(force) {
74
var drops = ((force) ? this.targets : this._lookup());
75
Y.Array.each(drops, function(v) {
81
* The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
88
* In point mode, a Drop is targeted by the cursor being over the Target
95
* In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
102
* In strict mode, a Drop is targeted by the "entire" drag node being over the Target
109
* Should we only check targets that are in the viewport on drags (for performance), default: true
115
* A reference to the active Drop Target
116
* @property activeDrop
121
* An array of the valid Drop Targets for this interaction.
122
* @property validDrops
125
//TODO Change array/object literals to be in sync..
128
* An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets)
129
* @property otherDrops
140
* Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation.
143
* @param {Object} drop
147
_addValid: function(drop) {
148
this.validDrops.push(drop);
152
* Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation.
154
* @method _removeValid
155
* @param {Object} drop
159
_removeValid: function(drop) {
161
Y.Array.each(this.validDrops, function(v) {
167
this.validDrops = drops;
171
* Check to see if the Drag element is over the target, method varies on current mode
172
* @method isOverTarget
173
* @param {Object} drop The drop to check against
176
isOverTarget: function(drop) {
177
if (this.activeDrag && drop) {
178
var xy = this.activeDrag.mouseXY, r, dMode = this.activeDrag.get('dragMode'),
179
aRegion, node = drop.shim;
180
if (xy && this.activeDrag) {
181
aRegion = this.activeDrag.region;
182
if (dMode === this.STRICT) {
183
return this.activeDrag.get('dragNode').inRegion(drop.region, true, aRegion);
185
if (drop && drop.shim) {
186
if ((dMode === this.INTERSECT) && this._noShim) {
187
r = aRegion || this.activeDrag.get('node');
188
return drop.get('node').intersect(r, drop.region).inRegion;
192
node = drop.get('node');
194
return node.intersect({
199
}, drop.region).inRegion;
206
* Clears the cache data used for this interaction.
209
clearCache: function() {
210
this.validDrops = [];
211
this.otherDrops = {};
212
this._activeShims = [];
215
* Clear the cache and activate the shims of all the targets
217
* @method _activateTargets
219
_activateTargets: function() {
222
Y.Array.each(this.targets, function(v) {
224
if (v.get('noShim') === true) {
225
this._noShim = false;
228
this._handleTargetOver();
232
* This method will gather the area for all potential targets and see which has the hightest covered area and return it.
233
* @method getBestMatch
234
* @param {Array} drops An Array of drops to scan for the best match.
235
* @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.
236
* @return {Object or Array}
238
getBestMatch: function(drops, all) {
239
var biggest = null, area = 0, out;
241
Y.Array.each(drops, function(v) {
242
var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
243
v.region.area = inter.area;
245
if (inter.inRegion) {
246
if (inter.area > area) {
254
//TODO Sort the others in numeric order by area covered..
255
Y.Array.each(drops, function(v) {
260
return [biggest, out];
265
* This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
267
* @method _deactivateTargets
269
_deactivateTargets: function() {
271
activeDrag = this.activeDrag,
272
activeDrop = this.activeDrop;
274
//TODO why is this check so hard??
275
if (activeDrag && activeDrop && this.otherDrops[activeDrop]) {
276
if (!activeDrag.get('dragMode')) {
277
//TODO otherDrops -- private..
278
other = this.otherDrops;
279
delete other[activeDrop];
281
tmp = this.getBestMatch(this.otherDrops, true);
285
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
287
activeDrop.fire('drop:hit', { drag: activeDrag, drop: activeDrop, others: other });
288
activeDrag.fire('drag:drophit', { drag: activeDrag, drop: activeDrop, others: other });
290
} else if (activeDrag && activeDrag.get('dragging')) {
291
activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over');
292
activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
295
this.activeDrop = null;
297
Y.Array.each(this.targets, function(v) {
298
v._deactivateShim([]);
302
* This method is called when the move method is called on the Drag Object.
306
_dropMove: function() {
307
if (this._hasActiveShim()) {
308
this._handleTargetOver();
310
Y.Array.each(this.otherDrops, function(v) {
311
v._handleOut.apply(v, []);
316
* Filters the list of Drops down to those in the viewport.
319
* @return {Array} The valid Drop Targets that are in the viewport.
321
_lookup: function() {
322
if (!this.useHash || this._noShim) {
323
return this.validDrops;
326
//Only scan drop shims that are in the Viewport
327
Y.Array.each(this.validDrops, function(v) {
328
if (v.shim && v.shim.inViewportRegion(false, v.region)) {
336
* This method execs _handleTargetOver on all valid Drop Targets
338
* @method _handleTargetOver
340
_handleTargetOver: function() {
341
var drops = this._lookup();
342
Y.Array.each(drops, function(v) {
343
v._handleTargetOver.call(v);
347
* Add the passed in Target to the targets collection
350
* @param {Object} t The Target to add to the targets collection
352
_regTarget: function(t) {
353
this.targets.push(t);
356
* Remove the passed in Target from the targets collection
358
* @method _unregTarget
359
* @param {Object} drop The Target to remove from the targets collection
361
_unregTarget: function(drop) {
362
var targets = [], vdrops;
363
Y.Array.each(this.targets, function(v) {
368
this.targets = targets;
371
Y.Array.each(this.validDrops, function(v) {
377
this.validDrops = vdrops;
380
* Get a valid Drop instance back from a Node or a selector string, false otherwise
382
* @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object
385
getDrop: function(node) {
388
if (n instanceof Y.Node) {
389
Y.Array.each(this.targets, function(v) {
390
if (n.compareTo(v.get('node'))) {
402
}, '3.9.1', {"requires": ["dd-ddm"]});