dragdrop.js

来自「国外很不错的一个开源OA系统Group-Office」· JavaScript 代码 · 共 2,084 行 · 第 1/5 页

JS
2,084
字号
     */    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<len; ++i) {            if (this.invalidHandleClasses[i] == cssClass) {                delete this.invalidHandleClasses[i];            }        }    },    /**     * Checks the tag exclusion list to see if this click should be ignored     * @method isValidHandleChild     * @param {HTMLElement} node the HTMLElement to evaluate     * @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 = !Dom.hasClass(node, this.invalidHandleClasses[i]);        }        return valid;    },    /**     * Create the array of horizontal tick marks if an interval was specified     * in setXConstraint().     * @method setXTicks     * @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) {            if (!tickMap[i]) {                this.xTicks[this.xTicks.length] = i;                tickMap[i] = true;            }        }        this.xTicks.sort(this.DDM.numericSort) ;    },    /**     * Create the array of vertical tick marks if an interval was specified in     * setYConstraint().     * @method setYTicks     * @private     */    setYTicks: function(iStartY, iTickSize) {        this.yTicks = [];        this.yTickSize = iTickSize;        var tickMap = {};        for (var i = this.initPageY; i >= this.minY; i = i - iTickSize) {            if (!tickMap[i]) {                this.yTicks[this.yTicks.length] = i;                tickMap[i] = true;            }        }        for (i = this.initPageY; i <= this.maxY; i = i + iTickSize) {            if (!tickMap[i]) {                this.yTicks[this.yTicks.length] = i;                tickMap[i] = true;            }        }        this.yTicks.sort(this.DDM.numericSort) ;    },    /**     * By default, the element can be dragged any place on the screen.  Use     * this method to limit the horizontal travel of the element.  Pass in     * 0,0 for the parameters if you want to lock the drag to the y axis.     * @method setXConstraint     * @param {int} iLeft the number of pixels the element can move to the left     * @param {int} iRight the number of pixels the element can move to the     * right     * @param {int} iTickSize optional parameter for specifying that the     * element     * should move iTickSize pixels at a time.     */    setXConstraint: function(iLeft, iRight, iTickSize) {        this.leftConstraint = iLeft;        this.rightConstraint = iRight;        this.minX = this.initPageX - iLeft;        this.maxX = this.initPageX + iRight;        if (iTickSize) { this.setXTicks(this.initPageX, iTickSize); }        this.constrainX = true;    },    /**     * Clears any constraints applied to this instance.  Also clears ticks     * since they can't exist independent of a constraint at this time.     * @method clearConstraints     */    clearConstraints: function() {        this.constrainX = false;        this.constrainY = false;        this.clearTicks();    },    /**     * Clears any tick interval defined for this instance     * @method clearTicks     */    clearTicks: function() {        this.xTicks = null;        this.yTicks = null;        this.xTickSize = 0;        this.yTickSize = 0;    },    /**     * By default, the element can be dragged any place on the screen.  Set     * this to limit the vertical travel of the element.  Pass in 0,0 for the     * parameters if you want to lock the drag to the x axis.     * @method setYConstraint     * @param {int} iUp the number of pixels the element can move up     * @param {int} iDown the number of pixels the element can move down     * @param {int} iTickSize optional parameter for specifying that the     * element should move iTickSize pixels at a time.     */    setYConstraint: function(iUp, iDown, iTickSize) {        this.topConstraint = iUp;        this.bottomConstraint = iDown;        this.minY = this.initPageY - iUp;        this.maxY = this.initPageY + iDown;        if (iTickSize) { this.setYTicks(this.initPageY, iTickSize); }        this.constrainY = true;    },    /**     * resetConstraints must be called if you manually reposition a dd element.     * @method resetConstraints     * @param {boolean} maintainOffset     */    resetConstraints: function() {        // Maintain offsets if necessary        if (this.initPageX || this.initPageX === 0) {            // figure out how much this thing has moved            var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;            var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;            this.setInitPosition(dx, dy);        // This is the first time we have detected the element's position        } else {            this.setInitPosition();        }        if (this.constrainX) {            this.setXConstraint( this.leftConstraint,                                 this.rightConstraint,                                 this.xTickSize        );        }        if (this.constrainY) {            this.setYConstraint( this.topConstraint,                                 this.bottomConstraint,                                 this.yTickSize         );        }    },    /**     * Normally the drag element is moved pixel by pixel, but we can specify     * that it move a number of pixels at a time.  This method resolves the     * location when we have it set up like this.     * @method getTick     * @param {int} val where we want to place the object     * @param {int[]} tickArray sorted array of valid points     * @return {int} the closest tick     * @private     */    getTick: function(val, tickArray) {        if (!tickArray) {            // If tick interval is not defined, it is effectively 1 pixel,            // so we return the value passed to us.            return val;        } else if (tickArray[0] >= val) {            // The value is lower than the first tick, so we return the first            // tick.            return tickArray[0];        } else {            for (var i=0, len=tickArray.length; i<len; ++i) {                var next = i + 1;                if (tickArray[next] && tickArray[next] >= val) {                    var diff1 = val - tickArray[i];                    var diff2 = tickArray[next] - val;                    return (diff2 > diff1) ? tickArray[i] : tickArray[next];                }            }            // The value is larger than the last tick, so we return the last            // tick.            return tickArray[tickArray.length - 1];        }    },    /**     * toString method     * @method toString     * @return {string} string representation of the dd obj     */    toString: function() {        return ("DragDrop " + this.id);    }};})();/** * The drag and drop utility provides a framework for building drag and drop * applications.  In addition to enabling drag and drop for specific elements, * the drag and drop elements are tracked by the manager class, and the * interactions between the various elements are tracked during the drag and * the implementing code is notified about these important moments. * @module dragdrop * @title Drag and Drop * @requires yahoo,dom,event * @namespace YAHOO.util */// Only load the library once.  Rewriting the manager class would orphan// existing drag and drop instances.if (!YAHOO.util.DragDropMgr) {/** * DragDropMgr is a singleton that tracks the element interaction for * all DragDrop items in the window.  Generally, you will not call * this class directly, but it does have helper methods that could * be useful in your DragDrop implementations. * @class DragDropMgr * @static */YAHOO.util.DragDropMgr = function() {    var Event = YAHOO.util.Event;    return {        /**         * Two dimensional Array of registered DragDrop objects.  The first         * dimension is the DragDrop item group, the second the DragDrop         * object.         * @property ids         * @type {string: string}         * @private         * @static         */        ids: {},        /**         * Array of element ids defined as drag handles.  Used to determine         * if the element that generated the mousedown event is actually the         * handle and not the html element itself.         * @property handleIds         * @type {string: string}         * @private         * @static         */        handleIds: {},        /**         * the DragDrop object that is currently being dragged         * @property dragCurrent         * @type DragDrop         * @private         * @static         **/        dragCurrent: null,        /**         * the DragDrop object(s) that are being hovered over         * @property dragOvers         * @type Array         * @private         * @static         */        dragOvers: {},        /**         * the X distance between the cursor and the object being dragged         * @property deltaX         * @type int         * @private         * @static         */        deltaX: 0,        /**         * the Y distance between the cursor and the object being dragged         * @property deltaY         * @type int         * @private         * @static         */        deltaY: 0,        /**         * Flag to determine if we should prevent the default behavior of the         * events we define. By default this is true, but this can be set to         * false if you need the default behavior (not recommended)         * @property preventDefault         * @type boolean         * @static         */        preventDefault: true,        /**         * Flag to determine if we should stop the propagation of the events         * we generate. This is true by default but you may want to set it to         * false if the html element contains other features that require the         * mouse click.         * @property stopPropagation         * @type boolean         * @static         */        stopPropagation: true,        /**         * Internal flag that is set to true when drag and drop has been         * intialized         * @property initialized         * @private         * @static         */        initalized: false,        /**         * All drag and drop can be disabled.         * @property locked         * @private         * @static         */        locked: false,        /**         * Called the first time an element is registered.         * @method init         * @private         * @static         */        init: function() {            this.initialized = true;        },        /**         * In point mode, drag and drop interaction is defined by the         * location of the cursor during the drag/drop         * @property POINT         * @type int         * @static         */        POINT: 0,        /**         * In intersect mode, drag and drop interactio nis defined by the         * overlap of two or more drag and drop objects.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?