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

📄 utils.js

📁 尚洋仓库管理系统,对仓库
💻 JS
📖 第 1 页 / 共 3 页
字号:
/** The Zapatec DHTML utils library** Copyright (c) 2004-2005 by Zapatec, Inc.* http://www.zapatec.com* 1700 MLK Way, Berkeley, California,* 94709, U.S.A.* All rights reserved.* $Id: utils.js 2245 2006-03-24 03:30:17Z ken $*** Various utility functions*/// This can be defined in other modulesif (typeof Zapatec == 'undefined') {  /// define the global Zapatec namespace  Zapatec = {};}/// define the Utils namespaceZapatec.Utils = {};/// Retrieves the absolute position (relative to <body>) of a given element.////// If it doesn't work in IE 6, try this:/// \code/// setTimeout(function() {///   var objPos = Zapatec.Utils.getAbsolutePos(objElement);///   do something with objPos/// }, 0);/// \endcode////// @param el [HTMLElement] reference to the element./// @return [object] { x, y } containing the position.Zapatec.Utils.getAbsolutePos = function(el) {	var SL = 0, ST = 0;	var is_div = /^div$/i.test(el.tagName);	if (is_div && el.scrollLeft)		SL = el.scrollLeft;	if (is_div && el.scrollTop)		ST = el.scrollTop;	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };	if (el.offsetParent) {		var tmp = this.getAbsolutePos(el.offsetParent);		r.x += tmp.x;		r.y += tmp.y;	}	return r;};/// Modify the position of a box to fit in browser's view.  This function will/// modify the passed object itself, so it doesn't need to return a value.////// @param [object] box { x, y, width, height } specifying the area.Zapatec.Utils.fixBoxPosition = function(box) {	if (box.x < 0)		box.x = 0;	if (box.y < 0)		box.y = 0;	var cp = Zapatec.Utils.createElement("div");	var s = cp.style;	s.position = "absolute";	s.right = s.bottom = s.width = s.height = "0px";	window.document.body.appendChild(cp);	var br = Zapatec.Utils.getAbsolutePos(cp);	window.document.body.removeChild(cp);	if (Zapatec.is_ie) {		br.y += window.document.body.scrollTop;		br.x += window.document.body.scrollLeft;	} else {		br.y += window.scrollY;		br.x += window.scrollX;	}	var tmp = box.x + box.width - br.x;	if (tmp > 0) box.x -= tmp;	tmp = box.y + box.height - br.y;	if (tmp > 0) box.y -= tmp;};/// Determines if an event is related to a certain element.  This is a poor/// substitute for some events that are missing from DOM since forever (like/// onenter, onleave, which MSIE provides).  Basically onmouseover and/// onmouseout are fired even if the mouse was already in the element but moved/// from text to a blank area, so in order not to close a popup element when/// onmouseout occurs in this situation, one would need to first check if the/// event is not related to that popup element:////// \code///      function handler_onMouseOut(event) {///         if (!Zapatec.Utils.isRelated(this, event)) {///            /// can safely hide it now///            this.style.display = "none";///         }///      }/// \endcode////// @param el [HTMLElement] reference to the element to check the event against/// @param evt [Event] reference to the Event object/// @return [boolean] true if the event is related to the elementZapatec.Utils.isRelated = function (el, evt) {	evt || (evt = window.event);	var related = evt.relatedTarget;	if (!related) {		var type = evt.type;		if (type == "mouseover") {			related = evt.fromElement;		} else if (type == "mouseout") {			related = evt.toElement;		}	}	try {		while (related) {			if (related == el) {				return true;			}			related = related.parentNode;		}	} catch(e) {};	return false;};/// Remove a certain [CSS] class from the given element./// @param el [HTMLElement] reference to the element./// @param className [string] the class to remove.Zapatec.Utils.removeClass = function(el, className) {	if (!(el && el.className)) {		return;	}	var cls = el.className.split(" ");	var ar = [];	for (var i = cls.length; i > 0;) {		if (cls[--i] != className) {			ar[ar.length] = cls[i];		}	}	el.className = ar.join(" ");};/// Appends a certain [CSS] class to the given element./// @param el [HTMLElement] reference to the element./// @param className [string] the class to append.Zapatec.Utils.addClass = function(el, className) {	Zapatec.Utils.removeClass(el, className);	el.className += " " + className;};/// Retrieves the current target element for some event (useful when bubbling)./// This function is not actually very useful, but it's legacy from the old calendar code./// @param ev [Event] the event object./// @return [HTMLElement] window.event.srcElement for MSIE, ev.currentTarget for other browsers.Zapatec.Utils.getElement = function(ev) {	if (Zapatec.is_ie) {		return window.event.srcElement;	} else {		return ev.currentTarget;	}};/// Retrieves the target element for some event (useful when bubbling)./// This function is not actually very useful, but it's legacy from the old calendar code./// @param ev [Event] the event object./// @return [HTMLElement] window.event.srcElement for MSIE, ev.target for other browsers.Zapatec.Utils.getTargetElement = function(ev) {	if (Zapatec.is_ie) {		return window.event.srcElement;	} else {		return ev.target;	}};/// Stops bubbling and propagation of some event./// @param ev [Event] the event object/// @return falseZapatec.Utils.stopEvent = function(ev) {	ev || (ev = window.event);	if (ev) {		if (Zapatec.is_ie) {			ev.cancelBubble = true;			ev.returnValue = false;		} else {			ev.preventDefault();			ev.stopPropagation();		}	}	return false;};/// Adds an event handler to a certain element.  This function adds a handler/// using the DOM2 addEventListener (or attachEvent for MSIE).  Doing this/// means that you can add multiple handlers for the same element and same/// event name, and they will be called in order.////// WARNING: for really old browsers that don't support attachEvent nor/// addEventListener, it falls back to the default way: el.onclick = func./// This means that you CANNOT add multiple handlers in those browsers, as a/// new one will override the old one.////// @param el [HTMLElement] reference to the element./// @param evname [string] the event name, excluding the "on" prefix./// @param func event handler function.Zapatec.Utils.addEvent = function(el, evname, func) {	if (el.attachEvent) { // IE		el.attachEvent("on" + evname, func);	} else if (el.addEventListener) { // Gecko / W3C		el.addEventListener(evname, func, false);	} else {		el["on" + evname] = func;	}};/// Removes an event handler added with Zapatec.Utils.removeEvent().  The/// prototype scheme is the same.Zapatec.Utils.removeEvent = function(el, evname, func) {	if (el.detachEvent) { // IE		el.detachEvent("on" + evname, func);	} else if (el.removeEventListener) { // Gecko / W3C		el.removeEventListener(evname, func, false);	} else {		el["on" + evname] = null;	}};/// Create an element of a certain type using document.createElement().  A/// function was needed in order to add some common attributes to all created/// elements, but also in order to be able to use it in XHTML too (Gecko and/// other W3C-compliant browsers).////// This function will create an element of the given type and set certain/// properties to it: unselectable for IE, and the CSS "-moz-user-select" for/// Gecko, in order to make the element unselectable in these browsers./// Optionally, if the second argument is passed, it will appendChild() the/// newly created element to its parent.////// @param type [string] the tag name of the new element./// @param parent [HTMLElement, optional] a parent for the new element./// @param selectable [boolean] the flag to indicate wether element is selectable(rather usefull)./// @return [HTMLElement] reference to the new element.Zapatec.Utils.createElement = function(type, parent, selectable) {	var el = null;	if (window.self.document.createElementNS)		// use the XHTML namespace; IE won't normally get here unless		// _they_ "fix" the DOM2 implementation.		el = window.self.document.createElementNS("http://www.w3.org/1999/xhtml", type);	else		el = window.self.document.createElement(type);	if (typeof parent != "undefined" &&parent != null)		parent.appendChild(el);	if (!selectable) {		if (Zapatec.is_ie)			el.setAttribute("unselectable", true);		if (Zapatec.is_gecko)			el.style.setProperty("-moz-user-select", "none", "");	}	return el;};// Cookie management/// Sets a cooke given certain specifications.  It overrides any existing/// cookie with the same name.////// @param name [string] the cookie name./// @param value [string] the cookie value./// @param domain [string, optional] the cookie domain./// @param path [string, optional] the cookie path./// @param exp_days [number, optional] number of days of cookie validity.Zapatec.Utils.writeCookie = function(name, value, domain, path, exp_days) {	value = escape(value);	var ck = name + "=" + value, exp;	if (domain)		ck += ";domain=" + domain;	if (path)		ck += ";path=" + path;	if (exp_days) {		exp = new Date();		exp.setTime(exp_days * 86400000 + exp.getTime());		ck += ";expires=" + exp.toGMTString();	}	document.cookie = ck;};/** * Retrieves the value of a cookie. * * @param name [string] the cookie name * @return [string or null] a string with the cookie value, or null if it can't be found. *//* ? inside regular expression is not supported in IE 5.0Zapatec.Utils.getCookie = function(name) {	var re = new RegExp("(^|;\\s*)" + name + "\\s*=(.*?)(;|$)");	if (re.test(document.cookie)) {		var value = RegExp.$2;		value = unescape(value);		return (value);	}	return null;};*/Zapatec.Utils.getCookie = function(name) {	var pattern = name + "=";	var tokenPos = 0;	while (tokenPos < document.cookie.length) {		var valuePos = tokenPos + pattern.length;		if (document.cookie.substring(tokenPos, valuePos) == pattern) {			var endValuePos = document.cookie.indexOf(";", valuePos);			if (endValuePos == -1) { // Last cookie				endValuePos = document.cookie.length;			}			return unescape(document.cookie.substring(valuePos, endValuePos));		}		tokenPos=document.cookie.indexOf(" ",tokenPos)+1;		if (tokenPos == 0) { // No more tokens			break;		}	}	return null;};/** * Given an object, create a string suitable for saving the object in a cookie. * This is similar to serialization.  WARNING: it does not support nested * objects. * * @param obj [Object] reference to the object to serialize. * @return [string] the serialized object. */Zapatec.Utils.makePref = function(obj) {	function stringify(val) {		if (typeof val == "object" && !val)			return "null";		else if (typeof val == "number" || typeof val == "boolean")			return val;		else if (typeof val == "string")			return '"' + val.replace(/\22/, "\\22") + '"';		else return null;	};	var txt = "", i;	for (i in obj)		txt += (txt ? ",'" : "'") + i + "':" + stringify(obj[i]);	return txt;};/** * The reverse of Zapatec.Utils.makePref(), this function unserializes the * given string and creates an object from it. * * @param txt [string] the serialized value. * @return [Object] a new object if it was created successfully or null otherwise.

⌨️ 快捷键说明

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