2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * 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.
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) {
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.
31 YAHOO.util.DragDropMgr = function() {
33 var Event = YAHOO.util.Event,
38 * This property is used to turn on global use of the shim element on all DragDrop instances, defaults to false for backcompat. (Use: YAHOO.util.DDM.useShim = true)
45 * This property is used to determine if the shim is active over the screen, default false.
47 * @property _shimActive
53 * This property is used when useShim is set on a DragDrop object to store the current state of DDM.useShim so it can be reset when a drag operation is done.
55 * @property _shimState
61 * This property is used when useShim is set to true, it will set the opacity on the shim to .5 for debugging. Use: (YAHOO.util.DDM._debugShim = true;)
63 * @property _debugShim
69 * This method will create a shim element (giving it the id of yui-ddm-shim), it also attaches the mousemove and mouseup listeners to it and attaches a scroll listener on the window
74 _createShim: function() {
75 var s = document.createElement('div');
76 s.id = 'yui-ddm-shim';
77 if (document.body.firstChild) {
78 document.body.insertBefore(s, document.body.firstChild);
80 document.body.appendChild(s);
82 s.style.display = 'none';
83 s.style.backgroundColor = 'red';
84 s.style.position = 'absolute';
85 s.style.zIndex = '99999';
86 Dom.setStyle(s, 'opacity', '0');
88 Event.on(s, "mouseup", this.handleMouseUp, this, true);
89 Event.on(s, "mousemove", this.handleMouseMove, this, true);
90 Event.on(window, 'scroll', this._sizeShim, this, true);
93 * This method will size the shim, called from activate and on window scroll event
98 _sizeShim: function() {
99 if (this._shimActive) {
101 s.style.height = Dom.getDocumentHeight() + 'px';
102 s.style.width = Dom.getDocumentWidth() + 'px';
108 * This method will create the shim element if needed, then show the shim element, size the element and set the _shimActive property to true
110 * @method _activateShim
113 _activateShim: function() {
118 this._shimActive = true;
121 if (this._debugShim) {
124 Dom.setStyle(s, 'opacity', o);
126 s.style.display = 'block';
130 * This method will hide the shim element and set the _shimActive property to false
132 * @method _deactivateShim
135 _deactivateShim: function() {
136 this._shim.style.display = 'none';
137 this._shimActive = false;
140 * The HTML element created to use as a shim over the document to track mouse movements
148 * Two dimensional Array of registered DragDrop objects. The first
149 * dimension is the DragDrop item group, the second the DragDrop
152 * @type {string: string}
159 * Array of element ids defined as drag handles. Used to determine
160 * if the element that generated the mousedown event is actually the
161 * handle and not the html element itself.
162 * @property handleIds
163 * @type {string: string}
170 * the DragDrop object that is currently being dragged
171 * @property dragCurrent
179 * the DragDrop object(s) that are being hovered over
180 * @property dragOvers
188 * the X distance between the cursor and the object being dragged
197 * the Y distance between the cursor and the object being dragged
206 * Flag to determine if we should prevent the default behavior of the
207 * events we define. By default this is true, but this can be set to
208 * false if you need the default behavior (not recommended)
209 * @property preventDefault
213 preventDefault: true,
216 * Flag to determine if we should stop the propagation of the events
217 * we generate. This is true by default but you may want to set it to
218 * false if the html element contains other features that require the
220 * @property stopPropagation
224 stopPropagation: true,
227 * Internal flag that is set to true when drag and drop has been
229 * @property initialized
236 * All drag and drop can be disabled.
244 * Provides additional information about the current set of
245 * interactions. Can be accessed from the event handlers. It
246 * contains the following properties:
248 * out: onDragOut interactions
249 * enter: onDragEnter interactions
250 * over: onDragOver interactions
251 * drop: onDragDrop interactions
252 * point: The location of the cursor
253 * draggedRegion: The location of dragged element at the time
255 * sourceRegion: The location of the source elemtn at the time
258 * @property interactionInfo
262 interactionInfo: null,
265 * Called the first time an element is registered.
271 this.initialized = true;
275 * In point mode, drag and drop interaction is defined by the
276 * location of the cursor during the drag/drop
285 * In intersect mode, drag and drop interaction is defined by the
286 * cursor position or the amount of overlap of two or more drag and
288 * @property INTERSECT
296 * In intersect mode, drag and drop interaction is defined only by the
297 * overlap of two or more drag and drop objects.
298 * @property STRICT_INTERSECT
306 * The current drag and drop mode. Default: POINT
314 * Runs method on all drag and drop objects
319 _execOnAll: function(sMethod, args) {
320 for (var i in this.ids) {
321 for (var j in this.ids[i]) {
322 var oDD = this.ids[i][j];
323 if (! this.isTypeOfDD(oDD)) {
326 oDD[sMethod].apply(oDD, args);
332 * Drag and drop initialization. Sets up the global event handlers
337 _onLoad: function() {
341 Event.on(document, "mouseup", this.handleMouseUp, this, true);
342 Event.on(document, "mousemove", this.handleMouseMove, this, true);
343 Event.on(window, "unload", this._onUnload, this, true);
344 Event.on(window, "resize", this._onResize, this, true);
345 // Event.on(window, "mouseout", this._test);
350 * Reset constraints on all drag and drop objs
355 _onResize: function(e) {
356 this._execOnAll("resetConstraints", []);
360 * Lock all drag and drop functionality
364 lock: function() { this.locked = true; },
367 * Unlock all drag and drop functionality
371 unlock: function() { this.locked = false; },
374 * Is drag and drop locked?
376 * @return {boolean} True if drag and drop is locked, false otherwise.
379 isLocked: function() { return this.locked; },
382 * Location cache that is set for all drag drop objects when a drag is
383 * initiated, cleared when the drag is finished.
384 * @property locationCache
391 * Set useCache to false if you want to force object the lookup of each
392 * drag and drop linked element constantly during a drag.
400 * The number of pixels that the mouse needs to move after the
401 * mousedown before the drag is initiated. Default=3;
402 * @property clickPixelThresh
409 * The number of milliseconds after the mousedown event to initiate the
410 * drag if we don't get a mouseup event. Default=1000
411 * @property clickTimeThresh
415 clickTimeThresh: 1000,
418 * Flag that indicates that either the drag pixel threshold or the
419 * mousdown time threshold has been met
420 * @property dragThreshMet
425 dragThreshMet: false,
428 * Timeout used for the click time threshold
429 * @property clickTimeout
437 * The X position of the mousedown event stored for later use when a
438 * drag threshold is met.
447 * The Y position of the mousedown event stored for later use when a
448 * drag threshold is met.
457 * Flag to determine if the drag event was fired from the click timeout and
458 * not the mouse move threshold.
459 * @property fromTimeout
467 * Each DragDrop instance must be registered with the DragDropMgr.
468 * This is executed in DragDrop.init()
469 * @method regDragDrop
470 * @param {DragDrop} oDD the DragDrop object to register
471 * @param {String} sGroup the name of the group this element belongs to
474 regDragDrop: function(oDD, sGroup) {
475 if (!this.initialized) { this.init(); }
477 if (!this.ids[sGroup]) {
478 this.ids[sGroup] = {};
480 this.ids[sGroup][oDD.id] = oDD;
484 * Removes the supplied dd instance from the supplied group. Executed
485 * by DragDrop.removeFromGroup, so don't call this function directly.
486 * @method removeDDFromGroup
490 removeDDFromGroup: function(oDD, sGroup) {
491 if (!this.ids[sGroup]) {
492 this.ids[sGroup] = {};
495 var obj = this.ids[sGroup];
496 if (obj && obj[oDD.id]) {
502 * Unregisters a drag and drop item. This is executed in
503 * DragDrop.unreg, use that method instead of calling this directly.
508 _remove: function(oDD) {
509 for (var g in oDD.groups) {
511 var item = this.ids[g];
512 if (item && item[oDD.id]) {
518 delete this.handleIds[oDD.id];
522 * Each DragDrop handle element must be registered. This is done
523 * automatically when executing DragDrop.setHandleElId()
525 * @param {String} sDDId the DragDrop id this element is a handle for
526 * @param {String} sHandleId the id of the element that is the drag
530 regHandle: function(sDDId, sHandleId) {
531 if (!this.handleIds[sDDId]) {
532 this.handleIds[sDDId] = {};
534 this.handleIds[sDDId][sHandleId] = sHandleId;
538 * Utility function to determine if a given element has been
539 * registered as a drag drop item.
541 * @param {String} id the element id to check
542 * @return {boolean} true if this element is a DragDrop item,
546 isDragDrop: function(id) {
547 return ( this.getDDById(id) ) ? true : false;
551 * Returns the drag and drop instances that are in all groups the
552 * passed in instance belongs to.
554 * @param {DragDrop} p_oDD the obj to get related data for
555 * @param {boolean} bTargetsOnly if true, only return targetable objs
556 * @return {DragDrop[]} the related instances
559 getRelated: function(p_oDD, bTargetsOnly) {
561 for (var i in p_oDD.groups) {
562 for (var j in this.ids[i]) {
563 var dd = this.ids[i][j];
564 if (! this.isTypeOfDD(dd)) {
567 if (!bTargetsOnly || dd.isTarget) {
568 oDDs[oDDs.length] = dd;
577 * Returns true if the specified dd target is a legal target for
578 * the specifice drag obj
579 * @method isLegalTarget
580 * @param {DragDrop} the drag obj
581 * @param {DragDrop} the target
582 * @return {boolean} true if the target is a legal target for the
586 isLegalTarget: function (oDD, oTargetDD) {
587 var targets = this.getRelated(oDD, true);
588 for (var i=0, len=targets.length;i<len;++i) {
589 if (targets[i].id == oTargetDD.id) {
598 * My goal is to be able to transparently determine if an object is
599 * typeof DragDrop, and the exact subclass of DragDrop. typeof
600 * returns "object", oDD.constructor.toString() always returns
601 * "DragDrop" and not the name of the subclass. So for now it just
602 * evaluates a well-known variable in DragDrop.
604 * @param {Object} the object to evaluate
605 * @return {boolean} true if typeof oDD = DragDrop
608 isTypeOfDD: function (oDD) {
609 return (oDD && oDD.__ygDragDrop);
613 * Utility function to determine if a given element has been
614 * registered as a drag drop handle for the given Drag Drop object.
616 * @param {String} id the element id to check
617 * @return {boolean} true if this element is a DragDrop handle, false
621 isHandle: function(sDDId, sHandleId) {
622 return ( this.handleIds[sDDId] &&
623 this.handleIds[sDDId][sHandleId] );
627 * Returns the DragDrop instance for a given id
629 * @param {String} id the id of the DragDrop object
630 * @return {DragDrop} the drag drop object, null if it is not found
633 getDDById: function(id) {
634 for (var i in this.ids) {
635 if (this.ids[i][id]) {
636 return this.ids[i][id];
643 * Fired after a registered DragDrop object gets the mousedown event.
644 * Sets up the events required to track the object being dragged
645 * @method handleMouseDown
646 * @param {Event} e the event
647 * @param oDD the DragDrop object being dragged
651 handleMouseDown: function(e, oDD) {
652 //this._activateShim();
654 this.currentTarget = YAHOO.util.Event.getTarget(e);
656 this.dragCurrent = oDD;
658 var el = oDD.getEl();
660 // track start position
661 this.startX = YAHOO.util.Event.getPageX(e);
662 this.startY = YAHOO.util.Event.getPageY(e);
664 this.deltaX = this.startX - el.offsetLeft;
665 this.deltaY = this.startY - el.offsetTop;
667 this.dragThreshMet = false;
669 this.clickTimeout = setTimeout(
671 var DDM = YAHOO.util.DDM;
672 DDM.startDrag(DDM.startX, DDM.startY);
673 DDM.fromTimeout = true;
675 this.clickTimeThresh );
679 * Fired when either the drag pixel threshold or the mousedown hold
680 * time threshold has been met.
682 * @param x {int} the X position of the original mousedown
683 * @param y {int} the Y position of the original mousedown
686 startDrag: function(x, y) {
687 if (this.dragCurrent && this.dragCurrent.useShim) {
688 this._shimState = this.useShim;
691 this._activateShim();
692 clearTimeout(this.clickTimeout);
693 var dc = this.dragCurrent;
694 if (dc && dc.events.b4StartDrag) {
695 dc.b4StartDrag(x, y);
696 dc.fireEvent('b4StartDragEvent', { x: x, y: y });
698 if (dc && dc.events.startDrag) {
700 dc.fireEvent('startDragEvent', { x: x, y: y });
702 this.dragThreshMet = true;
706 * Internal function to handle the mouseup event. Will be invoked
707 * from the context of the document.
708 * @method handleMouseUp
709 * @param {Event} e the event
713 handleMouseUp: function(e) {
714 if (this.dragCurrent) {
715 clearTimeout(this.clickTimeout);
717 if (this.dragThreshMet) {
718 if (this.fromTimeout) {
719 this.fromTimeout = false;
720 this.handleMouseMove(e);
722 this.fromTimeout = false;
723 this.fireEvents(e, true);
734 * Utility to stop event propagation and event default, if these
735 * features are turned on.
737 * @param {Event} e the event as returned by this.getEvent()
740 stopEvent: function(e) {
741 if (this.stopPropagation) {
742 YAHOO.util.Event.stopPropagation(e);
745 if (this.preventDefault) {
746 YAHOO.util.Event.preventDefault(e);
751 * Ends the current drag, cleans up the state, and fires the endDrag
752 * and mouseUp events. Called internally when a mouseup is detected
753 * during the drag. Can be fired manually during the drag by passing
754 * either another event (such as the mousemove event received in onDrag)
755 * or a fake event with pageX and pageY defined (so that endDrag and
756 * onMouseUp have usable position data.). Alternatively, pass true
757 * for the silent parameter so that the endDrag and onMouseUp events
758 * are skipped (so no event data is needed.)
761 * @param {Event} e the mouseup event, another event (or a fake event)
762 * with pageX and pageY defined, or nothing if the
763 * silent parameter is true
764 * @param {boolean} silent skips the enddrag and mouseup events if true
767 stopDrag: function(e, silent) {
768 var dc = this.dragCurrent;
769 // Fire the drag end event for the item that was dragged
771 if (this.dragThreshMet) {
772 if (dc.events.b4EndDrag) {
774 dc.fireEvent('b4EndDragEvent', { e: e });
776 if (dc.events.endDrag) {
778 dc.fireEvent('endDragEvent', { e: e });
781 if (dc.events.mouseUp) {
783 dc.fireEvent('mouseUpEvent', { e: e });
787 if (this._shimActive) {
788 this._deactivateShim();
789 if (this.dragCurrent && this.dragCurrent.useShim) {
790 this.useShim = this._shimState;
791 this._shimState = false;
795 this.dragCurrent = null;
800 * Internal function to handle the mousemove event. Will be invoked
801 * from the context of the html element.
803 * @TODO figure out what we can do about mouse events lost when the
804 * user drags objects beyond the window boundary. Currently we can
805 * detect this in internet explorer by verifying that the mouse is
806 * down during the mousemove event. Firefox doesn't give us the
807 * button state on the mousemove event.
808 * @method handleMouseMove
809 * @param {Event} e the event
813 handleMouseMove: function(e) {
815 var dc = this.dragCurrent;
818 // var button = e.which || e.button;
820 // check for IE mouseup outside of page boundary
821 if (YAHOO.util.Event.isIE && !e.button) {
823 return this.handleMouseUp(e);
825 if (e.clientX < 0 || e.clientY < 0) {
826 //This will stop the element from leaving the viewport in FF, Opera & Safari
833 if (!this.dragThreshMet) {
834 var diffX = Math.abs(this.startX - YAHOO.util.Event.getPageX(e));
835 var diffY = Math.abs(this.startY - YAHOO.util.Event.getPageY(e));
836 if (diffX > this.clickPixelThresh ||
837 diffY > this.clickPixelThresh) {
838 this.startDrag(this.startX, this.startY);
842 if (this.dragThreshMet) {
843 if (dc && dc.events.b4Drag) {
845 dc.fireEvent('b4DragEvent', { e: e});
847 if (dc && dc.events.drag) {
849 dc.fireEvent('dragEvent', { e: e});
852 this.fireEvents(e, false);
861 * Iterates over all of the DragDrop elements to find ones we are
862 * hovering over or dropping on
864 * @param {Event} e the event
865 * @param {boolean} isDrop is this a drop op or a mouseover op?
869 fireEvents: function(e, isDrop) {
870 var dc = this.dragCurrent;
872 // If the user did the mouse up outside of the window, we could
873 // get here even though we have ended the drag.
874 // If the config option dragOnly is true, bail out and don't fire the events
875 if (!dc || dc.isLocked() || dc.dragOnly) {
879 var x = YAHOO.util.Event.getPageX(e),
880 y = YAHOO.util.Event.getPageY(e),
881 pt = new YAHOO.util.Point(x,y),
882 pos = dc.getTargetCoord(pt.x, pt.y),
884 events = ['out', 'over', 'drop', 'enter'],
885 curRegion = new YAHOO.util.Region( pos.y,
886 pos.x + el.offsetWidth,
887 pos.y + el.offsetHeight,
890 oldOvers = [], // cache the previous dragOver array
901 // Check to see if the object(s) we were hovering over is no longer
902 // being hovered over so we can fire the onDragOut event
903 for (var i in this.dragOvers) {
905 var ddo = this.dragOvers[i];
907 if (! this.isTypeOfDD(ddo)) {
910 if (! this.isOverTarget(pt, ddo, this.mode, curRegion)) {
911 data.outEvts.push( ddo );
915 delete this.dragOvers[i];
918 for (var sGroup in dc.groups) {
920 if ("string" != typeof sGroup) {
924 for (i in this.ids[sGroup]) {
925 var oDD = this.ids[sGroup][i];
926 if (! this.isTypeOfDD(oDD)) {
930 if (oDD.isTarget && !oDD.isLocked() && oDD != dc) {
931 if (this.isOverTarget(pt, oDD, this.mode, curRegion)) {
932 inGroupsObj[sGroup] = true;
933 // look for drop interactions
935 data.dropEvts.push( oDD );
936 // look for drag enter and drag over interactions
939 // initial drag over: dragEnter fires
940 if (!oldOvers[oDD.id]) {
941 data.enterEvts.push( oDD );
942 // subsequent drag overs: dragOver fires
944 data.overEvts.push( oDD );
947 this.dragOvers[oDD.id] = oDD;
954 this.interactionInfo = {
956 enter: data.enterEvts,
960 draggedRegion: curRegion,
961 sourceRegion: this.locationCache[dc.id],
966 for (var inG in inGroupsObj) {
970 // notify about a drop that did not find a target
971 if (isDrop && !data.dropEvts.length) {
972 this.interactionInfo.validDrop = false;
973 if (dc.events.invalidDrop) {
975 dc.fireEvent('invalidDropEvent', { e: e });
978 for (i = 0; i < events.length; i++) {
980 if (data[events[i] + 'Evts']) {
981 tmp = data[events[i] + 'Evts'];
983 if (tmp && tmp.length) {
984 var type = events[i].charAt(0).toUpperCase() + events[i].substr(1),
985 ev = 'onDrag' + type,
986 b4 = 'b4Drag' + type,
987 cev = 'drag' + type + 'Event',
988 check = 'drag' + type;
991 dc[b4](e, tmp, inGroups);
992 dc.fireEvent(b4 + 'Event', { event: e, info: tmp, group: inGroups });
995 if (dc.events[check]) {
996 dc[ev](e, tmp, inGroups);
997 dc.fireEvent(cev, { event: e, info: tmp, group: inGroups });
1000 for (var b = 0, len = tmp.length; b < len; ++b) {
1001 if (dc.events[b4]) {
1002 dc[b4](e, tmp[b].id, inGroups[0]);
1003 dc.fireEvent(b4 + 'Event', { event: e, info: tmp[b].id, group: inGroups[0] });
1005 if (dc.events[check]) {
1006 dc[ev](e, tmp[b].id, inGroups[0]);
1007 dc.fireEvent(cev, { event: e, info: tmp[b].id, group: inGroups[0] });
1016 * Helper function for getting the best match from the list of drag
1017 * and drop objects returned by the drag and drop events when we are
1018 * in INTERSECT mode. It returns either the first object that the
1019 * cursor is over, or the object that has the greatest overlap with
1020 * the dragged element.
1021 * @method getBestMatch
1022 * @param {DragDrop[]} dds The array of drag and drop objects
1024 * @return {DragDrop} The best single match
1027 getBestMatch: function(dds) {
1030 var len = dds.length;
1035 // Loop through the targeted items
1036 for (var i=0; i<len; ++i) {
1038 // If the cursor is over the object, it wins. If the
1039 // cursor is over multiple matches, the first one we come
1041 if (this.mode == this.INTERSECT && dd.cursorIsOver) {
1044 // Otherwise the object with the most overlap wins
1046 if (!winner || !winner.overlap || (dd.overlap &&
1047 winner.overlap.getArea() < dd.overlap.getArea())) {
1058 * Refreshes the cache of the top-left and bottom-right points of the
1059 * drag and drop objects in the specified group(s). This is in the
1060 * format that is stored in the drag and drop instance, so typical
1063 * YAHOO.util.DragDropMgr.refreshCache(ddinstance.groups);
1067 * YAHOO.util.DragDropMgr.refreshCache({group1:true, group2:true});
1069 * @TODO this really should be an indexed array. Alternatively this
1070 * method could accept both.
1071 * @method refreshCache
1072 * @param {Object} groups an associative array of groups to refresh
1075 refreshCache: function(groups) {
1077 // refresh everything if group array is not provided
1078 var g = groups || this.ids;
1080 for (var sGroup in g) {
1081 if ("string" != typeof sGroup) {
1084 for (var i in this.ids[sGroup]) {
1085 var oDD = this.ids[sGroup][i];
1087 if (this.isTypeOfDD(oDD)) {
1088 var loc = this.getLocation(oDD);
1090 this.locationCache[oDD.id] = loc;
1092 delete this.locationCache[oDD.id];
1100 * This checks to make sure an element exists and is in the DOM. The
1101 * main purpose is to handle cases where innerHTML is used to remove
1102 * drag and drop objects from the DOM. IE provides an 'unspecified
1103 * error' when trying to access the offsetParent of such an element
1105 * @param {HTMLElement} el the element to check
1106 * @return {boolean} true if the element looks usable
1109 verifyEl: function(el) {
1112 var parent = el.offsetParent;
1124 * Returns a Region object containing the drag and drop element's position
1125 * and size, including the padding configured for it
1126 * @method getLocation
1127 * @param {DragDrop} oDD the drag and drop object to get the
1129 * @return {YAHOO.util.Region} a Region object representing the total area
1130 * the element occupies, including any padding
1131 * the instance is configured for.
1134 getLocation: function(oDD) {
1135 if (! this.isTypeOfDD(oDD)) {
1139 var el = oDD.getEl(), pos, x1, x2, y1, y2, t, r, b, l;
1142 pos= YAHOO.util.Dom.getXY(el);
1150 x2 = x1 + el.offsetWidth;
1152 y2 = y1 + el.offsetHeight;
1154 t = y1 - oDD.padding[0];
1155 r = x2 + oDD.padding[1];
1156 b = y2 + oDD.padding[2];
1157 l = x1 - oDD.padding[3];
1159 return new YAHOO.util.Region( t, r, b, l );
1163 * Checks the cursor location to see if it over the target
1164 * @method isOverTarget
1165 * @param {YAHOO.util.Point} pt The point to evaluate
1166 * @param {DragDrop} oTarget the DragDrop object we are inspecting
1167 * @param {boolean} intersect true if we are in intersect mode
1168 * @param {YAHOO.util.Region} pre-cached location of the dragged element
1169 * @return {boolean} true if the mouse is over the target
1173 isOverTarget: function(pt, oTarget, intersect, curRegion) {
1174 // use cache if available
1175 var loc = this.locationCache[oTarget.id];
1176 if (!loc || !this.useCache) {
1177 loc = this.getLocation(oTarget);
1178 this.locationCache[oTarget.id] = loc;
1186 oTarget.cursorIsOver = loc.contains( pt );
1188 // DragDrop is using this as a sanity check for the initial mousedown
1189 // in this case we are done. In POINT mode, if the drag obj has no
1190 // contraints, we are done. Otherwise we need to evaluate the
1191 // region the target as occupies to determine if the dragged element
1192 // overlaps with it.
1194 var dc = this.dragCurrent;
1195 if (!dc || (!intersect && !dc.constrainX && !dc.constrainY)) {
1197 //if (oTarget.cursorIsOver) {
1199 return oTarget.cursorIsOver;
1202 oTarget.overlap = null;
1205 // Get the current location of the drag element, this is the
1206 // location of the mouse event less the delta that represents
1207 // where the original mousedown happened on the element. We
1208 // need to consider constraints and ticks as well.
1211 var pos = dc.getTargetCoord(pt.x, pt.y);
1212 var el = dc.getDragEl();
1213 curRegion = new YAHOO.util.Region( pos.y,
1214 pos.x + el.offsetWidth,
1215 pos.y + el.offsetHeight,
1219 var overlap = curRegion.intersect(loc);
1222 oTarget.overlap = overlap;
1223 return (intersect) ? true : oTarget.cursorIsOver;
1230 * unload event handler
1235 _onUnload: function(e, me) {
1240 * Cleans up the drag and drop events and objects.
1245 unregAll: function() {
1247 if (this.dragCurrent) {
1249 this.dragCurrent = null;
1252 this._execOnAll("unreg", []);
1254 //for (var i in this.elementCache) {
1255 //delete this.elementCache[i];
1257 //this.elementCache = {};
1263 * A cache of DOM elements
1264 * @property elementCache
1267 * @deprecated elements are not cached now
1272 * Get the wrapper for the DOM element specified
1273 * @method getElWrapper
1274 * @param {String} id the id of the element to get
1275 * @return {YAHOO.util.DDM.ElementWrapper} the wrapped element
1277 * @deprecated This wrapper isn't that useful
1280 getElWrapper: function(id) {
1281 var oWrapper = this.elementCache[id];
1282 if (!oWrapper || !oWrapper.el) {
1283 oWrapper = this.elementCache[id] =
1284 new this.ElementWrapper(YAHOO.util.Dom.get(id));
1290 * Returns the actual DOM element
1291 * @method getElement
1292 * @param {String} id the id of the elment to get
1293 * @return {Object} The element
1294 * @deprecated use YAHOO.util.Dom.get instead
1297 getElement: function(id) {
1298 return YAHOO.util.Dom.get(id);
1302 * Returns the style property for the DOM element (i.e.,
1303 * document.getElById(id).style)
1305 * @param {String} id the id of the elment to get
1306 * @return {Object} The style property of the element
1307 * @deprecated use YAHOO.util.Dom instead
1310 getCss: function(id) {
1311 var el = YAHOO.util.Dom.get(id);
1312 return (el) ? el.style : null;
1316 * Inner class for cached elements
1317 * @class DragDropMgr.ElementWrapper
1322 ElementWrapper: function(el) {
1327 this.el = el || null;
1332 this.id = this.el && el.id;
1334 * A reference to the style property
1337 this.css = this.el && el.style;
1341 * Returns the X position of an html element
1343 * @param el the element for which to get the position
1344 * @return {int} the X coordinate
1346 * @deprecated use YAHOO.util.Dom.getX instead
1349 getPosX: function(el) {
1350 return YAHOO.util.Dom.getX(el);
1354 * Returns the Y position of an html element
1356 * @param el the element for which to get the position
1357 * @return {int} the Y coordinate
1358 * @deprecated use YAHOO.util.Dom.getY instead
1361 getPosY: function(el) {
1362 return YAHOO.util.Dom.getY(el);
1366 * Swap two nodes. In IE, we use the native method, for others we
1367 * emulate the IE behavior
1369 * @param n1 the first node to swap
1370 * @param n2 the other node to swap
1373 swapNode: function(n1, n2) {
1377 var p = n2.parentNode;
1378 var s = n2.nextSibling;
1381 p.insertBefore(n1, n2);
1382 } else if (n2 == n1.nextSibling) {
1383 p.insertBefore(n2, n1);
1385 n1.parentNode.replaceChild(n2, n1);
1386 p.insertBefore(n1, s);
1392 * Returns the current scroll position
1397 getScroll: function () {
1398 var t, l, dde=document.documentElement, db=document.body;
1399 if (dde && (dde.scrollTop || dde.scrollLeft)) {
1407 return { top: t, left: l };
1411 * Returns the specified element style property
1413 * @param {HTMLElement} el the element
1414 * @param {string} styleProp the style property
1415 * @return {string} The value of the style property
1416 * @deprecated use YAHOO.util.Dom.getStyle
1419 getStyle: function(el, styleProp) {
1420 return YAHOO.util.Dom.getStyle(el, styleProp);
1424 * Gets the scrollTop
1425 * @method getScrollTop
1426 * @return {int} the document's scrollTop
1429 getScrollTop: function () { return this.getScroll().top; },
1432 * Gets the scrollLeft
1433 * @method getScrollLeft
1434 * @return {int} the document's scrollTop
1437 getScrollLeft: function () { return this.getScroll().left; },
1440 * Sets the x/y position of an element to the location of the
1443 * @param {HTMLElement} moveEl The element to move
1444 * @param {HTMLElement} targetEl The position reference element
1447 moveToEl: function (moveEl, targetEl) {
1448 var aCoord = YAHOO.util.Dom.getXY(targetEl);
1449 YAHOO.util.Dom.setXY(moveEl, aCoord);
1453 * Gets the client height
1454 * @method getClientHeight
1455 * @return {int} client height in px
1456 * @deprecated use YAHOO.util.Dom.getViewportHeight instead
1459 getClientHeight: function() {
1460 return YAHOO.util.Dom.getViewportHeight();
1464 * Gets the client width
1465 * @method getClientWidth
1466 * @return {int} client width in px
1467 * @deprecated use YAHOO.util.Dom.getViewportWidth instead
1470 getClientWidth: function() {
1471 return YAHOO.util.Dom.getViewportWidth();
1475 * Numeric array sort function
1476 * @method numericSort
1479 numericSort: function(a, b) { return (a - b); },
1483 * @property _timeoutCount
1490 * Trying to make the load order less important. Without this we get
1491 * an error if this file is loaded before the Event Utility.
1492 * @method _addListeners
1496 _addListeners: function() {
1497 var DDM = YAHOO.util.DDM;
1498 if ( YAHOO.util.Event && document ) {
1501 if (DDM._timeoutCount > 2000) {
1503 setTimeout(DDM._addListeners, 10);
1504 if (document && document.body) {
1505 DDM._timeoutCount += 1;
1512 * Recursively searches the immediate parent and all child nodes for
1513 * the handle element in order to determine wheter or not it was
1515 * @method handleWasClicked
1516 * @param node the html element to inspect
1519 handleWasClicked: function(node, id) {
1520 if (this.isHandle(id, node.id)) {
1523 // check to see if this is a text node child of the one we want
1524 var p = node.parentNode;
1527 if (this.isHandle(id, p.id)) {
1542 // shorter alias, save a few bytes
1543 YAHOO.util.DDM = YAHOO.util.DragDropMgr;
1544 YAHOO.util.DDM._addListeners();
1550 var Event=YAHOO.util.Event;
1551 var Dom=YAHOO.util.Dom;
1554 * Defines the interface and base operation of items that that can be
1555 * dragged or can be drop targets. It was designed to be extended, overriding
1556 * the event handlers for startDrag, onDrag, onDragOver, onDragOut.
1557 * Up to three html elements can be associated with a DragDrop instance:
1559 * <li>linked element: the element that is passed into the constructor.
1560 * This is the element which defines the boundaries for interaction with
1561 * other DragDrop objects.</li>
1562 * <li>handle element(s): The drag operation only occurs if the element that
1563 * was clicked matches a handle element. By default this is the linked
1564 * element, but there are times that you will want only a portion of the
1565 * linked element to initiate the drag operation, and the setHandleElId()
1566 * method provides a way to define this.</li>
1567 * <li>drag element: this represents an the element that would be moved along
1568 * with the cursor during a drag operation. By default, this is the linked
1569 * element itself as in {@link YAHOO.util.DD}. setDragElId() lets you define
1570 * a separate element that would be moved, as in {@link YAHOO.util.DDProxy}
1573 * This class should not be instantiated until the onload event to ensure that
1574 * the associated elements are available.
1575 * The following would define a DragDrop obj that would interact with any
1576 * other DragDrop obj in the "group1" group:
1578 * dd = new YAHOO.util.DragDrop("div1", "group1");
1580 * Since none of the event handlers have been implemented, nothing would
1581 * actually happen if you were to run the code above. Normally you would
1582 * override this class or one of the default implementations, but you can
1583 * also override the methods you want on an instance of the class...
1585 * dd.onDragDrop = function(e, id) {
1586 * alert("dd was dropped on " + id);
1589 * @namespace YAHOO.util
1592 * @param {String} id of the element that is linked to this instance
1593 * @param {String} sGroup the group of related DragDrop objects
1594 * @param {object} config an object containing configurable attributes
1595 * Valid properties for DragDrop:
1596 * padding, isTarget, maintainOffset, primaryButtonOnly,
1598 YAHOO.util.DragDrop = function(id, sGroup, config) {
1600 this.init(id, sGroup, config);
1604 YAHOO.util.DragDrop.prototype = {
1606 * An Object Literal containing the events that we will be using: mouseDown, b4MouseDown, mouseUp, b4StartDrag, startDrag, b4EndDrag, endDrag, mouseUp, drag, b4Drag, invalidDrop, b4DragOut, dragOut, dragEnter, b4DragOver, dragOver, b4DragDrop, dragDrop
1607 * By setting any of these to false, then event will not be fired.
1614 * @description Shortcut for EventProvider.subscribe, see <a href="YAHOO.util.EventProvider.html#subscribe">YAHOO.util.EventProvider.subscribe</a>
1617 this.subscribe.apply(this, arguments);
1620 * The id of the element associated with this object. This is what we
1621 * refer to as the "linked element" because the size and position of
1622 * this element is used to determine when the drag and drop objects have
1630 * Configuration attributes passed into the constructor
1637 * The id of the element that will be dragged. By default this is same
1638 * as the linked element , but could be changed to another element. Ex:
1639 * YAHOO.util.DDProxy
1640 * @property dragElId
1647 * the id of the element that initiates the drag operation. By default
1648 * this is the linked element, but could be changed to be a child of this
1649 * element. This lets us do things like only starting the drag when the
1650 * header element within the linked html element is clicked.
1651 * @property handleElId
1658 * An associative array of HTML tags that will be ignored if clicked.
1659 * @property invalidHandleTypes
1660 * @type {string: string}
1662 invalidHandleTypes: null,
1665 * An associative array of ids for elements that will be ignored if clicked
1666 * @property invalidHandleIds
1667 * @type {string: string}
1669 invalidHandleIds: null,
1672 * An indexted array of css class names for elements that will be ignored
1674 * @property invalidHandleClasses
1677 invalidHandleClasses: null,
1680 * The linked element's absolute X position at the time the drag was
1682 * @property startPageX
1689 * The linked element's absolute X position at the time the drag was
1691 * @property startPageY
1698 * The group defines a logical collection of DragDrop objects that are
1699 * related. Instances only get events when interacting with other
1700 * DragDrop object in the same group. This lets us define multiple
1701 * groups using a single DragDrop subclass if we want.
1703 * @type {string: string}
1708 * Individual drag/drop instances can be locked. This will prevent
1709 * onmousedown start drag.
1717 * Lock this instance
1720 lock: function() { this.locked = true; },
1723 * Unlock this instace
1726 unlock: function() { this.locked = false; },
1729 * By default, all instances can be a drop target. This can be disabled by
1730 * setting isTarget to false.
1731 * @property isTarget
1737 * The padding configured for this drag and drop object for calculating
1738 * the drop zone intersection with this object.
1744 * If this flag is true, do not fire drop events. The element is a drag only element (for movement not dropping)
1745 * @property dragOnly
1751 * If this flag is true, a shim will be placed over the screen/viewable area to track mouse events. Should help with dragging elements over iframes and other controls.
1758 * Cached reference to the linked element
1765 * Internal typeof flag
1766 * @property __ygDragDrop
1772 * Set to true when horizontal contraints are applied
1773 * @property constrainX
1780 * Set to true when vertical contraints are applied
1781 * @property constrainY
1788 * The left constraint
1796 * The right constraint
1813 * The down constraint
1821 * The difference between the click position and the source element's location
1829 * The difference between the click position and the source element's location
1837 * Maintain offsets when we resetconstraints. Set to true when you want
1838 * the position of the element relative to its parent to stay the same
1839 * when the page changes
1841 * @property maintainOffset
1844 maintainOffset: false,
1847 * Array of pixel locations the element will snap to if we specified a
1848 * horizontal graduation/interval. This array is generated automatically
1849 * when you define a tick interval.
1856 * Array of pixel locations the element will snap to if we specified a
1857 * vertical graduation/interval. This array is generated automatically
1858 * when you define a tick interval.
1865 * By default the drag and drop instance will only respond to the primary
1866 * button click (left button for a right-handed mouse). Set to true to
1867 * allow drag and drop to start with any mouse click that is propogated
1869 * @property primaryButtonOnly
1872 primaryButtonOnly: true,
1875 * The availabe property is false until the linked dom element is accessible.
1876 * @property available
1882 * By default, drags can only be initiated if the mousedown occurs in the
1883 * region the linked element is. This is done in part to work around a
1884 * bug in some browsers that mis-report the mousedown if the previous
1885 * mouseup happened outside of the window. This property is set to true
1886 * if outer handles are defined.
1888 * @property hasOuterHandles
1892 hasOuterHandles: false,
1895 * Property that is assigned to a drag and drop object when testing to
1896 * see if it is being targeted by another dd object. This property
1897 * can be used in intersect mode to help determine the focus of
1898 * the mouse interaction. DDM.getBestMatch uses this property first to
1899 * determine the closest match in INTERSECT mode when multiple targets
1900 * are part of the same interaction.
1901 * @property cursorIsOver
1904 cursorIsOver: false,
1907 * Property that is assigned to a drag and drop object when testing to
1908 * see if it is being targeted by another dd object. This is a region
1909 * that represents the area the draggable element overlaps this target.
1910 * DDM.getBestMatch uses this property to compare the size of the overlap
1911 * to that of other targets in order to determine the closest match in
1912 * INTERSECT mode when multiple targets are part of the same interaction.
1914 * @type YAHOO.util.Region
1919 * Code that executes immediately before the startDrag event
1920 * @method b4StartDrag
1923 b4StartDrag: function(x, y) { },
1926 * Abstract method called after a drag/drop object is clicked
1927 * and the drag or mousedown time thresholds have beeen met.
1929 * @param {int} X click location
1930 * @param {int} Y click location
1932 startDrag: function(x, y) { /* override this */ },
1935 * Code that executes immediately before the onDrag event
1939 b4Drag: function(e) { },
1942 * Abstract method called during the onMouseMove event while dragging an
1945 * @param {Event} e the mousemove event
1947 onDrag: function(e) { /* override this */ },
1950 * Abstract method called when this element fist begins hovering over
1951 * another DragDrop obj
1952 * @method onDragEnter
1953 * @param {Event} e the mousemove event
1954 * @param {String|DragDrop[]} id In POINT mode, the element
1955 * id this is hovering over. In INTERSECT mode, an array of one or more
1956 * dragdrop items being hovered over.
1958 onDragEnter: function(e, id) { /* override this */ },
1961 * Code that executes immediately before the onDragOver event
1962 * @method b4DragOver
1965 b4DragOver: function(e) { },
1968 * Abstract method called when this element is hovering over another
1970 * @method onDragOver
1971 * @param {Event} e the mousemove event
1972 * @param {String|DragDrop[]} id In POINT mode, the element
1973 * id this is hovering over. In INTERSECT mode, an array of dd items
1974 * being hovered over.
1976 onDragOver: function(e, id) { /* override this */ },
1979 * Code that executes immediately before the onDragOut event
1983 b4DragOut: function(e) { },
1986 * Abstract method called when we are no longer hovering over an element
1988 * @param {Event} e the mousemove event
1989 * @param {String|DragDrop[]} id In POINT mode, the element
1990 * id this was hovering over. In INTERSECT mode, an array of dd items
1991 * that the mouse is no longer over.
1993 onDragOut: function(e, id) { /* override this */ },
1996 * Code that executes immediately before the onDragDrop event
1997 * @method b4DragDrop
2000 b4DragDrop: function(e) { },
2003 * Abstract method called when this item is dropped on another DragDrop
2005 * @method onDragDrop
2006 * @param {Event} e the mouseup event
2007 * @param {String|DragDrop[]} id In POINT mode, the element
2008 * id this was dropped on. In INTERSECT mode, an array of dd items this
2011 onDragDrop: function(e, id) { /* override this */ },
2014 * Abstract method called when this item is dropped on an area with no
2016 * @method onInvalidDrop
2017 * @param {Event} e the mouseup event
2019 onInvalidDrop: function(e) { /* override this */ },
2022 * Code that executes immediately before the endDrag event
2026 b4EndDrag: function(e) { },
2029 * Fired when we are done dragging the object
2031 * @param {Event} e the mouseup event
2033 endDrag: function(e) { /* override this */ },
2036 * Code executed immediately before the onMouseDown event
2037 * @method b4MouseDown
2038 * @param {Event} e the mousedown event
2041 b4MouseDown: function(e) { },
2044 * Event handler that fires when a drag/drop obj gets a mousedown
2045 * @method onMouseDown
2046 * @param {Event} e the mousedown event
2048 onMouseDown: function(e) { /* override this */ },
2051 * Event handler that fires when a drag/drop obj gets a mouseup
2053 * @param {Event} e the mouseup event
2055 onMouseUp: function(e) { /* override this */ },
2058 * Override the onAvailable method to do what is needed after the initial
2059 * position was determined.
2060 * @method onAvailable
2062 onAvailable: function () {
2066 * Returns a reference to the linked element
2068 * @return {HTMLElement} the html element
2071 if (!this._domRef) {
2072 this._domRef = Dom.get(this.id);
2075 return this._domRef;
2079 * Returns a reference to the actual element to drag. By default this is
2080 * the same as the html element, but it can be assigned to another
2081 * element. An example of this can be found in YAHOO.util.DDProxy
2083 * @return {HTMLElement} the html element
2085 getDragEl: function() {
2086 return Dom.get(this.dragElId);
2090 * Sets up the DragDrop object. Must be called in the constructor of any
2091 * YAHOO.util.DragDrop subclass
2093 * @param id the id of the linked element
2094 * @param {String} sGroup the group of related items
2095 * @param {object} config configuration attributes
2097 init: function(id, sGroup, config) {
2098 this.initTarget(id, sGroup, config);
2099 Event.on(this._domRef || this.id, "mousedown",
2100 this.handleMouseDown, this, true);
2102 // Event.on(this.id, "selectstart", Event.preventDefault);
2103 for (var i in this.events) {
2104 this.createEvent(i + 'Event');
2110 * Initializes Targeting functionality only... the object does not
2111 * get a mousedown handler.
2112 * @method initTarget
2113 * @param id the id of the linked element
2114 * @param {String} sGroup the group of related items
2115 * @param {object} config configuration attributes
2117 initTarget: function(id, sGroup, config) {
2119 // configuration attributes
2120 this.config = config || {};
2124 // create a local reference to the drag and drop manager
2125 this.DDM = YAHOO.util.DDM;
2127 // initialize the groups object
2130 // assume that we have an element reference instead of an id if the
2131 // parameter is not a string
2132 if (typeof id !== "string") {
2134 id = Dom.generateId(id);
2140 // add to an interaction group
2141 this.addToGroup((sGroup) ? sGroup : "default");
2143 // We don't want to register this as the handle with the manager
2144 // so we just set the id rather than calling the setter.
2145 this.handleElId = id;
2147 Event.onAvailable(id, this.handleOnAvailable, this, true);
2150 // the linked element is the element that gets dragged by default
2151 this.setDragElId(id);
2153 // by default, clicked anchors will not start drag operations.
2154 // @TODO what else should be here? Probably form fields.
2155 this.invalidHandleTypes = { A: "A" };
2156 this.invalidHandleIds = {};
2157 this.invalidHandleClasses = [];
2163 * Applies the configuration parameters that were passed into the constructor.
2164 * This is supposed to happen at each level through the inheritance chain. So
2165 * a DDProxy implentation will execute apply config on DDProxy, DD, and
2166 * DragDrop in order to get all of the parameters that are available in
2168 * @method applyConfig
2170 applyConfig: function() {
2191 if (this.config.events) {
2192 for (var i in this.config.events) {
2193 if (this.config.events[i] === false) {
2194 this.events[i] = false;
2200 // configurable properties:
2201 // padding, isTarget, maintainOffset, primaryButtonOnly
2202 this.padding = this.config.padding || [0, 0, 0, 0];
2203 this.isTarget = (this.config.isTarget !== false);
2204 this.maintainOffset = (this.config.maintainOffset);
2205 this.primaryButtonOnly = (this.config.primaryButtonOnly !== false);
2206 this.dragOnly = ((this.config.dragOnly === true) ? true : false);
2207 this.useShim = ((this.config.useShim === true) ? true : false);
2211 * Executed when the linked element is available
2212 * @method handleOnAvailable
2215 handleOnAvailable: function() {
2216 this.available = true;
2217 this.resetConstraints();
2222 * Configures the padding for the target zone in px. Effectively expands
2223 * (or reduces) the virtual object size for targeting calculations.
2224 * Supports css-style shorthand; if only one parameter is passed, all sides
2225 * will have that padding, and if only two are passed, the top and bottom
2226 * will have the first param, the left and right the second.
2227 * @method setPadding
2228 * @param {int} iTop Top pad
2229 * @param {int} iRight Right pad
2230 * @param {int} iBot Bot pad
2231 * @param {int} iLeft Left pad
2233 setPadding: function(iTop, iRight, iBot, iLeft) {
2234 // this.padding = [iLeft, iRight, iTop, iBot];
2235 if (!iRight && 0 !== iRight) {
2236 this.padding = [iTop, iTop, iTop, iTop];
2237 } else if (!iBot && 0 !== iBot) {
2238 this.padding = [iTop, iRight, iTop, iRight];
2240 this.padding = [iTop, iRight, iBot, iLeft];
2245 * Stores the initial placement of the linked element.
2246 * @method setInitialPosition
2247 * @param {int} diffX the X offset, default 0
2248 * @param {int} diffY the Y offset, default 0
2251 setInitPosition: function(diffX, diffY) {
2252 var el = this.getEl();
2254 if (!this.DDM.verifyEl(el)) {
2255 if (el && el.style && (el.style.display == 'none')) {
2261 var dx = diffX || 0;
2262 var dy = diffY || 0;
2264 var p = Dom.getXY( el );
2266 this.initPageX = p[0] - dx;
2267 this.initPageY = p[1] - dy;
2269 this.lastPageX = p[0];
2270 this.lastPageY = p[1];
2274 this.setStartPosition(p);
2278 * Sets the start position of the element. This is set when the obj
2279 * is initialized, the reset when a drag is started.
2280 * @method setStartPosition
2281 * @param pos current position (from previous lookup)
2284 setStartPosition: function(pos) {
2285 var p = pos || Dom.getXY(this.getEl());
2287 this.deltaSetXY = null;
2289 this.startPageX = p[0];
2290 this.startPageY = p[1];
2294 * Add this instance to a group of related drag/drop objects. All
2295 * instances belong to at least one group, and can belong to as many
2297 * @method addToGroup
2298 * @param sGroup {string} the name of the group
2300 addToGroup: function(sGroup) {
2301 this.groups[sGroup] = true;
2302 this.DDM.regDragDrop(this, sGroup);
2306 * Remove's this instance from the supplied interaction group
2307 * @method removeFromGroup
2308 * @param {string} sGroup The group to drop
2310 removeFromGroup: function(sGroup) {
2311 if (this.groups[sGroup]) {
2312 delete this.groups[sGroup];
2315 this.DDM.removeDDFromGroup(this, sGroup);
2319 * Allows you to specify that an element other than the linked element
2320 * will be moved with the cursor during a drag
2321 * @method setDragElId
2322 * @param id {string} the id of the element that will be used to initiate the drag
2324 setDragElId: function(id) {
2329 * Allows you to specify a child of the linked element that should be
2330 * used to initiate the drag operation. An example of this would be if
2331 * you have a content div with text and links. Clicking anywhere in the
2332 * content area would normally start the drag operation. Use this method
2333 * to specify that an element inside of the content div is the element
2334 * that starts the drag operation.
2335 * @method setHandleElId
2336 * @param id {string} the id of the element that will be used to
2337 * initiate the drag.
2339 setHandleElId: function(id) {
2340 if (typeof id !== "string") {
2341 id = Dom.generateId(id);
2343 this.handleElId = id;
2344 this.DDM.regHandle(this.id, id);
2348 * Allows you to set an element outside of the linked element as a drag
2350 * @method setOuterHandleElId
2351 * @param id the id of the element that will be used to initiate the drag
2353 setOuterHandleElId: function(id) {
2354 if (typeof id !== "string") {
2355 id = Dom.generateId(id);
2357 Event.on(id, "mousedown",
2358 this.handleMouseDown, this, true);
2359 this.setHandleElId(id);
2361 this.hasOuterHandles = true;
2365 * Remove all drag and drop hooks for this element
2369 Event.removeListener(this.id, "mousedown",
2370 this.handleMouseDown);
2371 this._domRef = null;
2372 this.DDM._remove(this);
2376 * Returns true if this instance is locked, or the drag drop mgr is locked
2377 * (meaning that all drag/drop is disabled on the page.)
2379 * @return {boolean} true if this obj or all drag/drop is locked, else
2382 isLocked: function() {
2383 return (this.DDM.isLocked() || this.locked);
2387 * Fired when this object is clicked
2388 * @method handleMouseDown
2390 * @param {YAHOO.util.DragDrop} oDD the clicked dd object (this dd obj)
2393 handleMouseDown: function(e, oDD) {
2395 var button = e.which || e.button;
2397 if (this.primaryButtonOnly && button > 1) {
2401 if (this.isLocked()) {
2407 // firing the mousedown events prior to calculating positions
2408 var b4Return = this.b4MouseDown(e),
2411 if (this.events.b4MouseDown) {
2412 b4Return2 = this.fireEvent('b4MouseDownEvent', e);
2414 var mDownReturn = this.onMouseDown(e),
2415 mDownReturn2 = true;
2416 if (this.events.mouseDown) {
2417 mDownReturn2 = this.fireEvent('mouseDownEvent', e);
2420 if ((b4Return === false) || (mDownReturn === false) || (b4Return2 === false) || (mDownReturn2 === false)) {
2424 this.DDM.refreshCache(this.groups);
2426 // setTimeout( function() { self.DDM.refreshCache(self.groups); }, 0);
2428 // Only process the event if we really clicked within the linked
2429 // element. The reason we make this check is that in the case that
2430 // another element was moved between the clicked element and the
2431 // cursor in the time between the mousedown and mouseup events. When
2432 // this happens, the element gets the next mousedown event
2433 // regardless of where on the screen it happened.
2434 var pt = new YAHOO.util.Point(Event.getPageX(e), Event.getPageY(e));
2435 if (!this.hasOuterHandles && !this.DDM.isOverTarget(pt, this) ) {
2437 if (this.clickValidator(e)) {
2440 // set the initial element position
2441 this.setStartPosition();
2443 // start tracking mousemove distance and mousedown time to
2444 // determine when to start the actual drag
2445 this.DDM.handleMouseDown(e, this);
2447 // this mousedown is mine
2448 this.DDM.stopEvent(e);
2457 * @method clickValidator
2458 * @description Method validates that the clicked element
2459 * was indeed the handle or a valid child of the handle
2462 clickValidator: function(e) {
2463 var target = YAHOO.util.Event.getTarget(e);
2464 return ( this.isValidHandleChild(target) &&
2465 (this.id == this.handleElId ||
2466 this.DDM.handleWasClicked(target, this.id)) );
2470 * Finds the location the element should be placed if we want to move
2471 * it to where the mouse location less the click offset would place us.
2472 * @method getTargetCoord
2473 * @param {int} iPageX the X coordinate of the click
2474 * @param {int} iPageY the Y coordinate of the click
2475 * @return an object that contains the coordinates (Object.x and Object.y)
2478 getTargetCoord: function(iPageX, iPageY) {
2481 var x = iPageX - this.deltaX;
2482 var y = iPageY - this.deltaY;
2484 if (this.constrainX) {
2485 if (x < this.minX) { x = this.minX; }
2486 if (x > this.maxX) { x = this.maxX; }
2489 if (this.constrainY) {
2490 if (y < this.minY) { y = this.minY; }
2491 if (y > this.maxY) { y = this.maxY; }
2494 x = this.getTick(x, this.xTicks);
2495 y = this.getTick(y, this.yTicks);
2502 * Allows you to specify a tag name that should not start a drag operation
2503 * when clicked. This is designed to facilitate embedding links within a
2504 * drag handle that do something other than start the drag.
2505 * @method addInvalidHandleType
2506 * @param {string} tagName the type of element to exclude
2508 addInvalidHandleType: function(tagName) {
2509 var type = tagName.toUpperCase();
2510 this.invalidHandleTypes[type] = type;
2514 * Lets you to specify an element id for a child of a drag handle
2515 * that should not initiate a drag
2516 * @method addInvalidHandleId
2517 * @param {string} id the element id of the element you wish to ignore
2519 addInvalidHandleId: function(id) {
2520 if (typeof id !== "string") {
2521 id = Dom.generateId(id);
2523 this.invalidHandleIds[id] = id;
2528 * Lets you specify a css class of elements that will not initiate a drag
2529 * @method addInvalidHandleClass
2530 * @param {string} cssClass the class of the elements you wish to ignore
2532 addInvalidHandleClass: function(cssClass) {
2533 this.invalidHandleClasses.push(cssClass);
2537 * Unsets an excluded tag name set by addInvalidHandleType
2538 * @method removeInvalidHandleType
2539 * @param {string} tagName the type of element to unexclude
2541 removeInvalidHandleType: function(tagName) {
2542 var type = tagName.toUpperCase();
2543 // this.invalidHandleTypes[type] = null;
2544 delete this.invalidHandleTypes[type];
2548 * Unsets an invalid handle id
2549 * @method removeInvalidHandleId
2550 * @param {string} id the id of the element to re-enable
2552 removeInvalidHandleId: function(id) {
2553 if (typeof id !== "string") {
2554 id = Dom.generateId(id);
2556 delete this.invalidHandleIds[id];
2560 * Unsets an invalid css class
2561 * @method removeInvalidHandleClass
2562 * @param {string} cssClass the class of the element(s) you wish to
2565 removeInvalidHandleClass: function(cssClass) {
2566 for (var i=0, len=this.invalidHandleClasses.length; i<len; ++i) {
2567 if (this.invalidHandleClasses[i] == cssClass) {
2568 delete this.invalidHandleClasses[i];
2574 * Checks the tag exclusion list to see if this click should be ignored
2575 * @method isValidHandleChild
2576 * @param {HTMLElement} node the HTMLElement to evaluate
2577 * @return {boolean} true if this is a valid tag type, false if not
2579 isValidHandleChild: function(node) {
2582 // var n = (node.nodeName == "#text") ? node.parentNode : node;
2585 nodeName = node.nodeName.toUpperCase();
2587 nodeName = node.nodeName;
2589 valid = valid && !this.invalidHandleTypes[nodeName];
2590 valid = valid && !this.invalidHandleIds[node.id];
2592 for (var i=0, len=this.invalidHandleClasses.length; valid && i<len; ++i) {
2593 valid = !Dom.hasClass(node, this.invalidHandleClasses[i]);
2602 * Create the array of horizontal tick marks if an interval was specified
2603 * in setXConstraint().
2607 setXTicks: function(iStartX, iTickSize) {
2609 this.xTickSize = iTickSize;
2613 for (var i = this.initPageX; i >= this.minX; i = i - iTickSize) {
2615 this.xTicks[this.xTicks.length] = i;
2620 for (i = this.initPageX; i <= this.maxX; i = i + iTickSize) {
2622 this.xTicks[this.xTicks.length] = i;
2627 this.xTicks.sort(this.DDM.numericSort) ;
2631 * Create the array of vertical tick marks if an interval was specified in
2636 setYTicks: function(iStartY, iTickSize) {
2638 this.yTickSize = iTickSize;
2642 for (var i = this.initPageY; i >= this.minY; i = i - iTickSize) {
2644 this.yTicks[this.yTicks.length] = i;
2649 for (i = this.initPageY; i <= this.maxY; i = i + iTickSize) {
2651 this.yTicks[this.yTicks.length] = i;
2656 this.yTicks.sort(this.DDM.numericSort) ;
2660 * By default, the element can be dragged any place on the screen. Use
2661 * this method to limit the horizontal travel of the element. Pass in
2662 * 0,0 for the parameters if you want to lock the drag to the y axis.
2663 * @method setXConstraint
2664 * @param {int} iLeft the number of pixels the element can move to the left
2665 * @param {int} iRight the number of pixels the element can move to the
2667 * @param {int} iTickSize optional parameter for specifying that the
2669 * should move iTickSize pixels at a time.
2671 setXConstraint: function(iLeft, iRight, iTickSize) {
2672 this.leftConstraint = parseInt(iLeft, 10);
2673 this.rightConstraint = parseInt(iRight, 10);
2675 this.minX = this.initPageX - this.leftConstraint;
2676 this.maxX = this.initPageX + this.rightConstraint;
2677 if (iTickSize) { this.setXTicks(this.initPageX, iTickSize); }
2679 this.constrainX = true;
2683 * Clears any constraints applied to this instance. Also clears ticks
2684 * since they can't exist independent of a constraint at this time.
2685 * @method clearConstraints
2687 clearConstraints: function() {
2688 this.constrainX = false;
2689 this.constrainY = false;
2694 * Clears any tick interval defined for this instance
2695 * @method clearTicks
2697 clearTicks: function() {
2705 * By default, the element can be dragged any place on the screen. Set
2706 * this to limit the vertical travel of the element. Pass in 0,0 for the
2707 * parameters if you want to lock the drag to the x axis.
2708 * @method setYConstraint
2709 * @param {int} iUp the number of pixels the element can move up
2710 * @param {int} iDown the number of pixels the element can move down
2711 * @param {int} iTickSize optional parameter for specifying that the
2712 * element should move iTickSize pixels at a time.
2714 setYConstraint: function(iUp, iDown, iTickSize) {
2715 this.topConstraint = parseInt(iUp, 10);
2716 this.bottomConstraint = parseInt(iDown, 10);
2718 this.minY = this.initPageY - this.topConstraint;
2719 this.maxY = this.initPageY + this.bottomConstraint;
2720 if (iTickSize) { this.setYTicks(this.initPageY, iTickSize); }
2722 this.constrainY = true;
2727 * resetConstraints must be called if you manually reposition a dd element.
2728 * @method resetConstraints
2730 resetConstraints: function() {
2733 // Maintain offsets if necessary
2734 if (this.initPageX || this.initPageX === 0) {
2735 // figure out how much this thing has moved
2736 var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;
2737 var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;
2739 this.setInitPosition(dx, dy);
2741 // This is the first time we have detected the element's position
2743 this.setInitPosition();
2746 if (this.constrainX) {
2747 this.setXConstraint( this.leftConstraint,
2748 this.rightConstraint,
2752 if (this.constrainY) {
2753 this.setYConstraint( this.topConstraint,
2754 this.bottomConstraint,
2760 * Normally the drag element is moved pixel by pixel, but we can specify
2761 * that it move a number of pixels at a time. This method resolves the
2762 * location when we have it set up like this.
2764 * @param {int} val where we want to place the object
2765 * @param {int[]} tickArray sorted array of valid points
2766 * @return {int} the closest tick
2769 getTick: function(val, tickArray) {
2772 // If tick interval is not defined, it is effectively 1 pixel,
2773 // so we return the value passed to us.
2775 } else if (tickArray[0] >= val) {
2776 // The value is lower than the first tick, so we return the first
2778 return tickArray[0];
2780 for (var i=0, len=tickArray.length; i<len; ++i) {
2782 if (tickArray[next] && tickArray[next] >= val) {
2783 var diff1 = val - tickArray[i];
2784 var diff2 = tickArray[next] - val;
2785 return (diff2 > diff1) ? tickArray[i] : tickArray[next];
2789 // The value is larger than the last tick, so we return the last
2791 return tickArray[tickArray.length - 1];
2798 * @return {string} string representation of the dd obj
2800 toString: function() {
2801 return ("DragDrop " + this.id);
2805 YAHOO.augment(YAHOO.util.DragDrop, YAHOO.util.EventProvider);
2808 * @event mouseDownEvent
2809 * @description Provides access to the mousedown event. The mousedown does not always result in a drag operation.
2810 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2814 * @event b4MouseDownEvent
2815 * @description Provides access to the mousedown event, before the mouseDownEvent gets fired. Returning false will cancel the drag.
2816 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2820 * @event mouseUpEvent
2821 * @description Fired from inside DragDropMgr when the drag operation is finished.
2822 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2826 * @event b4StartDragEvent
2827 * @description Fires before the startDragEvent, returning false will cancel the startDrag Event.
2828 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2832 * @event startDragEvent
2833 * @description Occurs after a mouse down and the drag threshold has been met. The drag threshold default is either 3 pixels of mouse movement or 1 full second of holding the mousedown.
2834 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2838 * @event b4EndDragEvent
2839 * @description Fires before the endDragEvent. Returning false will cancel.
2840 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2844 * @event endDragEvent
2845 * @description Fires on the mouseup event after a drag has been initiated (startDrag fired).
2846 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2851 * @description Occurs every mousemove event while dragging.
2852 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2855 * @event b4DragEvent
2856 * @description Fires before the dragEvent.
2857 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2860 * @event invalidDropEvent
2861 * @description Fires when the dragged objects is dropped in a location that contains no drop targets.
2862 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2865 * @event b4DragOutEvent
2866 * @description Fires before the dragOutEvent
2867 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2870 * @event dragOutEvent
2871 * @description Fires when a dragged object is no longer over an object that had the onDragEnter fire.
2872 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2875 * @event dragEnterEvent
2876 * @description Occurs when the dragged object first interacts with another targettable drag and drop object.
2877 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2880 * @event b4DragOverEvent
2881 * @description Fires before the dragOverEvent.
2882 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2885 * @event dragOverEvent
2886 * @description Fires every mousemove event while over a drag and drop object.
2887 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2890 * @event b4DragDropEvent
2891 * @description Fires before the dragDropEvent
2892 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2895 * @event dragDropEvent
2896 * @description Fires when the dragged objects is dropped on another.
2897 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
2901 * A DragDrop implementation where the linked element follows the
2902 * mouse cursor during a drag.
2904 * @extends YAHOO.util.DragDrop
2906 * @param {String} id the id of the linked element
2907 * @param {String} sGroup the group of related DragDrop items
2908 * @param {object} config an object containing configurable attributes
2909 * Valid properties for DD:
2912 YAHOO.util.DD = function(id, sGroup, config) {
2914 this.init(id, sGroup, config);
2918 YAHOO.extend(YAHOO.util.DD, YAHOO.util.DragDrop, {
2921 * When set to true, the utility automatically tries to scroll the browser
2922 * window when a drag and drop element is dragged near the viewport boundary.
2930 * Sets the pointer offset to the distance between the linked element's top
2931 * left corner and the location the element was clicked
2932 * @method autoOffset
2933 * @param {int} iPageX the X coordinate of the click
2934 * @param {int} iPageY the Y coordinate of the click
2936 autoOffset: function(iPageX, iPageY) {
2937 var x = iPageX - this.startPageX;
2938 var y = iPageY - this.startPageY;
2939 this.setDelta(x, y);
2943 * Sets the pointer offset. You can call this directly to force the
2944 * offset to be in a particular location (e.g., pass in 0,0 to set it
2945 * to the center of the object, as done in YAHOO.widget.Slider)
2947 * @param {int} iDeltaX the distance from the left
2948 * @param {int} iDeltaY the distance from the top
2950 setDelta: function(iDeltaX, iDeltaY) {
2951 this.deltaX = iDeltaX;
2952 this.deltaY = iDeltaY;
2956 * Sets the drag element to the location of the mousedown or click event,
2957 * maintaining the cursor location relative to the location on the element
2958 * that was clicked. Override this if you want to place the element in a
2959 * location other than where the cursor is.
2960 * @method setDragElPos
2961 * @param {int} iPageX the X coordinate of the mousedown or drag event
2962 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2964 setDragElPos: function(iPageX, iPageY) {
2965 // the first time we do this, we are going to check to make sure
2966 // the element has css positioning
2968 var el = this.getDragEl();
2969 this.alignElWithMouse(el, iPageX, iPageY);
2973 * Sets the element to the location of the mousedown or click event,
2974 * maintaining the cursor location relative to the location on the element
2975 * that was clicked. Override this if you want to place the element in a
2976 * location other than where the cursor is.
2977 * @method alignElWithMouse
2978 * @param {HTMLElement} el the element to move
2979 * @param {int} iPageX the X coordinate of the mousedown or drag event
2980 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2982 alignElWithMouse: function(el, iPageX, iPageY) {
2983 var oCoord = this.getTargetCoord(iPageX, iPageY);
2985 if (!this.deltaSetXY) {
2986 var aCoord = [oCoord.x, oCoord.y];
2987 YAHOO.util.Dom.setXY(el, aCoord);
2989 var newLeft = parseInt( YAHOO.util.Dom.getStyle(el, "left"), 10 );
2990 var newTop = parseInt( YAHOO.util.Dom.getStyle(el, "top" ), 10 );
2992 this.deltaSetXY = [ newLeft - oCoord.x, newTop - oCoord.y ];
2994 YAHOO.util.Dom.setStyle(el, "left", (oCoord.x + this.deltaSetXY[0]) + "px");
2995 YAHOO.util.Dom.setStyle(el, "top", (oCoord.y + this.deltaSetXY[1]) + "px");
2998 this.cachePosition(oCoord.x, oCoord.y);
3000 setTimeout(function() {
3001 self.autoScroll.call(self, oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
3006 * Saves the most recent position so that we can reset the constraints and
3007 * tick marks on-demand. We need to know this so that we can calculate the
3008 * number of pixels the element is offset from its original position.
3009 * @method cachePosition
3010 * @param iPageX the current x position (optional, this just makes it so we
3011 * don't have to look it up again)
3012 * @param iPageY the current y position (optional, this just makes it so we
3013 * don't have to look it up again)
3015 cachePosition: function(iPageX, iPageY) {
3017 this.lastPageX = iPageX;
3018 this.lastPageY = iPageY;
3020 var aCoord = YAHOO.util.Dom.getXY(this.getEl());
3021 this.lastPageX = aCoord[0];
3022 this.lastPageY = aCoord[1];
3027 * Auto-scroll the window if the dragged object has been moved beyond the
3028 * visible window boundary.
3029 * @method autoScroll
3030 * @param {int} x the drag element's x position
3031 * @param {int} y the drag element's y position
3032 * @param {int} h the height of the drag element
3033 * @param {int} w the width of the drag element
3036 autoScroll: function(x, y, h, w) {
3039 // The client height
3040 var clientH = this.DDM.getClientHeight();
3043 var clientW = this.DDM.getClientWidth();
3045 // The amt scrolled down
3046 var st = this.DDM.getScrollTop();
3048 // The amt scrolled right
3049 var sl = this.DDM.getScrollLeft();
3051 // Location of the bottom of the element
3054 // Location of the right of the element
3057 // The distance from the cursor to the bottom of the visible area,
3058 // adjusted so that we don't scroll if the cursor is beyond the
3059 // element drag constraints
3060 var toBot = (clientH + st - y - this.deltaY);
3062 // The distance from the cursor to the right of the visible area
3063 var toRight = (clientW + sl - x - this.deltaX);
3066 // How close to the edge the cursor must be before we scroll
3067 // var thresh = (document.all) ? 100 : 40;
3070 // How many pixels to scroll per autoscroll op. This helps to reduce
3071 // clunky scrolling. IE is more sensitive about this ... it needs this
3072 // value to be higher.
3073 var scrAmt = (document.all) ? 80 : 30;
3075 // Scroll down if we are near the bottom of the visible page and the
3076 // obj extends below the crease
3077 if ( bot > clientH && toBot < thresh ) {
3078 window.scrollTo(sl, st + scrAmt);
3081 // Scroll up if the window is scrolled down and the top of the object
3082 // goes above the top border
3083 if ( y < st && st > 0 && y - st < thresh ) {
3084 window.scrollTo(sl, st - scrAmt);
3087 // Scroll right if the obj is beyond the right border and the cursor is
3089 if ( right > clientW && toRight < thresh ) {
3090 window.scrollTo(sl + scrAmt, st);
3093 // Scroll left if the window has been scrolled to the right and the obj
3094 // extends past the left border
3095 if ( x < sl && sl > 0 && x - sl < thresh ) {
3096 window.scrollTo(sl - scrAmt, st);
3102 * Sets up config options specific to this class. Overrides
3103 * YAHOO.util.DragDrop, but all versions of this method through the
3104 * inheritance chain are called
3106 applyConfig: function() {
3107 YAHOO.util.DD.superclass.applyConfig.call(this);
3108 this.scroll = (this.config.scroll !== false);
3112 * Event that fires prior to the onMouseDown event. Overrides
3113 * YAHOO.util.DragDrop.
3115 b4MouseDown: function(e) {
3116 this.setStartPosition();
3117 // this.resetConstraints();
3118 this.autoOffset(YAHOO.util.Event.getPageX(e),
3119 YAHOO.util.Event.getPageY(e));
3123 * Event that fires prior to the onDrag event. Overrides
3124 * YAHOO.util.DragDrop.
3126 b4Drag: function(e) {
3127 this.setDragElPos(YAHOO.util.Event.getPageX(e),
3128 YAHOO.util.Event.getPageY(e));
3131 toString: function() {
3132 return ("DD " + this.id);
3135 //////////////////////////////////////////////////////////////////////////
3136 // Debugging ygDragDrop events that can be overridden
3137 //////////////////////////////////////////////////////////////////////////
3139 startDrag: function(x, y) {
3142 onDrag: function(e) {
3145 onDragEnter: function(e, id) {
3148 onDragOver: function(e, id) {
3151 onDragOut: function(e, id) {
3154 onDragDrop: function(e, id) {
3157 endDrag: function(e) {
3163 * @event mouseDownEvent
3164 * @description Provides access to the mousedown event. The mousedown does not always result in a drag operation.
3165 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3169 * @event b4MouseDownEvent
3170 * @description Provides access to the mousedown event, before the mouseDownEvent gets fired. Returning false will cancel the drag.
3171 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3175 * @event mouseUpEvent
3176 * @description Fired from inside DragDropMgr when the drag operation is finished.
3177 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3181 * @event b4StartDragEvent
3182 * @description Fires before the startDragEvent, returning false will cancel the startDrag Event.
3183 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3187 * @event startDragEvent
3188 * @description Occurs after a mouse down and the drag threshold has been met. The drag threshold default is either 3 pixels of mouse movement or 1 full second of holding the mousedown.
3189 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3193 * @event b4EndDragEvent
3194 * @description Fires before the endDragEvent. Returning false will cancel.
3195 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3199 * @event endDragEvent
3200 * @description Fires on the mouseup event after a drag has been initiated (startDrag fired).
3201 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3206 * @description Occurs every mousemove event while dragging.
3207 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3210 * @event b4DragEvent
3211 * @description Fires before the dragEvent.
3212 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3215 * @event invalidDropEvent
3216 * @description Fires when the dragged objects is dropped in a location that contains no drop targets.
3217 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3220 * @event b4DragOutEvent
3221 * @description Fires before the dragOutEvent
3222 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3225 * @event dragOutEvent
3226 * @description Fires when a dragged object is no longer over an object that had the onDragEnter fire.
3227 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3230 * @event dragEnterEvent
3231 * @description Occurs when the dragged object first interacts with another targettable drag and drop object.
3232 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3235 * @event b4DragOverEvent
3236 * @description Fires before the dragOverEvent.
3237 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3240 * @event dragOverEvent
3241 * @description Fires every mousemove event while over a drag and drop object.
3242 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3245 * @event b4DragDropEvent
3246 * @description Fires before the dragDropEvent
3247 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3250 * @event dragDropEvent
3251 * @description Fires when the dragged objects is dropped on another.
3252 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3256 * A DragDrop implementation that inserts an empty, bordered div into
3257 * the document that follows the cursor during drag operations. At the time of
3258 * the click, the frame div is resized to the dimensions of the linked html
3259 * element, and moved to the exact location of the linked element.
3261 * References to the "frame" element refer to the single proxy element that
3262 * was created to be dragged in place of all DDProxy elements on the
3266 * @extends YAHOO.util.DD
3268 * @param {String} id the id of the linked html element
3269 * @param {String} sGroup the group of related DragDrop objects
3270 * @param {object} config an object containing configurable attributes
3271 * Valid properties for DDProxy in addition to those in DragDrop:
3272 * resizeFrame, centerFrame, dragElId
3274 YAHOO.util.DDProxy = function(id, sGroup, config) {
3276 this.init(id, sGroup, config);
3282 * The default drag frame div id
3283 * @property YAHOO.util.DDProxy.dragElId
3287 YAHOO.util.DDProxy.dragElId = "ygddfdiv";
3289 YAHOO.extend(YAHOO.util.DDProxy, YAHOO.util.DD, {
3292 * By default we resize the drag frame to be the same size as the element
3293 * we want to drag (this is to get the frame effect). We can turn it off
3294 * if we want a different behavior.
3295 * @property resizeFrame
3301 * By default the frame is positioned exactly where the drag element is, so
3302 * we use the cursor offset provided by YAHOO.util.DD. Another option that works only if
3303 * you do not have constraints on the obj is to have the drag frame centered
3304 * around the cursor. Set centerFrame to true for this effect.
3305 * @property centerFrame
3311 * Creates the proxy element if it does not yet exist
3312 * @method createFrame
3314 createFrame: function() {
3315 var self=this, body=document.body;
3317 if (!body || !body.firstChild) {
3318 setTimeout( function() { self.createFrame(); }, 50 );
3322 var div=this.getDragEl(), Dom=YAHOO.util.Dom;
3325 div = document.createElement("div");
3326 div.id = this.dragElId;
3329 s.position = "absolute";
3330 s.visibility = "hidden";
3332 s.border = "2px solid #aaa";
3337 var _data = document.createElement('div');
3338 Dom.setStyle(_data, 'height', '100%');
3339 Dom.setStyle(_data, 'width', '100%');
3341 * If the proxy element has no background-color, then it is considered to the "transparent" by Internet Explorer.
3342 * Since it is "transparent" then the events pass through it to the iframe below.
3343 * So creating a "fake" div inside the proxy element and giving it a background-color, then setting it to an
3344 * opacity of 0, it appears to not be there, however IE still thinks that it is so the events never pass through.
3346 Dom.setStyle(_data, 'background-color', '#ccc');
3347 Dom.setStyle(_data, 'opacity', '0');
3348 div.appendChild(_data);
3350 // appendChild can blow up IE if invoked prior to the window load event
3351 // while rendering a table. It is possible there are other scenarios
3352 // that would cause this to happen as well.
3353 body.insertBefore(div, body.firstChild);
3358 * Initialization for the drag frame element. Must be called in the
3359 * constructor of all subclasses
3362 initFrame: function() {
3366 applyConfig: function() {
3367 YAHOO.util.DDProxy.superclass.applyConfig.call(this);
3369 this.resizeFrame = (this.config.resizeFrame !== false);
3370 this.centerFrame = (this.config.centerFrame);
3371 this.setDragElId(this.config.dragElId || YAHOO.util.DDProxy.dragElId);
3375 * Resizes the drag frame to the dimensions of the clicked object, positions
3376 * it over the object, and finally displays it
3378 * @param {int} iPageX X click position
3379 * @param {int} iPageY Y click position
3382 showFrame: function(iPageX, iPageY) {
3383 var el = this.getEl();
3384 var dragEl = this.getDragEl();
3385 var s = dragEl.style;
3387 this._resizeProxy();
3389 if (this.centerFrame) {
3390 this.setDelta( Math.round(parseInt(s.width, 10)/2),
3391 Math.round(parseInt(s.height, 10)/2) );
3394 this.setDragElPos(iPageX, iPageY);
3396 YAHOO.util.Dom.setStyle(dragEl, "visibility", "visible");
3400 * The proxy is automatically resized to the dimensions of the linked
3401 * element when a drag is initiated, unless resizeFrame is set to false
3402 * @method _resizeProxy
3405 _resizeProxy: function() {
3406 if (this.resizeFrame) {
3407 var DOM = YAHOO.util.Dom;
3408 var el = this.getEl();
3409 var dragEl = this.getDragEl();
3411 var bt = parseInt( DOM.getStyle(dragEl, "borderTopWidth" ), 10);
3412 var br = parseInt( DOM.getStyle(dragEl, "borderRightWidth" ), 10);
3413 var bb = parseInt( DOM.getStyle(dragEl, "borderBottomWidth" ), 10);
3414 var bl = parseInt( DOM.getStyle(dragEl, "borderLeftWidth" ), 10);
3416 if (isNaN(bt)) { bt = 0; }
3417 if (isNaN(br)) { br = 0; }
3418 if (isNaN(bb)) { bb = 0; }
3419 if (isNaN(bl)) { bl = 0; }
3422 var newWidth = Math.max(0, el.offsetWidth - br - bl);
3423 var newHeight = Math.max(0, el.offsetHeight - bt - bb);
3426 DOM.setStyle( dragEl, "width", newWidth + "px" );
3427 DOM.setStyle( dragEl, "height", newHeight + "px" );
3431 // overrides YAHOO.util.DragDrop
3432 b4MouseDown: function(e) {
3433 this.setStartPosition();
3434 var x = YAHOO.util.Event.getPageX(e);
3435 var y = YAHOO.util.Event.getPageY(e);
3436 this.autoOffset(x, y);
3438 // This causes the autoscroll code to kick off, which means autoscroll can
3439 // happen prior to the check for a valid drag handle.
3440 // this.setDragElPos(x, y);
3443 // overrides YAHOO.util.DragDrop
3444 b4StartDrag: function(x, y) {
3445 // show the drag frame
3446 this.showFrame(x, y);
3449 // overrides YAHOO.util.DragDrop
3450 b4EndDrag: function(e) {
3451 YAHOO.util.Dom.setStyle(this.getDragEl(), "visibility", "hidden");
3454 // overrides YAHOO.util.DragDrop
3455 // By default we try to move the element to the last location of the frame.
3456 // This is so that the default behavior mirrors that of YAHOO.util.DD.
3457 endDrag: function(e) {
3458 var DOM = YAHOO.util.Dom;
3459 var lel = this.getEl();
3460 var del = this.getDragEl();
3462 // Show the drag frame briefly so we can get its position
3463 // del.style.visibility = "";
3464 DOM.setStyle(del, "visibility", "");
3466 // Hide the linked element before the move to get around a Safari
3468 //lel.style.visibility = "hidden";
3469 DOM.setStyle(lel, "visibility", "hidden");
3470 YAHOO.util.DDM.moveToEl(lel, del);
3471 //del.style.visibility = "hidden";
3472 DOM.setStyle(del, "visibility", "hidden");
3473 //lel.style.visibility = "";
3474 DOM.setStyle(lel, "visibility", "");
3477 toString: function() {
3478 return ("DDProxy " + this.id);
3481 * @event mouseDownEvent
3482 * @description Provides access to the mousedown event. The mousedown does not always result in a drag operation.
3483 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3487 * @event b4MouseDownEvent
3488 * @description Provides access to the mousedown event, before the mouseDownEvent gets fired. Returning false will cancel the drag.
3489 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3493 * @event mouseUpEvent
3494 * @description Fired from inside DragDropMgr when the drag operation is finished.
3495 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3499 * @event b4StartDragEvent
3500 * @description Fires before the startDragEvent, returning false will cancel the startDrag Event.
3501 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3505 * @event startDragEvent
3506 * @description Occurs after a mouse down and the drag threshold has been met. The drag threshold default is either 3 pixels of mouse movement or 1 full second of holding the mousedown.
3507 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3511 * @event b4EndDragEvent
3512 * @description Fires before the endDragEvent. Returning false will cancel.
3513 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3517 * @event endDragEvent
3518 * @description Fires on the mouseup event after a drag has been initiated (startDrag fired).
3519 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3524 * @description Occurs every mousemove event while dragging.
3525 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3528 * @event b4DragEvent
3529 * @description Fires before the dragEvent.
3530 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3533 * @event invalidDropEvent
3534 * @description Fires when the dragged objects is dropped in a location that contains no drop targets.
3535 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3538 * @event b4DragOutEvent
3539 * @description Fires before the dragOutEvent
3540 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3543 * @event dragOutEvent
3544 * @description Fires when a dragged object is no longer over an object that had the onDragEnter fire.
3545 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3548 * @event dragEnterEvent
3549 * @description Occurs when the dragged object first interacts with another targettable drag and drop object.
3550 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3553 * @event b4DragOverEvent
3554 * @description Fires before the dragOverEvent.
3555 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3558 * @event dragOverEvent
3559 * @description Fires every mousemove event while over a drag and drop object.
3560 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3563 * @event b4DragDropEvent
3564 * @description Fires before the dragDropEvent
3565 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3568 * @event dragDropEvent
3569 * @description Fires when the dragged objects is dropped on another.
3570 * @type YAHOO.util.CustomEvent See <a href="YAHOO.util.Element.html#addListener">Element.addListener</a> for more information on listening for this event.
3575 * A DragDrop implementation that does not move, but can be a drop
3576 * target. You would get the same result by simply omitting implementation
3577 * for the event callbacks, but this way we reduce the processing cost of the
3578 * event listener and the callbacks.
3580 * @extends YAHOO.util.DragDrop
3582 * @param {String} id the id of the element that is a drop target
3583 * @param {String} sGroup the group of related DragDrop objects
3584 * @param {object} config an object containing configurable attributes
3585 * Valid properties for DDTarget in addition to those in
3589 YAHOO.util.DDTarget = function(id, sGroup, config) {
3591 this.initTarget(id, sGroup, config);
3595 // YAHOO.util.DDTarget.prototype = new YAHOO.util.DragDrop();
3596 YAHOO.extend(YAHOO.util.DDTarget, YAHOO.util.DragDrop, {
3597 toString: function() {
3598 return ("DDTarget " + this.id);
3601 YAHOO.register("dragdrop", YAHOO.util.DragDropMgr, {version: "2.8.0r4", build: "2449"});