📄 dragdrop.js
字号:
YAHOO.util.DragDrop = function (id, sGroup, config) {if (id) {this.init(id, sGroup, config);}};YAHOO.util.DragDrop.prototype = {id:null, config:null, dragElId:null, handleElId:null, invalidHandleTypes:null, invalidHandleIds:null, invalidHandleClasses:null, startPageX:0, startPageY:0, groups:null, locked:false, lock:function () {this.locked = true;}, unlock:function () {this.locked = false;}, isTarget:true, padding:null, _domRef:null, __ygDragDrop:true, constrainX:false, constrainY:false, minX:0, maxX:0, minY:0, maxY:0, maintainOffset:false, xTicks:null, yTicks:null, primaryButtonOnly:true, available:false, b4StartDrag:function (x, y) {}, startDrag:function (x, y) {}, b4Drag:function (e) {}, onDrag:function (e) {}, onDragEnter:function (e, id) {}, b4DragOver:function (e) {}, onDragOver:function (e, id) {}, b4DragOut:function (e) {}, onDragOut:function (e, id) {}, b4DragDrop:function (e) {}, onDragDrop:function (e, id) {}, b4EndDrag:function (e) {}, endDrag:function (e) {}, b4MouseDown:function (e) {}, onMouseDown:function (e) {}, onMouseUp:function (e) {}, onAvailable:function () {}, getEl:function () {if (!this._domRef) {this._domRef = YAHOO.util.Dom.get(this.id);}return this._domRef;}, getDragEl:function () {return YAHOO.util.Dom.get(this.dragElId);}, init:function (id, sGroup, config) {this.initTarget(id, sGroup, config);YAHOO.util.Event.addListener(this.id, "mousedown", this.handleMouseDown, this, true);}, initTarget:function (id, sGroup, config) {this.config = config || {};this.DDM = YAHOO.util.DDM;this.groups = {};this.id = id;this.addToGroup((sGroup) ? sGroup : "default");this.handleElId = id;YAHOO.util.Event.onAvailable(id, this.handleOnAvailable, this, true);this.setDragElId(id);this.invalidHandleTypes = {A:"A"};this.invalidHandleIds = {};this.invalidHandleClasses = [];this.applyConfig();}, applyConfig:function () {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);}, handleOnAvailable:function () {this.available = true;this.resetConstraints();this.onAvailable();}, setPadding:function (iTop, iRight, iBot, iLeft) {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];}}}, 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);}, setStartPosition:function (pos) {var p = pos || YAHOO.util.Dom.getXY(this.getEl());this.deltaSetXY = null;this.startPageX = p[0];this.startPageY = p[1];}, addToGroup:function (sGroup) {this.groups[sGroup] = true;this.DDM.regDragDrop(this, sGroup);}, removeFromGroup:function (sGroup) {if (this.groups[sGroup]) {delete this.groups[sGroup];}this.DDM.removeDDFromGroup(this, sGroup);}, setDragElId:function (id) {this.dragElId = id;}, setHandleElId:function (id) {this.handleElId = id;this.DDM.regHandle(this.id, id);}, setOuterHandleElId:function (id) {YAHOO.util.Event.addListener(id, "mousedown", this.handleMouseDown, this, true);this.setHandleElId(id);}, unreg:function () {YAHOO.util.Event.removeListener(this.id, "mousedown", this.handleMouseDown);this._domRef = null;this.DDM._remove(this);}, isLocked:function () {return (this.DDM.isLocked() || this.locked);}, 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 pt = new YAHOO.util.Point(EU.getPageX(e), EU.getPageY(e));if (this.DDM.isOverTarget(pt, this)) {var srcEl = EU.getTarget(e);if (this.isValidHandleChild(srcEl) && (this.id == this.handleElId || this.DDM.handleWasClicked(srcEl, this.id))) {this.setStartPosition();this.b4MouseDown(e);this.onMouseDown(e);this.DDM.handleMouseDown(e, this);this.DDM.stopEvent(e);}}}, addInvalidHandleType:function (tagName) {var type = tagName.toUpperCase();this.invalidHandleTypes[type] = type;}, addInvalidHandleId:function (id) {this.invalidHandleIds[id] = id;}, addInvalidHandleClass:function (cssClass) {this.invalidHandleClasses.push(cssClass);}, removeInvalidHandleType:function (tagName) {var type = tagName.toUpperCase();delete this.invalidHandleTypes[type];}, removeInvalidHandleId:function (id) {delete this.invalidHandleIds[id];}, removeInvalidHandleClass:function (cssClass) {for (var i = 0, len = this.invalidHandleClasses.length; i < len; ++i) {if (this.invalidHandleClasses[i] == cssClass) {delete this.invalidHandleClasses[i];}}}, isValidHandleChild:function (node) {var valid = true;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;}, 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);}, 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);}, 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;}, clearConstraints:function () {this.constrainX = false;this.constrainY = false;this.clearTicks();}, clearTicks:function () {this.xTicks = null;this.yTicks = null;this.xTickSize = 0;this.yTickSize = 0;}, 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:function () {if (this.initPageX || this.initPageX === 0) {var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;this.setInitPosition(dx, dy);} 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);}}, getTick:function (val, tickArray) {if (!tickArray) {return val;} else {if (tickArray[0] >= val) {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];}}return tickArray[tickArray.length - 1];}}}, toString:function () {return ("DragDrop " + this.id);}};if (!YAHOO.util.DragDropMgr) {YAHOO.util.DragDropMgr = new function () {this.ids = {};this.handleIds = {};this.dragCurrent = null;this.dragOvers = {};this.deltaX = 0;this.deltaY = 0;this.preventDefault = true;this.stopPropagation = true;this.initalized = false;this.locked = false;this.init = function () {this.initialized = true;};this.POINT = 0;this.INTERSECT = 1;this.mode = this.POINT;this._execOnAll = function (sMethod, args) {for (var i in this.ids) {for (var j in this.ids[i]) {var oDD = this.ids[i][j];if (!this.isTypeOfDD(oDD)) {continue;}oDD[sMethod].apply(oDD, args);}}};this._onLoad = function () {this.init();var EU = YAHOO.util.Event;EU.on(document, "mouseup", this.handleMouseUp, this, true);EU.on(document, "mousemove", this.handleMouseMove, this, true);EU.on(window, "unload", this._onUnload, this, true);EU.on(window, "resize", this._onResize, this, true);};this._onResize = function (e) {this._execOnAll("resetConstraints", []);};this.lock = function () {this.locked = true;};this.unlock = function () {this.locked = false;};this.isLocked = function () {return this.locked;};this.locationCache = {};this.useCache = true;this.clickPixelThresh = 3;this.clickTimeThresh = 1000;this.dragThreshMet = false;this.clickTimeout = null;this.startX = 0;this.startY = 0;this.regDragDrop = function (oDD, sGroup) {if (!this.initialized) {this.init();}if (!this.ids[sGroup]) {this.ids[sGroup] = {};}this.ids[sGroup][oDD.id] = oDD;};this.removeDDFromGroup = function (oDD, sGroup) {if (!this.ids[sGroup]) {this.ids[sGroup] = {};}var obj = this.ids[sGroup];if (obj && obj[oDD.id]) {delete obj[oDD.id];}};this._remove = function (oDD) {for (var g in oDD.groups) {if (g && this.ids[g][oDD.id]) {delete this.ids[g][oDD.id];}}delete this.handleIds[oDD.id];};this.regHandle = function (sDDId, sHandleId) {if (!this.handleIds[sDDId]) {this.handleIds[sDDId] = {};}this.handleIds[sDDId][sHandleId] = sHandleId;};this.isDragDrop = function (id) {return (this.getDDById(id)) ? true : false;};this.getRelated = function (p_oDD, bTargetsOnly) {var oDDs = [];for (var i in p_oDD.groups) {for (j in this.ids[i]) {var dd = this.ids[i][j];if (!this.isTypeOfDD(dd)) {continue;}if (!bTargetsOnly || dd.isTarget) {oDDs[oDDs.length] = dd;}}}return oDDs;};this.isLegalTarget = function (oDD, oTargetDD) {var targets = this.getRelated(oDD, true);for (var i = 0, len = targets.length; i < len; ++i) {if (targets[i].id == oTargetDD.id) {return true;}}return false;};this.isTypeOfDD = function (oDD) {return (oDD && oDD.__ygDragDrop);};this.isHandle = function (sDDId, sHandleId) {return (this.handleIds[sDDId] && this.handleIds[sDDId][sHandleId]);};this.getDDById = function (id) {for (var i in this.ids) {if (this.ids[i][id]) {return this.ids[i][id];}}return null;};this.handleMouseDown = function (e, oDD) {this.currentTarget = YAHOO.util.Event.getTarget(e);this.dragCurrent = oDD;var el = oDD.getEl();this.startX = YAHOO.util.Event.getPageX(e);this.startY = YAHOO.util.Event.getPageY(e);this.deltaX = this.startX - el.offsetLeft;this.deltaY = this.startY - el.offsetTop;this.dragThreshMet = false;this.clickTimeout = setTimeout(function () {var DDM = YAHOO.util.DDM;DDM.startDrag(DDM.startX, DDM.startY);}, this.clickTimeThresh);};this.startDrag = function (x, y) {clearTimeout(this.clickTimeout);if (this.dragCurrent) {this.dragCurrent.b4StartDrag(x, y);this.dragCurrent.startDrag(x, y);}this.dragThreshMet = true;};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);};this.stopEvent = function (e) {if (this.stopPropagation) {YAHOO.util.Event.stopPropagation(e);}if (this.preventDefault) {YAHOO.util.Event.preventDefault(e);}};this.stopDrag = function (e) {if (this.dragCurrent) {if (this.dragThreshMet) {this.dragCurrent.b4EndDrag(e);this.dragCurrent.endDrag(e);}this.dragCurrent.onMouseUp(e);}this.dragCurrent = null;this.dragOvers = {};};this.handleMouseMove = function (e) {if (!this.dragCurrent) {return true;}if (YAHOO.util.Event.isIE && !e.button) {this.stopEvent(e);return this.handleMouseUp(e);}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) {this.dragCurrent.b4Drag(e);this.dragCurrent.onDrag(e);this.fireEvents(e, false);}this.stopEvent(e);return true;};this.fireEvents = function (e, isDrop) {var dc = this.dragCurrent;if (!dc || dc.isLocked()) {return;}var x = YAHOO.util.Event.getPageX(e);var y = YAHOO.util.Event.getPageY(e);var pt = new YAHOO.util.Point(x, y);var oldOvers = [];var outEvts = [];var overEvts = [];var dropEvts = [];var enterEvts = [];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)) {if (isDrop) {dropEvts.push(oDD);} else {if (!oldOvers[oDD.id]) {enterEvts.push(oDD);} else {overEvts.push(oDD);}this.dragOvers[oDD.id] = oDD;}}}}}if (this.mode) {if (outEvts.length) {dc.b4DragOut(e, outEvts);dc.onDragOut(e, outEvts);}if (enterEvts.length) {dc.onDragEnter(e, enterEvts);}if (overEvts.length) {dc.b4DragOver(e, overEvts);dc.onDragOver(e, overEvts);}if (dropEvts.length) {dc.b4DragDrop(e, dropEvts);dc.onDragDrop(e, dropEvts);}} else {var len = 0;for (i = 0, len = outEvts.length; i < len; ++i) {dc.b4DragOut(e, outEvts[i].id);dc.onDragOut(e, outEvts[i].id);}for (i = 0, len = enterEvts.length; i < len; ++i) {dc.onDragEnter(e, enterEvts[i].id);}for (i = 0, len = overEvts.length; i < len; ++i) {dc.b4DragOver(e, overEvts[i].id);dc.onDragOver(e, overEvts[i].id);}for (i = 0, len = dropEvts.length; i < len; ++i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -