📄 dragdrop.js
字号:
/** * Cleans up the drag and drop events and objects. * * @private */ this.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 * * @private */ this.elementCache = {}; /** * Get the wrapper for the DOM element specified * * @param {String} id the id of the elment to get * @return {YAHOO.util.DDM.ElementWrapper} the wrapped element * @private */ this.getElWrapper = function(id) { var oWrapper = this.elementCache[id]; if (!oWrapper || !oWrapper.el) { oWrapper = this.elementCache[id] = new this.ElementWrapper(document.getElementById(id)); } return oWrapper; }; /** * Returns the actual DOM element * * @param {String} id the id of the elment to get * @return {Object} The element */ this.getElement = function(id) { // return this.getElWrapper(id).el; return document.getElementById(id); }; /** * Returns the style property for the DOM element (i.e., * document.getElById(id).style) * * @param {String} id the id of the elment to get * @return {Object} The style property of the element */ this.getCss = function(id) { // return this.getElWrapper(id).css; var css = null; var el = document.getElementById(id); if (el) { css = el.style; } return css; }; /** * Inner class for cached elements */ this.ElementWrapper = function(el) { /** * @private */ this.el = el || null; /** * @private */ this.id = this.el && el.id; /** * @private */ this.css = this.el && el.style; }; /** * Returns the X position of an html element * @param el the element for which to get the position * @return {int} the X coordinate */ this.getPosX = function(el) { return YAHOO.util.Dom.getX(el); }; /** * Returns the Y position of an html element * @param el the element for which to get the position * @return {int} the Y coordinate */ this.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 * * @param n1 the first node to swap * @param n2 the other node to swap */ this.swapNode = function(n1, n2) { if (n1.swapNode) { n1.swapNode(n2); } else { // the node reference order for the swap is a little tricky. var p = n2.parentNode; var s = n2.nextSibling; n1.parentNode.replaceChild(n2,n1); p.insertBefore(n1,s); } }; /** * @private */ this.getScroll = function () { var t, l; if (document.documentElement && document.documentElement.scrollTop) { t = document.documentElement.scrollTop; l = document.documentElement.scrollLeft; } else if (document.body) { t = document.body.scrollTop; l = document.body.scrollLeft; } return { top: t, left: l }; }; /** * Returns the specified element style property * @param {HTMLElement} el the element * @param {string} styleProp the style property * @return {string} The value of the style property */ this.getStyle = function(el, styleProp) { if (el.style.styleProp) { return el.style.styleProp; } else if (el.currentStyle) { return el.currentStyle[styleProp]; } else if (document.defaultView) { return document.defaultView.getComputedStyle(el, null). getPropertyValue(styleProp); } }; /** * Gets the scrollTop * * @return {int} the document's scrollTop */ this.getScrollTop = function () { return this.getScroll().top; }; /** * Gets the scrollLeft * * @return {int} the document's scrollTop */ this.getScrollLeft = function () { return this.getScroll().left; }; this.moveToEl = function (moveEl, targetEl) { var aCoord = YAHOO.util.Dom.getXY(targetEl); YAHOO.util.Dom.setXY(moveEl, aCoord); }; /** * Gets the client height * * @return {int} client height in px */ this.getClientHeight = function() { return (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight; }; /** * Gets the client width * * @return {int} client width in px */ this.getClientWidth = function() { return (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth; }; /** * numeric array sort function */ this.numericSort = function(a, b) { return (a - b); }; /** * @private */ this._timeoutCount = 0; /** * @private * Trying to make the load order less important. Without this we get * an error if this file is loaded before the Event Utility. */ this._addListeners = function() { if ( UTIL.Event && document && document.body ) { this._onLoad(); } else { if (this._timeoutCount > 500) { } else { setTimeout("YAHOO.util.DDM._addListeners()", 10); this._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. * * @param node the html element to inspect */ this.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();}/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. *//** * @class a DragDrop implementation where the linked element follows the * mouse cursor. * * @extends DragDrop * @constructor * @param {String} id the id of the linked element * @param {String} sGroup the group of related DragDrop items */YAHOO.util.DD = function(id, sGroup) { if (id) { this.init(id, sGroup); }};YAHOO.util.DD.prototype = new YAHOO.util.DragDrop();/** * Should we auto-scroll? Defaults to true * * @type boolean */YAHOO.util.DD.prototype.scroll = true;/** * Sets the pointer offset to the distance between the linked element's top * left corner and the location the element was clicked * * @param {int} iPageX the X coordinate of the click * @param {int} iPageY the Y coordinate of the click */YAHOO.util.DD.prototype.autoOffset = function(iPageX, iPageY) { var el = this.getEl(); var aCoord = YAHOO.util.Dom.getXY(el); var x = iPageX - aCoord[0]; var y = iPageY - aCoord[1]; 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 ygDDSliderBG) * * @param {int} iDeltaX the distance from the left * @param {int} iDeltaY the distance from the top */YAHOO.util.DD.prototype.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. * * @param {int} iPageX the X coordinate of the mousedown or drag event * @param {int} iPageY the Y coordinate of the mousedown or drag event */YAHOO.util.DD.prototype.setDragElPos = function(iPageX, iPageY) { this.alignElWithMouse(this.getDragEl(), 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. * * @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 */YAHOO.util.DD.prototype.alignElWithMouse = function(el, iPageX, iPageY) { var oCoord = this.getTargetCoord(iPageX, iPageY); var aCoord = [oCoord.x, oCoord.y]; YAHOO.util.Dom.setXY(el, aCoord); 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. */YAHOO.util.DD.prototype.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. * * @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 */YAHOO.util.DD.prototype.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 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -