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-drop', function(Y) {
10
* 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.
15
* This class provides the ability to create a Drop Target.
23
OFFSET_HEIGHT = 'offsetHeight',
24
OFFSET_WIDTH = 'offsetWidth',
27
* @description Fires when a drag element is over this target.
31
EV_DROP_OVER = 'drop:over',
34
* @description Fires when a drag element enters this target.
38
EV_DROP_ENTER = 'drop:enter',
41
* @description Fires when a drag element exits this target.
45
EV_DROP_EXIT = 'drop:exit';
49
* @description Fires when a draggable node is dropped on this Drop Target. (Fired from dd-ddm-drop)
55
var Drop = function() {
56
Drop.superclass.constructor.apply(this, arguments);
61
if (Dom.getStyle(this.el, 'position') == 'fixed') {
62
Event.on(window, 'scroll', function() {
74
* @description Y.Node instanace to use as the element to make a Drop Target
79
var n = Y.Node.get(node);
81
Y.fail('DD.Drop: Invalid Node Given: ' + node);
88
* @description Array of groups to add this drop into.
95
Y.each(g, function(v, k) {
96
this._groups[v] = true;
102
* @description CSS style padding to make the Drop Target bigger than the node.
108
return DDM.cssSizestoObject(p);
113
* @description Set to lock this drop element.
118
set: function(lock) {
120
this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-locked');
122
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-locked');
128
Y.extend(Drop, Y.Base, {
131
* @method _createEvents
132
* @description This method creates all the events for this Event Target and publishes them so we get Event Bubbling.
134
_createEvents: function() {
143
Y.each(ev, function(v, k) {
159
* @description Flag for determining if the target is valid in this operation.
166
* @description The groups this target belongs to.
172
* @description Node reference to the targets shim
178
* @description A region object associated with this target, used for checking regions while dragging.
183
* @property overTarget
184
* @description This flag is tripped when a drag element is over this target.
190
* @description Check if this target is in one of the supplied groups.
191
* @param {Array} groups The groups to check against
194
inGroup: function(groups) {
197
Y.each(groups, function(v, k) {
198
if (this._groups[v]) {
207
* @method initializer
208
* @description Private lifecycle method
210
initializer: function() {
211
this._createEvents();
212
var node = this.get(NODE);
213
if (!node.get('id')) {
214
var id = Y.stamp(node);
217
node.addClass(DDM.CSS_PREFIX + '-drop');
222
* @description Lifecycle destructor, unreg the drag from the DDM and remove listeners
224
destructor: function() {
225
DDM._unregTarget(this);
227
this.shim.get('parentNode').removeChild(this.shim);
230
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop');
234
* @method _deactivateShim
235
* @description Removes classes from the target, resets some flags and sets the shims deactive position [-999, -999]
237
_deactivateShim: function() {
238
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
239
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
240
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
241
this.shim.setStyles({
245
this.overTarget = false;
249
* @method _activateShim
250
* @description Activates the shim and adds some interaction CSS classes
252
_activateShim: function() {
253
if (!DDM.activeDrag) {
254
return false; //Nothing is dragging, no reason to activate.
256
if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
259
if (this.get('lock')) {
262
var node = this.get(NODE);
263
//TODO Visibility Check..
264
//if (this.inGroup(DDM.activeDrag.get('groups')) && this.get(NODE).isVisible()) {
265
if (this.inGroup(DDM.activeDrag.get('groups'))) {
266
node.removeClass(DDM.CSS_PREFIX + '-drop-active-invalid');
267
node.addClass(DDM.CSS_PREFIX + '-drop-active-valid');
269
this.overTarget = false;
272
DDM._removeValid(this);
273
node.removeClass(DDM.CSS_PREFIX + '-drop-active-valid');
274
node.addClass(DDM.CSS_PREFIX + '-drop-active-invalid');
279
* @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..
281
sizeShim: function() {
282
if (!DDM.activeDrag) {
283
return false; //Nothing is dragging, no reason to activate.
285
if (this.get(NODE) === DDM.activeDrag.get(NODE)) {
288
if (this.get('lock')) {
291
var node = this.get(NODE),
292
nh = node.get(OFFSET_HEIGHT),
293
nw = node.get(OFFSET_WIDTH),
295
p = this.get('padding');
299
nw = nw + p.left + p.right;
300
nh = nh + p.top + p.bottom;
301
xy[0] = xy[0] - p.left;
302
xy[1] = xy[1] - p.top;
305
if (DDM.activeDrag.get('dragMode') === DDM.INTERSECT) {
306
//Intersect Mode, make the shim bigger
307
var dd = DDM.activeDrag,
308
dH = dd.get(NODE).get(OFFSET_HEIGHT),
309
dW = dd.get(NODE).get(OFFSET_WIDTH);
313
xy[0] = xy[0] - (dW - dd.deltaXY[0]);
314
xy[1] = xy[1] - (dH - dd.deltaXY[1]);
318
//Set the style on the shim
319
this.shim.setStyles({
326
//Create the region to be used by intersect when a drag node is over us.
339
* @method _createShim
340
* @description Creates the Target shim and adds it to the DDM's playground..
342
_createShim: function() {
343
//var s = Y.Node.create(['div', { id: this.get(NODE).get('id') + '_shim' }]);
344
var s = Y.Node.create('<div id="' + this.get(NODE).get('id') + '_shim"></div>');
346
height: this.get(NODE).get(OFFSET_HEIGHT) + 'px',
347
width: this.get(NODE).get(OFFSET_WIDTH) + 'px',
348
backgroundColor: 'yellow',
356
DDM._pg.appendChild(s);
359
s.on('mouseover', this._handleOverEvent, this, true);
360
s.on('mouseout', this._handleOutEvent, this, true);
364
* @method _handleOverTarget
365
* @description This handles the over target call made from this object or from the DDM
367
_handleTargetOver: function() {
368
if (DDM.isOverTarget(this)) {
369
this.get(NODE).addClass(DDM.CSS_PREFIX + '-drop-over');
370
DDM.activeDrop = this;
371
DDM.otherDrops[this] = this;
372
if (this.overTarget) {
373
DDM.activeDrag.fire('drag:over', { drop: this, drag: DDM.activeDrag });
374
this.fire(EV_DROP_OVER, { drop: this, drag: DDM.activeDrag });
376
this.overTarget = true;
377
this.fire(EV_DROP_ENTER, { drop: this, drag: DDM.activeDrag });
378
DDM.activeDrag.fire('drag:enter', { drop: this, drag: DDM.activeDrag });
379
DDM.activeDrag.get(NODE).addClass(DDM.CSS_PREFIX + '-drag-over');
380
DDM._handleTargetOver();
389
* @method _handleOverEvent
390
* @description Handles the mouseover DOM event on the Target Shim
392
_handleOverEvent: function() {
393
DDM._addActiveShim(this);
398
* @description Handles the mouseout DOM event on the Target Shim
400
_handleOutEvent: function() {
401
DDM._removeActiveShim(this);
406
* @description Handles out of target calls/checks
408
_handleOut: function() {
409
if (!DDM.isOverTarget(this)) {
410
if (this.overTarget) {
411
this.overTarget = false;
412
DDM._removeActiveShim(this);
413
if (DDM.activeDrag) {
414
this.get(NODE).removeClass(DDM.CSS_PREFIX + '-drop-over');
415
DDM.activeDrag.get(NODE).removeClass(DDM.CSS_PREFIX + '-drag-over');
416
this.fire(EV_DROP_EXIT);
417
DDM.activeDrag.fire('drag:exit', { drop: this });
418
delete DDM.otherDrops[this];
429
}, '3.0.0pr1' ,{requires:['dd-ddm-drop', 'dd-drag'], skinnable:false});