2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('dd-drop', function(Y) {
12 * The Drag & Drop Utility allows you to create a draggable interface efficiently, buffering you from browser-level abnormalities and enabling you to focus on the interesting logic surrounding your particular implementation. This component enables you to create a variety of standard draggable objects with just a few lines of code and then, using its extensive API, add your own specific implementation logic.
17 * This class provides the ability to create a Drop Target.
26 OFFSET_HEIGHT = 'offsetHeight',
27 OFFSET_WIDTH = 'offsetWidth',
30 * @description Fires when a drag element is over this target.
32 * @type {Event.Custom}
34 EV_DROP_OVER = 'drop:over',
37 * @description Fires when a drag element enters this target.
39 * @type {Event.Custom}
41 EV_DROP_ENTER = 'drop:enter',
44 * @description Fires when a drag element exits this target.
46 * @type {Event.Custom}
48 EV_DROP_EXIT = 'drop:exit',
52 * @description Fires when a draggable node is dropped on this Drop Target. (Fired from dd-ddm-drop)
54 * @type {Event.Custom}
59 this._lazyAddAttrs = false;
60 Drop.superclass.constructor.apply(this, arguments);
64 Y.on('domready', Y.bind(function() {
65 Y.later(100, this, this._createShim);
70 if (Dom.getStyle(this.el, 'position') == 'fixed') {
71 Event.on(window, 'scroll', function() {
83 * @description Y.Node instanace to use as the element to make a Drop Target
87 setter: function(node) {
88 var n = Y.Node.get(node);
90 Y.error('DD.Drop: Invalid Node Given: ' + node);
97 * @description Array of groups to add this drop into.
102 setter: function(g) {
104 Y.each(g, function(v, k) {
105 this._groups[v] = true;
112 * @description CSS style padding to make the Drop Target bigger than the node.
117 setter: function(p) {
118 return DDM.cssSizestoObject(p);
123 * @description Set to lock this drop element.
128 setter: function(lock) {
130 this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-locked');
132 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-locked');
139 * @description Controls the default bubble parent for this Drop instance. Default: Y.DD.DDM. Set to false to disable bubbling.
148 Y.extend(Drop, Y.Base, {
151 * @method _createEvents
152 * @description This method creates all the events for this Event Target and publishes them so we get Event Bubbling.
154 _createEvents: function() {
163 Y.each(ev, function(v, k) {
174 if (this.get('bubbles')) {
175 this.addTarget(this.get('bubbles'));
182 * @description Flag for determining if the target is valid in this operation.
189 * @description The groups this target belongs to.
195 * @description Node reference to the targets shim
201 * @description A region object associated with this target, used for checking regions while dragging.
206 * @property overTarget
207 * @description This flag is tripped when a drag element is over this target.
213 * @description Check if this target is in one of the supplied groups.
214 * @param {Array} groups The groups to check against
217 inGroup: function(groups) {
220 Y.each(groups, function(v, k) {
221 if (this._groups[v]) {
230 * @method initializer
231 * @description Private lifecycle method
233 initializer: function() {
234 //this._createEvents();
235 Y.later(100, this, this._createEvents);
237 var node = this.get(NODE), id;
238 if (!node.get('id')) {
242 node.addClass(DDM.CSS_PREFIX + '-drop');
243 //Shouldn't have to do this..
244 this.set('groups', this.get('groups'));
249 * @description Lifecycle destructor, unreg the drag from the DDM and remove listeners
251 destructor: function() {
252 DDM._unregTarget(this);
254 this.shim.detachAll();
255 this.shim.get('parentNode').removeChild(this.shim);
258 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop');
263 * @method _deactivateShim
264 * @description Removes classes from the target, resets some flags and sets the shims deactive position [-999, -999]
266 _deactivateShim: function() {
270 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
271 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
272 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
273 this.shim.setStyles({
278 this.overTarget = false;
282 * @method _activateShim
283 * @description Activates the shim and adds some interaction CSS classes
285 _activateShim: function() {
286 if (!DDM.activeDrag) {
287 return false; //Nothing is dragging, no reason to activate.
289 if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
292 if (this.get('lock')) {
295 var node = this.get(NODE);
296 //TODO Visibility Check..
297 //if (this.inGroup(DDM.activeDrag.get('groups')) && this.get(NODE).isVisible()) {
298 if (this.inGroup(DDM.activeDrag.get('groups'))) {
299 node.removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
300 node.addClass(DDM.CSS_PREFIX + '-drop-active-valid');
302 this.overTarget = false;
305 DDM._removeValid(this);
306 node.removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
307 node.addClass(DDM.CSS_PREFIX + '-drop-active-invalid');
312 * @description Positions and sizes the shim with the raw data from the node, this can be used to programatically adjust the Targets shim for Animation..
314 sizeShim: function() {
315 if (!DDM.activeDrag) {
316 return false; //Nothing is dragging, no reason to activate.
318 if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
321 if (this.get('lock')) {
325 Y.later(100, this, this.sizeShim);
328 var node = this.get(NODE),
329 nh = node.get(OFFSET_HEIGHT),
330 nw = node.get(OFFSET_WIDTH),
332 p = this.get('padding'),
337 nw = nw + p.left + p.right;
338 nh = nh + p.top + p.bottom;
339 xy[0] = xy[0] - p.left;
340 xy[1] = xy[1] - p.top;
343 if (DDM.activeDrag.get('dragMode') === DDM.INTERSECT) {
344 //Intersect Mode, make the shim bigger
346 dH = dd.get(NODE).get(OFFSET_HEIGHT);
347 dW = dd.get(NODE).get(OFFSET_WIDTH);
351 xy[0] = xy[0] - (dW - dd.deltaXY[0]);
352 xy[1] = xy[1] - (dH - dd.deltaXY[1]);
356 //Set the style on the shim
357 this.shim.setStyles({
364 //Create the region to be used by intersect when a drag node is over us.
377 * @method _createShim
378 * @description Creates the Target shim and adds it to the DDM's playground..
380 _createShim: function() {
381 //No playground, defer
383 Y.later(10, this, this._createShim);
386 //Shim already here, cancel
390 var s = Y.Node.create('<div id="' + this.get(NODE).get('id') + '_shim"></div>');
393 height: this.get(NODE).get(OFFSET_HEIGHT) + 'px',
394 width: this.get(NODE).get(OFFSET_WIDTH) + 'px',
395 backgroundColor: 'yellow',
403 DDM._pg.appendChild(s);
406 s.on('mouseover', Y.bind(this._handleOverEvent, this));
407 s.on('mouseout', Y.bind(this._handleOutEvent, this));
411 * @method _handleOverTarget
412 * @description This handles the over target call made from this object or from the DDM
414 _handleTargetOver: function() {
415 if (DDM.isOverTarget(this)) {
416 this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-over');
417 DDM.activeDrop = this;
418 DDM.otherDrops[this] = this;
419 if (this.overTarget) {
420 DDM.activeDrag.fire('drag:over', { drop: this, drag: DDM.activeDrag });
421 this.fire(EV_DROP_OVER, { drop: this, drag: DDM.activeDrag });
423 this.overTarget = true;
424 this.fire(EV_DROP_ENTER, { drop: this, drag: DDM.activeDrag });
425 DDM.activeDrag.fire('drag:enter', { drop: this, drag: DDM.activeDrag });
426 DDM.activeDrag.get(NODE).addClass(DDM.CSS_PREFIX + '-drag-over');
427 //TODO - Is this needed??
428 //DDM._handleTargetOver();
436 * @method _handleOverEvent
437 * @description Handles the mouseover DOM event on the Target Shim
439 _handleOverEvent: function() {
440 this.shim.setStyle('zIndex', '999');
441 DDM._addActiveShim(this);
445 * @method _handleOutEvent
446 * @description Handles the mouseout DOM event on the Target Shim
448 _handleOutEvent: function() {
449 this.shim.setStyle('zIndex', '1');
450 DDM._removeActiveShim(this);
455 * @description Handles out of target calls/checks
457 _handleOut: function(force) {
458 if (!DDM.isOverTarget(this) || force) {
459 if (this.overTarget) {
460 this.overTarget = false;
462 DDM._removeActiveShim(this);
464 if (DDM.activeDrag) {
465 this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
466 DDM.activeDrag.get(NODE).removeClass(DDM.CSS_PREFIX + '-drag-over');
467 this.fire(EV_DROP_EXIT);
468 DDM.activeDrag.fire('drag:exit', { drop: this });
469 delete DDM.otherDrops[this];
470 //if (DDM.activeDrop === this) {
471 // DDM.activeDrop = null;
485 }, '3.0.0' ,{requires:['dd-ddm-drop', 'dd-drag'], skinnable:false});