📄 dragdrop.js
字号:
* 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. * @method handleMouseMove * @param {Event} e the event * @private * @static */ handleMouseMove: function(e) { var dc = this.dragCurrent; if (dc) { // var button = e.which || e.button; // check for IE mouseup outside of page boundary if (YAHOO.util.Event.isIE && !e.button) { this.stopEvent(e); return this.handleMouseUp(e); } else { if (e.clientX < 0 || e.clientY < 0) { //This will stop the element from leaving the viewport in FF, Opera & Safari //Not turned on yet //this.stopEvent(e); //return false; } } if (!this.dragThreshMet) { var diffX = Math.abs(this.startX - YAHOO.util.Event.getPageX(e)); var diffY = Math.abs(this.startY - YAHOO.util.Event.getPageY(e)); if (diffX > this.clickPixelThresh || diffY > this.clickPixelThresh) { this.startDrag(this.startX, this.startY); } } if (this.dragThreshMet) { if (dc && dc.events.b4Drag) { dc.b4Drag(e); dc.fireEvent('b4DragEvent', { e: e}); } if (dc && dc.events.drag) { dc.onDrag(e); dc.fireEvent('dragEvent', { e: e}); } if (dc) { this.fireEvents(e, false); } } this.stopEvent(e); } }, /** * Iterates over all of the DragDrop elements to find ones we are * hovering over or dropping on * @method fireEvents * @param {Event} e the event * @param {boolean} isDrop is this a drop op or a mouseover op? * @private * @static */ 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 the config option dragOnly is true, bail out and don't fire the events if (!dc || dc.isLocked() || dc.dragOnly) { return; } var x = YAHOO.util.Event.getPageX(e), y = YAHOO.util.Event.getPageY(e), pt = new YAHOO.util.Point(x,y), pos = dc.getTargetCoord(pt.x, pt.y), el = dc.getDragEl(), events = ['out', 'over', 'drop', 'enter'], curRegion = new YAHOO.util.Region( pos.y, pos.x + el.offsetWidth, pos.y + el.offsetHeight, pos.x ), oldOvers = [], // cache the previous dragOver array inGroupsObj = {}, inGroups = [], data = { outEvts: [], overEvts: [], dropEvts: [], enterEvts: [] }; // Check to see if the object(s) 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, curRegion)) { data.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, curRegion)) { inGroupsObj[sGroup] = true; // look for drop interactions if (isDrop) { data.dropEvts.push( oDD ); // look for drag enter and drag over interactions } else { // initial drag over: dragEnter fires if (!oldOvers[oDD.id]) { data.enterEvts.push( oDD ); // subsequent drag overs: dragOver fires } else { data.overEvts.push( oDD ); } this.dragOvers[oDD.id] = oDD; } } } } } this.interactionInfo = { out: data.outEvts, enter: data.enterEvts, over: data.overEvts, drop: data.dropEvts, point: pt, draggedRegion: curRegion, sourceRegion: this.locationCache[dc.id], validDrop: isDrop }; for (var inG in inGroupsObj) { inGroups.push(inG); } // notify about a drop that did not find a target if (isDrop && !data.dropEvts.length) { this.interactionInfo.validDrop = false; if (dc.events.invalidDrop) { dc.onInvalidDrop(e); dc.fireEvent('invalidDropEvent', { e: e }); } } for (i = 0; i < events.length; i++) { var tmp = null; if (data[events[i] + 'Evts']) { tmp = data[events[i] + 'Evts']; } if (tmp && tmp.length) { var type = events[i].charAt(0).toUpperCase() + events[i].substr(1), ev = 'onDrag' + type, b4 = 'b4Drag' + type, cev = 'drag' + type + 'Event', check = 'drag' + type; if (this.mode) { if (dc.events[b4]) { dc[b4](e, tmp, inGroups); dc.fireEvent(b4 + 'Event', { event: e, info: tmp, group: inGroups }); } if (dc.events[check]) { dc[ev](e, tmp, inGroups); dc.fireEvent(cev, { event: e, info: tmp, group: inGroups }); } } else { for (var b = 0, len = tmp.length; b < len; ++b) { if (dc.events[b4]) { dc[b4](e, tmp[b].id, inGroups[0]); dc.fireEvent(b4 + 'Event', { event: e, info: tmp[b].id, group: inGroups[0] }); } if (dc.events[check]) { dc[ev](e, tmp[b].id, inGroups[0]); dc.fireEvent(cev, { event: e, info: tmp[b].id, group: inGroups[0] }); } } } } } }, /** * 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. * @method getBestMatch * @param {DragDrop[]} dds The array of drag and drop objects * targeted * @return {DragDrop} The best single match * @static */ getBestMatch: function(dds) { var winner = null; var len = dds.length; if (len == 1) { winner = dds[0]; } else { // Loop through the targeted items for (var i=0; i<len; ++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 (this.mode == this.INTERSECT && dd.cursorIsOver) { winner = dd; break; // Otherwise the object with the most overlap wins } else { if (!winner || !winner.overlap || (dd.overlap && 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 group(s). This is in the * format that is stored in the drag and drop instance, so typical * usage is: * <code> * YAHOO.util.DragDropMgr.refreshCache(ddinstance.groups); * </code> * Alternatively: * <code> * YAHOO.util.DragDropMgr.refreshCache({group1:true, group2:true}); * </code> * @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) { // refresh everything if group array is not provided var g = groups || this.ids; for (var sGroup in g) { if ("string" != typeof sGroup) { continue; } for (var 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 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 * @param {boolean} intersect true if we are in intersect mode * @param {YAHOO.util.Region} pre-cached location of the dragged element * @return {boolean} true if the mouse is over the target * @private * @static */ isOverTarget: function(pt, oTarget, intersect, curRegion) { // 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 done. Otherwise we need to evaluate the // region the target as occupies to determine if the dragged element // overlaps with it. var dc = this.dragCurrent; if (!dc || (!intersect && !dc.constrainX && !dc.constrainY)) { //if (oTarget.cursorIsOver) { //} 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -