Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('dd-drag', function (Y, NAME) {
2
 
3
 
4
    /**
5
     * Provides the ability to drag a Node.
6
     * @module dd
7
     * @submodule dd-drag
8
     */
9
    /**
10
     * Provides the ability to drag a Node.
11
     * @class Drag
12
     * @extends Base
13
     * @constructor
14
     * @namespace DD
15
     */
16
 
17
    var DDM = Y.DD.DDM,
18
        NODE = 'node',
19
        DRAGGING = 'dragging',
20
        DRAG_NODE = 'dragNode',
21
        OFFSET_HEIGHT = 'offsetHeight',
22
        OFFSET_WIDTH = 'offsetWidth',
23
        /**
24
        * Handles the mouseup DOM event, does nothing internally just fires.
25
        * @event drag:mouseup
26
        * @bubbles DDM
27
        * @type {CustomEvent}
28
        */
29
        /**
30
        * Handles the mousedown DOM event, checks to see if you have a valid handle then starts the drag timers.
31
        * @event drag:mouseDown
32
        * @preventable _defMouseDownFn
33
        * @param {EventFacade} event An Event Facade object with the following specific property added:
34
        * <dl><dt>ev</dt><dd>The original mousedown event.</dd></dl>
35
        * @bubbles DDM
36
        * @type {CustomEvent}
37
        */
38
        EV_MOUSE_DOWN = 'drag:mouseDown',
39
        /**
40
        * Fires after the mousedown event has been cleared.
41
        * @event drag:afterMouseDown
42
        * @param {EventFacade} event An Event Facade object with the following specific property added:
43
        * <dl><dt>ev</dt><dd>The original mousedown event.</dd></dl>
44
        * @bubbles DDM
45
        * @type {CustomEvent}
46
        */
47
        EV_AFTER_MOUSE_DOWN = 'drag:afterMouseDown',
48
        /**
49
        * Fires after a handle is removed.
50
        * @event drag:removeHandle
51
        * @param {EventFacade} event An Event Facade object with the following specific property added:
52
        * <dl><dt>handle</dt><dd>The handle that was removed.</dd></dl>
53
        * @bubbles DDM
54
        * @type {CustomEvent}
55
        */
56
        EV_REMOVE_HANDLE = 'drag:removeHandle',
57
        /**
58
        * Fires after a handle is added.
59
        * @event drag:addHandle
60
        * @param {EventFacade} event An Event Facade object with the following specific property added:
61
        * <dl><dt>handle</dt><dd>The handle that was added.</dd></dl>
62
        * @bubbles DDM
63
        * @type {CustomEvent}
64
        */
65
        EV_ADD_HANDLE = 'drag:addHandle',
66
        /**
67
        * Fires after an invalid selector is removed.
68
        * @event drag:removeInvalid
69
        * @param {EventFacade} event An Event Facade object with the following specific property added:
70
        * <dl><dt>handle</dt><dd>The handle that was removed.</dd></dl>
71
        * @bubbles DDM
72
        * @type {CustomEvent}
73
        */
74
        EV_REMOVE_INVALID = 'drag:removeInvalid',
75
        /**
76
        * Fires after an invalid selector is added.
77
        * @event drag:addInvalid
78
        * @param {EventFacade} event An Event Facade object with the following specific property added:
79
        * <dl><dt>handle</dt><dd>The handle that was added.</dd></dl>
80
        * @bubbles DDM
81
        * @type {CustomEvent}
82
        */
83
        EV_ADD_INVALID = 'drag:addInvalid',
84
        /**
85
        * Fires at the start of a drag operation.
86
        * @event drag:start
87
        * @param {EventFacade} event An Event Facade object with the following specific property added:
88
        * <dl>
89
        * <dt>pageX</dt><dd>The original node position X.</dd>
90
        * <dt>pageY</dt><dd>The original node position Y.</dd>
91
        * <dt>startTime</dt><dd>The startTime of the event. getTime on the current Date object.</dd>
92
        * </dl>
93
        * @bubbles DDM
94
        * @type {CustomEvent}
95
        */
96
        EV_START = 'drag:start',
97
        /**
98
        * Fires at the end of a drag operation.
99
        * @event drag:end
100
        * @param {EventFacade} event An Event Facade object with the following specific property added:
101
        * <dl>
102
        * <dt>pageX</dt><dd>The current node position X.</dd>
103
        * <dt>pageY</dt><dd>The current node position Y.</dd>
104
        * <dt>startTime</dt><dd>The startTime of the event, from the start event.</dd>
105
        * <dt>endTime</dt><dd>The endTime of the event. getTime on the current Date object.</dd>
106
        * </dl>
107
        * @bubbles DDM
108
        * @type {CustomEvent}
109
        */
110
        EV_END = 'drag:end',
111
        /**
112
        * Fires every mousemove during a drag operation.
113
        * @event drag:drag
114
        * @param {EventFacade} event An Event Facade object with the following specific property added:
115
        * <dl>
116
        * <dt>pageX</dt><dd>The current node position X.</dd>
117
        * <dt>pageY</dt><dd>The current node position Y.</dd>
118
        * <dt>scroll</dt><dd>Should a scroll action occur.</dd>
119
        * <dt>info</dt><dd>Object hash containing calculated XY arrays: start, xy, delta, offset</dd>
120
        * </dl>
121
        * @bubbles DDM
122
        * @type {CustomEvent}
123
        */
124
        EV_DRAG = 'drag:drag',
125
        /**
126
        * Fires when this node is aligned.
127
        * @event drag:align
128
        * @preventable _defAlignFn
129
        * @param {EventFacade} event An Event Facade object with the following specific property added:
130
        * <dl>
131
        * <dt>pageX</dt><dd>The current node position X.</dd>
132
        * <dt>pageY</dt><dd>The current node position Y.</dd>
133
        * </dl>
134
        * @bubbles DDM
135
        * @type {CustomEvent}
136
        */
137
        EV_ALIGN = 'drag:align',
138
        /**
139
        * Fires when this node is over a Drop Target. (Fired from dd-drop)
140
        * @event drag:over
141
        * @param {EventFacade} event An Event Facade object with the following specific property added:
142
        * <dl>
143
        * <dt>drop</dt><dd>The drop object at the time of the event.</dd>
144
        * <dt>drag</dt><dd>The drag object at the time of the event.</dd>
145
        * </dl>
146
        * @bubbles DDM
147
        * @type {CustomEvent}
148
        */
149
        /**
150
        * Fires when this node enters a Drop Target. (Fired from dd-drop)
151
        * @event drag:enter
152
        * @param {EventFacade} event An Event Facade object with the following specific property added:
153
        * <dl>
154
        * <dt>drop</dt><dd>The drop object at the time of the event.</dd>
155
        * <dt>drag</dt><dd>The drag object at the time of the event.</dd>
156
        * </dl>
157
        * @bubbles DDM
158
        * @type {CustomEvent}
159
        */
160
        /**
161
        * Fires when this node exits a Drop Target. (Fired from dd-drop)
162
        * @event drag:exit
163
        * @param {EventFacade} event An Event Facade object with the following specific property added:
164
        * <dl>
165
        * <dt>drop</dt><dd>The drop object at the time of the event.</dd>
166
        * </dl>
167
        * @bubbles DDM
168
        * @type {CustomEvent}
169
        */
170
        /**
171
        * Fires when this node is dropped on a valid Drop Target. (Fired from dd-ddm-drop)
172
        * @event drag:drophit
173
        * @param {EventFacade} event An Event Facade object with the following specific property added:
174
        * <dl>
175
        * <dt>drop</dt><dd>The best guess on what was dropped on.</dd>
176
        * <dt>drag</dt><dd>The drag object at the time of the event.</dd>
177
        * <dt>others</dt><dd>An array of all the other drop targets that was dropped on.</dd>
178
        * </dl>
179
        * @bubbles DDM
180
        * @type {CustomEvent}
181
        */
182
        /**
183
        * Fires when this node is dropped on an invalid Drop Target. (Fired from dd-ddm-drop)
184
        * @event drag:dropmiss
185
        * @param {EventFacade} event An Event Facade object with the following specific property added:
186
        * <dl>
187
        * <dt>pageX</dt><dd>The current node position X.</dd>
188
        * <dt>pageY</dt><dd>The current node position Y.</dd>
189
        * </dl>
190
        * @bubbles DDM
191
        * @type {CustomEvent}
192
        */
193
 
194
    Drag = function(o) {
195
        this._lazyAddAttrs = false;
196
        Drag.superclass.constructor.apply(this, arguments);
197
 
198
        var valid = DDM._regDrag(this);
199
        if (!valid) {
200
            Y.error('Failed to register node, already in use: ' + o.node);
201
        }
202
    };
203
 
204
    Drag.NAME = 'drag';
205
 
206
    /**
207
    * This property defaults to "mousedown", but when drag-gestures is loaded, it is changed to "gesturemovestart"
208
    * @static
209
    * @property START_EVENT
210
    */
211
    Drag.START_EVENT = 'mousedown';
212
 
213
    Drag.ATTRS = {
214
        /**
215
        * Y.Node instance to use as the element to initiate a drag operation
216
        * @attribute node
217
        * @type Node
218
        */
219
        node: {
220
            setter: function(node) {
221
                if (this._canDrag(node)) {
222
                    return node;
223
                }
224
                var n = Y.one(node);
225
                if (!n) {
226
                    Y.error('DD.Drag: Invalid Node Given: ' + node);
227
                }
228
                return n;
229
            }
230
        },
231
        /**
232
        * Y.Node instance to use as the draggable element, defaults to node
233
        * @attribute dragNode
234
        * @type Node
235
        */
236
        dragNode: {
237
            setter: function(node) {
238
                if (this._canDrag(node)) {
239
                    return node;
240
                }
241
                var n = Y.one(node);
242
                if (!n) {
243
                    Y.error('DD.Drag: Invalid dragNode Given: ' + node);
244
                }
245
                return n;
246
            }
247
        },
248
        /**
249
        * Offset the drag element by the difference in cursor position: default true
250
        * @attribute offsetNode
251
        * @type Boolean
252
        */
253
        offsetNode: {
254
            value: true
255
        },
256
        /**
257
        * Center the dragNode to the mouse position on drag:start: default false
258
        * @attribute startCentered
259
        * @type Boolean
260
        */
261
        startCentered: {
262
            value: false
263
        },
264
        /**
265
        * The number of pixels to move to start a drag operation, default is 3.
266
        * @attribute clickPixelThresh
267
        * @type Number
268
        */
269
        clickPixelThresh: {
270
            value: DDM.get('clickPixelThresh')
271
        },
272
        /**
273
        * The number of milliseconds a mousedown has to pass to start a drag operation, default is 1000.
274
        * @attribute clickTimeThresh
275
        * @type Number
276
        */
277
        clickTimeThresh: {
278
            value: DDM.get('clickTimeThresh')
279
        },
280
        /**
281
        * Set to lock this drag element so that it can't be dragged: default false.
282
        * @attribute lock
283
        * @type Boolean
284
        */
285
        lock: {
286
            value: false,
287
            setter: function(lock) {
288
                if (lock) {
289
                    this.get(NODE).addClass(DDM.CSS_PREFIX + '-locked');
290
                } else {
291
                    this.get(NODE).removeClass(DDM.CSS_PREFIX + '-locked');
292
                }
293
                return lock;
294
            }
295
        },
296
        /**
297
        * A payload holder to store arbitrary data about this drag object, can be used to store any value.
298
        * @attribute data
299
        * @type Mixed
300
        */
301
        data: {
302
            value: false
303
        },
304
        /**
305
        * If this is false, the drag element will not move with the cursor: default true. Can be used to "resize" the element.
306
        * @attribute move
307
        * @type Boolean
308
        */
309
        move: {
310
            value: true
311
        },
312
        /**
313
        * Use the protective shim on all drag operations: default true. Only works with dd-ddm, not dd-ddm-base.
314
        * @attribute useShim
315
        * @type Boolean
316
        */
317
        useShim: {
318
            value: true
319
        },
320
        /**
321
        * Config option is set by Drag to inform you of which handle fired the drag event (in the case that there are several handles): default false.
322
        * @attribute activeHandle
323
        * @type Node
324
        */
325
        activeHandle: {
326
            value: false
327
        },
328
        /**
329
        * By default a drag operation will only begin if the mousedown occurred with the primary mouse button.
330
        * Setting this to false will allow for all mousedown events to trigger a drag.
331
        * @attribute primaryButtonOnly
332
        * @type Boolean
333
        */
334
        primaryButtonOnly: {
335
            value: true
336
        },
337
        /**
338
        * This attribute is not meant to be used by the implementor, it is meant to be used as an Event tracker so you can listen for it to change.
339
        * @attribute dragging
340
        * @type Boolean
341
        */
342
        dragging: {
343
            value: false
344
        },
345
        parent: {
346
            value: false
347
        },
348
        /**
349
        * This attribute only works if the dd-drop module has been loaded. It will make this node a drop target as well as draggable.
350
        * @attribute target
351
        * @type Boolean
352
        */
353
        target: {
354
            value: false,
355
            setter: function(config) {
356
                this._handleTarget(config);
357
                return config;
358
            }
359
        },
360
        /**
361
        * This attribute only works if the dd-drop module is active. It will set the dragMode (point, intersect, strict) of this Drag instance.
362
        * @attribute dragMode
363
        * @type String
364
        */
365
        dragMode: {
366
            value: null,
367
            setter: function(mode) {
368
                return DDM._setDragMode(mode);
369
            }
370
        },
371
        /**
372
        * Array of groups to add this drag into.
373
        * @attribute groups
374
        * @type Array
375
        */
376
        groups: {
377
            value: ['default'],
378
            getter: function() {
379
                if (!this._groups) {
380
                    this._groups = {};
381
                    return [];
382
                }
383
 
384
                return Y.Object.keys(this._groups);
385
            },
386
            setter: function(g) {
387
                this._groups = Y.Array.hash(g);
388
                return g;
389
            }
390
        },
391
        /**
392
        * Array of valid handles to add. Adding something here will set all handles, even if previously added with addHandle
393
        * @attribute handles
394
        * @type Array
395
        */
396
        handles: {
397
            value: null,
398
            setter: function(g) {
399
                if (g) {
400
                    this._handles = {};
401
                    Y.Array.each(g, function(v) {
402
                        var key = v;
403
                        if (v instanceof Y.Node || v instanceof Y.NodeList) {
404
                            key = v._yuid;
405
                        }
406
                        this._handles[key] = v;
407
                    }, this);
408
                } else {
409
                    this._handles = null;
410
                }
411
                return g;
412
            }
413
        },
414
        /**
415
        * Controls the default bubble parent for this Drag instance. Default: Y.DD.DDM. Set to false to disable bubbling. Use bubbleTargets in config
416
        * @deprecated
417
        * @attribute bubbles
418
        * @type Object
419
        */
420
        bubbles: {
421
            setter: function(t) {
422
                this.addTarget(t);
423
                return t;
424
            }
425
        },
426
        /**
427
        * Should the mousedown event be halted. Default: true
428
        * @attribute haltDown
429
        * @type Boolean
430
        */
431
        haltDown: {
432
            value: true
433
        }
434
    };
435
 
436
    Y.extend(Drag, Y.Base, {
437
        /**
438
        * Checks the object for the methods needed to drag the object around.
439
        * Normally this would be a node instance, but in the case of Graphics, it
440
        * may be an SVG node or something similar.
441
        * @method _canDrag
442
        * @private
443
        * @param {Object} n The object to check
444
        * @return {Boolean} True or false if the Object contains the methods needed to Drag
445
        */
446
        _canDrag: function(n) {
447
            if (n && n.setXY && n.getXY && n.test && n.contains) {
448
                return true;
449
            }
450
            return false;
451
        },
452
        /**
453
        * The default bubbleTarget for this object. Default: Y.DD.DDM
454
        * @private
455
        * @property _bubbleTargets
456
        */
457
        _bubbleTargets: Y.DD.DDM,
458
        /**
459
        * Add this Drag instance to a group, this should be used for on-the-fly group additions.
460
        * @method addToGroup
461
        * @param {String} g The group to add this Drag Instance to.
462
        * @chainable
463
        */
464
        addToGroup: function(g) {
465
            this._groups[g] = true;
466
            DDM._activateTargets();
467
            return this;
468
        },
469
        /**
470
        * Remove this Drag instance from a group, this should be used for on-the-fly group removals.
471
        * @method removeFromGroup
472
        * @param {String} g The group to remove this Drag Instance from.
473
        * @chainable
474
        */
475
        removeFromGroup: function(g) {
476
            delete this._groups[g];
477
            DDM._activateTargets();
478
            return this;
479
        },
480
        /**
481
        * This will be a reference to the Drop instance associated with this drag if the target: true config attribute is set..
482
        * @property target
483
        * @type {Object}
484
        */
485
        target: null,
486
        /**
487
        * Attribute handler for the target config attribute.
488
        * @private
489
        * @method _handleTarget
490
        * @param {Boolean/Object} config The Config
491
        */
492
        _handleTarget: function(config) {
493
            if (Y.DD.Drop) {
494
                if (config === false) {
495
                    if (this.target) {
496
                        DDM._unregTarget(this.target);
497
                        this.target = null;
498
                    }
499
                } else {
500
                    if (!Y.Lang.isObject(config)) {
501
                        config = {};
502
                    }
503
                    config.bubbleTargets = config.bubbleTargets || this.getTargets();
504
                    config.node = this.get(NODE);
505
                    config.groups = config.groups || this.get('groups');
506
                    this.target = new Y.DD.Drop(config);
507
                }
508
            }
509
        },
510
        /**
511
        * Storage Array for the groups this drag belongs to.
512
        * @private
513
        * @property _groups
514
        * @type {Array}
515
        */
516
        _groups: null,
517
        /**
518
        * This method creates all the events for this Event Target and publishes them so we get Event Bubbling.
519
        * @private
520
        * @method _createEvents
521
        */
522
        _createEvents: function() {
523
 
524
            this.publish(EV_MOUSE_DOWN, {
525
                defaultFn: this._defMouseDownFn,
526
                queuable: false,
527
                emitFacade: true,
528
                bubbles: true,
529
                prefix: 'drag'
530
            });
531
 
532
            this.publish(EV_ALIGN, {
533
                defaultFn: this._defAlignFn,
534
                queuable: false,
535
                emitFacade: true,
536
                bubbles: true,
537
                prefix: 'drag'
538
            });
539
 
540
            this.publish(EV_DRAG, {
541
                defaultFn: this._defDragFn,
542
                queuable: false,
543
                emitFacade: true,
544
                bubbles: true,
545
                prefix: 'drag'
546
            });
547
 
548
            this.publish(EV_END, {
549
                defaultFn: this._defEndFn,
550
                preventedFn: this._prevEndFn,
551
                queuable: false,
552
                emitFacade: true,
553
                bubbles: true,
554
                prefix: 'drag'
555
            });
556
 
557
            var ev = [
558
                EV_AFTER_MOUSE_DOWN,
559
                EV_REMOVE_HANDLE,
560
                EV_ADD_HANDLE,
561
                EV_REMOVE_INVALID,
562
                EV_ADD_INVALID,
563
                EV_START,
564
                'drag:drophit',
565
                'drag:dropmiss',
566
                'drag:over',
567
                'drag:enter',
568
                'drag:exit'
569
            ];
570
 
571
            Y.Array.each(ev, function(v) {
572
                this.publish(v, {
573
                    type: v,
574
                    emitFacade: true,
575
                    bubbles: true,
576
                    preventable: false,
577
                    queuable: false,
578
                    prefix: 'drag'
579
                });
580
            }, this);
581
        },
582
        /**
583
        * A private reference to the mousedown DOM event
584
        * @private
585
        * @property _ev_md
586
        * @type {EventFacade}
587
        */
588
        _ev_md: null,
589
        /**
590
        * The getTime of the mousedown event. Not used, just here in case someone wants/needs to use it.
591
        * @private
592
        * @property _startTime
593
        * @type Date
594
        */
595
        _startTime: null,
596
        /**
597
        * The getTime of the mouseup event. Not used, just here in case someone wants/needs to use it.
598
        * @private
599
        * @property _endTime
600
        * @type Date
601
        */
602
        _endTime: null,
603
        /**
604
        * A private hash of the valid drag handles
605
        * @private
606
        * @property _handles
607
        * @type {Object}
608
        */
609
        _handles: null,
610
        /**
611
        * A private hash of the invalid selector strings
612
        * @private
613
        * @property _invalids
614
        * @type {Object}
615
        */
616
        _invalids: null,
617
        /**
618
        * A private hash of the default invalid selector strings: {'textarea': true, 'input': true, 'a': true, 'button': true, 'select': true}
619
        * @private
620
        * @property _invalidsDefault
621
        * @type {Object}
622
        */
623
        _invalidsDefault: {'textarea': true, 'input': true, 'a': true, 'button': true, 'select': true },
624
        /**
625
        * Private flag to see if the drag threshhold was met
626
        * @private
627
        * @property _dragThreshMet
628
        * @type {Boolean}
629
        */
630
        _dragThreshMet: null,
631
        /**
632
        * Flag to determine if the drag operation came from a timeout
633
        * @private
634
        * @property _fromTimeout
635
        * @type {Boolean}
636
        */
637
        _fromTimeout: null,
638
        /**
639
        * Holder for the setTimeout call
640
        * @private
641
        * @property _clickTimeout
642
        * @type {Boolean}
643
        */
644
        _clickTimeout: null,
645
        /**
646
        * The offset of the mouse position to the element's position
647
        * @property deltaXY
648
        * @type {Array}
649
        */
650
        deltaXY: null,
651
        /**
652
        * The initial mouse position
653
        * @property startXY
654
        * @type {Array}
655
        */
656
        startXY: null,
657
        /**
658
        * The initial element position
659
        * @property nodeXY
660
        * @type {Array}
661
        */
662
        nodeXY: null,
663
        /**
664
        * The position of the element as it's moving (for offset calculations)
665
        * @property lastXY
666
        * @type {Array}
667
        */
668
        lastXY: null,
669
        /**
670
        * The xy that the node will be set to. Changing this will alter the position as it's dragged.
671
        * @property actXY
672
        * @type {Array}
673
        */
674
        actXY: null,
675
        /**
676
        * The real xy position of the node.
677
        * @property realXY
678
        * @type {Array}
679
        */
680
        realXY: null,
681
        /**
682
        * The XY coords of the mousemove
683
        * @property mouseXY
684
        * @type {Array}
685
        */
686
        mouseXY: null,
687
        /**
688
        * A region object associated with this drag, used for checking regions while dragging.
689
        * @property region
690
        * @type Object
691
        */
692
        region: null,
693
        /**
694
        * Handler for the mouseup DOM event
695
        * @private
696
        * @method _handleMouseUp
697
        * @param {EventFacade} ev The Event
698
        */
699
        _handleMouseUp: function() {
700
            this.fire('drag:mouseup');
701
            this._fixIEMouseUp();
702
            if (DDM.activeDrag) {
703
                DDM._end();
704
            }
705
        },
706
        /**
707
        * The function we use as the ondragstart handler when we start a drag
708
        * in Internet Explorer. This keeps IE from blowing up on images as drag handles.
709
        * @private
710
        * @method _fixDragStart
711
        * @param {Event} e The Event
712
        */
713
        _fixDragStart: function(e) {
714
            if (this.validClick(e)) {
715
                e.preventDefault();
716
            }
717
        },
718
        /**
719
        * The function we use as the onselectstart handler when we start a drag in Internet Explorer
720
        * @private
721
        * @method _ieSelectFix
722
        */
723
        _ieSelectFix: function() {
724
            return false;
725
        },
726
        /**
727
        * We will hold a copy of the current "onselectstart" method on this property, and reset it after we are done using it.
728
        * @private
729
        * @property _ieSelectBack
730
        */
731
        _ieSelectBack: null,
732
        /**
733
        * This method copies the onselectstart listner on the document to the _ieSelectFix property
734
        * @private
735
        * @method _fixIEMouseDown
736
        */
737
        _fixIEMouseDown: function() {
738
            if (Y.UA.ie) {
739
                this._ieSelectBack = Y.config.doc.body.onselectstart;
740
                Y.config.doc.body.onselectstart = this._ieSelectFix;
741
            }
742
        },
743
        /**
744
        * This method copies the _ieSelectFix property back to the onselectstart listner on the document.
745
        * @private
746
        * @method _fixIEMouseUp
747
        */
748
        _fixIEMouseUp: function() {
749
            if (Y.UA.ie) {
750
                Y.config.doc.body.onselectstart = this._ieSelectBack;
751
            }
752
        },
753
        /**
754
        * Handler for the mousedown DOM event
755
        * @private
756
        * @method _handleMouseDownEvent
757
        * @param {EventFacade} ev  The Event
758
        */
759
        _handleMouseDownEvent: function(ev) {
760
            if (this.validClick(ev)) {
761
                ev.preventDefault();
762
            }
763
            this.fire(EV_MOUSE_DOWN, { ev: ev });
764
        },
765
        /**
766
        * Handler for the mousedown DOM event
767
        * @private
768
        * @method _defMouseDownFn
769
        * @param {EventFacade} e  The Event
770
        */
771
        _defMouseDownFn: function(e) {
772
            var ev = e.ev;
773
 
774
            this._dragThreshMet = false;
775
            this._ev_md = ev;
776
 
777
            if (this.get('primaryButtonOnly') && ev.button > 1) {
778
                return false;
779
            }
780
            if (this.validClick(ev)) {
781
                this._fixIEMouseDown(ev);
782
                if (Drag.START_EVENT.indexOf('gesture') !== 0) {
783
                    //Only do these if it's not a gesture
784
                    if (this.get('haltDown')) {
785
                        ev.halt();
786
                    } else {
787
                        ev.preventDefault();
788
                    }
789
                }
790
 
791
                this._setStartPosition([ev.pageX, ev.pageY]);
792
 
793
                DDM.activeDrag = this;
794
 
795
                this._clickTimeout = Y.later(this.get('clickTimeThresh'), this, this._timeoutCheck);
796
            }
797
            this.fire(EV_AFTER_MOUSE_DOWN, { ev: ev });
798
        },
799
        /**
800
        * Method first checks to see if we have handles, if so it validates the click
801
        * against the handle. Then if it finds a valid handle, it checks it against
802
        * the invalid handles list. Returns true if a good handle was used, false otherwise.
803
        * @method validClick
804
        * @param {EventFacade} ev  The Event
805
        * @return {Boolean}
806
        */
807
        validClick: function(ev) {
808
            var r = false, n = false,
809
            tar = ev.target,
810
            hTest = null,
811
            els = null,
812
            nlist = null,
813
            set = false;
814
            if (this._handles) {
815
                Y.Object.each(this._handles, function(i, n) {
816
                    if (i instanceof Y.Node || i instanceof Y.NodeList) {
817
                        if (!r) {
818
                            nlist = i;
819
                            if (nlist instanceof Y.Node) {
820
                                nlist = new Y.NodeList(i._node);
821
                            }
822
                            nlist.each(function(nl) {
823
                                if (nl.contains(tar)) {
824
                                    r = true;
825
                                }
826
                            });
827
                        }
828
                    } else if (Y.Lang.isString(n)) {
829
                        //Am I this or am I inside this
830
                        if (tar.test(n + ', ' + n + ' *') && !hTest) {
831
                            hTest = n;
832
                            r = true;
833
                        }
834
                    }
835
                });
836
            } else {
837
                n = this.get(NODE);
838
                if (n.contains(tar) || n.compareTo(tar)) {
839
                    r = true;
840
                }
841
            }
842
            if (r) {
843
                if (this._invalids) {
844
                    Y.Object.each(this._invalids, function(i, n) {
845
                        if (Y.Lang.isString(n)) {
846
                            //Am I this or am I inside this
847
                            if (tar.test(n + ', ' + n + ' *')) {
848
                                r = false;
849
                            }
850
                        }
851
                    });
852
                }
853
            }
854
            if (r) {
855
                if (hTest) {
856
                    els = ev.currentTarget.all(hTest);
857
                    set = false;
858
                    els.each(function(n) {
859
                        if ((n.contains(tar) || n.compareTo(tar)) && !set) {
860
                            set = true;
861
                            this.set('activeHandle', n);
862
                        }
863
                    }, this);
864
                } else {
865
                    this.set('activeHandle', this.get(NODE));
866
                }
867
            }
868
            return r;
869
        },
870
        /**
871
        * Sets the current position of the Element and calculates the offset
872
        * @private
873
        * @method _setStartPosition
874
        * @param {Array} xy The XY coords to set the position to.
875
        */
876
        _setStartPosition: function(xy) {
877
            this.startXY = xy;
878
 
879
            this.nodeXY = this.lastXY = this.realXY = this.get(NODE).getXY();
880
 
881
            if (this.get('offsetNode')) {
882
                this.deltaXY = [(this.startXY[0] - this.nodeXY[0]), (this.startXY[1] - this.nodeXY[1])];
883
            } else {
884
                this.deltaXY = [0, 0];
885
            }
886
        },
887
        /**
888
        * The method passed to setTimeout to determine if the clickTimeThreshold was met.
889
        * @private
890
        * @method _timeoutCheck
891
        */
892
        _timeoutCheck: function() {
893
            if (!this.get('lock') && !this._dragThreshMet && this._ev_md) {
894
                this._fromTimeout = this._dragThreshMet = true;
895
                this.start();
896
                this._alignNode([this._ev_md.pageX, this._ev_md.pageY], true);
897
            }
898
        },
899
        /**
900
        * Remove a Selector added by addHandle
901
        * @method removeHandle
902
        * @param {String} str The selector for the handle to be removed.
903
        * @chainable
904
        */
905
        removeHandle: function(str) {
906
            var key = str;
907
            if (str instanceof Y.Node || str instanceof Y.NodeList) {
908
                key = str._yuid;
909
            }
910
            if (this._handles[key]) {
911
                delete this._handles[key];
912
                this.fire(EV_REMOVE_HANDLE, { handle: str });
913
            }
914
            return this;
915
        },
916
        /**
917
        * Add a handle to a drag element. Drag only initiates when a mousedown happens on this element.
918
        * @method addHandle
919
        * @param {String} str The selector to test for a valid handle. Must be a child of the element.
920
        * @chainable
921
        */
922
        addHandle: function(str) {
923
            if (!this._handles) {
924
                this._handles = {};
925
            }
926
            var key = str;
927
            if (str instanceof Y.Node || str instanceof Y.NodeList) {
928
                key = str._yuid;
929
            }
930
            this._handles[key] = str;
931
            this.fire(EV_ADD_HANDLE, { handle: str });
932
            return this;
933
        },
934
        /**
935
        * Remove an invalid handle added by addInvalid
936
        * @method removeInvalid
937
        * @param {String} str The invalid handle to remove from the internal list.
938
        * @chainable
939
        */
940
        removeInvalid: function(str) {
941
            if (this._invalids[str]) {
942
                this._invalids[str] = null;
943
                delete this._invalids[str];
944
                this.fire(EV_REMOVE_INVALID, { handle: str });
945
            }
946
            return this;
947
        },
948
        /**
949
        * Add a selector string to test the handle against. If the test passes the drag operation will not continue.
950
        * @method addInvalid
951
        * @param {String} str The selector to test against to determine if this is an invalid drag handle.
952
        * @chainable
953
        */
954
        addInvalid: function(str) {
955
            if (Y.Lang.isString(str)) {
956
                this._invalids[str] = true;
957
                this.fire(EV_ADD_INVALID, { handle: str });
958
            }
959
            return this;
960
        },
961
        /**
962
        * Internal init handler
963
        * @private
964
        * @method initializer
965
        */
966
        initializer: function() {
967
 
968
            this.get(NODE).dd = this;
969
 
970
            if (!this.get(NODE).get('id')) {
971
                var id = Y.stamp(this.get(NODE));
972
                this.get(NODE).set('id', id);
973
            }
974
 
975
            this.actXY = [];
976
 
977
            this._invalids = Y.clone(this._invalidsDefault, true);
978
 
979
            this._createEvents();
980
 
981
            if (!this.get(DRAG_NODE)) {
982
                this.set(DRAG_NODE, this.get(NODE));
983
            }
984
 
985
            //Fix for #2528096
986
            //Don't prep the DD instance until all plugins are loaded.
987
            this.on('initializedChange', Y.bind(this._prep, this));
988
 
989
            //Shouldn't have to do this..
990
            this.set('groups', this.get('groups'));
991
        },
992
        /**
993
        * Attach event listners and add classname
994
        * @private
995
        * @method _prep
996
        */
997
        _prep: function() {
998
            this._dragThreshMet = false;
999
            var node = this.get(NODE);
1000
            node.addClass(DDM.CSS_PREFIX + '-draggable');
1001
            node.on(Drag.START_EVENT, Y.bind(this._handleMouseDownEvent, this));
1002
            node.on('mouseup', Y.bind(this._handleMouseUp, this));
1003
            node.on('dragstart', Y.bind(this._fixDragStart, this));
1004
        },
1005
        /**
1006
        * Detach event listeners and remove classname
1007
        * @private
1008
        * @method _unprep
1009
        */
1010
        _unprep: function() {
1011
            var node = this.get(NODE);
1012
            node.removeClass(DDM.CSS_PREFIX + '-draggable');
1013
            node.detachAll('mouseup');
1014
            node.detachAll('dragstart');
1015
            node.detachAll(Drag.START_EVENT);
1016
            this.mouseXY = [];
1017
            this.deltaXY = [0,0];
1018
            this.startXY = [];
1019
            this.nodeXY = [];
1020
            this.lastXY = [];
1021
            this.actXY = [];
1022
            this.realXY = [];
1023
        },
1024
        /**
1025
        * Starts the drag operation
1026
        * @method start
1027
        * @chainable
1028
        */
1029
        start: function() {
1030
            if (!this.get('lock') && !this.get(DRAGGING)) {
1031
                var node = this.get(NODE), ow, oh, xy;
1032
                this._startTime = (new Date()).getTime();
1033
 
1034
                DDM._start();
1035
                node.addClass(DDM.CSS_PREFIX + '-dragging');
1036
                this.fire(EV_START, {
1037
                    pageX: this.nodeXY[0],
1038
                    pageY: this.nodeXY[1],
1039
                    startTime: this._startTime
1040
                });
1041
                node = this.get(DRAG_NODE);
1042
                xy = this.nodeXY;
1043
 
1044
                ow = node.get(OFFSET_WIDTH);
1045
                oh = node.get(OFFSET_HEIGHT);
1046
 
1047
                if (this.get('startCentered')) {
1048
                    this._setStartPosition([xy[0] + (ow / 2), xy[1] + (oh / 2)]);
1049
                }
1050
 
1051
 
1052
                this.region = {
1053
                    '0': xy[0],
1054
                    '1': xy[1],
1055
                    area: 0,
1056
                    top: xy[1],
1057
                    right: xy[0] + ow,
1058
                    bottom: xy[1] + oh,
1059
                    left: xy[0]
1060
                };
1061
                this.set(DRAGGING, true);
1062
            }
1063
            return this;
1064
        },
1065
        /**
1066
        * Ends the drag operation
1067
        * @method end
1068
        * @chainable
1069
        */
1070
        end: function() {
1071
            this._endTime = (new Date()).getTime();
1072
            if (this._clickTimeout) {
1073
                this._clickTimeout.cancel();
1074
            }
1075
            this._dragThreshMet = this._fromTimeout = false;
1076
 
1077
            if (!this.get('lock') && this.get(DRAGGING)) {
1078
                this.fire(EV_END, {
1079
                    pageX: this.lastXY[0],
1080
                    pageY: this.lastXY[1],
1081
                    startTime: this._startTime,
1082
                    endTime: this._endTime
1083
                });
1084
            }
1085
            this.get(NODE).removeClass(DDM.CSS_PREFIX + '-dragging');
1086
            this.set(DRAGGING, false);
1087
            this.deltaXY = [0, 0];
1088
 
1089
            return this;
1090
        },
1091
        /**
1092
        * Handler for fixing the selection in IE
1093
        * @private
1094
        * @method _defEndFn
1095
        */
1096
        _defEndFn: function() {
1097
            this._fixIEMouseUp();
1098
            this._ev_md = null;
1099
        },
1100
        /**
1101
        * Handler for preventing the drag:end event. It will reset the node back to it's start position
1102
        * @private
1103
        * @method _prevEndFn
1104
        */
1105
        _prevEndFn: function() {
1106
            this._fixIEMouseUp();
1107
            //Bug #1852577
1108
            this.get(DRAG_NODE).setXY(this.nodeXY);
1109
            this._ev_md = null;
1110
            this.region = null;
1111
        },
1112
        /**
1113
        * Calculates the offsets and set's the XY that the element will move to.
1114
        * @private
1115
        * @method _align
1116
        * @param {Array} xy The xy coords to align with.
1117
        */
1118
        _align: function(xy) {
1119
            this.fire(EV_ALIGN, {pageX: xy[0], pageY: xy[1] });
1120
        },
1121
        /**
1122
        * Calculates the offsets and set's the XY that the element will move to.
1123
        * @private
1124
        * @method _defAlignFn
1125
        * @param {EventFacade} e The drag:align event.
1126
        */
1127
        _defAlignFn: function(e) {
1128
            this.actXY = [e.pageX - this.deltaXY[0], e.pageY - this.deltaXY[1]];
1129
        },
1130
        /**
1131
        * This method performs the alignment before the element move.
1132
        * @private
1133
        * @method _alignNode
1134
        * @param {Array} eXY The XY to move the element to, usually comes from the mousemove DOM event.
1135
        */
1136
        _alignNode: function(eXY, scroll) {
1137
            this._align(eXY);
1138
            if (!scroll) {
1139
                this._moveNode();
1140
            }
1141
        },
1142
        /**
1143
        * This method performs the actual element move.
1144
        * @private
1145
        * @method _moveNode
1146
        */
1147
        _moveNode: function(scroll) {
1148
            //if (!this.get(DRAGGING)) {
1149
            //    return;
1150
            //}
1151
            var diffXY = [], diffXY2 = [], startXY = this.nodeXY, xy = this.actXY;
1152
 
1153
            diffXY[0] = (xy[0] - this.lastXY[0]);
1154
            diffXY[1] = (xy[1] - this.lastXY[1]);
1155
 
1156
            diffXY2[0] = (xy[0] - this.nodeXY[0]);
1157
            diffXY2[1] = (xy[1] - this.nodeXY[1]);
1158
 
1159
 
1160
            this.region = {
1161
                '0': xy[0],
1162
                '1': xy[1],
1163
                area: 0,
1164
                top: xy[1],
1165
                right: xy[0] + this.get(DRAG_NODE).get(OFFSET_WIDTH),
1166
                bottom: xy[1] + this.get(DRAG_NODE).get(OFFSET_HEIGHT),
1167
                left: xy[0]
1168
            };
1169
 
1170
            this.fire(EV_DRAG, {
1171
                pageX: xy[0],
1172
                pageY: xy[1],
1173
                scroll: scroll,
1174
                info: {
1175
                    start: startXY,
1176
                    xy: xy,
1177
                    delta: diffXY,
1178
                    offset: diffXY2
1179
                }
1180
            });
1181
 
1182
            this.lastXY = xy;
1183
        },
1184
        /**
1185
        * Default function for drag:drag. Fired from _moveNode.
1186
        * @private
1187
        * @method _defDragFn
1188
        * @param {EventFacade} ev The drag:drag event
1189
        */
1190
        _defDragFn: function(e) {
1191
            if (this.get('move')) {
1192
                if (e.scroll && e.scroll.node) {
1193
                    var domNode = e.scroll.node.getDOMNode();
1194
                    //If it's the window
1195
                    if (domNode === Y.config.win) {
1196
                        domNode.scrollTo(e.scroll.left, e.scroll.top);
1197
                    } else {
1198
                        e.scroll.node.set('scrollTop', e.scroll.top);
1199
                        e.scroll.node.set('scrollLeft', e.scroll.left);
1200
                    }
1201
                }
1202
                this.get(DRAG_NODE).setXY([e.pageX, e.pageY]);
1203
                this.realXY = [e.pageX, e.pageY];
1204
            }
1205
        },
1206
        /**
1207
        * Fired from DragDropMgr (DDM) on mousemove.
1208
        * @private
1209
        * @method _move
1210
        * @param {EventFacade} ev The mousemove DOM event
1211
        */
1212
        _move: function(ev) {
1213
            if (this.get('lock')) {
1214
                return false;
1215
            }
1216
 
1217
            this.mouseXY = [ev.pageX, ev.pageY];
1218
            if (!this._dragThreshMet) {
1219
                var diffX = Math.abs(this.startXY[0] - ev.pageX),
1220
                diffY = Math.abs(this.startXY[1] - ev.pageY);
1221
                if (diffX > this.get('clickPixelThresh') || diffY > this.get('clickPixelThresh')) {
1222
                    this._dragThreshMet = true;
1223
                    this.start();
1224
                    //This only happens on gestures to stop the page from scrolling
1225
                    if (ev && ev.preventDefault) {
1226
                        ev.preventDefault();
1227
                    }
1228
                    this._alignNode([ev.pageX, ev.pageY]);
1229
                }
1230
            } else {
1231
                if (this._clickTimeout) {
1232
                    this._clickTimeout.cancel();
1233
                }
1234
                this._alignNode([ev.pageX, ev.pageY]);
1235
            }
1236
        },
1237
        /**
1238
        * Method will forcefully stop a drag operation. For example calling this from inside an ESC keypress handler will stop this drag.
1239
        * @method stopDrag
1240
        * @chainable
1241
        */
1242
        stopDrag: function() {
1243
            if (this.get(DRAGGING)) {
1244
                DDM._end();
1245
            }
1246
            return this;
1247
        },
1248
        /**
1249
        * Lifecycle destructor, unreg the drag from the DDM and remove listeners
1250
        * @private
1251
        * @method destructor
1252
        */
1253
        destructor: function() {
1254
            this._unprep();
1255
            if (this.target) {
1256
                this.target.destroy();
1257
            }
1258
            DDM._unregDrag(this);
1259
        }
1260
    });
1261
    Y.namespace('DD');
1262
    Y.DD.Drag = Drag;
1263
 
1264
 
1265
 
1266
 
1267
}, '3.18.1', {"requires": ["dd-ddm-base", "selector-css2"]});