⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dragdrop.js

📁 YahooUI,搞WEB开发的朋友值得看一看
💻 JS
📖 第 1 页 / 共 5 页
字号:
            if (this.dragCurrent) {                this.dragCurrent.b4StartDrag(x, y);                this.dragCurrent.startDrag(x, y);            }            this.dragThreshMet = true;        };        /**         * Internal function to handle the mouseup event.  Will be invoked         * from the context of the document.         *         * @param {Event} e the event         * @private         */        this.handleMouseUp = function(e) {            if (! this.dragCurrent) {                return;            }            clearTimeout(this.clickTimeout);            if (this.dragThreshMet) {                this.fireEvents(e, true);            } else {            }            this.stopDrag(e);            this.stopEvent(e);        };        /**         * Utility to stop event propagation and event default, if these         * features are turned on.         *         * @param {Event} e the event as returned by this.getEvent()         */        this.stopEvent = function(e) {            if (this.stopPropagation) {                UTIL.Event.stopPropagation(e);            }            if (this.preventDefault) {                UTIL.Event.preventDefault(e);            }        };        /**         * Internal function to clean up event handlers after the drag         * operation is complete         *         * @param {Event} e the event         * @private         */        this.stopDrag = function(e) {            // Fire the drag end event for the item that was dragged            if (this.dragCurrent) {                if (this.dragThreshMet) {                    this.dragCurrent.b4EndDrag(e);                    this.dragCurrent.endDrag(e);                }                this.dragCurrent.onMouseUp(e);            }            this.dragCurrent = null;            this.dragOvers = {};        };        /**         * Internal function to handle the mousemove event.  Will be invoked         * from the context of the html element.         *         * @TODO figure out what we can do about mouse events lost when the         * user drags objects beyond the window boundary.  Currently we can         * detect this in internet explorer by verifying that the mouse is         * down during the mousemove event.  Firefox doesn't give us the         * button state on the mousemove event.         *         * @param {Event} e the event         * @private         */        this.handleMouseMove = function(e) {            if (! this.dragCurrent) {                return;            }            // var button = e.which || e.button;            // check for IE mouseup outside of page boundary            if (UTIL.Event.isIE && !e.button) {                this.stopEvent(e);                return this.handleMouseUp(e);            }            if (!this.dragThreshMet) {                var diffX = Math.abs(this.startX - UTIL.Event.getPageX(e));                var diffY = Math.abs(this.startY - UTIL.Event.getPageY(e));                if (diffX > this.clickPixelThresh ||                            diffY > this.clickPixelThresh) {                    this.startDrag(this.startX, this.startY);                }            }            if (this.dragThreshMet) {                this.dragCurrent.b4Drag(e);                this.dragCurrent.onDrag(e);                this.fireEvents(e, false);            }            this.stopEvent(e);        };        /**         * Iterates over all of the DragDrop elements to find ones we are         * hovering over or dropping on         *         * @param {Event} e the event         * @param {boolean} isDrop is this a drop op or a mouseover op?         * @private         */        this.fireEvents = function(e, isDrop) {            var dc = this.dragCurrent;            // If the user did the mouse up outside of the window, we could            // get here even though we have ended the drag.            if (!dc || dc.isLocked()) {                return;            }            var x = UTIL.Event.getPageX(e);            var y = UTIL.Event.getPageY(e);            var pt = new YAHOO.util.Point(x,y);            // cache the previous dragOver array            var oldOvers = [];            var outEvts   = [];            var overEvts  = [];            var dropEvts  = [];            var enterEvts = [];            // Check to see if the object we were hovering over is no longer            // being hovered over so we can fire the onDragOut event            for (var i in this.dragOvers) {                var ddo = this.dragOvers[i];                if (! this.isTypeOfDD(ddo)) {                    continue;                }                if (! this.isOverTarget(pt, ddo, this.mode)) {                    outEvts.push( ddo );                }                oldOvers[i] = true;                delete this.dragOvers[i];            }            for (var sGroup in dc.groups) {                if ("string" != typeof sGroup) {                    continue;                }                for (i in this.ids[sGroup]) {                    var oDD = this.ids[sGroup][i];                    if (! this.isTypeOfDD(oDD)) {                        continue;                    }                    if (oDD.isTarget && !oDD.isLocked() && oDD != dc) {                        if (this.isOverTarget(pt, oDD, this.mode)) {                            // look for drop interactions                            if (isDrop) {                                dropEvts.push( oDD );                            // look for drag enter and drag over interactions                            } else {                                // initial drag over: dragEnter fires                                if (!oldOvers[oDD.id]) {                                    enterEvts.push( oDD );                                // subsequent drag overs: dragOver fires                                } else {                                    overEvts.push( oDD );                                }                                this.dragOvers[oDD.id] = oDD;                            }                        }                    }                }            }            if (this.mode) {                if (outEvts.length > 0) {                    dc.b4DragOut(e, outEvts);                    dc.onDragOut(e, outEvts);                }                if (enterEvts.length > 0) {                    dc.onDragEnter(e, enterEvts);                }                if (overEvts.length > 0) {                    dc.b4DragOver(e, overEvts);                    dc.onDragOver(e, overEvts);                }                if (dropEvts.length > 0) {                    dc.b4DragDrop(e, dropEvts);                    dc.onDragDrop(e, dropEvts);                }            } else {                // fire dragout events                for (i=0; i < outEvts.length; ++i) {                    dc.b4DragOut(e, outEvts[i].id);                    dc.onDragOut(e, outEvts[i].id);                }                // fire enter events                for (i=0; i < enterEvts.length; ++i) {                    // dc.b4DragEnter(e, oDD.id);                    dc.onDragEnter(e, enterEvts[i].id);                }                // fire over events                for (i=0; i < overEvts.length; ++i) {                    dc.b4DragOver(e, overEvts[i].id);                    dc.onDragOver(e, overEvts[i].id);                }                // fire drop events                for (i=0; i < dropEvts.length; ++i) {                    dc.b4DragDrop(e, dropEvts[i].id);                    dc.onDragDrop(e, dropEvts[i].id);                }            }        };        /**         * Helper function for getting the best match from the list of drag         * and drop objects returned by the drag and drop events when we are         * in INTERSECT mode.  It returns either the first object that the         * cursor is over, or the object that has the greatest overlap with         * the dragged element.         *         * @param  {ygDragDrop[]} dds The array of drag and drop objects         * targeted         * @return {ygDragDrop}       The best single match         */        this.getBestMatch = function(dds) {            var winner = null;            // Return null if the input is not what we expect            //if (!dds || !dds.length || dds.length == 0) {               // winner = null;            // If there is only one item, it wins            //} else if (dds.length == 1) {            if (dds.length == 1) {                winner = dds[0];            } else {                // Loop through the targeted items                for (var i=0; i<dds.length; ++i) {                    var dd = dds[i];                    // If the cursor is over the object, it wins.  If the                    // cursor is over multiple matches, the first one we come                    // to wins.                    if (dd.cursorIsOver) {                        winner = dd;                        break;                    // Otherwise the object with the most overlap wins                    } else {                        if (!winner ||                            winner.overlap.getArea() < dd.overlap.getArea()) {                            winner = dd;                        }                    }                }            }            return winner;        };        /**         * Refreshes the cache of the top-left and bottom-right points of the         * drag and drop objects in the specified groups         *         * @param {Array} aGroups an associative array of groups to refresh         */        this.refreshCache = function(aGroups) {            for (sGroup in aGroups) {                if ("string" != typeof sGroup) {                    continue;                }                for (i in this.ids[sGroup]) {                    var oDD = this.ids[sGroup][i];                    if (this.isTypeOfDD(oDD)) {                        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         * @param {HTMLElement} el the element to check         * @return {boolean} true if the element looks usable         */        this.verifyEl = function(el) {            try {                if (el) {                    var parent = el.offsetParent;                    if (parent) {                        return true;                    }                }            } catch(e) {            }            return false;        };        /**         * Returns the an array containing the drag and drop element's position         * and size, including the ygDragDrop.padding configured for it         *         * @param {ygDragDrop} oDD the drag and drop object to get the         * location for         * @return array containing the top left and bottom right points of the         * element         */        this.getLocation = function(oDD) {            if (! this.isTypeOfDD(oDD)) {                return null;            }            var el = oDD.getEl();            if (!this.verifyEl(el)) {                return null;            }            // var aPos = ygPos.getPos(el);            var aPos = YAHOO.util.Dom.getXY(el);            x1 = aPos[0];            x2 = x1 + el.offsetWidth;            y1 = aPos[1];            y2 = y1 + el.offsetHeight;            var t = y1 - oDD.padding[0];            var r = x2 + oDD.padding[1];            var b = y2 + oDD.padding[2];            var 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         *         * @param {YAHOO.util.Point} pt The point to evaluate         * @param {ygDragDrop} oDDTarget the DragDrop object we are inspecting         * @return {boolean} true if the mouse is over the target         * @private         */        this.isOverTarget = function(pt, oDDTarget, intersect) {            // use cache if available            var loc = this.locationCache[oDDTarget.id];            if (!loc || !this.useCache) {                loc = this.getLocation(oDDTarget);                this.locationCache[oDDTarget.id] = loc;            }            // var cursorIsOver =  (x >= loc[3] && x <= loc[1] && y >= loc[0] && y <= loc[2]);            //oDDTarget.cursorIsOver = loc.contains( new YAHOO.util.Point(x, y) );            oDDTarget.cursorIsOver = loc.contains( pt );            oDDTarget.overlap = null;            // if (this.INTERSECT == this.mode) {            if (intersect) {                var curRegion =                    YAHOO.util.Region.getRegion(this.dragCurrent.getDragEl());                var overlap = curRegion.intersect(loc);                if (overlap) {                    oDDTarget.overlap = overlap;                    return true;                } else {                    return false;                }            } else {                return oDDTarget.cursorIsOver;            }        };        /**         * @private         */        this._onUnload = function(e, me) {            this.unregAll();        };

⌨️ 快捷键说明

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