1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('dd-drop', function (Y, NAME) {
6
* Provides the ability to create a Drop Target.
11
* Provides the ability to create a Drop Target.
20
OFFSET_HEIGHT = 'offsetHeight',
21
OFFSET_WIDTH = 'offsetWidth',
23
* Fires when a drag element is over this target.
25
* @param {EventFacade} event An Event Facade object with the following specific property added:
27
* <dt>drop</dt><dd>The drop object at the time of the event.</dd>
28
* <dt>drag</dt><dd>The drag object at the time of the event.</dd>
33
EV_DROP_OVER = 'drop:over',
35
* Fires when a drag element enters this target.
37
* @param {EventFacade} event An Event Facade object with the following specific property added:
39
* <dt>drop</dt><dd>The drop object at the time of the event.</dd>
40
* <dt>drag</dt><dd>The drag object at the time of the event.</dd>
45
EV_DROP_ENTER = 'drop:enter',
47
* Fires when a drag element exits this target.
49
* @param {EventFacade} event An Event Facade object
53
EV_DROP_EXIT = 'drop:exit',
56
* Fires when a draggable node is dropped on this Drop Target. (Fired from dd-ddm-drop)
58
* @param {EventFacade} event An Event Facade object with the following specific property added:
60
* <dt>drop</dt><dd>The best guess on what was dropped on.</dd>
61
* <dt>drag</dt><dd>The drag object at the time of the event.</dd>
62
* <dt>others</dt><dd>An array of all the other drop targets that was dropped on.</dd>
70
this._lazyAddAttrs = false;
71
Drop.superclass.constructor.apply(this, arguments);
75
Y.on('domready', Y.bind(function() {
76
Y.later(100, this, this._createShim);
81
if (Dom.getStyle(this.el, 'position') == 'fixed') {
82
Event.on(window, 'scroll', function() {
93
* Y.Node instanace to use as the element to make a Drop Target
98
setter: function(node) {
101
Y.error('DD.Drop: Invalid Node Given: ' + node);
107
* Array of groups to add this drop into.
119
return Y.Object.keys(this._groups);
121
setter: function(g) {
122
this._groups = Y.Array.hash(g);
127
* CSS style padding to make the Drop Target bigger than the node.
133
setter: function(p) {
134
return DDM.cssSizestoObject(p);
138
* Set to lock this drop element.
144
setter: function(lock) {
146
this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-locked');
148
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-locked');
154
* Controls the default bubble parent for this Drop instance. Default: Y.DD.DDM. Set to false to disable bubbling.
155
* Use bubbleTargets in config.
161
setter: function(t) {
167
* Use the Drop shim. Default: true
174
setter: function(v) {
175
Y.DD.DDM._noShim = !v;
181
Y.extend(Drop, Y.Base, {
183
* The default bubbleTarget for this object. Default: Y.DD.DDM
185
* @property _bubbleTargets
187
_bubbleTargets: Y.DD.DDM,
189
* Add this Drop instance to a group, this should be used for on-the-fly group additions.
191
* @param {String} g The group to add this Drop Instance to.
195
addToGroup: function(g) {
196
this._groups[g] = true;
200
* Remove this Drop instance from a group, this should be used for on-the-fly group removals.
201
* @method removeFromGroup
202
* @param {String} g The group to remove this Drop Instance from.
206
removeFromGroup: function(g) {
207
delete this._groups[g];
211
* This method creates all the events for this Event Target and publishes them so we get Event Bubbling.
213
* @method _createEvents
215
_createEvents: function() {
224
Y.Array.each(ev, function(v) {
236
* Flag for determining if the target is valid in this operation.
243
* The groups this target belongs to.
250
* Node reference to the targets shim
256
* A region object associated with this target, used for checking regions while dragging.
262
* This flag is tripped when a drag element is over this target.
263
* @property overTarget
268
* Check if this target is in one of the supplied groups.
270
* @param {Array} groups The groups to check against
273
inGroup: function(groups) {
276
Y.Array.each(groups, function(v) {
277
if (this._groups[v]) {
285
* Private lifecycle method
287
* @method initializer
289
initializer: function() {
290
Y.later(100, this, this._createEvents);
292
var node = this.get(NODE), id;
293
if (!node.get('id')) {
297
node.addClass(DDM.CSS_PREFIX + '-drop');
298
//Shouldn't have to do this..
299
this.set('groups', this.get('groups'));
302
* Lifecycle destructor, unreg the drag from the DDM and remove listeners
306
destructor: function() {
307
DDM._unregTarget(this);
308
if (this.shim && (this.shim !== this.get(NODE))) {
309
this.shim.detachAll();
313
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop');
317
* Removes classes from the target, resets some flags and sets the shims deactive position [-999, -999]
319
* @method _deactivateShim
321
_deactivateShim: function() {
325
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
326
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
327
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
329
if (this.get('useShim')) {
330
this.shim.setStyles({
336
this.overTarget = false;
339
* Activates the shim and adds some interaction CSS classes
341
* @method _activateShim
343
_activateShim: function() {
344
if (!DDM.activeDrag) {
345
return false; //Nothing is dragging, no reason to activate.
347
if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
350
if (this.get('lock')) {
353
var node = this.get(NODE);
354
//TODO Visibility Check..
355
//if (this.inGroup(DDM.activeDrag.get('groups')) && this.get(NODE).isVisible()) {
356
if (this.inGroup(DDM.activeDrag.get('groups'))) {
357
node.removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
358
node.addClass(DDM.CSS_PREFIX + '-drop-active-valid');
360
this.overTarget = false;
361
if (!this.get('useShim')) {
362
this.shim = this.get(NODE);
366
DDM._removeValid(this);
367
node.removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
368
node.addClass(DDM.CSS_PREFIX + '-drop-active-invalid');
372
* Positions and sizes the shim with the raw data from the node,
373
* this can be used to programatically adjust the Targets shim for Animation..
376
sizeShim: function() {
377
if (!DDM.activeDrag) {
378
return false; //Nothing is dragging, no reason to activate.
380
if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
383
//if (this.get('lock') || !this.get('useShim')) {
384
if (this.get('lock')) {
388
Y.later(100, this, this.sizeShim);
391
var node = this.get(NODE),
392
nh = node.get(OFFSET_HEIGHT),
393
nw = node.get(OFFSET_WIDTH),
395
p = this.get('padding'),
400
nw = nw + p.left + p.right;
401
nh = nh + p.top + p.bottom;
402
xy[0] = xy[0] - p.left;
403
xy[1] = xy[1] - p.top;
406
if (DDM.activeDrag.get('dragMode') === DDM.INTERSECT) {
407
//Intersect Mode, make the shim bigger
409
dH = dd.get(NODE).get(OFFSET_HEIGHT);
410
dW = dd.get(NODE).get(OFFSET_WIDTH);
414
xy[0] = xy[0] - (dW - dd.deltaXY[0]);
415
xy[1] = xy[1] - (dH - dd.deltaXY[1]);
419
if (this.get('useShim')) {
420
//Set the style on the shim
421
this.shim.setStyles({
429
//Create the region to be used by intersect when a drag node is over us.
441
* Creates the Target shim and adds it to the DDM's playground..
443
* @method _createShim
445
_createShim: function() {
446
//No playground, defer
448
Y.later(10, this, this._createShim);
451
//Shim already here, cancel
455
var s = this.get('node');
457
if (this.get('useShim')) {
458
s = Y.Node.create('<div id="' + this.get(NODE).get('id') + '_shim"></div>');
460
height: this.get(NODE).get(OFFSET_HEIGHT) + 'px',
461
width: this.get(NODE).get(OFFSET_WIDTH) + 'px',
462
backgroundColor: 'yellow',
471
DDM._pg.appendChild(s);
473
s.on('mouseover', Y.bind(this._handleOverEvent, this));
474
s.on('mouseout', Y.bind(this._handleOutEvent, this));
481
* This handles the over target call made from this object or from the DDM
483
* @method _handleOverTarget
485
_handleTargetOver: function() {
486
if (DDM.isOverTarget(this)) {
487
this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-over');
488
DDM.activeDrop = this;
489
DDM.otherDrops[this] = this;
490
if (this.overTarget) {
491
DDM.activeDrag.fire('drag:over', { drop: this, drag: DDM.activeDrag });
492
this.fire(EV_DROP_OVER, { drop: this, drag: DDM.activeDrag });
494
//Prevent an enter before a start..
495
if (DDM.activeDrag.get('dragging')) {
496
this.overTarget = true;
497
this.fire(EV_DROP_ENTER, { drop: this, drag: DDM.activeDrag });
498
DDM.activeDrag.fire('drag:enter', { drop: this, drag: DDM.activeDrag });
499
DDM.activeDrag.get(NODE).addClass(DDM.CSS_PREFIX + '-drag-over');
500
//TODO - Is this needed??
501
//DDM._handleTargetOver();
509
* Handles the mouseover DOM event on the Target Shim
511
* @method _handleOverEvent
513
_handleOverEvent: function() {
514
this.shim.setStyle('zIndex', '999');
515
DDM._addActiveShim(this);
518
* Handles the mouseout DOM event on the Target Shim
520
* @method _handleOutEvent
522
_handleOutEvent: function() {
523
this.shim.setStyle('zIndex', '1');
524
DDM._removeActiveShim(this);
527
* Handles out of target calls/checks
531
_handleOut: function(force) {
532
if (!DDM.isOverTarget(this) || force) {
533
if (this.overTarget) {
534
this.overTarget = false;
536
DDM._removeActiveShim(this);
538
if (DDM.activeDrag) {
539
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
540
DDM.activeDrag.get(NODE).removeClass(DDM.CSS_PREFIX + '-drag-over');
541
this.fire(EV_DROP_EXIT, { drop: this, drag: DDM.activeDrag });
542
DDM.activeDrag.fire('drag:exit', { drop: this, drag: DDM.activeDrag });
543
delete DDM.otherDrops[this];
555
}, '3.9.1', {"requires": ["dd-drag", "dd-ddm-drop"]});