/* Copyright (c) 2006, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.net/yui/license.txt version: 0.12.0 */ (function() { var Event=YAHOO.util.Event; var Dom=YAHOO.util.Dom; /** * Defines the interface and base operation of items that that can be * dragged or can be drop targets. It was designed to be extended, overriding * the event handlers for startDrag, onDrag, onDragOver, onDragOut. * Up to three html elements can be associated with a DragDrop instance: *
* dd = new YAHOO.util.DragDrop("div1", "group1");
*
* Since none of the event handlers have been implemented, nothing would
* actually happen if you were to run the code above. Normally you would
* override this class or one of the default implementations, but you can
* also override the methods you want on an instance of the class...
*
* dd.onDragDrop = function(e, id) {
* alert("dd was dropped on " + id);
* }
*
* @namespace YAHOO.util
* @class DragDrop
* @constructor
* @param {String} id of the element that is linked to this instance
* @param {String} sGroup the group of related DragDrop objects
* @param {object} config an object containing configurable attributes
* Valid properties for DragDrop:
* padding, isTarget, maintainOffset, primaryButtonOnly
*/
YAHOO.util.DragDrop = function(id, sGroup, config) {
if (id) {
this.init(id, sGroup, config);
}
};
YAHOO.util.DragDrop.prototype = {
/**
* The id of the element associated with this object. This is what we
* refer to as the "linked element" because the size and position of
* this element is used to determine when the drag and drop objects have
* interacted.
* @property id
* @type String
*/
id: null,
/**
* Configuration attributes passed into the constructor
* @property config
* @type object
*/
config: null,
/**
* The id of the element that will be dragged. By default this is same
* as the linked element , but could be changed to another element. Ex:
* YAHOO.util.DDProxy
* @property dragElId
* @type String
* @private
*/
dragElId: null,
/**
* the id of the element that initiates the drag operation. By default
* this is the linked element, but could be changed to be a child of this
* element. This lets us do things like only starting the drag when the
* header element within the linked html element is clicked.
* @property handleElId
* @type String
* @private
*/
handleElId: null,
/**
* An associative array of HTML tags that will be ignored if clicked.
* @property invalidHandleTypes
* @type {string: string}
*/
invalidHandleTypes: null,
/**
* An associative array of ids for elements that will be ignored if clicked
* @property invalidHandleIds
* @type {string: string}
*/
invalidHandleIds: null,
/**
* An indexted array of css class names for elements that will be ignored
* if clicked.
* @property invalidHandleClasses
* @type string[]
*/
invalidHandleClasses: null,
/**
* The linked element's absolute X position at the time the drag was
* started
* @property startPageX
* @type int
* @private
*/
startPageX: 0,
/**
* The linked element's absolute X position at the time the drag was
* started
* @property startPageY
* @type int
* @private
*/
startPageY: 0,
/**
* The group defines a logical collection of DragDrop objects that are
* related. Instances only get events when interacting with other
* DragDrop object in the same group. This lets us define multiple
* groups using a single DragDrop subclass if we want.
* @property groups
* @type {string: string}
*/
groups: null,
/**
* Individual drag/drop instances can be locked. This will prevent
* onmousedown start drag.
* @property locked
* @type boolean
* @private
*/
locked: false,
/**
* Lock this instance
* @method lock
*/
lock: function() { this.locked = true; },
/**
* Unlock this instace
* @method unlock
*/
unlock: function() { this.locked = false; },
/**
* By default, all insances can be a drop target. This can be disabled by
* setting isTarget to false.
* @method isTarget
* @type boolean
*/
isTarget: true,
/**
* The padding configured for this drag and drop object for calculating
* the drop zone intersection with this object.
* @method padding
* @type int[]
*/
padding: null,
/**
* Cached reference to the linked element
* @property _domRef
* @private
*/
_domRef: null,
/**
* Internal typeof flag
* @property __ygDragDrop
* @private
*/
__ygDragDrop: true,
/**
* Set to true when horizontal contraints are applied
* @property constrainX
* @type boolean
* @private
*/
constrainX: false,
/**
* Set to true when vertical contraints are applied
* @property constrainY
* @type boolean
* @private
*/
constrainY: false,
/**
* The left constraint
* @property minX
* @type int
* @private
*/
minX: 0,
/**
* The right constraint
* @property maxX
* @type int
* @private
*/
maxX: 0,
/**
* The up constraint
* @property minY
* @type int
* @type int
* @private
*/
minY: 0,
/**
* The down constraint
* @property maxY
* @type int
* @private
*/
maxY: 0,
/**
* Maintain offsets when we resetconstraints. Set to true when you want
* the position of the element relative to its parent to stay the same
* when the page changes
*
* @property maintainOffset
* @type boolean
*/
maintainOffset: false,
/**
* Array of pixel locations the element will snap to if we specified a
* horizontal graduation/interval. This array is generated automatically
* when you define a tick interval.
* @property xTicks
* @type int[]
*/
xTicks: null,
/**
* Array of pixel locations the element will snap to if we specified a
* vertical graduation/interval. This array is generated automatically
* when you define a tick interval.
* @property yTicks
* @type int[]
*/
yTicks: null,
/**
* By default the drag and drop instance will only respond to the primary
* button click (left button for a right-handed mouse). Set to true to
* allow drag and drop to start with any mouse click that is propogated
* by the browser
* @property primaryButtonOnly
* @type boolean
*/
primaryButtonOnly: true,
/**
* The availabe property is false until the linked dom element is accessible.
* @property available
* @type boolean
*/
available: false,
/**
* By default, drags can only be initiated if the mousedown occurs in the
* region the linked element is. This is done in part to work around a
* bug in some browsers that mis-report the mousedown if the previous
* mouseup happened outside of the window. This property is set to true
* if outer handles are defined.
*
* @property hasOuterHandles
* @type boolean
* @default false
*/
hasOuterHandles: false,
/**
* Code that executes immediately before the startDrag event
* @method b4StartDrag
* @private
*/
b4StartDrag: function(x, y) { },
/**
* Abstract method called after a drag/drop object is clicked
* and the drag or mousedown time thresholds have beeen met.
* @method startDrag
* @param {int} X click location
* @param {int} Y click location
*/
startDrag: function(x, y) { /* override this */ },
/**
* Code that executes immediately before the onDrag event
* @method b4Drag
* @private
*/
b4Drag: function(e) { },
/**
* Abstract method called during the onMouseMove event while dragging an
* object.
* @method onDrag
* @param {Event} e the mousemove event
*/
onDrag: function(e) { /* override this */ },
/**
* Abstract method called when this element fist begins hovering over
* another DragDrop obj
* @method onDragEnter
* @param {Event} e the mousemove event
* @param {String|DragDrop[]} id In POINT mode, the element
* id this is hovering over. In INTERSECT mode, an array of one or more
* dragdrop items being hovered over.
*/
onDragEnter: function(e, id) { /* override this */ },
/**
* Code that executes immediately before the onDragOver event
* @method b4DragOver
* @private
*/
b4DragOver: function(e) { },
/**
* Abstract method called when this element is hovering over another
* DragDrop obj
* @method onDragOver
* @param {Event} e the mousemove event
* @param {String|DragDrop[]} id In POINT mode, the element
* id this is hovering over. In INTERSECT mode, an array of dd items
* being hovered over.
*/
onDragOver: function(e, id) { /* override this */ },
/**
* Code that executes immediately before the onDragOut event
* @method b4DragOut
* @private
*/
b4DragOut: function(e) { },
/**
* Abstract method called when we are no longer hovering over an element
* @method onDragOut
* @param {Event} e the mousemove event
* @param {String|DragDrop[]} id In POINT mode, the element
* id this was hovering over. In INTERSECT mode, an array of dd items
* that the mouse is no longer over.
*/
onDragOut: function(e, id) { /* override this */ },
/**
* Code that executes immediately before the onDragDrop event
* @method b4DragDrop
* @private
*/
b4DragDrop: function(e) { },
/**
* Abstract method called when this item is dropped on another DragDrop
* obj
* @method onDragDrop
* @param {Event} e the mouseup event
* @param {String|DragDrop[]} id In POINT mode, the element
* id this was dropped on. In INTERSECT mode, an array of dd items this
* was dropped on.
*/
onDragDrop: function(e, id) { /* override this */ },
/**
* Abstract method called when this item is dropped on an area with no
* drop target
* @method onInvalidDrop
* @param {Event} e the mouseup event
*/
onInvalidDrop: function(e) { /* override this */ },
/**
* Code that executes immediately before the endDrag event
* @method b4EndDrag
* @private
*/
b4EndDrag: function(e) { },
/**
* Fired when we are done dragging the object
* @method endDrag
* @param {Event} e the mouseup event
*/
endDrag: function(e) { /* override this */ },
/**
* Code executed immediately before the onMouseDown event
* @method b4MouseDown
* @param {Event} e the mousedown event
* @private
*/
b4MouseDown: function(e) { },
/**
* Event handler that fires when a drag/drop obj gets a mousedown
* @method onMouseDown
* @param {Event} e the mousedown event
*/
onMouseDown: function(e) { /* override this */ },
/**
* Event handler that fires when a drag/drop obj gets a mouseup
* @method onMouseUp
* @param {Event} e the mouseup event
*/
onMouseUp: function(e) { /* override this */ },
/**
* Override the onAvailable method to do what is needed after the initial
* position was determined.
* @method onAvailable
*/
onAvailable: function () {
},
/**
* Returns a reference to the linked element
* @method getEl
* @return {HTMLElement} the html element
*/
getEl: function() {
if (!this._domRef) {
this._domRef = Dom.get(this.id);
}
return this._domRef;
},
/**
* Returns a reference to the actual element to drag. By default this is
* the same as the html element, but it can be assigned to another
* element. An example of this can be found in YAHOO.util.DDProxy
* @method getDragEl
* @return {HTMLElement} the html element
*/
getDragEl: function() {
return Dom.get(this.dragElId);
},
/**
* Sets up the DragDrop object. Must be called in the constructor of any
* YAHOO.util.DragDrop subclass
* @method init
* @param id the id of the linked element
* @param {String} sGroup the group of related items
* @param {object} config configuration attributes
*/
init: function(id, sGroup, config) {
this.initTarget(id, sGroup, config);
Event.on(this.id, "mousedown", this.handleMouseDown, this, true);
// Event.on(this.id, "selectstart", Event.preventDefault);
},
/**
* Initializes Targeting functionality only... the object does not
* get a mousedown handler.
* @method initTarget
* @param id the id of the linked element
* @param {String} sGroup the group of related items
* @param {object} config configuration attributes
*/
initTarget: function(id, sGroup, config) {
// configuration attributes
this.config = config || {};
// create a local reference to the drag and drop manager
this.DDM = YAHOO.util.DDM;
// initialize the groups array
this.groups = {};
// assume that we have an element reference instead of an id if the
// parameter is not a string
if (typeof id !== "string") {
YAHOO.log("id is not a string, assuming it is an HTMLElement");
id = Dom.generateId(id);
}
// set the id
this.id = id;
// add to an interaction group
this.addToGroup((sGroup) ? sGroup : "default");
// We don't want to register this as the handle with the manager
// so we just set the id rather than calling the setter.
this.handleElId = id;
Event.onAvailable(id, this.handleOnAvailable, this, true);
// the linked element is the element that gets dragged by default
this.setDragElId(id);
// by default, clicked anchors will not start drag operations.
// @TODO what else should be here? Probably form fields.
this.invalidHandleTypes = { A: "A" };
this.invalidHandleIds = {};
this.invalidHandleClasses = [];
this.applyConfig();
},
/**
* Applies the configuration parameters that were passed into the constructor.
* This is supposed to happen at each level through the inheritance chain. So
* a DDProxy implentation will execute apply config on DDProxy, DD, and
* DragDrop in order to get all of the parameters that are available in
* each object.
* @method applyConfig
*/
applyConfig: function() {
// configurable properties:
// padding, isTarget, maintainOffset, primaryButtonOnly
this.padding = this.config.padding || [0, 0, 0, 0];
this.isTarget = (this.config.isTarget !== false);
this.maintainOffset = (this.config.maintainOffset);
this.primaryButtonOnly = (this.config.primaryButtonOnly !== false);
},
/**
* Executed when the linked element is available
* @method handleOnAvailable
* @private
*/
handleOnAvailable: function() {
this.available = true;
this.resetConstraints();
this.onAvailable();
},
/**
* Configures the padding for the target zone in px. Effectively expands
* (or reduces) the virtual object size for targeting calculations.
* Supports css-style shorthand; if only one parameter is passed, all sides
* will have that padding, and if only two are passed, the top and bottom
* will have the first param, the left and right the second.
* @method setPadding
* @param {int} iTop Top pad
* @param {int} iRight Right pad
* @param {int} iBot Bot pad
* @param {int} iLeft Left pad
*/
setPadding: function(iTop, iRight, iBot, iLeft) {
// this.padding = [iLeft, iRight, iTop, iBot];
if (!iRight && 0 !== iRight) {
this.padding = [iTop, iTop, iTop, iTop];
} else if (!iBot && 0 !== iBot) {
this.padding = [iTop, iRight, iTop, iRight];
} else {
this.padding = [iTop, iRight, iBot, iLeft];
}
},
/**
* Stores the initial placement of the linked element.
* @method setInitialPosition
* @param {int} diffX the X offset, default 0
* @param {int} diffY the Y offset, default 0
*/
setInitPosition: function(diffX, diffY) {
var el = this.getEl();
if (!this.DDM.verifyEl(el)) {
return;
}
var dx = diffX || 0;
var dy = diffY || 0;
var p = Dom.getXY( el );
this.initPageX = p[0] - dx;
this.initPageY = p[1] - dy;
this.lastPageX = p[0];
this.lastPageY = p[1];
this.setStartPosition(p);
},
/**
* Sets the start position of the element. This is set when the obj
* is initialized, the reset when a drag is started.
* @method setStartPosition
* @param pos current position (from previous lookup)
* @private
*/
setStartPosition: function(pos) {
var p = pos || Dom.getXY( this.getEl() );
this.deltaSetXY = null;
this.startPageX = p[0];
this.startPageY = p[1];
},
/**
* Add this instance to a group of related drag/drop objects. All
* instances belong to at least one group, and can belong to as many
* groups as needed.
* @method addToGroup
* @param sGroup {string} the name of the group
*/
addToGroup: function(sGroup) {
this.groups[sGroup] = true;
this.DDM.regDragDrop(this, sGroup);
},
/**
* Remove's this instance from the supplied interaction group
* @method removeFromGroup
* @param {string} sGroup The group to drop
*/
removeFromGroup: function(sGroup) {
if (this.groups[sGroup]) {
delete this.groups[sGroup];
}
this.DDM.removeDDFromGroup(this, sGroup);
},
/**
* Allows you to specify that an element other than the linked element
* will be moved with the cursor during a drag
* @method setDragElId
* @param id {string} the id of the element that will be used to initiate the drag
*/
setDragElId: function(id) {
this.dragElId = id;
},
/**
* Allows you to specify a child of the linked element that should be
* used to initiate the drag operation. An example of this would be if
* you have a content div with text and links. Clicking anywhere in the
* content area would normally start the drag operation. Use this method
* to specify that an element inside of the content div is the element
* that starts the drag operation.
* @method setHandleElId
* @param id {string} the id of the element that will be used to
* initiate the drag.
*/
setHandleElId: function(id) {
if (typeof id !== "string") {
YAHOO.log("id is not a string, assuming it is an HTMLElement");
id = Dom.generateId(id);
}
this.handleElId = id;
this.DDM.regHandle(this.id, id);
},
/**
* Allows you to set an element outside of the linked element as a drag
* handle
* @method setOuterHandleElId
* @param id the id of the element that will be used to initiate the drag
*/
setOuterHandleElId: function(id) {
if (typeof id !== "string") {
YAHOO.log("id is not a string, assuming it is an HTMLElement");
id = Dom.generateId(id);
}
Event.on(id, "mousedown",
this.handleMouseDown, this, true);
this.setHandleElId(id);
this.hasOuterHandles = true;
},
/**
* Remove all drag and drop hooks for this element
* @method unreg
*/
unreg: function() {
Event.removeListener(this.id, "mousedown",
this.handleMouseDown);
this._domRef = null;
this.DDM._remove(this);
},
/**
* Returns true if this instance is locked, or the drag drop mgr is locked
* (meaning that all drag/drop is disabled on the page.)
* @method isLocked
* @return {boolean} true if this obj or all drag/drop is locked, else
* false
*/
isLocked: function() {
return (this.DDM.isLocked() || this.locked);
},
/**
* Fired when this object is clicked
* @method handleMouseDown
* @param {Event} e
* @param {YAHOO.util.DragDrop} oDD the clicked dd object (this dd obj)
* @private
*/
handleMouseDown: function(e, oDD) {
var button = e.which || e.button;
if (this.primaryButtonOnly && button > 1) {
return;
}
if (this.isLocked()) {
return;
}
this.DDM.refreshCache(this.groups);
// var self = this;
// setTimeout( function() { self.DDM.refreshCache(self.groups); }, 0);
// Only process the event if we really clicked within the linked
// element. The reason we make this check is that in the case that
// another element was moved between the clicked element and the
// cursor in the time between the mousedown and mouseup events. When
// this happens, the element gets the next mousedown event
// regardless of where on the screen it happened.
var pt = new YAHOO.util.Point(Event.getPageX(e), Event.getPageY(e));
if (!this.hasOuterHandles && !this.DDM.isOverTarget(pt, this) ) {
} else {
if (this.clickValidator(e)) {
// set the initial element position
this.setStartPosition();
this.b4MouseDown(e);
this.onMouseDown(e);
this.DDM.handleMouseDown(e, this);
this.DDM.stopEvent(e);
} else {
}
}
},
clickValidator: function(e) {
var target = Event.getTarget(e);
return ( this.isValidHandleChild(target) &&
(this.id == this.handleElId ||
this.DDM.handleWasClicked(target, this.id)) );
},
/**
* Allows you to specify a tag name that should not start a drag operation
* when clicked. This is designed to facilitate embedding links within a
* drag handle that do something other than start the drag.
* @method addInvalidHandleType
* @param {string} tagName the type of element to exclude
*/
addInvalidHandleType: function(tagName) {
var type = tagName.toUpperCase();
this.invalidHandleTypes[type] = type;
},
/**
* Lets you to specify an element id for a child of a drag handle
* that should not initiate a drag
* @method addInvalidHandleId
* @param {string} id the element id of the element you wish to ignore
*/
addInvalidHandleId: function(id) {
if (typeof id !== "string") {
YAHOO.log("id is not a string, assuming it is an HTMLElement");
id = Dom.generateId(id);
}
this.invalidHandleIds[id] = id;
},
/**
* Lets you specify a css class of elements that will not initiate a drag
* @method addInvalidHandleClass
* @param {string} cssClass the class of the elements you wish to ignore
*/
addInvalidHandleClass: function(cssClass) {
this.invalidHandleClasses.push(cssClass);
},
/**
* Unsets an excluded tag name set by addInvalidHandleType
* @method removeInvalidHandleType
* @param {string} tagName the type of element to unexclude
*/
removeInvalidHandleType: function(tagName) {
var type = tagName.toUpperCase();
// this.invalidHandleTypes[type] = null;
delete this.invalidHandleTypes[type];
},
/**
* Unsets an invalid handle id
* @method removeInvalidHandleId
* @param {string} id the id of the element to re-enable
*/
removeInvalidHandleId: function(id) {
if (typeof id !== "string") {
YAHOO.log("id is not a string, assuming it is an HTMLElement");
id = Dom.generateId(id);
}
delete this.invalidHandleIds[id];
},
/**
* Unsets an invalid css class
* @method removeInvalidHandleClass
* @param {string} cssClass the class of the element(s) you wish to
* re-enable
*/
removeInvalidHandleClass: function(cssClass) {
for (var i=0, len=this.invalidHandleClasses.length; i
* YAHOO.util.DragDropMgr.refreshCache({group1:true, group2:true});
*
* @TODO this really should be an indexed array. Alternatively this
* method could accept both.
* @method refreshCache
* @param {Object} groups an associative array of groups to refresh
* @static
*/
refreshCache: function(groups) {
for (var sGroup in groups) {
if ("string" != typeof sGroup) {
continue;
}
for (var i in this.ids[sGroup]) {
var oDD = this.ids[sGroup][i];
if (this.isTypeOfDD(oDD)) {
// if (this.isTypeOfDD(oDD) && oDD.isTarget) {
var loc = this.getLocation(oDD);
if (loc) {
this.locationCache[oDD.id] = loc;
} else {
delete this.locationCache[oDD.id];
// this will unregister the drag and drop object if
// the element is not in a usable state
// oDD.unreg();
}
}
}
}
},
/**
* This checks to make sure an element exists and is in the DOM. The
* main purpose is to handle cases where innerHTML is used to remove
* drag and drop objects from the DOM. IE provides an 'unspecified
* error' when trying to access the offsetParent of such an element
* @method verifyEl
* @param {HTMLElement} el the element to check
* @return {boolean} true if the element looks usable
* @static
*/
verifyEl: function(el) {
try {
if (el) {
var parent = el.offsetParent;
if (parent) {
return true;
}
}
} catch(e) {
}
return false;
},
/**
* Returns a Region object containing the drag and drop element's position
* and size, including the padding configured for it
* @method getLocation
* @param {DragDrop} oDD the drag and drop object to get the
* location for
* @return {YAHOO.util.Region} a Region object representing the total area
* the element occupies, including any padding
* the instance is configured for.
* @static
*/
getLocation: function(oDD) {
if (! this.isTypeOfDD(oDD)) {
return null;
}
var el = oDD.getEl(), pos, x1, x2, y1, y2, t, r, b, l;
try {
pos= YAHOO.util.Dom.getXY(el);
} catch (e) { }
if (!pos) {
return null;
}
x1 = pos[0];
x2 = x1 + el.offsetWidth;
y1 = pos[1];
y2 = y1 + el.offsetHeight;
t = y1 - oDD.padding[0];
r = x2 + oDD.padding[1];
b = y2 + oDD.padding[2];
l = x1 - oDD.padding[3];
return new YAHOO.util.Region( t, r, b, l );
},
/**
* Checks the cursor location to see if it over the target
* @method isOverTarget
* @param {YAHOO.util.Point} pt The point to evaluate
* @param {DragDrop} oTarget the DragDrop object we are inspecting
* @return {boolean} true if the mouse is over the target
* @private
* @static
*/
isOverTarget: function(pt, oTarget, intersect) {
// use cache if available
var loc = this.locationCache[oTarget.id];
if (!loc || !this.useCache) {
loc = this.getLocation(oTarget);
this.locationCache[oTarget.id] = loc;
}
if (!loc) {
return false;
}
oTarget.cursorIsOver = loc.contains( pt );
// DragDrop is using this as a sanity check for the initial mousedown
// in this case we are done. In POINT mode, if the drag obj has no
// contraints, we are also done. Otherwise we need to evaluate the
// location of the target as related to the actual location of the
// dragged element.
var dc = this.dragCurrent;
if (!dc || !dc.getTargetCoord ||
(!intersect && !dc.constrainX && !dc.constrainY)) {
return oTarget.cursorIsOver;
}
oTarget.overlap = null;
// Get the current location of the drag element, this is the
// location of the mouse event less the delta that represents
// where the original mousedown happened on the element. We
// need to consider constraints and ticks as well.
var pos = dc.getTargetCoord(pt.x, pt.y);
var el = dc.getDragEl();
var curRegion = new YAHOO.util.Region( pos.y,
pos.x + el.offsetWidth,
pos.y + el.offsetHeight,
pos.x );
var overlap = curRegion.intersect(loc);
if (overlap) {
oTarget.overlap = overlap;
return (intersect) ? true : oTarget.cursorIsOver;
} else {
return false;
}
},
/**
* unload event handler
* @method _onUnload
* @private
* @static
*/
_onUnload: function(e, me) {
this.unregAll();
},
/**
* Cleans up the drag and drop events and objects.
* @method unregAll
* @private
* @static
*/
unregAll: function() {
if (this.dragCurrent) {
this.stopDrag();
this.dragCurrent = null;
}
this._execOnAll("unreg", []);
for (i in this.elementCache) {
delete this.elementCache[i];
}
this.elementCache = {};
this.ids = {};
},
/**
* A cache of DOM elements
* @property elementCache
* @private
* @static
*/
elementCache: {},
/**
* Get the wrapper for the DOM element specified
* @method getElWrapper
* @param {String} id the id of the element to get
* @return {YAHOO.util.DDM.ElementWrapper} the wrapped element
* @private
* @deprecated This wrapper isn't that useful
* @static
*/
getElWrapper: function(id) {
var oWrapper = this.elementCache[id];
if (!oWrapper || !oWrapper.el) {
oWrapper = this.elementCache[id] =
new this.ElementWrapper(YAHOO.util.Dom.get(id));
}
return oWrapper;
},
/**
* Returns the actual DOM element
* @method getElement
* @param {String} id the id of the elment to get
* @return {Object} The element
* @deprecated use YAHOO.util.Dom.get instead
* @static
*/
getElement: function(id) {
return YAHOO.util.Dom.get(id);
},
/**
* Returns the style property for the DOM element (i.e.,
* document.getElById(id).style)
* @method getCss
* @param {String} id the id of the elment to get
* @return {Object} The style property of the element
* @deprecated use YAHOO.util.Dom instead
* @static
*/
getCss: function(id) {
var el = YAHOO.util.Dom.get(id);
return (el) ? el.style : null;
},
/**
* Inner class for cached elements
* @class DragDropMgr.ElementWrapper
* @for DragDropMgr
* @private
* @deprecated
*/
ElementWrapper: function(el) {
/**
* The element
* @property el
*/
this.el = el || null;
/**
* The element id
* @property id
*/
this.id = this.el && el.id;
/**
* A reference to the style property
* @property css
*/
this.css = this.el && el.style;
},
/**
* Returns the X position of an html element
* @method getPosX
* @param el the element for which to get the position
* @return {int} the X coordinate
* @for DragDropMgr
* @deprecated use YAHOO.util.Dom.getX instead
* @static
*/
getPosX: function(el) {
return YAHOO.util.Dom.getX(el);
},
/**
* Returns the Y position of an html element
* @method getPosY
* @param el the element for which to get the position
* @return {int} the Y coordinate
* @deprecated use YAHOO.util.Dom.getY instead
* @static
*/
getPosY: function(el) {
return YAHOO.util.Dom.getY(el);
},
/**
* Swap two nodes. In IE, we use the native method, for others we
* emulate the IE behavior
* @method swapNode
* @param n1 the first node to swap
* @param n2 the other node to swap
* @static
*/
swapNode: function(n1, n2) {
if (n1.swapNode) {
n1.swapNode(n2);
} else {
var p = n2.parentNode;
var s = n2.nextSibling;
if (s == n1) {
p.insertBefore(n1, n2);
} else if (n2 == n1.nextSibling) {
p.insertBefore(n2, n1);
} else {
n1.parentNode.replaceChild(n2, n1);
p.insertBefore(n1, s);
}
}
},
/**
* Returns the current scroll position
* @method getScroll
* @private
* @static
*/
getScroll: function () {
var t, l, dde=document.documentElement, db=document.body;
if (dde && (dde.scrollTop || dde.scrollLeft)) {
t = dde.scrollTop;
l = dde.scrollLeft;
} else if (db) {
t = db.scrollTop;
l = db.scrollLeft;
} else {
YAHOO.log("could not get scroll property");
}
return { top: t, left: l };
},
/**
* Returns the specified element style property
* @method getStyle
* @param {HTMLElement} el the element
* @param {string} styleProp the style property
* @return {string} The value of the style property
* @deprecated use YAHOO.util.Dom.getStyle
* @static
*/
getStyle: function(el, styleProp) {
return YAHOO.util.Dom.getStyle(el, styleProp);
},
/**
* Gets the scrollTop
* @method getScrollTop
* @return {int} the document's scrollTop
* @static
*/
getScrollTop: function () { return this.getScroll().top; },
/**
* Gets the scrollLeft
* @method getScrollLeft
* @return {int} the document's scrollTop
* @static
*/
getScrollLeft: function () { return this.getScroll().left; },
/**
* Sets the x/y position of an element to the location of the
* target element.
* @method moveToEl
* @param {HTMLElement} moveEl The element to move
* @param {HTMLElement} targetEl The position reference element
* @static
*/
moveToEl: function (moveEl, targetEl) {
var aCoord = YAHOO.util.Dom.getXY(targetEl);
YAHOO.util.Dom.setXY(moveEl, aCoord);
},
/**
* Gets the client height
* @method getClientHeight
* @return {int} client height in px
* @deprecated use YAHOO.util.Dom.getViewportHeight instead
* @static
*/
getClientHeight: function() {
return YAHOO.util.Dom.getViewportHeight();
},
/**
* Gets the client width
* @method getClientWidth
* @return {int} client width in px
* @deprecated use YAHOO.util.Dom.getViewportWidth instead
* @static
*/
getClientWidth: function() {
return YAHOO.util.Dom.getViewportWidth();
},
/**
* Numeric array sort function
* @method numericSort
* @static
*/
numericSort: function(a, b) { return (a - b); },
/**
* Internal counter
* @property _timeoutCount
* @private
* @static
*/
_timeoutCount: 0,
/**
* Trying to make the load order less important. Without this we get
* an error if this file is loaded before the Event Utility.
* @method _addListeners
* @private
* @static
*/
_addListeners: function() {
var DDM = YAHOO.util.DDM;
if ( YAHOO.util.Event && document ) {
DDM._onLoad();
} else {
if (DDM._timeoutCount > 2000) {
} else {
setTimeout(DDM._addListeners, 10);
if (document && document.body) {
DDM._timeoutCount += 1;
}
}
}
},
/**
* Recursively searches the immediate parent and all child nodes for
* the handle element in order to determine wheter or not it was
* clicked.
* @method handleWasClicked
* @param node the html element to inspect
* @static
*/
handleWasClicked: function(node, id) {
if (this.isHandle(id, node.id)) {
return true;
} else {
// check to see if this is a text node child of the one we want
var p = node.parentNode;
while (p) {
if (this.isHandle(id, p.id)) {
return true;
} else {
p = p.parentNode;
}
}
}
return false;
}
};
}();
// shorter alias, save a few bytes
YAHOO.util.DDM = YAHOO.util.DragDropMgr;
YAHOO.util.DDM._addListeners();
}
/**
* A DragDrop implementation where the linked element follows the
* mouse cursor during a drag.
* @class DD
* @extends YAHOO.util.DragDrop
* @constructor
* @param {String} id the id of the linked element
* @param {String} sGroup the group of related DragDrop items
* @param {object} config an object containing configurable attributes
* Valid properties for DD:
* scroll
*/
YAHOO.util.DD = function(id, sGroup, config) {
if (id) {
this.init(id, sGroup, config);
}
};
YAHOO.extend(YAHOO.util.DD, YAHOO.util.DragDrop, {
/**
* When set to true, the utility automatically tries to scroll the browser
* window wehn a drag and drop element is dragged near the viewport boundary.
* Defaults to true.
* @property scroll
* @type boolean
*/
scroll: true,
/**
* Sets the pointer offset to the distance between the linked element's top
* left corner and the location the element was clicked
* @method autoOffset
* @param {int} iPageX the X coordinate of the click
* @param {int} iPageY the Y coordinate of the click
*/
autoOffset: function(iPageX, iPageY) {
var x = iPageX - this.startPageX;
var y = iPageY - this.startPageY;
this.setDelta(x, y);
},
/**
* Sets the pointer offset. You can call this directly to force the
* offset to be in a particular location (e.g., pass in 0,0 to set it
* to the center of the object, as done in YAHOO.widget.Slider)
* @method setDelta
* @param {int} iDeltaX the distance from the left
* @param {int} iDeltaY the distance from the top
*/
setDelta: function(iDeltaX, iDeltaY) {
this.deltaX = iDeltaX;
this.deltaY = iDeltaY;
},
/**
* Sets the drag element to the location of the mousedown or click event,
* maintaining the cursor location relative to the location on the element
* that was clicked. Override this if you want to place the element in a
* location other than where the cursor is.
* @method setDragElPos
* @param {int} iPageX the X coordinate of the mousedown or drag event
* @param {int} iPageY the Y coordinate of the mousedown or drag event
*/
setDragElPos: function(iPageX, iPageY) {
// the first time we do this, we are going to check to make sure
// the element has css positioning
var el = this.getDragEl();
this.alignElWithMouse(el, iPageX, iPageY);
},
/**
* Sets the element to the location of the mousedown or click event,
* maintaining the cursor location relative to the location on the element
* that was clicked. Override this if you want to place the element in a
* location other than where the cursor is.
* @method alignElWithMouse
* @param {HTMLElement} el the element to move
* @param {int} iPageX the X coordinate of the mousedown or drag event
* @param {int} iPageY the Y coordinate of the mousedown or drag event
*/
alignElWithMouse: function(el, iPageX, iPageY) {
var oCoord = this.getTargetCoord(iPageX, iPageY);
if (!this.deltaSetXY) {
var aCoord = [oCoord.x, oCoord.y];
YAHOO.util.Dom.setXY(el, aCoord);
var newLeft = parseInt( YAHOO.util.Dom.getStyle(el, "left"), 10 );
var newTop = parseInt( YAHOO.util.Dom.getStyle(el, "top" ), 10 );
this.deltaSetXY = [ newLeft - oCoord.x, newTop - oCoord.y ];
} else {
YAHOO.util.Dom.setStyle(el, "left", (oCoord.x + this.deltaSetXY[0]) + "px");
YAHOO.util.Dom.setStyle(el, "top", (oCoord.y + this.deltaSetXY[1]) + "px");
}
this.cachePosition(oCoord.x, oCoord.y);
this.autoScroll(oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
},
/**
* Saves the most recent position so that we can reset the constraints and
* tick marks on-demand. We need to know this so that we can calculate the
* number of pixels the element is offset from its original position.
* @method cachePosition
* @param iPageX the current x position (optional, this just makes it so we
* don't have to look it up again)
* @param iPageY the current y position (optional, this just makes it so we
* don't have to look it up again)
*/
cachePosition: function(iPageX, iPageY) {
if (iPageX) {
this.lastPageX = iPageX;
this.lastPageY = iPageY;
} else {
var aCoord = YAHOO.util.Dom.getXY(this.getEl());
this.lastPageX = aCoord[0];
this.lastPageY = aCoord[1];
}
},
/**
* Auto-scroll the window if the dragged object has been moved beyond the
* visible window boundary.
* @method autoScroll
* @param {int} x the drag element's x position
* @param {int} y the drag element's y position
* @param {int} h the height of the drag element
* @param {int} w the width of the drag element
* @private
*/
autoScroll: function(x, y, h, w) {
if (this.scroll) {
// The client height
var clientH = this.DDM.getClientHeight();
// The client width
var clientW = this.DDM.getClientWidth();
// The amt scrolled down
var st = this.DDM.getScrollTop();
// The amt scrolled right
var sl = this.DDM.getScrollLeft();
// Location of the bottom of the element
var bot = h + y;
// Location of the right of the element
var right = w + x;
// The distance from the cursor to the bottom of the visible area,
// adjusted so that we don't scroll if the cursor is beyond the
// element drag constraints
var toBot = (clientH + st - y - this.deltaY);
// The distance from the cursor to the right of the visible area
var toRight = (clientW + sl - x - this.deltaX);
// How close to the edge the cursor must be before we scroll
// var thresh = (document.all) ? 100 : 40;
var thresh = 40;
// How many pixels to scroll per autoscroll op. This helps to reduce
// clunky scrolling. IE is more sensitive about this ... it needs this
// value to be higher.
var scrAmt = (document.all) ? 80 : 30;
// Scroll down if we are near the bottom of the visible page and the
// obj extends below the crease
if ( bot > clientH && toBot < thresh ) {
window.scrollTo(sl, st + scrAmt);
}
// Scroll up if the window is scrolled down and the top of the object
// goes above the top border
if ( y < st && st > 0 && y - st < thresh ) {
window.scrollTo(sl, st - scrAmt);
}
// Scroll right if the obj is beyond the right border and the cursor is
// near the border.
if ( right > clientW && toRight < thresh ) {
window.scrollTo(sl + scrAmt, st);
}
// Scroll left if the window has been scrolled to the right and the obj
// extends past the left border
if ( x < sl && sl > 0 && x - sl < thresh ) {
window.scrollTo(sl - scrAmt, st);
}
}
},
/**
* Finds the location the element should be placed if we want to move
* it to where the mouse location less the click offset would place us.
* @method getTargetCoord
* @param {int} iPageX the X coordinate of the click
* @param {int} iPageY the Y coordinate of the click
* @return an object that contains the coordinates (Object.x and Object.y)
* @private
*/
getTargetCoord: function(iPageX, iPageY) {
var x = iPageX - this.deltaX;
var y = iPageY - this.deltaY;
if (this.constrainX) {
if (x < this.minX) { x = this.minX; }
if (x > this.maxX) { x = this.maxX; }
}
if (this.constrainY) {
if (y < this.minY) { y = this.minY; }
if (y > this.maxY) { y = this.maxY; }
}
x = this.getTick(x, this.xTicks);
y = this.getTick(y, this.yTicks);
return {x:x, y:y};
},
/*
* Sets up config options specific to this class. Overrides
* YAHOO.util.DragDrop, but all versions of this method through the
* inheritance chain are called
*/
applyConfig: function() {
YAHOO.util.DD.superclass.applyConfig.call(this);
this.scroll = (this.config.scroll !== false);
},
/*
* Event that fires prior to the onMouseDown event. Overrides
* YAHOO.util.DragDrop.
*/
b4MouseDown: function(e) {
// this.resetConstraints();
this.autoOffset(YAHOO.util.Event.getPageX(e),
YAHOO.util.Event.getPageY(e));
},
/*
* Event that fires prior to the onDrag event. Overrides
* YAHOO.util.DragDrop.
*/
b4Drag: function(e) {
this.setDragElPos(YAHOO.util.Event.getPageX(e),
YAHOO.util.Event.getPageY(e));
},
toString: function() {
return ("DD " + this.id);
}
//////////////////////////////////////////////////////////////////////////
// Debugging ygDragDrop events that can be overridden
//////////////////////////////////////////////////////////////////////////
/*
startDrag: function(x, y) {
},
onDrag: function(e) {
},
onDragEnter: function(e, id) {
},
onDragOver: function(e, id) {
},
onDragOut: function(e, id) {
},
onDragDrop: function(e, id) {
},
endDrag: function(e) {
}
*/
});
/**
* A DragDrop implementation that inserts an empty, bordered div into
* the document that follows the cursor during drag operations. At the time of
* the click, the frame div is resized to the dimensions of the linked html
* element, and moved to the exact location of the linked element.
*
* References to the "frame" element refer to the single proxy element that
* was created to be dragged in place of all DDProxy elements on the
* page.
*
* @class DDProxy
* @extends YAHOO.util.DD
* @constructor
* @param {String} id the id of the linked html element
* @param {String} sGroup the group of related DragDrop objects
* @param {object} config an object containing configurable attributes
* Valid properties for DDProxy in addition to those in DragDrop:
* resizeFrame, centerFrame, dragElId
*/
YAHOO.util.DDProxy = function(id, sGroup, config) {
if (id) {
this.init(id, sGroup, config);
this.initFrame();
}
};
/**
* The default drag frame div id
* @property YAHOO.util.DDProxy.dragElId
* @type String
* @static
*/
YAHOO.util.DDProxy.dragElId = "ygddfdiv";
YAHOO.extend(YAHOO.util.DDProxy, YAHOO.util.DD, {
/**
* By default we resize the drag frame to be the same size as the element
* we want to drag (this is to get the frame effect). We can turn it off
* if we want a different behavior.
* @property resizeFrame
* @type boolean
*/
resizeFrame: true,
/**
* By default the frame is positioned exactly where the drag element is, so
* we use the cursor offset provided by YAHOO.util.DD. Another option that works only if
* you do not have constraints on the obj is to have the drag frame centered
* around the cursor. Set centerFrame to true for this effect.
* @property centerFrame
* @type boolean
*/
centerFrame: false,
/**
* Creates the proxy element if it does not yet exist
* @method createFrame
*/
createFrame: function() {
var self = this;
var body = document.body;
if (!body || !body.firstChild) {
setTimeout( function() { self.createFrame(); }, 50 );
return;
}
var div = this.getDragEl();
if (!div) {
div = document.createElement("div");
div.id = this.dragElId;
var s = div.style;
s.position = "absolute";
s.visibility = "hidden";
s.cursor = "move";
s.border = "2px solid #aaa";
s.zIndex = 999;
// appendChild can blow up IE if invoked prior to the window load event
// while rendering a table. It is possible there are other scenarios
// that would cause this to happen as well.
body.insertBefore(div, body.firstChild);
}
},
/**
* Initialization for the drag frame element. Must be called in the
* constructor of all subclasses
* @method initFrame
*/
initFrame: function() {
this.createFrame();
},
applyConfig: function() {
YAHOO.util.DDProxy.superclass.applyConfig.call(this);
this.resizeFrame = (this.config.resizeFrame !== false);
this.centerFrame = (this.config.centerFrame);
this.setDragElId(this.config.dragElId || YAHOO.util.DDProxy.dragElId);
},
/**
* Resizes the drag frame to the dimensions of the clicked object, positions
* it over the object, and finally displays it
* @method showFrame
* @param {int} iPageX X click position
* @param {int} iPageY Y click position
* @private
*/
showFrame: function(iPageX, iPageY) {
var el = this.getEl();
var dragEl = this.getDragEl();
var s = dragEl.style;
this._resizeProxy();
if (this.centerFrame) {
this.setDelta( Math.round(parseInt(s.width, 10)/2),
Math.round(parseInt(s.height, 10)/2) );
}
this.setDragElPos(iPageX, iPageY);
YAHOO.util.Dom.setStyle(dragEl, "visibility", "visible");
},
/**
* The proxy is automatically resized to the dimensions of the linked
* element when a drag is initiated, unless resizeFrame is set to false
* @method _resizeProxy
* @private
*/
_resizeProxy: function() {
if (this.resizeFrame) {
var DOM = YAHOO.util.Dom;
var el = this.getEl();
var dragEl = this.getDragEl();
var bt = parseInt( DOM.getStyle(dragEl, "borderTopWidth" ), 10);
var br = parseInt( DOM.getStyle(dragEl, "borderRightWidth" ), 10);
var bb = parseInt( DOM.getStyle(dragEl, "borderBottomWidth" ), 10);
var bl = parseInt( DOM.getStyle(dragEl, "borderLeftWidth" ), 10);
if (isNaN(bt)) { bt = 0; }
if (isNaN(br)) { br = 0; }
if (isNaN(bb)) { bb = 0; }
if (isNaN(bl)) { bl = 0; }
var newWidth = Math.max(0, el.offsetWidth - br - bl);
var newHeight = Math.max(0, el.offsetHeight - bt - bb);
DOM.setStyle( dragEl, "width", newWidth + "px" );
DOM.setStyle( dragEl, "height", newHeight + "px" );
}
},
// overrides YAHOO.util.DragDrop
b4MouseDown: function(e) {
var x = YAHOO.util.Event.getPageX(e);
var y = YAHOO.util.Event.getPageY(e);
this.autoOffset(x, y);
this.setDragElPos(x, y);
},
// overrides YAHOO.util.DragDrop
b4StartDrag: function(x, y) {
// show the drag frame
this.showFrame(x, y);
},
// overrides YAHOO.util.DragDrop
b4EndDrag: function(e) {
YAHOO.util.Dom.setStyle(this.getDragEl(), "visibility", "hidden");
},
// overrides YAHOO.util.DragDrop
// By default we try to move the element to the last location of the frame.
// This is so that the default behavior mirrors that of YAHOO.util.DD.
endDrag: function(e) {
var DOM = YAHOO.util.Dom;
var lel = this.getEl();
var del = this.getDragEl();
// Show the drag frame briefly so we can get its position
// del.style.visibility = "";
DOM.setStyle(del, "visibility", "");
// Hide the linked element before the move to get around a Safari
// rendering bug.
//lel.style.visibility = "hidden";
DOM.setStyle(lel, "visibility", "hidden");
YAHOO.util.DDM.moveToEl(lel, del);
//del.style.visibility = "hidden";
DOM.setStyle(del, "visibility", "hidden");
//lel.style.visibility = "";
DOM.setStyle(lel, "visibility", "");
},
toString: function() {
return ("DDProxy " + this.id);
}
});
/**
* A DragDrop implementation that does not move, but can be a drop
* target. You would get the same result by simply omitting implementation
* for the event callbacks, but this way we reduce the processing cost of the
* event listener and the callbacks.
* @class DDTarget
* @extends YAHOO.util.DragDrop
* @constructor
* @param {String} id the id of the element that is a drop target
* @param {String} sGroup the group of related DragDrop objects
* @param {object} config an object containing configurable attributes
* Valid properties for DDTarget in addition to those in
* DragDrop:
* none
*/
YAHOO.util.DDTarget = function(id, sGroup, config) {
if (id) {
this.initTarget(id, sGroup, config);
}
};
// YAHOO.util.DDTarget.prototype = new YAHOO.util.DragDrop();
YAHOO.extend(YAHOO.util.DDTarget, YAHOO.util.DragDrop, {
toString: function() {
return ("DDTarget " + this.id);
}
});