📄 dragdrop.js
字号:
*/
getEl: function() {
if (!this._domRef) {
this._domRef = YAHOO.util.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
*
* @return {HTMLElement} the html element
*/
getDragEl: function() {
return YAHOO.util.Dom.get(this.dragElId);
},
/**
* Sets up the DragDrop object. Must be called in the constructor of any
* YAHOO.util.DragDrop subclass
*
* @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);
YAHOO.util.Event.addListener(this.id, "mousedown",
this.handleMouseDown, this, true);
},
/**
* Initializes Targeting functionality only... the object does not
* get a mousedown handler.
*
* @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 = {};
// 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;
YAHOO.util.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.
*/
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
* @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.
* @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 dd element
*/
setInitPosition: function(diffX, diffY) {
var el = this.getEl();
if (!this.DDM.verifyEl(el)) {
return;
}
var dx = diffX || 0;
var dy = diffY || 0;
var p = YAHOO.util.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.
* @param pos current position (from previous lookup)
* @private
*/
setStartPosition: function(pos) {
var p = pos || YAHOO.util.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.
*
* @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
* @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
*
* @param id 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.
*
* @param id the id of the element that will be used to initiate the drag
*/
setHandleElId: function(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
*/
setOuterHandleElId: function(id) {
YAHOO.util.Event.addListener(id, "mousedown",
this.handleMouseDown, this, true);
this.setHandleElId(id);
},
/**
* Remove all drag and drop hooks for this element
*/
unreg: function() {
YAHOO.util.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.)
*
* @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
*
* @param {Event} e
* @param {YAHOO.util.DragDrop} oDD the clicked dd object (this dd obj)
* @private
*/
handleMouseDown: function(e, oDD) {
var EU = YAHOO.util.Event;
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(EU.getPageX(e), EU.getPageY(e));
if ( !this.DDM.isOverTarget(pt, this) ) {
} else {
// check to see if the handle was clicked
var srcEl = EU.getTarget(e);
if (this.isValidHandleChild(srcEl) &&
(this.id == this.handleElId ||
this.DDM.handleWasClicked(srcEl, this.id)) ) {
// set the initial element position
this.setStartPosition();
this.b4MouseDown(e);
this.onMouseDown(e);
this.DDM.handleMouseDown(e, this);
this.DDM.stopEvent(e);
}
}
},
/**
* 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.
*
* @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
* @param {string} id the element id of the element you wish to ignore
*/
addInvalidHandleId: function(id) {
this.invalidHandleIds[id] = id;
},
/**
* Lets you specify a css class of elements that will not initiate a drag
* @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
*
* @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
* @param {string} the id of the element to re-enable
*/
removeInvalidHandleId: function(id) {
delete this.invalidHandleIds[id];
},
/**
* Unsets an invalid css class
* @param {string} the class of the element(s) you wish to re-enable
*/
removeInvalidHandleClass: function(cssClass) {
for (var i=0, len=this.invalidHandleClasses.length; i<len; ++i) {
if (this.invalidHandleClasses[i] == cssClass) {
delete this.invalidHandleClasses[i];
}
}
},
/**
* Checks the tag exclusion list to see if this click should be ignored
*
* @param {ygNode} node
* @return {boolean} true if this is a valid tag type, false if not
*/
isValidHandleChild: function(node) {
var valid = true;
// var n = (node.nodeName == "#text") ? node.parentNode : node;
var nodeName;
try {
nodeName = node.nodeName.toUpperCase();
} catch(e) {
nodeName = node.nodeName;
}
valid = valid && !this.invalidHandleTypes[nodeName];
valid = valid && !this.invalidHandleIds[node.id];
for (var i=0, len=this.invalidHandleClasses.length; valid && i<len; ++i) {
valid = !YAHOO.util.Dom.hasClass(node, this.invalidHandleClasses[i]);
}
return valid;
},
/**
* Create the array of horizontal tick marks if an interval was specified
* in setXConstraint().
*
* @private
*/
setXTicks: function(iStartX, iTickSize) {
this.xTicks = [];
this.xTickSize = iTickSize;
var tickMap = {};
for (var i = this.initPageX; i >= this.minX; i = i - iTickSize) {
if (!tickMap[i]) {
this.xTicks[this.xTicks.length] = i;
tickMap[i] = true;
}
}
for (i = this.initPageX; i <= this.maxX; i = i + iTickSize) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -