MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / yui / dragdrop / dragdrop.js
blob1cb2857fa2cb7e9b1c427785f2b26c1a9e6c0b77
1 /*
2 Copyright (c) 2007, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 2.3.0
6 */
7 /**
8 * The drag and drop utility provides a framework for building drag and drop
9 * applications. In addition to enabling drag and drop for specific elements,
10 * the drag and drop elements are tracked by the manager class, and the
11 * interactions between the various elements are tracked during the drag and
12 * the implementing code is notified about these important moments.
13 * @module dragdrop
14 * @title Drag and Drop
15 * @requires yahoo,dom,event
16 * @namespace YAHOO.util
19 // Only load the library once. Rewriting the manager class would orphan
20 // existing drag and drop instances.
21 if (!YAHOO.util.DragDropMgr) {
23 /**
24 * DragDropMgr is a singleton that tracks the element interaction for
25 * all DragDrop items in the window. Generally, you will not call
26 * this class directly, but it does have helper methods that could
27 * be useful in your DragDrop implementations.
28 * @class DragDropMgr
29 * @static
31 YAHOO.util.DragDropMgr = function() {
33 var Event = YAHOO.util.Event;
35 return {
37 /**
38 * Two dimensional Array of registered DragDrop objects. The first
39 * dimension is the DragDrop item group, the second the DragDrop
40 * object.
41 * @property ids
42 * @type {string: string}
43 * @private
44 * @static
46 ids: {},
48 /**
49 * Array of element ids defined as drag handles. Used to determine
50 * if the element that generated the mousedown event is actually the
51 * handle and not the html element itself.
52 * @property handleIds
53 * @type {string: string}
54 * @private
55 * @static
57 handleIds: {},
59 /**
60 * the DragDrop object that is currently being dragged
61 * @property dragCurrent
62 * @type DragDrop
63 * @private
64 * @static
65 **/
66 dragCurrent: null,
68 /**
69 * the DragDrop object(s) that are being hovered over
70 * @property dragOvers
71 * @type Array
72 * @private
73 * @static
75 dragOvers: {},
77 /**
78 * the X distance between the cursor and the object being dragged
79 * @property deltaX
80 * @type int
81 * @private
82 * @static
84 deltaX: 0,
86 /**
87 * the Y distance between the cursor and the object being dragged
88 * @property deltaY
89 * @type int
90 * @private
91 * @static
93 deltaY: 0,
95 /**
96 * Flag to determine if we should prevent the default behavior of the
97 * events we define. By default this is true, but this can be set to
98 * false if you need the default behavior (not recommended)
99 * @property preventDefault
100 * @type boolean
101 * @static
103 preventDefault: true,
106 * Flag to determine if we should stop the propagation of the events
107 * we generate. This is true by default but you may want to set it to
108 * false if the html element contains other features that require the
109 * mouse click.
110 * @property stopPropagation
111 * @type boolean
112 * @static
114 stopPropagation: true,
117 * Internal flag that is set to true when drag and drop has been
118 * initialized
119 * @property initialized
120 * @private
121 * @static
123 initialized: false,
126 * All drag and drop can be disabled.
127 * @property locked
128 * @private
129 * @static
131 locked: false,
134 * Provides additional information about the the current set of
135 * interactions. Can be accessed from the event handlers. It
136 * contains the following properties:
138 * out: onDragOut interactions
139 * enter: onDragEnter interactions
140 * over: onDragOver interactions
141 * drop: onDragDrop interactions
142 * point: The location of the cursor
143 * draggedRegion: The location of dragged element at the time
144 * of the interaction
145 * sourceRegion: The location of the source elemtn at the time
146 * of the interaction
147 * validDrop: boolean
148 * @property interactionInfo
149 * @type object
150 * @static
152 interactionInfo: null,
155 * Called the first time an element is registered.
156 * @method init
157 * @private
158 * @static
160 init: function() {
161 this.initialized = true;
165 * In point mode, drag and drop interaction is defined by the
166 * location of the cursor during the drag/drop
167 * @property POINT
168 * @type int
169 * @static
170 * @final
172 POINT: 0,
175 * In intersect mode, drag and drop interaction is defined by the
176 * cursor position or the amount of overlap of two or more drag and
177 * drop objects.
178 * @property INTERSECT
179 * @type int
180 * @static
181 * @final
183 INTERSECT: 1,
186 * In intersect mode, drag and drop interaction is defined only by the
187 * overlap of two or more drag and drop objects.
188 * @property STRICT_INTERSECT
189 * @type int
190 * @static
191 * @final
193 STRICT_INTERSECT: 2,
196 * The current drag and drop mode. Default: POINT
197 * @property mode
198 * @type int
199 * @static
201 mode: 0,
204 * Runs method on all drag and drop objects
205 * @method _execOnAll
206 * @private
207 * @static
209 _execOnAll: function(sMethod, args) {
210 for (var i in this.ids) {
211 for (var j in this.ids[i]) {
212 var oDD = this.ids[i][j];
213 if (! this.isTypeOfDD(oDD)) {
214 continue;
216 oDD[sMethod].apply(oDD, args);
222 * Drag and drop initialization. Sets up the global event handlers
223 * @method _onLoad
224 * @private
225 * @static
227 _onLoad: function() {
229 this.init();
232 Event.on(document, "mouseup", this.handleMouseUp, this, true);
233 Event.on(document, "mousemove", this.handleMouseMove, this, true);
234 Event.on(window, "unload", this._onUnload, this, true);
235 Event.on(window, "resize", this._onResize, this, true);
236 // Event.on(window, "mouseout", this._test);
241 * Reset constraints on all drag and drop objs
242 * @method _onResize
243 * @private
244 * @static
246 _onResize: function(e) {
247 this._execOnAll("resetConstraints", []);
251 * Lock all drag and drop functionality
252 * @method lock
253 * @static
255 lock: function() { this.locked = true; },
258 * Unlock all drag and drop functionality
259 * @method unlock
260 * @static
262 unlock: function() { this.locked = false; },
265 * Is drag and drop locked?
266 * @method isLocked
267 * @return {boolean} True if drag and drop is locked, false otherwise.
268 * @static
270 isLocked: function() { return this.locked; },
273 * Location cache that is set for all drag drop objects when a drag is
274 * initiated, cleared when the drag is finished.
275 * @property locationCache
276 * @private
277 * @static
279 locationCache: {},
282 * Set useCache to false if you want to force object the lookup of each
283 * drag and drop linked element constantly during a drag.
284 * @property useCache
285 * @type boolean
286 * @static
288 useCache: true,
291 * The number of pixels that the mouse needs to move after the
292 * mousedown before the drag is initiated. Default=3;
293 * @property clickPixelThresh
294 * @type int
295 * @static
297 clickPixelThresh: 3,
300 * The number of milliseconds after the mousedown event to initiate the
301 * drag if we don't get a mouseup event. Default=1000
302 * @property clickTimeThresh
303 * @type int
304 * @static
306 clickTimeThresh: 1000,
309 * Flag that indicates that either the drag pixel threshold or the
310 * mousdown time threshold has been met
311 * @property dragThreshMet
312 * @type boolean
313 * @private
314 * @static
316 dragThreshMet: false,
319 * Timeout used for the click time threshold
320 * @property clickTimeout
321 * @type Object
322 * @private
323 * @static
325 clickTimeout: null,
328 * The X position of the mousedown event stored for later use when a
329 * drag threshold is met.
330 * @property startX
331 * @type int
332 * @private
333 * @static
335 startX: 0,
338 * The Y position of the mousedown event stored for later use when a
339 * drag threshold is met.
340 * @property startY
341 * @type int
342 * @private
343 * @static
345 startY: 0,
348 * Each DragDrop instance must be registered with the DragDropMgr.
349 * This is executed in DragDrop.init()
350 * @method regDragDrop
351 * @param {DragDrop} oDD the DragDrop object to register
352 * @param {String} sGroup the name of the group this element belongs to
353 * @static
355 regDragDrop: function(oDD, sGroup) {
356 if (!this.initialized) { this.init(); }
358 if (!this.ids[sGroup]) {
359 this.ids[sGroup] = {};
361 this.ids[sGroup][oDD.id] = oDD;
365 * Removes the supplied dd instance from the supplied group. Executed
366 * by DragDrop.removeFromGroup, so don't call this function directly.
367 * @method removeDDFromGroup
368 * @private
369 * @static
371 removeDDFromGroup: function(oDD, sGroup) {
372 if (!this.ids[sGroup]) {
373 this.ids[sGroup] = {};
376 var obj = this.ids[sGroup];
377 if (obj && obj[oDD.id]) {
378 delete obj[oDD.id];
383 * Unregisters a drag and drop item. This is executed in
384 * DragDrop.unreg, use that method instead of calling this directly.
385 * @method _remove
386 * @private
387 * @static
389 _remove: function(oDD) {
390 for (var g in oDD.groups) {
391 if (g && this.ids[g][oDD.id]) {
392 delete this.ids[g][oDD.id];
395 delete this.handleIds[oDD.id];
399 * Each DragDrop handle element must be registered. This is done
400 * automatically when executing DragDrop.setHandleElId()
401 * @method regHandle
402 * @param {String} sDDId the DragDrop id this element is a handle for
403 * @param {String} sHandleId the id of the element that is the drag
404 * handle
405 * @static
407 regHandle: function(sDDId, sHandleId) {
408 if (!this.handleIds[sDDId]) {
409 this.handleIds[sDDId] = {};
411 this.handleIds[sDDId][sHandleId] = sHandleId;
415 * Utility function to determine if a given element has been
416 * registered as a drag drop item.
417 * @method isDragDrop
418 * @param {String} id the element id to check
419 * @return {boolean} true if this element is a DragDrop item,
420 * false otherwise
421 * @static
423 isDragDrop: function(id) {
424 return ( this.getDDById(id) ) ? true : false;
428 * Returns the drag and drop instances that are in all groups the
429 * passed in instance belongs to.
430 * @method getRelated
431 * @param {DragDrop} p_oDD the obj to get related data for
432 * @param {boolean} bTargetsOnly if true, only return targetable objs
433 * @return {DragDrop[]} the related instances
434 * @static
436 getRelated: function(p_oDD, bTargetsOnly) {
437 var oDDs = [];
438 for (var i in p_oDD.groups) {
439 for (j in this.ids[i]) {
440 var dd = this.ids[i][j];
441 if (! this.isTypeOfDD(dd)) {
442 continue;
444 if (!bTargetsOnly || dd.isTarget) {
445 oDDs[oDDs.length] = dd;
450 return oDDs;
454 * Returns true if the specified dd target is a legal target for
455 * the specifice drag obj
456 * @method isLegalTarget
457 * @param {DragDrop} the drag obj
458 * @param {DragDrop} the target
459 * @return {boolean} true if the target is a legal target for the
460 * dd obj
461 * @static
463 isLegalTarget: function (oDD, oTargetDD) {
464 var targets = this.getRelated(oDD, true);
465 for (var i=0, len=targets.length;i<len;++i) {
466 if (targets[i].id == oTargetDD.id) {
467 return true;
471 return false;
475 * My goal is to be able to transparently determine if an object is
476 * typeof DragDrop, and the exact subclass of DragDrop. typeof
477 * returns "object", oDD.constructor.toString() always returns
478 * "DragDrop" and not the name of the subclass. So for now it just
479 * evaluates a well-known variable in DragDrop.
480 * @method isTypeOfDD
481 * @param {Object} the object to evaluate
482 * @return {boolean} true if typeof oDD = DragDrop
483 * @static
485 isTypeOfDD: function (oDD) {
486 return (oDD && oDD.__ygDragDrop);
490 * Utility function to determine if a given element has been
491 * registered as a drag drop handle for the given Drag Drop object.
492 * @method isHandle
493 * @param {String} id the element id to check
494 * @return {boolean} true if this element is a DragDrop handle, false
495 * otherwise
496 * @static
498 isHandle: function(sDDId, sHandleId) {
499 return ( this.handleIds[sDDId] &&
500 this.handleIds[sDDId][sHandleId] );
504 * Returns the DragDrop instance for a given id
505 * @method getDDById
506 * @param {String} id the id of the DragDrop object
507 * @return {DragDrop} the drag drop object, null if it is not found
508 * @static
510 getDDById: function(id) {
511 for (var i in this.ids) {
512 if (this.ids[i][id]) {
513 return this.ids[i][id];
516 return null;
520 * Fired after a registered DragDrop object gets the mousedown event.
521 * Sets up the events required to track the object being dragged
522 * @method handleMouseDown
523 * @param {Event} e the event
524 * @param oDD the DragDrop object being dragged
525 * @private
526 * @static
528 handleMouseDown: function(e, oDD) {
530 this.currentTarget = YAHOO.util.Event.getTarget(e);
532 this.dragCurrent = oDD;
534 var el = oDD.getEl();
536 // track start position
537 this.startX = YAHOO.util.Event.getPageX(e);
538 this.startY = YAHOO.util.Event.getPageY(e);
540 this.deltaX = this.startX - el.offsetLeft;
541 this.deltaY = this.startY - el.offsetTop;
543 this.dragThreshMet = false;
545 this.clickTimeout = setTimeout(
546 function() {
547 var DDM = YAHOO.util.DDM;
548 DDM.startDrag(DDM.startX, DDM.startY);
550 this.clickTimeThresh );
554 * Fired when either the drag pixel threshol or the mousedown hold
555 * time threshold has been met.
556 * @method startDrag
557 * @param x {int} the X position of the original mousedown
558 * @param y {int} the Y position of the original mousedown
559 * @static
561 startDrag: function(x, y) {
562 clearTimeout(this.clickTimeout);
563 var dc = this.dragCurrent;
564 if (dc) {
565 dc.b4StartDrag(x, y);
567 if (dc) {
568 dc.startDrag(x, y);
570 this.dragThreshMet = true;
574 * Internal function to handle the mouseup event. Will be invoked
575 * from the context of the document.
576 * @method handleMouseUp
577 * @param {Event} e the event
578 * @private
579 * @static
581 handleMouseUp: function(e) {
582 if (this.dragCurrent) {
583 clearTimeout(this.clickTimeout);
585 if (this.dragThreshMet) {
586 this.fireEvents(e, true);
587 } else {
590 this.stopDrag(e);
592 this.stopEvent(e);
597 * Utility to stop event propagation and event default, if these
598 * features are turned on.
599 * @method stopEvent
600 * @param {Event} e the event as returned by this.getEvent()
601 * @static
603 stopEvent: function(e) {
604 if (this.stopPropagation) {
605 YAHOO.util.Event.stopPropagation(e);
608 if (this.preventDefault) {
609 YAHOO.util.Event.preventDefault(e);
613 /**
614 * Ends the current drag, cleans up the state, and fires the endDrag
615 * and mouseUp events. Called internally when a mouseup is detected
616 * during the drag. Can be fired manually during the drag by passing
617 * either another event (such as the mousemove event received in onDrag)
618 * or a fake event with pageX and pageY defined (so that endDrag and
619 * onMouseUp have usable position data.). Alternatively, pass true
620 * for the silent parameter so that the endDrag and onMouseUp events
621 * are skipped (so no event data is needed.)
623 * @method stopDrag
624 * @param {Event} e the mouseup event, another event (or a fake event)
625 * with pageX and pageY defined, or nothing if the
626 * silent parameter is true
627 * @param {boolean} silent skips the enddrag and mouseup events if true
628 * @static
630 stopDrag: function(e, silent) {
632 // Fire the drag end event for the item that was dragged
633 if (this.dragCurrent && !silent) {
634 if (this.dragThreshMet) {
635 this.dragCurrent.b4EndDrag(e);
636 this.dragCurrent.endDrag(e);
639 this.dragCurrent.onMouseUp(e);
642 this.dragCurrent = null;
643 this.dragOvers = {};
646 /**
647 * Internal function to handle the mousemove event. Will be invoked
648 * from the context of the html element.
650 * @TODO figure out what we can do about mouse events lost when the
651 * user drags objects beyond the window boundary. Currently we can
652 * detect this in internet explorer by verifying that the mouse is
653 * down during the mousemove event. Firefox doesn't give us the
654 * button state on the mousemove event.
655 * @method handleMouseMove
656 * @param {Event} e the event
657 * @private
658 * @static
660 handleMouseMove: function(e) {
662 var dc = this.dragCurrent;
663 if (dc) {
665 // var button = e.which || e.button;
667 // check for IE mouseup outside of page boundary
668 if (YAHOO.util.Event.isIE && !e.button) {
669 this.stopEvent(e);
670 return this.handleMouseUp(e);
673 if (!this.dragThreshMet) {
674 var diffX = Math.abs(this.startX - YAHOO.util.Event.getPageX(e));
675 var diffY = Math.abs(this.startY - YAHOO.util.Event.getPageY(e));
676 if (diffX > this.clickPixelThresh ||
677 diffY > this.clickPixelThresh) {
678 this.startDrag(this.startX, this.startY);
682 if (this.dragThreshMet) {
683 dc.b4Drag(e);
684 if (dc) {
685 dc.onDrag(e);
687 if (dc) {
688 this.fireEvents(e, false);
692 this.stopEvent(e);
697 * Iterates over all of the DragDrop elements to find ones we are
698 * hovering over or dropping on
699 * @method fireEvents
700 * @param {Event} e the event
701 * @param {boolean} isDrop is this a drop op or a mouseover op?
702 * @private
703 * @static
705 fireEvents: function(e, isDrop) {
706 var dc = this.dragCurrent;
708 // If the user did the mouse up outside of the window, we could
709 // get here even though we have ended the drag.
710 if (!dc || dc.isLocked()) {
711 return;
714 var x = YAHOO.util.Event.getPageX(e);
715 var y = YAHOO.util.Event.getPageY(e);
716 var pt = new YAHOO.util.Point(x,y);
717 var pos = dc.getTargetCoord(pt.x, pt.y);
718 var el = dc.getDragEl();
719 curRegion = new YAHOO.util.Region( pos.y,
720 pos.x + el.offsetWidth,
721 pos.y + el.offsetHeight,
722 pos.x );
723 // cache the previous dragOver array
724 var oldOvers = [];
726 var outEvts = [];
727 var overEvts = [];
728 var dropEvts = [];
729 var enterEvts = [];
732 // Check to see if the object(s) we were hovering over is no longer
733 // being hovered over so we can fire the onDragOut event
734 for (var i in this.dragOvers) {
736 var ddo = this.dragOvers[i];
738 if (! this.isTypeOfDD(ddo)) {
739 continue;
742 if (! this.isOverTarget(pt, ddo, this.mode, curRegion)) {
743 outEvts.push( ddo );
746 oldOvers[i] = true;
747 delete this.dragOvers[i];
750 for (var sGroup in dc.groups) {
752 if ("string" != typeof sGroup) {
753 continue;
756 for (i in this.ids[sGroup]) {
757 var oDD = this.ids[sGroup][i];
758 if (! this.isTypeOfDD(oDD)) {
759 continue;
762 if (oDD.isTarget && !oDD.isLocked() && oDD != dc) {
763 if (this.isOverTarget(pt, oDD, this.mode, curRegion)) {
764 // look for drop interactions
765 if (isDrop) {
766 dropEvts.push( oDD );
767 // look for drag enter and drag over interactions
768 } else {
770 // initial drag over: dragEnter fires
771 if (!oldOvers[oDD.id]) {
772 enterEvts.push( oDD );
773 // subsequent drag overs: dragOver fires
774 } else {
775 overEvts.push( oDD );
778 this.dragOvers[oDD.id] = oDD;
785 this.interactionInfo = {
786 out: outEvts,
787 enter: enterEvts,
788 over: overEvts,
789 drop: dropEvts,
790 point: pt,
791 draggedRegion: curRegion,
792 sourceRegion: this.locationCache[dc.id],
793 validDrop: isDrop
796 // notify about a drop that did not find a target
797 if (isDrop && !dropEvts.length) {
798 this.interactionInfo.validDrop = false;
799 dc.onInvalidDrop(e);
803 if (this.mode) {
804 if (outEvts.length) {
805 dc.b4DragOut(e, outEvts);
806 if (dc) {
807 dc.onDragOut(e, outEvts);
811 if (enterEvts.length) {
812 if (dc) {
813 dc.onDragEnter(e, enterEvts);
817 if (overEvts.length) {
818 if (dc) {
819 dc.b4DragOver(e, overEvts);
822 if (dc) {
823 dc.onDragOver(e, overEvts);
827 if (dropEvts.length) {
828 if (dc) {
829 dc.b4DragDrop(e, dropEvts);
831 if (dc) {
832 dc.onDragDrop(e, dropEvts);
836 } else {
837 // fire dragout events
838 var len = 0;
839 for (i=0, len=outEvts.length; i<len; ++i) {
840 if (dc) {
841 dc.b4DragOut(e, outEvts[i].id);
843 if (dc) {
844 dc.onDragOut(e, outEvts[i].id);
848 // fire enter events
849 for (i=0,len=enterEvts.length; i<len; ++i) {
850 // dc.b4DragEnter(e, oDD.id);
852 if (dc) {
853 dc.onDragEnter(e, enterEvts[i].id);
857 // fire over events
858 for (i=0,len=overEvts.length; i<len; ++i) {
859 if (dc) {
860 dc.b4DragOver(e, overEvts[i].id);
862 if (dc) {
863 dc.onDragOver(e, overEvts[i].id);
867 // fire drop events
868 for (i=0, len=dropEvts.length; i<len; ++i) {
869 if (dc) {
870 dc.b4DragDrop(e, dropEvts[i].id);
872 if (dc) {
873 dc.onDragDrop(e, dropEvts[i].id);
881 * Helper function for getting the best match from the list of drag
882 * and drop objects returned by the drag and drop events when we are
883 * in INTERSECT mode. It returns either the first object that the
884 * cursor is over, or the object that has the greatest overlap with
885 * the dragged element.
886 * @method getBestMatch
887 * @param {DragDrop[]} dds The array of drag and drop objects
888 * targeted
889 * @return {DragDrop} The best single match
890 * @static
892 getBestMatch: function(dds) {
893 var winner = null;
895 var len = dds.length;
897 if (len == 1) {
898 winner = dds[0];
899 } else {
900 // Loop through the targeted items
901 for (var i=0; i<len; ++i) {
902 var dd = dds[i];
903 // If the cursor is over the object, it wins. If the
904 // cursor is over multiple matches, the first one we come
905 // to wins.
906 if (this.mode == this.INTERSECT && dd.cursorIsOver) {
907 winner = dd;
908 break;
909 // Otherwise the object with the most overlap wins
910 } else {
911 if (!winner || !winner.overlap || (dd.overlap &&
912 winner.overlap.getArea() < dd.overlap.getArea())) {
913 winner = dd;
919 return winner;
923 * Refreshes the cache of the top-left and bottom-right points of the
924 * drag and drop objects in the specified group(s). This is in the
925 * format that is stored in the drag and drop instance, so typical
926 * usage is:
927 * <code>
928 * YAHOO.util.DragDropMgr.refreshCache(ddinstance.groups);
929 * </code>
930 * Alternatively:
931 * <code>
932 * YAHOO.util.DragDropMgr.refreshCache({group1:true, group2:true});
933 * </code>
934 * @TODO this really should be an indexed array. Alternatively this
935 * method could accept both.
936 * @method refreshCache
937 * @param {Object} groups an associative array of groups to refresh
938 * @static
940 refreshCache: function(groups) {
942 // refresh everything if group array is not provided
943 var g = groups || this.ids;
945 for (var sGroup in g) {
946 if ("string" != typeof sGroup) {
947 continue;
949 for (var i in this.ids[sGroup]) {
950 var oDD = this.ids[sGroup][i];
952 if (this.isTypeOfDD(oDD)) {
953 var loc = this.getLocation(oDD);
954 if (loc) {
955 this.locationCache[oDD.id] = loc;
956 } else {
957 delete this.locationCache[oDD.id];
965 * This checks to make sure an element exists and is in the DOM. The
966 * main purpose is to handle cases where innerHTML is used to remove
967 * drag and drop objects from the DOM. IE provides an 'unspecified
968 * error' when trying to access the offsetParent of such an element
969 * @method verifyEl
970 * @param {HTMLElement} el the element to check
971 * @return {boolean} true if the element looks usable
972 * @static
974 verifyEl: function(el) {
975 try {
976 if (el) {
977 var parent = el.offsetParent;
978 if (parent) {
979 return true;
982 } catch(e) {
985 return false;
989 * Returns a Region object containing the drag and drop element's position
990 * and size, including the padding configured for it
991 * @method getLocation
992 * @param {DragDrop} oDD the drag and drop object to get the
993 * location for
994 * @return {YAHOO.util.Region} a Region object representing the total area
995 * the element occupies, including any padding
996 * the instance is configured for.
997 * @static
999 getLocation: function(oDD) {
1000 if (! this.isTypeOfDD(oDD)) {
1001 return null;
1004 var el = oDD.getEl(), pos, x1, x2, y1, y2, t, r, b, l;
1006 try {
1007 pos= YAHOO.util.Dom.getXY(el);
1008 } catch (e) { }
1010 if (!pos) {
1011 return null;
1014 x1 = pos[0];
1015 x2 = x1 + el.offsetWidth;
1016 y1 = pos[1];
1017 y2 = y1 + el.offsetHeight;
1019 t = y1 - oDD.padding[0];
1020 r = x2 + oDD.padding[1];
1021 b = y2 + oDD.padding[2];
1022 l = x1 - oDD.padding[3];
1024 return new YAHOO.util.Region( t, r, b, l );
1028 * Checks the cursor location to see if it over the target
1029 * @method isOverTarget
1030 * @param {YAHOO.util.Point} pt The point to evaluate
1031 * @param {DragDrop} oTarget the DragDrop object we are inspecting
1032 * @param {boolean} intersect true if we are in intersect mode
1033 * @param {YAHOO.util.Region} pre-cached location of the dragged element
1034 * @return {boolean} true if the mouse is over the target
1035 * @private
1036 * @static
1038 isOverTarget: function(pt, oTarget, intersect, curRegion) {
1039 // use cache if available
1040 var loc = this.locationCache[oTarget.id];
1041 if (!loc || !this.useCache) {
1042 loc = this.getLocation(oTarget);
1043 this.locationCache[oTarget.id] = loc;
1047 if (!loc) {
1048 return false;
1051 oTarget.cursorIsOver = loc.contains( pt );
1053 // DragDrop is using this as a sanity check for the initial mousedown
1054 // in this case we are done. In POINT mode, if the drag obj has no
1055 // contraints, we are done. Otherwise we need to evaluate the
1056 // region the target as occupies to determine if the dragged element
1057 // overlaps with it.
1059 var dc = this.dragCurrent;
1060 if (!dc || (!intersect && !dc.constrainX && !dc.constrainY)) {
1062 //if (oTarget.cursorIsOver) {
1064 return oTarget.cursorIsOver;
1067 oTarget.overlap = null;
1069 // Get the current location of the drag element, this is the
1070 // location of the mouse event less the delta that represents
1071 // where the original mousedown happened on the element. We
1072 // need to consider constraints and ticks as well.
1074 if (!curRegion) {
1075 var pos = dc.getTargetCoord(pt.x, pt.y);
1076 var el = dc.getDragEl();
1077 curRegion = new YAHOO.util.Region( pos.y,
1078 pos.x + el.offsetWidth,
1079 pos.y + el.offsetHeight,
1080 pos.x );
1083 var overlap = curRegion.intersect(loc);
1085 if (overlap) {
1086 oTarget.overlap = overlap;
1087 return (intersect) ? true : oTarget.cursorIsOver;
1088 } else {
1089 return false;
1094 * unload event handler
1095 * @method _onUnload
1096 * @private
1097 * @static
1099 _onUnload: function(e, me) {
1100 this.unregAll();
1104 * Cleans up the drag and drop events and objects.
1105 * @method unregAll
1106 * @private
1107 * @static
1109 unregAll: function() {
1111 if (this.dragCurrent) {
1112 this.stopDrag();
1113 this.dragCurrent = null;
1116 this._execOnAll("unreg", []);
1118 for (i in this.elementCache) {
1119 delete this.elementCache[i];
1122 this.elementCache = {};
1123 this.ids = {};
1127 * A cache of DOM elements
1128 * @property elementCache
1129 * @private
1130 * @static
1132 elementCache: {},
1135 * Get the wrapper for the DOM element specified
1136 * @method getElWrapper
1137 * @param {String} id the id of the element to get
1138 * @return {YAHOO.util.DDM.ElementWrapper} the wrapped element
1139 * @private
1140 * @deprecated This wrapper isn't that useful
1141 * @static
1143 getElWrapper: function(id) {
1144 var oWrapper = this.elementCache[id];
1145 if (!oWrapper || !oWrapper.el) {
1146 oWrapper = this.elementCache[id] =
1147 new this.ElementWrapper(YAHOO.util.Dom.get(id));
1149 return oWrapper;
1153 * Returns the actual DOM element
1154 * @method getElement
1155 * @param {String} id the id of the elment to get
1156 * @return {Object} The element
1157 * @deprecated use YAHOO.util.Dom.get instead
1158 * @static
1160 getElement: function(id) {
1161 return YAHOO.util.Dom.get(id);
1165 * Returns the style property for the DOM element (i.e.,
1166 * document.getElById(id).style)
1167 * @method getCss
1168 * @param {String} id the id of the elment to get
1169 * @return {Object} The style property of the element
1170 * @deprecated use YAHOO.util.Dom instead
1171 * @static
1173 getCss: function(id) {
1174 var el = YAHOO.util.Dom.get(id);
1175 return (el) ? el.style : null;
1179 * Inner class for cached elements
1180 * @class DragDropMgr.ElementWrapper
1181 * @for DragDropMgr
1182 * @private
1183 * @deprecated
1185 ElementWrapper: function(el) {
1187 * The element
1188 * @property el
1190 this.el = el || null;
1192 * The element id
1193 * @property id
1195 this.id = this.el && el.id;
1197 * A reference to the style property
1198 * @property css
1200 this.css = this.el && el.style;
1204 * Returns the X position of an html element
1205 * @method getPosX
1206 * @param el the element for which to get the position
1207 * @return {int} the X coordinate
1208 * @for DragDropMgr
1209 * @deprecated use YAHOO.util.Dom.getX instead
1210 * @static
1212 getPosX: function(el) {
1213 return YAHOO.util.Dom.getX(el);
1217 * Returns the Y position of an html element
1218 * @method getPosY
1219 * @param el the element for which to get the position
1220 * @return {int} the Y coordinate
1221 * @deprecated use YAHOO.util.Dom.getY instead
1222 * @static
1224 getPosY: function(el) {
1225 return YAHOO.util.Dom.getY(el);
1229 * Swap two nodes. In IE, we use the native method, for others we
1230 * emulate the IE behavior
1231 * @method swapNode
1232 * @param n1 the first node to swap
1233 * @param n2 the other node to swap
1234 * @static
1236 swapNode: function(n1, n2) {
1237 if (n1.swapNode) {
1238 n1.swapNode(n2);
1239 } else {
1240 var p = n2.parentNode;
1241 var s = n2.nextSibling;
1243 if (s == n1) {
1244 p.insertBefore(n1, n2);
1245 } else if (n2 == n1.nextSibling) {
1246 p.insertBefore(n2, n1);
1247 } else {
1248 n1.parentNode.replaceChild(n2, n1);
1249 p.insertBefore(n1, s);
1255 * Returns the current scroll position
1256 * @method getScroll
1257 * @private
1258 * @static
1260 getScroll: function () {
1261 var t, l, dde=document.documentElement, db=document.body;
1262 if (dde && (dde.scrollTop || dde.scrollLeft)) {
1263 t = dde.scrollTop;
1264 l = dde.scrollLeft;
1265 } else if (db) {
1266 t = db.scrollTop;
1267 l = db.scrollLeft;
1268 } else {
1270 return { top: t, left: l };
1274 * Returns the specified element style property
1275 * @method getStyle
1276 * @param {HTMLElement} el the element
1277 * @param {string} styleProp the style property
1278 * @return {string} The value of the style property
1279 * @deprecated use YAHOO.util.Dom.getStyle
1280 * @static
1282 getStyle: function(el, styleProp) {
1283 return YAHOO.util.Dom.getStyle(el, styleProp);
1287 * Gets the scrollTop
1288 * @method getScrollTop
1289 * @return {int} the document's scrollTop
1290 * @static
1292 getScrollTop: function () { return this.getScroll().top; },
1295 * Gets the scrollLeft
1296 * @method getScrollLeft
1297 * @return {int} the document's scrollTop
1298 * @static
1300 getScrollLeft: function () { return this.getScroll().left; },
1303 * Sets the x/y position of an element to the location of the
1304 * target element.
1305 * @method moveToEl
1306 * @param {HTMLElement} moveEl The element to move
1307 * @param {HTMLElement} targetEl The position reference element
1308 * @static
1310 moveToEl: function (moveEl, targetEl) {
1311 var aCoord = YAHOO.util.Dom.getXY(targetEl);
1312 YAHOO.util.Dom.setXY(moveEl, aCoord);
1316 * Gets the client height
1317 * @method getClientHeight
1318 * @return {int} client height in px
1319 * @deprecated use YAHOO.util.Dom.getViewportHeight instead
1320 * @static
1322 getClientHeight: function() {
1323 return YAHOO.util.Dom.getViewportHeight();
1327 * Gets the client width
1328 * @method getClientWidth
1329 * @return {int} client width in px
1330 * @deprecated use YAHOO.util.Dom.getViewportWidth instead
1331 * @static
1333 getClientWidth: function() {
1334 return YAHOO.util.Dom.getViewportWidth();
1338 * Numeric array sort function
1339 * @method numericSort
1340 * @static
1342 numericSort: function(a, b) { return (a - b); },
1345 * Internal counter
1346 * @property _timeoutCount
1347 * @private
1348 * @static
1350 _timeoutCount: 0,
1353 * Trying to make the load order less important. Without this we get
1354 * an error if this file is loaded before the Event Utility.
1355 * @method _addListeners
1356 * @private
1357 * @static
1359 _addListeners: function() {
1360 var DDM = YAHOO.util.DDM;
1361 if ( YAHOO.util.Event && document ) {
1362 DDM._onLoad();
1363 } else {
1364 if (DDM._timeoutCount > 2000) {
1365 } else {
1366 setTimeout(DDM._addListeners, 10);
1367 if (document && document.body) {
1368 DDM._timeoutCount += 1;
1375 * Recursively searches the immediate parent and all child nodes for
1376 * the handle element in order to determine wheter or not it was
1377 * clicked.
1378 * @method handleWasClicked
1379 * @param node the html element to inspect
1380 * @static
1382 handleWasClicked: function(node, id) {
1383 if (this.isHandle(id, node.id)) {
1384 return true;
1385 } else {
1386 // check to see if this is a text node child of the one we want
1387 var p = node.parentNode;
1389 while (p) {
1390 if (this.isHandle(id, p.id)) {
1391 return true;
1392 } else {
1393 p = p.parentNode;
1398 return false;
1403 }();
1405 // shorter alias, save a few bytes
1406 YAHOO.util.DDM = YAHOO.util.DragDropMgr;
1407 YAHOO.util.DDM._addListeners();
1411 (function() {
1413 var Event=YAHOO.util.Event;
1414 var Dom=YAHOO.util.Dom;
1417 * Defines the interface and base operation of items that that can be
1418 * dragged or can be drop targets. It was designed to be extended, overriding
1419 * the event handlers for startDrag, onDrag, onDragOver, onDragOut.
1420 * Up to three html elements can be associated with a DragDrop instance:
1421 * <ul>
1422 * <li>linked element: the element that is passed into the constructor.
1423 * This is the element which defines the boundaries for interaction with
1424 * other DragDrop objects.</li>
1425 * <li>handle element(s): The drag operation only occurs if the element that
1426 * was clicked matches a handle element. By default this is the linked
1427 * element, but there are times that you will want only a portion of the
1428 * linked element to initiate the drag operation, and the setHandleElId()
1429 * method provides a way to define this.</li>
1430 * <li>drag element: this represents an the element that would be moved along
1431 * with the cursor during a drag operation. By default, this is the linked
1432 * element itself as in {@link YAHOO.util.DD}. setDragElId() lets you define
1433 * a separate element that would be moved, as in {@link YAHOO.util.DDProxy}
1434 * </li>
1435 * </ul>
1436 * This class should not be instantiated until the onload event to ensure that
1437 * the associated elements are available.
1438 * The following would define a DragDrop obj that would interact with any
1439 * other DragDrop obj in the "group1" group:
1440 * <pre>
1441 * dd = new YAHOO.util.DragDrop("div1", "group1");
1442 * </pre>
1443 * Since none of the event handlers have been implemented, nothing would
1444 * actually happen if you were to run the code above. Normally you would
1445 * override this class or one of the default implementations, but you can
1446 * also override the methods you want on an instance of the class...
1447 * <pre>
1448 * dd.onDragDrop = function(e, id) {
1449 * &nbsp;&nbsp;alert("dd was dropped on " + id);
1451 * </pre>
1452 * @namespace YAHOO.util
1453 * @class DragDrop
1454 * @constructor
1455 * @param {String} id of the element that is linked to this instance
1456 * @param {String} sGroup the group of related DragDrop objects
1457 * @param {object} config an object containing configurable attributes
1458 * Valid properties for DragDrop:
1459 * padding, isTarget, maintainOffset, primaryButtonOnly,
1461 YAHOO.util.DragDrop = function(id, sGroup, config) {
1462 if (id) {
1463 this.init(id, sGroup, config);
1467 YAHOO.util.DragDrop.prototype = {
1470 * The id of the element associated with this object. This is what we
1471 * refer to as the "linked element" because the size and position of
1472 * this element is used to determine when the drag and drop objects have
1473 * interacted.
1474 * @property id
1475 * @type String
1477 id: null,
1480 * Configuration attributes passed into the constructor
1481 * @property config
1482 * @type object
1484 config: null,
1487 * The id of the element that will be dragged. By default this is same
1488 * as the linked element , but could be changed to another element. Ex:
1489 * YAHOO.util.DDProxy
1490 * @property dragElId
1491 * @type String
1492 * @private
1494 dragElId: null,
1497 * the id of the element that initiates the drag operation. By default
1498 * this is the linked element, but could be changed to be a child of this
1499 * element. This lets us do things like only starting the drag when the
1500 * header element within the linked html element is clicked.
1501 * @property handleElId
1502 * @type String
1503 * @private
1505 handleElId: null,
1508 * An associative array of HTML tags that will be ignored if clicked.
1509 * @property invalidHandleTypes
1510 * @type {string: string}
1512 invalidHandleTypes: null,
1515 * An associative array of ids for elements that will be ignored if clicked
1516 * @property invalidHandleIds
1517 * @type {string: string}
1519 invalidHandleIds: null,
1522 * An indexted array of css class names for elements that will be ignored
1523 * if clicked.
1524 * @property invalidHandleClasses
1525 * @type string[]
1527 invalidHandleClasses: null,
1530 * The linked element's absolute X position at the time the drag was
1531 * started
1532 * @property startPageX
1533 * @type int
1534 * @private
1536 startPageX: 0,
1539 * The linked element's absolute X position at the time the drag was
1540 * started
1541 * @property startPageY
1542 * @type int
1543 * @private
1545 startPageY: 0,
1548 * The group defines a logical collection of DragDrop objects that are
1549 * related. Instances only get events when interacting with other
1550 * DragDrop object in the same group. This lets us define multiple
1551 * groups using a single DragDrop subclass if we want.
1552 * @property groups
1553 * @type {string: string}
1555 groups: null,
1558 * Individual drag/drop instances can be locked. This will prevent
1559 * onmousedown start drag.
1560 * @property locked
1561 * @type boolean
1562 * @private
1564 locked: false,
1567 * Lock this instance
1568 * @method lock
1570 lock: function() { this.locked = true; },
1573 * Unlock this instace
1574 * @method unlock
1576 unlock: function() { this.locked = false; },
1579 * By default, all instances can be a drop target. This can be disabled by
1580 * setting isTarget to false.
1581 * @method isTarget
1582 * @type boolean
1584 isTarget: true,
1587 * The padding configured for this drag and drop object for calculating
1588 * the drop zone intersection with this object.
1589 * @method padding
1590 * @type int[]
1592 padding: null,
1595 * Cached reference to the linked element
1596 * @property _domRef
1597 * @private
1599 _domRef: null,
1602 * Internal typeof flag
1603 * @property __ygDragDrop
1604 * @private
1606 __ygDragDrop: true,
1609 * Set to true when horizontal contraints are applied
1610 * @property constrainX
1611 * @type boolean
1612 * @private
1614 constrainX: false,
1617 * Set to true when vertical contraints are applied
1618 * @property constrainY
1619 * @type boolean
1620 * @private
1622 constrainY: false,
1625 * The left constraint
1626 * @property minX
1627 * @type int
1628 * @private
1630 minX: 0,
1633 * The right constraint
1634 * @property maxX
1635 * @type int
1636 * @private
1638 maxX: 0,
1641 * The up constraint
1642 * @property minY
1643 * @type int
1644 * @type int
1645 * @private
1647 minY: 0,
1650 * The down constraint
1651 * @property maxY
1652 * @type int
1653 * @private
1655 maxY: 0,
1658 * The difference between the click position and the source element's location
1659 * @property deltaX
1660 * @type int
1661 * @private
1663 deltaX: 0,
1666 * The difference between the click position and the source element's location
1667 * @property deltaY
1668 * @type int
1669 * @private
1671 deltaY: 0,
1674 * Maintain offsets when we resetconstraints. Set to true when you want
1675 * the position of the element relative to its parent to stay the same
1676 * when the page changes
1678 * @property maintainOffset
1679 * @type boolean
1681 maintainOffset: false,
1684 * Array of pixel locations the element will snap to if we specified a
1685 * horizontal graduation/interval. This array is generated automatically
1686 * when you define a tick interval.
1687 * @property xTicks
1688 * @type int[]
1690 xTicks: null,
1693 * Array of pixel locations the element will snap to if we specified a
1694 * vertical graduation/interval. This array is generated automatically
1695 * when you define a tick interval.
1696 * @property yTicks
1697 * @type int[]
1699 yTicks: null,
1702 * By default the drag and drop instance will only respond to the primary
1703 * button click (left button for a right-handed mouse). Set to true to
1704 * allow drag and drop to start with any mouse click that is propogated
1705 * by the browser
1706 * @property primaryButtonOnly
1707 * @type boolean
1709 primaryButtonOnly: true,
1712 * The availabe property is false until the linked dom element is accessible.
1713 * @property available
1714 * @type boolean
1716 available: false,
1719 * By default, drags can only be initiated if the mousedown occurs in the
1720 * region the linked element is. This is done in part to work around a
1721 * bug in some browsers that mis-report the mousedown if the previous
1722 * mouseup happened outside of the window. This property is set to true
1723 * if outer handles are defined.
1725 * @property hasOuterHandles
1726 * @type boolean
1727 * @default false
1729 hasOuterHandles: false,
1732 * Property that is assigned to a drag and drop object when testing to
1733 * see if it is being targeted by another dd object. This property
1734 * can be used in intersect mode to help determine the focus of
1735 * the mouse interaction. DDM.getBestMatch uses this property first to
1736 * determine the closest match in INTERSECT mode when multiple targets
1737 * are part of the same interaction.
1738 * @property cursorIsOver
1739 * @type boolean
1741 cursorIsOver: false,
1744 * Property that is assigned to a drag and drop object when testing to
1745 * see if it is being targeted by another dd object. This is a region
1746 * that represents the area the draggable element overlaps this target.
1747 * DDM.getBestMatch uses this property to compare the size of the overlap
1748 * to that of other targets in order to determine the closest match in
1749 * INTERSECT mode when multiple targets are part of the same interaction.
1750 * @property overlap
1751 * @type YAHOO.util.Region
1753 overlap: null,
1756 * Code that executes immediately before the startDrag event
1757 * @method b4StartDrag
1758 * @private
1760 b4StartDrag: function(x, y) { },
1763 * Abstract method called after a drag/drop object is clicked
1764 * and the drag or mousedown time thresholds have beeen met.
1765 * @method startDrag
1766 * @param {int} X click location
1767 * @param {int} Y click location
1769 startDrag: function(x, y) { /* override this */ },
1772 * Code that executes immediately before the onDrag event
1773 * @method b4Drag
1774 * @private
1776 b4Drag: function(e) { },
1779 * Abstract method called during the onMouseMove event while dragging an
1780 * object.
1781 * @method onDrag
1782 * @param {Event} e the mousemove event
1784 onDrag: function(e) { /* override this */ },
1787 * Abstract method called when this element fist begins hovering over
1788 * another DragDrop obj
1789 * @method onDragEnter
1790 * @param {Event} e the mousemove event
1791 * @param {String|DragDrop[]} id In POINT mode, the element
1792 * id this is hovering over. In INTERSECT mode, an array of one or more
1793 * dragdrop items being hovered over.
1795 onDragEnter: function(e, id) { /* override this */ },
1798 * Code that executes immediately before the onDragOver event
1799 * @method b4DragOver
1800 * @private
1802 b4DragOver: function(e) { },
1805 * Abstract method called when this element is hovering over another
1806 * DragDrop obj
1807 * @method onDragOver
1808 * @param {Event} e the mousemove event
1809 * @param {String|DragDrop[]} id In POINT mode, the element
1810 * id this is hovering over. In INTERSECT mode, an array of dd items
1811 * being hovered over.
1813 onDragOver: function(e, id) { /* override this */ },
1816 * Code that executes immediately before the onDragOut event
1817 * @method b4DragOut
1818 * @private
1820 b4DragOut: function(e) { },
1823 * Abstract method called when we are no longer hovering over an element
1824 * @method onDragOut
1825 * @param {Event} e the mousemove event
1826 * @param {String|DragDrop[]} id In POINT mode, the element
1827 * id this was hovering over. In INTERSECT mode, an array of dd items
1828 * that the mouse is no longer over.
1830 onDragOut: function(e, id) { /* override this */ },
1833 * Code that executes immediately before the onDragDrop event
1834 * @method b4DragDrop
1835 * @private
1837 b4DragDrop: function(e) { },
1840 * Abstract method called when this item is dropped on another DragDrop
1841 * obj
1842 * @method onDragDrop
1843 * @param {Event} e the mouseup event
1844 * @param {String|DragDrop[]} id In POINT mode, the element
1845 * id this was dropped on. In INTERSECT mode, an array of dd items this
1846 * was dropped on.
1848 onDragDrop: function(e, id) { /* override this */ },
1851 * Abstract method called when this item is dropped on an area with no
1852 * drop target
1853 * @method onInvalidDrop
1854 * @param {Event} e the mouseup event
1856 onInvalidDrop: function(e) { /* override this */ },
1859 * Code that executes immediately before the endDrag event
1860 * @method b4EndDrag
1861 * @private
1863 b4EndDrag: function(e) { },
1866 * Fired when we are done dragging the object
1867 * @method endDrag
1868 * @param {Event} e the mouseup event
1870 endDrag: function(e) { /* override this */ },
1873 * Code executed immediately before the onMouseDown event
1874 * @method b4MouseDown
1875 * @param {Event} e the mousedown event
1876 * @private
1878 b4MouseDown: function(e) { },
1881 * Event handler that fires when a drag/drop obj gets a mousedown
1882 * @method onMouseDown
1883 * @param {Event} e the mousedown event
1885 onMouseDown: function(e) { /* override this */ },
1888 * Event handler that fires when a drag/drop obj gets a mouseup
1889 * @method onMouseUp
1890 * @param {Event} e the mouseup event
1892 onMouseUp: function(e) { /* override this */ },
1895 * Override the onAvailable method to do what is needed after the initial
1896 * position was determined.
1897 * @method onAvailable
1899 onAvailable: function () {
1903 * Returns a reference to the linked element
1904 * @method getEl
1905 * @return {HTMLElement} the html element
1907 getEl: function() {
1908 if (!this._domRef) {
1909 this._domRef = Dom.get(this.id);
1912 return this._domRef;
1916 * Returns a reference to the actual element to drag. By default this is
1917 * the same as the html element, but it can be assigned to another
1918 * element. An example of this can be found in YAHOO.util.DDProxy
1919 * @method getDragEl
1920 * @return {HTMLElement} the html element
1922 getDragEl: function() {
1923 return Dom.get(this.dragElId);
1927 * Sets up the DragDrop object. Must be called in the constructor of any
1928 * YAHOO.util.DragDrop subclass
1929 * @method init
1930 * @param id the id of the linked element
1931 * @param {String} sGroup the group of related items
1932 * @param {object} config configuration attributes
1934 init: function(id, sGroup, config) {
1935 this.initTarget(id, sGroup, config);
1936 Event.on(this._domRef || this.id, "mousedown",
1937 this.handleMouseDown, this, true);
1938 // Event.on(this.id, "selectstart", Event.preventDefault);
1942 * Initializes Targeting functionality only... the object does not
1943 * get a mousedown handler.
1944 * @method initTarget
1945 * @param id the id of the linked element
1946 * @param {String} sGroup the group of related items
1947 * @param {object} config configuration attributes
1949 initTarget: function(id, sGroup, config) {
1951 // configuration attributes
1952 this.config = config || {};
1954 // create a local reference to the drag and drop manager
1955 this.DDM = YAHOO.util.DDM;
1957 // initialize the groups object
1958 this.groups = {};
1960 // assume that we have an element reference instead of an id if the
1961 // parameter is not a string
1962 if (typeof id !== "string") {
1963 this._domRef = id;
1964 id = Dom.generateId(id);
1967 // set the id
1968 this.id = id;
1970 // add to an interaction group
1971 this.addToGroup((sGroup) ? sGroup : "default");
1973 // We don't want to register this as the handle with the manager
1974 // so we just set the id rather than calling the setter.
1975 this.handleElId = id;
1977 Event.onAvailable(id, this.handleOnAvailable, this, true);
1980 // the linked element is the element that gets dragged by default
1981 this.setDragElId(id);
1983 // by default, clicked anchors will not start drag operations.
1984 // @TODO what else should be here? Probably form fields.
1985 this.invalidHandleTypes = { A: "A" };
1986 this.invalidHandleIds = {};
1987 this.invalidHandleClasses = [];
1989 this.applyConfig();
1993 * Applies the configuration parameters that were passed into the constructor.
1994 * This is supposed to happen at each level through the inheritance chain. So
1995 * a DDProxy implentation will execute apply config on DDProxy, DD, and
1996 * DragDrop in order to get all of the parameters that are available in
1997 * each object.
1998 * @method applyConfig
2000 applyConfig: function() {
2002 // configurable properties:
2003 // padding, isTarget, maintainOffset, primaryButtonOnly
2004 this.padding = this.config.padding || [0, 0, 0, 0];
2005 this.isTarget = (this.config.isTarget !== false);
2006 this.maintainOffset = (this.config.maintainOffset);
2007 this.primaryButtonOnly = (this.config.primaryButtonOnly !== false);
2012 * Executed when the linked element is available
2013 * @method handleOnAvailable
2014 * @private
2016 handleOnAvailable: function() {
2017 this.available = true;
2018 this.resetConstraints();
2019 this.onAvailable();
2023 * Configures the padding for the target zone in px. Effectively expands
2024 * (or reduces) the virtual object size for targeting calculations.
2025 * Supports css-style shorthand; if only one parameter is passed, all sides
2026 * will have that padding, and if only two are passed, the top and bottom
2027 * will have the first param, the left and right the second.
2028 * @method setPadding
2029 * @param {int} iTop Top pad
2030 * @param {int} iRight Right pad
2031 * @param {int} iBot Bot pad
2032 * @param {int} iLeft Left pad
2034 setPadding: function(iTop, iRight, iBot, iLeft) {
2035 // this.padding = [iLeft, iRight, iTop, iBot];
2036 if (!iRight && 0 !== iRight) {
2037 this.padding = [iTop, iTop, iTop, iTop];
2038 } else if (!iBot && 0 !== iBot) {
2039 this.padding = [iTop, iRight, iTop, iRight];
2040 } else {
2041 this.padding = [iTop, iRight, iBot, iLeft];
2046 * Stores the initial placement of the linked element.
2047 * @method setInitialPosition
2048 * @param {int} diffX the X offset, default 0
2049 * @param {int} diffY the Y offset, default 0
2050 * @private
2052 setInitPosition: function(diffX, diffY) {
2053 var el = this.getEl();
2055 if (!this.DDM.verifyEl(el)) {
2056 return;
2059 var dx = diffX || 0;
2060 var dy = diffY || 0;
2062 var p = Dom.getXY( el );
2064 this.initPageX = p[0] - dx;
2065 this.initPageY = p[1] - dy;
2067 this.lastPageX = p[0];
2068 this.lastPageY = p[1];
2072 this.setStartPosition(p);
2076 * Sets the start position of the element. This is set when the obj
2077 * is initialized, the reset when a drag is started.
2078 * @method setStartPosition
2079 * @param pos current position (from previous lookup)
2080 * @private
2082 setStartPosition: function(pos) {
2083 var p = pos || Dom.getXY(this.getEl());
2085 this.deltaSetXY = null;
2087 this.startPageX = p[0];
2088 this.startPageY = p[1];
2092 * Add this instance to a group of related drag/drop objects. All
2093 * instances belong to at least one group, and can belong to as many
2094 * groups as needed.
2095 * @method addToGroup
2096 * @param sGroup {string} the name of the group
2098 addToGroup: function(sGroup) {
2099 this.groups[sGroup] = true;
2100 this.DDM.regDragDrop(this, sGroup);
2104 * Remove's this instance from the supplied interaction group
2105 * @method removeFromGroup
2106 * @param {string} sGroup The group to drop
2108 removeFromGroup: function(sGroup) {
2109 if (this.groups[sGroup]) {
2110 delete this.groups[sGroup];
2113 this.DDM.removeDDFromGroup(this, sGroup);
2117 * Allows you to specify that an element other than the linked element
2118 * will be moved with the cursor during a drag
2119 * @method setDragElId
2120 * @param id {string} the id of the element that will be used to initiate the drag
2122 setDragElId: function(id) {
2123 this.dragElId = id;
2127 * Allows you to specify a child of the linked element that should be
2128 * used to initiate the drag operation. An example of this would be if
2129 * you have a content div with text and links. Clicking anywhere in the
2130 * content area would normally start the drag operation. Use this method
2131 * to specify that an element inside of the content div is the element
2132 * that starts the drag operation.
2133 * @method setHandleElId
2134 * @param id {string} the id of the element that will be used to
2135 * initiate the drag.
2137 setHandleElId: function(id) {
2138 if (typeof id !== "string") {
2139 id = Dom.generateId(id);
2141 this.handleElId = id;
2142 this.DDM.regHandle(this.id, id);
2146 * Allows you to set an element outside of the linked element as a drag
2147 * handle
2148 * @method setOuterHandleElId
2149 * @param id the id of the element that will be used to initiate the drag
2151 setOuterHandleElId: function(id) {
2152 if (typeof id !== "string") {
2153 id = Dom.generateId(id);
2155 Event.on(id, "mousedown",
2156 this.handleMouseDown, this, true);
2157 this.setHandleElId(id);
2159 this.hasOuterHandles = true;
2163 * Remove all drag and drop hooks for this element
2164 * @method unreg
2166 unreg: function() {
2167 Event.removeListener(this.id, "mousedown",
2168 this.handleMouseDown);
2169 this._domRef = null;
2170 this.DDM._remove(this);
2174 * Returns true if this instance is locked, or the drag drop mgr is locked
2175 * (meaning that all drag/drop is disabled on the page.)
2176 * @method isLocked
2177 * @return {boolean} true if this obj or all drag/drop is locked, else
2178 * false
2180 isLocked: function() {
2181 return (this.DDM.isLocked() || this.locked);
2185 * Fired when this object is clicked
2186 * @method handleMouseDown
2187 * @param {Event} e
2188 * @param {YAHOO.util.DragDrop} oDD the clicked dd object (this dd obj)
2189 * @private
2191 handleMouseDown: function(e, oDD) {
2193 var button = e.which || e.button;
2195 if (this.primaryButtonOnly && button > 1) {
2196 return;
2199 if (this.isLocked()) {
2200 return;
2205 // firing the mousedown events prior to calculating positions
2206 this.b4MouseDown(e);
2207 this.onMouseDown(e);
2209 this.DDM.refreshCache(this.groups);
2210 // var self = this;
2211 // setTimeout( function() { self.DDM.refreshCache(self.groups); }, 0);
2213 // Only process the event if we really clicked within the linked
2214 // element. The reason we make this check is that in the case that
2215 // another element was moved between the clicked element and the
2216 // cursor in the time between the mousedown and mouseup events. When
2217 // this happens, the element gets the next mousedown event
2218 // regardless of where on the screen it happened.
2219 var pt = new YAHOO.util.Point(Event.getPageX(e), Event.getPageY(e));
2220 if (!this.hasOuterHandles && !this.DDM.isOverTarget(pt, this) ) {
2221 } else {
2222 if (this.clickValidator(e)) {
2225 // set the initial element position
2226 this.setStartPosition();
2228 // start tracking mousemove distance and mousedown time to
2229 // determine when to start the actual drag
2230 this.DDM.handleMouseDown(e, this);
2232 // this mousedown is mine
2233 this.DDM.stopEvent(e);
2234 } else {
2241 clickValidator: function(e) {
2242 var target = Event.getTarget(e);
2243 return ( this.isValidHandleChild(target) &&
2244 (this.id == this.handleElId ||
2245 this.DDM.handleWasClicked(target, this.id)) );
2249 * Finds the location the element should be placed if we want to move
2250 * it to where the mouse location less the click offset would place us.
2251 * @method getTargetCoord
2252 * @param {int} iPageX the X coordinate of the click
2253 * @param {int} iPageY the Y coordinate of the click
2254 * @return an object that contains the coordinates (Object.x and Object.y)
2255 * @private
2257 getTargetCoord: function(iPageX, iPageY) {
2260 var x = iPageX - this.deltaX;
2261 var y = iPageY - this.deltaY;
2263 if (this.constrainX) {
2264 if (x < this.minX) { x = this.minX; }
2265 if (x > this.maxX) { x = this.maxX; }
2268 if (this.constrainY) {
2269 if (y < this.minY) { y = this.minY; }
2270 if (y > this.maxY) { y = this.maxY; }
2273 x = this.getTick(x, this.xTicks);
2274 y = this.getTick(y, this.yTicks);
2277 return {x:x, y:y};
2281 * Allows you to specify a tag name that should not start a drag operation
2282 * when clicked. This is designed to facilitate embedding links within a
2283 * drag handle that do something other than start the drag.
2284 * @method addInvalidHandleType
2285 * @param {string} tagName the type of element to exclude
2287 addInvalidHandleType: function(tagName) {
2288 var type = tagName.toUpperCase();
2289 this.invalidHandleTypes[type] = type;
2293 * Lets you to specify an element id for a child of a drag handle
2294 * that should not initiate a drag
2295 * @method addInvalidHandleId
2296 * @param {string} id the element id of the element you wish to ignore
2298 addInvalidHandleId: function(id) {
2299 if (typeof id !== "string") {
2300 id = Dom.generateId(id);
2302 this.invalidHandleIds[id] = id;
2307 * Lets you specify a css class of elements that will not initiate a drag
2308 * @method addInvalidHandleClass
2309 * @param {string} cssClass the class of the elements you wish to ignore
2311 addInvalidHandleClass: function(cssClass) {
2312 this.invalidHandleClasses.push(cssClass);
2316 * Unsets an excluded tag name set by addInvalidHandleType
2317 * @method removeInvalidHandleType
2318 * @param {string} tagName the type of element to unexclude
2320 removeInvalidHandleType: function(tagName) {
2321 var type = tagName.toUpperCase();
2322 // this.invalidHandleTypes[type] = null;
2323 delete this.invalidHandleTypes[type];
2327 * Unsets an invalid handle id
2328 * @method removeInvalidHandleId
2329 * @param {string} id the id of the element to re-enable
2331 removeInvalidHandleId: function(id) {
2332 if (typeof id !== "string") {
2333 id = Dom.generateId(id);
2335 delete this.invalidHandleIds[id];
2339 * Unsets an invalid css class
2340 * @method removeInvalidHandleClass
2341 * @param {string} cssClass the class of the element(s) you wish to
2342 * re-enable
2344 removeInvalidHandleClass: function(cssClass) {
2345 for (var i=0, len=this.invalidHandleClasses.length; i<len; ++i) {
2346 if (this.invalidHandleClasses[i] == cssClass) {
2347 delete this.invalidHandleClasses[i];
2353 * Checks the tag exclusion list to see if this click should be ignored
2354 * @method isValidHandleChild
2355 * @param {HTMLElement} node the HTMLElement to evaluate
2356 * @return {boolean} true if this is a valid tag type, false if not
2358 isValidHandleChild: function(node) {
2360 var valid = true;
2361 // var n = (node.nodeName == "#text") ? node.parentNode : node;
2362 var nodeName;
2363 try {
2364 nodeName = node.nodeName.toUpperCase();
2365 } catch(e) {
2366 nodeName = node.nodeName;
2368 valid = valid && !this.invalidHandleTypes[nodeName];
2369 valid = valid && !this.invalidHandleIds[node.id];
2371 for (var i=0, len=this.invalidHandleClasses.length; valid && i<len; ++i) {
2372 valid = !Dom.hasClass(node, this.invalidHandleClasses[i]);
2376 return valid;
2381 * Create the array of horizontal tick marks if an interval was specified
2382 * in setXConstraint().
2383 * @method setXTicks
2384 * @private
2386 setXTicks: function(iStartX, iTickSize) {
2387 this.xTicks = [];
2388 this.xTickSize = iTickSize;
2390 var tickMap = {};
2392 for (var i = this.initPageX; i >= this.minX; i = i - iTickSize) {
2393 if (!tickMap[i]) {
2394 this.xTicks[this.xTicks.length] = i;
2395 tickMap[i] = true;
2399 for (i = this.initPageX; i <= this.maxX; i = i + iTickSize) {
2400 if (!tickMap[i]) {
2401 this.xTicks[this.xTicks.length] = i;
2402 tickMap[i] = true;
2406 this.xTicks.sort(this.DDM.numericSort) ;
2410 * Create the array of vertical tick marks if an interval was specified in
2411 * setYConstraint().
2412 * @method setYTicks
2413 * @private
2415 setYTicks: function(iStartY, iTickSize) {
2416 this.yTicks = [];
2417 this.yTickSize = iTickSize;
2419 var tickMap = {};
2421 for (var i = this.initPageY; i >= this.minY; i = i - iTickSize) {
2422 if (!tickMap[i]) {
2423 this.yTicks[this.yTicks.length] = i;
2424 tickMap[i] = true;
2428 for (i = this.initPageY; i <= this.maxY; i = i + iTickSize) {
2429 if (!tickMap[i]) {
2430 this.yTicks[this.yTicks.length] = i;
2431 tickMap[i] = true;
2435 this.yTicks.sort(this.DDM.numericSort) ;
2439 * By default, the element can be dragged any place on the screen. Use
2440 * this method to limit the horizontal travel of the element. Pass in
2441 * 0,0 for the parameters if you want to lock the drag to the y axis.
2442 * @method setXConstraint
2443 * @param {int} iLeft the number of pixels the element can move to the left
2444 * @param {int} iRight the number of pixels the element can move to the
2445 * right
2446 * @param {int} iTickSize optional parameter for specifying that the
2447 * element
2448 * should move iTickSize pixels at a time.
2450 setXConstraint: function(iLeft, iRight, iTickSize) {
2451 this.leftConstraint = parseInt(iLeft, 10);
2452 this.rightConstraint = parseInt(iRight, 10);
2454 this.minX = this.initPageX - this.leftConstraint;
2455 this.maxX = this.initPageX + this.rightConstraint;
2456 if (iTickSize) { this.setXTicks(this.initPageX, iTickSize); }
2458 this.constrainX = true;
2462 * Clears any constraints applied to this instance. Also clears ticks
2463 * since they can't exist independent of a constraint at this time.
2464 * @method clearConstraints
2466 clearConstraints: function() {
2467 this.constrainX = false;
2468 this.constrainY = false;
2469 this.clearTicks();
2473 * Clears any tick interval defined for this instance
2474 * @method clearTicks
2476 clearTicks: function() {
2477 this.xTicks = null;
2478 this.yTicks = null;
2479 this.xTickSize = 0;
2480 this.yTickSize = 0;
2484 * By default, the element can be dragged any place on the screen. Set
2485 * this to limit the vertical travel of the element. Pass in 0,0 for the
2486 * parameters if you want to lock the drag to the x axis.
2487 * @method setYConstraint
2488 * @param {int} iUp the number of pixels the element can move up
2489 * @param {int} iDown the number of pixels the element can move down
2490 * @param {int} iTickSize optional parameter for specifying that the
2491 * element should move iTickSize pixels at a time.
2493 setYConstraint: function(iUp, iDown, iTickSize) {
2494 this.topConstraint = parseInt(iUp, 10);
2495 this.bottomConstraint = parseInt(iDown, 10);
2497 this.minY = this.initPageY - this.topConstraint;
2498 this.maxY = this.initPageY + this.bottomConstraint;
2499 if (iTickSize) { this.setYTicks(this.initPageY, iTickSize); }
2501 this.constrainY = true;
2506 * resetConstraints must be called if you manually reposition a dd element.
2507 * @method resetConstraints
2509 resetConstraints: function() {
2512 // Maintain offsets if necessary
2513 if (this.initPageX || this.initPageX === 0) {
2514 // figure out how much this thing has moved
2515 var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;
2516 var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;
2518 this.setInitPosition(dx, dy);
2520 // This is the first time we have detected the element's position
2521 } else {
2522 this.setInitPosition();
2525 if (this.constrainX) {
2526 this.setXConstraint( this.leftConstraint,
2527 this.rightConstraint,
2528 this.xTickSize );
2531 if (this.constrainY) {
2532 this.setYConstraint( this.topConstraint,
2533 this.bottomConstraint,
2534 this.yTickSize );
2539 * Normally the drag element is moved pixel by pixel, but we can specify
2540 * that it move a number of pixels at a time. This method resolves the
2541 * location when we have it set up like this.
2542 * @method getTick
2543 * @param {int} val where we want to place the object
2544 * @param {int[]} tickArray sorted array of valid points
2545 * @return {int} the closest tick
2546 * @private
2548 getTick: function(val, tickArray) {
2550 if (!tickArray) {
2551 // If tick interval is not defined, it is effectively 1 pixel,
2552 // so we return the value passed to us.
2553 return val;
2554 } else if (tickArray[0] >= val) {
2555 // The value is lower than the first tick, so we return the first
2556 // tick.
2557 return tickArray[0];
2558 } else {
2559 for (var i=0, len=tickArray.length; i<len; ++i) {
2560 var next = i + 1;
2561 if (tickArray[next] && tickArray[next] >= val) {
2562 var diff1 = val - tickArray[i];
2563 var diff2 = tickArray[next] - val;
2564 return (diff2 > diff1) ? tickArray[i] : tickArray[next];
2568 // The value is larger than the last tick, so we return the last
2569 // tick.
2570 return tickArray[tickArray.length - 1];
2575 * toString method
2576 * @method toString
2577 * @return {string} string representation of the dd obj
2579 toString: function() {
2580 return ("DragDrop " + this.id);
2585 })();
2587 * A DragDrop implementation where the linked element follows the
2588 * mouse cursor during a drag.
2589 * @class DD
2590 * @extends YAHOO.util.DragDrop
2591 * @constructor
2592 * @param {String} id the id of the linked element
2593 * @param {String} sGroup the group of related DragDrop items
2594 * @param {object} config an object containing configurable attributes
2595 * Valid properties for DD:
2596 * scroll
2598 YAHOO.util.DD = function(id, sGroup, config) {
2599 if (id) {
2600 this.init(id, sGroup, config);
2604 YAHOO.extend(YAHOO.util.DD, YAHOO.util.DragDrop, {
2607 * When set to true, the utility automatically tries to scroll the browser
2608 * window wehn a drag and drop element is dragged near the viewport boundary.
2609 * Defaults to true.
2610 * @property scroll
2611 * @type boolean
2613 scroll: true,
2616 * Sets the pointer offset to the distance between the linked element's top
2617 * left corner and the location the element was clicked
2618 * @method autoOffset
2619 * @param {int} iPageX the X coordinate of the click
2620 * @param {int} iPageY the Y coordinate of the click
2622 autoOffset: function(iPageX, iPageY) {
2623 var x = iPageX - this.startPageX;
2624 var y = iPageY - this.startPageY;
2625 this.setDelta(x, y);
2628 /**
2629 * Sets the pointer offset. You can call this directly to force the
2630 * offset to be in a particular location (e.g., pass in 0,0 to set it
2631 * to the center of the object, as done in YAHOO.widget.Slider)
2632 * @method setDelta
2633 * @param {int} iDeltaX the distance from the left
2634 * @param {int} iDeltaY the distance from the top
2636 setDelta: function(iDeltaX, iDeltaY) {
2637 this.deltaX = iDeltaX;
2638 this.deltaY = iDeltaY;
2642 * Sets the drag element to the location of the mousedown or click event,
2643 * maintaining the cursor location relative to the location on the element
2644 * that was clicked. Override this if you want to place the element in a
2645 * location other than where the cursor is.
2646 * @method setDragElPos
2647 * @param {int} iPageX the X coordinate of the mousedown or drag event
2648 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2650 setDragElPos: function(iPageX, iPageY) {
2651 // the first time we do this, we are going to check to make sure
2652 // the element has css positioning
2654 var el = this.getDragEl();
2655 this.alignElWithMouse(el, iPageX, iPageY);
2659 * Sets the element to the location of the mousedown or click event,
2660 * maintaining the cursor location relative to the location on the element
2661 * that was clicked. Override this if you want to place the element in a
2662 * location other than where the cursor is.
2663 * @method alignElWithMouse
2664 * @param {HTMLElement} el the element to move
2665 * @param {int} iPageX the X coordinate of the mousedown or drag event
2666 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2668 alignElWithMouse: function(el, iPageX, iPageY) {
2669 var oCoord = this.getTargetCoord(iPageX, iPageY);
2671 if (!this.deltaSetXY) {
2672 var aCoord = [oCoord.x, oCoord.y];
2673 YAHOO.util.Dom.setXY(el, aCoord);
2674 var newLeft = parseInt( YAHOO.util.Dom.getStyle(el, "left"), 10 );
2675 var newTop = parseInt( YAHOO.util.Dom.getStyle(el, "top" ), 10 );
2677 this.deltaSetXY = [ newLeft - oCoord.x, newTop - oCoord.y ];
2678 } else {
2679 YAHOO.util.Dom.setStyle(el, "left", (oCoord.x + this.deltaSetXY[0]) + "px");
2680 YAHOO.util.Dom.setStyle(el, "top", (oCoord.y + this.deltaSetXY[1]) + "px");
2683 this.cachePosition(oCoord.x, oCoord.y);
2684 this.autoScroll(oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
2688 * Saves the most recent position so that we can reset the constraints and
2689 * tick marks on-demand. We need to know this so that we can calculate the
2690 * number of pixels the element is offset from its original position.
2691 * @method cachePosition
2692 * @param iPageX the current x position (optional, this just makes it so we
2693 * don't have to look it up again)
2694 * @param iPageY the current y position (optional, this just makes it so we
2695 * don't have to look it up again)
2697 cachePosition: function(iPageX, iPageY) {
2698 if (iPageX) {
2699 this.lastPageX = iPageX;
2700 this.lastPageY = iPageY;
2701 } else {
2702 var aCoord = YAHOO.util.Dom.getXY(this.getEl());
2703 this.lastPageX = aCoord[0];
2704 this.lastPageY = aCoord[1];
2709 * Auto-scroll the window if the dragged object has been moved beyond the
2710 * visible window boundary.
2711 * @method autoScroll
2712 * @param {int} x the drag element's x position
2713 * @param {int} y the drag element's y position
2714 * @param {int} h the height of the drag element
2715 * @param {int} w the width of the drag element
2716 * @private
2718 autoScroll: function(x, y, h, w) {
2720 if (this.scroll) {
2721 // The client height
2722 var clientH = this.DDM.getClientHeight();
2724 // The client width
2725 var clientW = this.DDM.getClientWidth();
2727 // The amt scrolled down
2728 var st = this.DDM.getScrollTop();
2730 // The amt scrolled right
2731 var sl = this.DDM.getScrollLeft();
2733 // Location of the bottom of the element
2734 var bot = h + y;
2736 // Location of the right of the element
2737 var right = w + x;
2739 // The distance from the cursor to the bottom of the visible area,
2740 // adjusted so that we don't scroll if the cursor is beyond the
2741 // element drag constraints
2742 var toBot = (clientH + st - y - this.deltaY);
2744 // The distance from the cursor to the right of the visible area
2745 var toRight = (clientW + sl - x - this.deltaX);
2748 // How close to the edge the cursor must be before we scroll
2749 // var thresh = (document.all) ? 100 : 40;
2750 var thresh = 40;
2752 // How many pixels to scroll per autoscroll op. This helps to reduce
2753 // clunky scrolling. IE is more sensitive about this ... it needs this
2754 // value to be higher.
2755 var scrAmt = (document.all) ? 80 : 30;
2757 // Scroll down if we are near the bottom of the visible page and the
2758 // obj extends below the crease
2759 if ( bot > clientH && toBot < thresh ) {
2760 window.scrollTo(sl, st + scrAmt);
2763 // Scroll up if the window is scrolled down and the top of the object
2764 // goes above the top border
2765 if ( y < st && st > 0 && y - st < thresh ) {
2766 window.scrollTo(sl, st - scrAmt);
2769 // Scroll right if the obj is beyond the right border and the cursor is
2770 // near the border.
2771 if ( right > clientW && toRight < thresh ) {
2772 window.scrollTo(sl + scrAmt, st);
2775 // Scroll left if the window has been scrolled to the right and the obj
2776 // extends past the left border
2777 if ( x < sl && sl > 0 && x - sl < thresh ) {
2778 window.scrollTo(sl - scrAmt, st);
2784 * Sets up config options specific to this class. Overrides
2785 * YAHOO.util.DragDrop, but all versions of this method through the
2786 * inheritance chain are called
2788 applyConfig: function() {
2789 YAHOO.util.DD.superclass.applyConfig.call(this);
2790 this.scroll = (this.config.scroll !== false);
2794 * Event that fires prior to the onMouseDown event. Overrides
2795 * YAHOO.util.DragDrop.
2797 b4MouseDown: function(e) {
2798 this.setStartPosition();
2799 // this.resetConstraints();
2800 this.autoOffset(YAHOO.util.Event.getPageX(e),
2801 YAHOO.util.Event.getPageY(e));
2805 * Event that fires prior to the onDrag event. Overrides
2806 * YAHOO.util.DragDrop.
2808 b4Drag: function(e) {
2809 this.setDragElPos(YAHOO.util.Event.getPageX(e),
2810 YAHOO.util.Event.getPageY(e));
2813 toString: function() {
2814 return ("DD " + this.id);
2817 //////////////////////////////////////////////////////////////////////////
2818 // Debugging ygDragDrop events that can be overridden
2819 //////////////////////////////////////////////////////////////////////////
2821 startDrag: function(x, y) {
2824 onDrag: function(e) {
2827 onDragEnter: function(e, id) {
2830 onDragOver: function(e, id) {
2833 onDragOut: function(e, id) {
2836 onDragDrop: function(e, id) {
2839 endDrag: function(e) {
2846 * A DragDrop implementation that inserts an empty, bordered div into
2847 * the document that follows the cursor during drag operations. At the time of
2848 * the click, the frame div is resized to the dimensions of the linked html
2849 * element, and moved to the exact location of the linked element.
2851 * References to the "frame" element refer to the single proxy element that
2852 * was created to be dragged in place of all DDProxy elements on the
2853 * page.
2855 * @class DDProxy
2856 * @extends YAHOO.util.DD
2857 * @constructor
2858 * @param {String} id the id of the linked html element
2859 * @param {String} sGroup the group of related DragDrop objects
2860 * @param {object} config an object containing configurable attributes
2861 * Valid properties for DDProxy in addition to those in DragDrop:
2862 * resizeFrame, centerFrame, dragElId
2864 YAHOO.util.DDProxy = function(id, sGroup, config) {
2865 if (id) {
2866 this.init(id, sGroup, config);
2867 this.initFrame();
2872 * The default drag frame div id
2873 * @property YAHOO.util.DDProxy.dragElId
2874 * @type String
2875 * @static
2877 YAHOO.util.DDProxy.dragElId = "ygddfdiv";
2879 YAHOO.extend(YAHOO.util.DDProxy, YAHOO.util.DD, {
2882 * By default we resize the drag frame to be the same size as the element
2883 * we want to drag (this is to get the frame effect). We can turn it off
2884 * if we want a different behavior.
2885 * @property resizeFrame
2886 * @type boolean
2888 resizeFrame: true,
2891 * By default the frame is positioned exactly where the drag element is, so
2892 * we use the cursor offset provided by YAHOO.util.DD. Another option that works only if
2893 * you do not have constraints on the obj is to have the drag frame centered
2894 * around the cursor. Set centerFrame to true for this effect.
2895 * @property centerFrame
2896 * @type boolean
2898 centerFrame: false,
2901 * Creates the proxy element if it does not yet exist
2902 * @method createFrame
2904 createFrame: function() {
2905 var self=this, body=document.body;
2907 if (!body || !body.firstChild) {
2908 setTimeout( function() { self.createFrame(); }, 50 );
2909 return;
2912 var div=this.getDragEl(), Dom=YAHOO.util.Dom;
2914 if (!div) {
2915 div = document.createElement("div");
2916 div.id = this.dragElId;
2917 var s = div.style;
2919 s.position = "absolute";
2920 s.visibility = "hidden";
2921 s.cursor = "move";
2922 s.border = "2px solid #aaa";
2923 s.zIndex = 999;
2924 s.height = "25px";
2925 s.width = "25px";
2927 var _data = document.createElement('div');
2928 Dom.setStyle(_data, 'height', '100%');
2929 Dom.setStyle(_data, 'width', '100%');
2931 * If the proxy element has no background-color, then it is considered to the "transparent" by Internet Explorer.
2932 * Since it is "transparent" then the events pass through it to the iframe below.
2933 * So creating a "fake" div inside the proxy element and giving it a background-color, then setting it to an
2934 * opacity of 0, it appears to not be there, however IE still thinks that it is so the events never pass through.
2936 Dom.setStyle(_data, 'background-color', '#ccc');
2937 Dom.setStyle(_data, 'opacity', '0');
2938 div.appendChild(_data);
2940 // appendChild can blow up IE if invoked prior to the window load event
2941 // while rendering a table. It is possible there are other scenarios
2942 // that would cause this to happen as well.
2943 body.insertBefore(div, body.firstChild);
2948 * Initialization for the drag frame element. Must be called in the
2949 * constructor of all subclasses
2950 * @method initFrame
2952 initFrame: function() {
2953 this.createFrame();
2956 applyConfig: function() {
2957 YAHOO.util.DDProxy.superclass.applyConfig.call(this);
2959 this.resizeFrame = (this.config.resizeFrame !== false);
2960 this.centerFrame = (this.config.centerFrame);
2961 this.setDragElId(this.config.dragElId || YAHOO.util.DDProxy.dragElId);
2965 * Resizes the drag frame to the dimensions of the clicked object, positions
2966 * it over the object, and finally displays it
2967 * @method showFrame
2968 * @param {int} iPageX X click position
2969 * @param {int} iPageY Y click position
2970 * @private
2972 showFrame: function(iPageX, iPageY) {
2973 var el = this.getEl();
2974 var dragEl = this.getDragEl();
2975 var s = dragEl.style;
2977 this._resizeProxy();
2979 if (this.centerFrame) {
2980 this.setDelta( Math.round(parseInt(s.width, 10)/2),
2981 Math.round(parseInt(s.height, 10)/2) );
2984 this.setDragElPos(iPageX, iPageY);
2986 YAHOO.util.Dom.setStyle(dragEl, "visibility", "visible");
2990 * The proxy is automatically resized to the dimensions of the linked
2991 * element when a drag is initiated, unless resizeFrame is set to false
2992 * @method _resizeProxy
2993 * @private
2995 _resizeProxy: function() {
2996 if (this.resizeFrame) {
2997 var DOM = YAHOO.util.Dom;
2998 var el = this.getEl();
2999 var dragEl = this.getDragEl();
3001 var bt = parseInt( DOM.getStyle(dragEl, "borderTopWidth" ), 10);
3002 var br = parseInt( DOM.getStyle(dragEl, "borderRightWidth" ), 10);
3003 var bb = parseInt( DOM.getStyle(dragEl, "borderBottomWidth" ), 10);
3004 var bl = parseInt( DOM.getStyle(dragEl, "borderLeftWidth" ), 10);
3006 if (isNaN(bt)) { bt = 0; }
3007 if (isNaN(br)) { br = 0; }
3008 if (isNaN(bb)) { bb = 0; }
3009 if (isNaN(bl)) { bl = 0; }
3012 var newWidth = Math.max(0, el.offsetWidth - br - bl);
3013 var newHeight = Math.max(0, el.offsetHeight - bt - bb);
3016 DOM.setStyle( dragEl, "width", newWidth + "px" );
3017 DOM.setStyle( dragEl, "height", newHeight + "px" );
3021 // overrides YAHOO.util.DragDrop
3022 b4MouseDown: function(e) {
3023 this.setStartPosition();
3024 var x = YAHOO.util.Event.getPageX(e);
3025 var y = YAHOO.util.Event.getPageY(e);
3026 this.autoOffset(x, y);
3028 // This causes the autoscroll code to kick off, which means autoscroll can
3029 // happen prior to the check for a valid drag handle.
3030 // this.setDragElPos(x, y);
3033 // overrides YAHOO.util.DragDrop
3034 b4StartDrag: function(x, y) {
3035 // show the drag frame
3036 this.showFrame(x, y);
3039 // overrides YAHOO.util.DragDrop
3040 b4EndDrag: function(e) {
3041 YAHOO.util.Dom.setStyle(this.getDragEl(), "visibility", "hidden");
3044 // overrides YAHOO.util.DragDrop
3045 // By default we try to move the element to the last location of the frame.
3046 // This is so that the default behavior mirrors that of YAHOO.util.DD.
3047 endDrag: function(e) {
3048 var DOM = YAHOO.util.Dom;
3049 var lel = this.getEl();
3050 var del = this.getDragEl();
3052 // Show the drag frame briefly so we can get its position
3053 // del.style.visibility = "";
3054 DOM.setStyle(del, "visibility", "");
3056 // Hide the linked element before the move to get around a Safari
3057 // rendering bug.
3058 //lel.style.visibility = "hidden";
3059 DOM.setStyle(lel, "visibility", "hidden");
3060 YAHOO.util.DDM.moveToEl(lel, del);
3061 //del.style.visibility = "hidden";
3062 DOM.setStyle(del, "visibility", "hidden");
3063 //lel.style.visibility = "";
3064 DOM.setStyle(lel, "visibility", "");
3067 toString: function() {
3068 return ("DDProxy " + this.id);
3073 * A DragDrop implementation that does not move, but can be a drop
3074 * target. You would get the same result by simply omitting implementation
3075 * for the event callbacks, but this way we reduce the processing cost of the
3076 * event listener and the callbacks.
3077 * @class DDTarget
3078 * @extends YAHOO.util.DragDrop
3079 * @constructor
3080 * @param {String} id the id of the element that is a drop target
3081 * @param {String} sGroup the group of related DragDrop objects
3082 * @param {object} config an object containing configurable attributes
3083 * Valid properties for DDTarget in addition to those in
3084 * DragDrop:
3085 * none
3087 YAHOO.util.DDTarget = function(id, sGroup, config) {
3088 if (id) {
3089 this.initTarget(id, sGroup, config);
3093 // YAHOO.util.DDTarget.prototype = new YAHOO.util.DragDrop();
3094 YAHOO.extend(YAHOO.util.DDTarget, YAHOO.util.DragDrop, {
3095 toString: function() {
3096 return ("DDTarget " + this.id);
3099 YAHOO.register("dragdrop", YAHOO.util.DragDropMgr, {version: "2.3.0", build: "442"});