📄 common.js
字号:
/* common.js{{IS_NOTE Purpose: Common utiltiies. Description: History: Fri Jun 10 18:16:11 2005, Created by tomyeh}}IS_NOTECopyright (C) 2005 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/if (!window.anima) { //avoid eval twice// Standard //String.prototype.startsWith = function (prefix) { return this.substring(0,prefix.length) == prefix;};String.prototype.endsWith = function (suffix) { return this.substring(this.length-suffix.length) == suffix;};String.prototype.trim = function () { var j = 0, k = this.length - 1; while (j < this.length && this.charAt(j) <= ' ') ++j; while (k >= j && this.charAt(k) <= ' ') --k; return j > k ? "": this.substring(j, k + 1);};String.prototype.skipWhitespaces = function (j) { for (;j < this.length; ++j) { var cc = this.charAt(j); if (cc != ' ' && cc != '\t' && cc != '\n' && cc != '\r') break; } return j;};String.prototype.nextWhitespace = function (j) { for (;j < this.length; ++j) { var cc = this.charAt(j); if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r') break; } return j;};/** Tom Yeh 20070630: Remove unused codesString.prototype.skipWhitespacesBackward = function (j) { for (;j >= 0; --j) { var cc = this.charAt(j); if (cc != ' ' && cc != '\t' && cc != '\n' && cc != '\r') break; } return j;};*//** Removes the specified object from the array if any. * Returns false if not found. */Array.prototype.remove = function (o) { for (var j = 0; j < this.length; ++j) { if (o == this[j]) { this.splice(j, 1); return true; } } return false;};/** Returns whether the array contains the specified object. */Array.prototype.contains = function (o) { for (var j = 0; j < this.length; ++j) { if (o == this[j]) return true; } return false;};//////Form//function z_fmsubm(a, b, c) { var fns = this._submfns; for (var j = 0; j < (fns ? fns.length: 0); ++j) fns[j].apply(this, arguments); return this._ogsubm(a, b, c); //If IE, we cannot use _ogsubm.apply. Reason unknown.};if (zk.ie) { zk.fixSubmit = function (n) { n._ogsubm = n.submit; n.submit = z_fmsubm; } //Step 1. Override document.createElement zk._newElem = document.createElement; document.createElement = function (tag) { var n = zk._newElem(tag); //we cannot use zk._newElem.apply. Reason unknown. if (tag.toUpperCase() == "FORM") zk.fixSubmit(n); return n; }; //Step 2: HTC (http://delete.me.uk/2004/09/addbehaviour.html) //Due to performance issue and unable to really make it work //we change submit in zk.init} else { HTMLFormElement.prototype._ogsubm = HTMLFormElement.prototype.submit; HTMLFormElement.prototype.submit = z_fmsubm;}//////// More zk utilities (defined also in boot.js) ///** Returns whether it is part of the class name * of the specified element. */zk.hasClass = function (el, clsnm) { var cn = el.className; return cn && (' '+cn+' ').indexOf(' '+clsnm+' ') != -1;};/** Adds the specified class name to the class name of the specified element. * @since 3.0.0 */zk.addClass = function (el, clsnm, bAdd) { if (bAdd == false) { zk.rmClass(el, clsnm); return; } if (!zk.hasClass(el, clsnm)) { var cn = el.className; if (cn.length) cn += ' '; el.className = cn + clsnm; }};/** Removes the specified class name from the the class name of the specified * element. * @since 3.0.0 */zk.rmClass = function (el, clsnm, bRemove) { if (bRemove == false) { zk.addClass(el, clsnm); return; } if (zk.hasClass(el, clsnm)) { var re = new RegExp('(?:^|\\s+)' + clsnm + '(?:\\s+|$)', "g"); el.className = el.className.replace(re, " "); }};/** Sets the offset height. */zk.setOffsetHeight = function (el, hgh) { hgh = hgh - $int(Element.getStyle(el, "padding-top")) - $int(Element.getStyle(el, "padding-bottom")) - $int(Element.getStyle(el, "margin-top")) - $int(Element.getStyle(el, "margin-bottom")) - $int(Element.getStyle(el, "border-top-width")) - $int(Element.getStyle(el, "border-bottom-width")); el.style.height = (hgh > 0 ? hgh: 0) + "px";};/** Return el.offsetWidth, which solving Safari's bug. */zk.offsetWidth = function (el) { if (!el) return 0; if (!zk.safari || $tag(el) != "TR") return el.offsetWidth; var wd = 0; for (var j = el.cells.length; --j >= 0;) wd += el.cells[j].offsetWidth; return wd;};/** Return el.offsetHeight, which solving Safari's bug. */zk.offsetHeight = function (el) { if (!el) return 0; if (!zk.safari || $tag(el) != "TR") return el.offsetHeight; var hgh = 0; for (var j = el.cells.length; --j >= 0;) { var h = el.cells[j].offsetHeight; if (h > hgh) hgh = h; } return hgh;};/** Returns el.offsetTop, which solving Safari's bug. */zk.offsetTop = function (el) { if (!el) return 0; if (zk.safari && $tag(el) === "TR" && el.cells.length) el = el.cells[0]; return el.offsetTop;};/** Returns el.offsetLeft, which solving Safari's bug. */zk.offsetLeft = function (el) { if (!el) return 0; if (zk.safari && $tag(el) === "TR" && el.cells.length) el = el.cells[0]; return el.offsetLeft;};zk.borders = {l: "border-left-width", r: "border-right-width", t: "border-top-width", b: "border-bottom-width"};zk.paddings = {l: "padding-left", r: "padding-right", t: "padding-top", b: "padding-bottom"};/** Returns the summation of the specified styles. * For example, * zk.sumStyles(el, "lr", zk.paddings) sums the style values of * zk.paddings['l'] and zk.paddings['r']. * * @param {String} areas the areas is abbreviation for left "l", right "r", top "t", and bottom "b". * So you can specify to be "lr" or "tb" or more. * @param styles {zk.paddings} or {zk.borders}. * @return {Number} * @since 3.0.0 */zk.sumStyles = function (el, areas, styles) { var val = 0; for (var i = 0, l = areas.length; i < l; i++){ var w = $int(Element.getStyle(el, styles[areas.charAt(i)])); if (!isNaN(w)) val += w; } return val;};/** * Returns the revised size, which subtracted the size of its CSS border or padding, for the specified element. * @param {Number} size original size of the specified element. * @param {Boolean} isHgh if true it will be "tb" top and bottom. * @return {Number} * @since 3.0.0 */zk.revisedSize = function (el, size, isHgh) { var areas = "lr"; if (isHgh) areas = "tb"; size -= (zk.sumStyles(el, areas, zk.borders) + zk.sumStyles(el, areas, zk.paddings)); if (size < 0) size = 0; return size;};/** * Returns the revised position, which subtracted the offset of its scrollbar, * for the specified element. * @param {Object} el * @param {Array} ofs [left, top]; * @return {Array} [left, top]; * @since 3.0.0 */zk.revisedOffset = function (el, ofs) { if(!ofs) { if (el.getBoundingClientRect){ // IE var b = el.getBoundingClientRect(); return [b.left + zk.innerX() - 3 , b.top + zk.innerY() - 3]; } ofs = Position.cumulativeOffset(el); } var scrolls = Position.realOffset(el); scrolls[0] -= zk.innerX(); scrolls[1] -= zk.innerY(); return [ofs[0] - scrolls[0], ofs[1] - scrolls[1]];};if (zk.safari) { //fix safari's bug zk._oldposofs = Position.positionedOffset; Position.positionedOffset = function (el) { if ($tag(el) === "TR" && el.cells.length) el = el.cells[0]; return zk._oldposofs(el); };}if (zk.gecko || zk.safari) { zk._oldcumofs = Position.cumulativeOffset; Position.cumulativeOffset = function (el) { //fix safari's bug: TR has no offsetXxx if (zk.safari && $tag(el) === "TR" && el.cells.length) el = el.cells[0]; //fix gecko and safari's bug: if not visible before, offset is wrong var ofs; if (!$visible(el) && !zk.offsetWidth(el)) { el.style.display = ""; ofs = zk._oldcumofs(el); el.style.display = "none"; } else { ofs = zk._oldcumofs(el); } return ofs; };}/** Center the specified element. * @param flags a combination of center, left, right, top and bottom. * If omitted, center is assigned. */zk.center = function (el, flags) { var wdgap = zk.offsetWidth(el), hghgap = zk.offsetHeight(el); if ((!wdgap || !hghgap) && !$visible(el)) { el.style.top = "-10000px"; //avoid annoying effect el.style.display = "block"; //we need to calculate the size wdgap = zk.offsetWidth(el); hghgap = zk.offsetHeight(el), el.style.display = "none"; //avoid Firefox to display it too early } var left = zk.innerX(), top = zk.innerY(); var x, y, skipx, skipy; wdgap = zk.innerWidth() - wdgap; if (!flags) x = left + wdgap / 2; else if (flags.indexOf("left") >= 0) x = left; else if (flags.indexOf("right") >= 0) x = left + wdgap - 1; //just in case else if (flags.indexOf("center") >= 0) x = left + wdgap / 2; else { x = 0; skipx = true; } hghgap = zk.innerHeight() - hghgap; if (!flags) y = top + hghgap / 2; else if (flags.indexOf("top") >= 0) y = top; else if (flags.indexOf("bottom") >= 0) y = top + hghgap - 1; //just in case else if (flags.indexOf("center") >= 0) y = top + hghgap / 2; else { y = 0; skipy = true; } if (x < left) x = left; if (y < top) y = top; var ofs = zk.toStyleOffset(el, x, y); if (!skipx) el.style.left = ofs[0] + "px"; if (!skipy) el.style.top = ofs[1] + "px";};/** Returns the width and height. * In additions, it fixes brwoser's bugs, so call it as soon as possible. */zk.getDimension = function (el) { var wd = zk.offsetWidth(el), hgh; if (!$visible(el) && !wd) { var fixleft = el.style.left == "" || el.style.left == "auto"; if (fixleft) el.style.left = "0"; var fixtop = el.style.top == "" || el.style.top == "auto"; if (fixtop) el.style.top = "0"; //IE6/gecko: otherwise, cumulativeOffset is wrong el.style.display = ""; wd = zk.offsetWidth(el); hgh = zk.offsetHeight(el); el.style.display = "none"; if (fixleft) el.style.left = ""; if (fixtop) el.style.top = ""; } else { hgh = zk.offsetHeight(el); } return [wd, hgh];};/** Position a component being releted to another. */zk.position = function (el, ref, type) { var refofs = zk.getDimension(el); var wd = refofs[0], hgh = refofs[1]; refofs = zk.revisedOffset(ref); var x, y; var scx = zk.innerX(), scy = zk.innerY(), scmaxx = scx + zk.innerWidth(), scmaxy = scy + zk.innerHeight(); if (type == "end_before") { //el's upper-left = ref's upper-right x = refofs[0] + zk.offsetWidth(ref); y = refofs[1]; if (zk.ie) { var diff = $int(Element.getStyle(ref, "margin-top")); if (!isNaN(diff)) y += diff; diff = $int(Element.getStyle(ref, "margin-right")); if (!isNaN(diff)) x += diff; } if (x + wd > scmaxx) x = refofs[0] - wd; if (y + hgh > scmaxy) y = scmaxy - hgh; } else { //after-start: el's upper-left = ref's lower-left x = refofs[0]; y = refofs[1] + zk.offsetHeight(ref); if (zk.ie) { var diff = $int(Element.getStyle(ref, "margin-bottom")); if (!isNaN(diff)) y += diff; diff = $int(Element.getStyle(ref, "margin-left")); if (!isNaN(diff)) x += diff; } if (y + hgh > scmaxy) y = refofs[1] - hgh; if (x + wd > scmaxx) x = scmaxx - wd; } if (x < scx) x = scx; if (y < scy) y = scy; refofs = zk.toStyleOffset(el, x, y); el.style.left = refofs[0] + "px"; el.style.top = refofs[1] + "px";};/** Returns the style's coordination in [integer, integer]. * Note: it ignores the unit and assumes px (so pt or others will be wrong) */zk.getStyleOffset = function (el) { return [$int(el.style.left), $int(el.style.top)];};/** Converts from absolute coordination to style's coordination. * It is only useful for table's cell. * We cannot use zk.toParentCoord, because * after calling Draggable, offsetParent becomes BODY but * style.left/top is still relevant to original offsetParent */zk.toStyleOffset = function (el, x, y) { var oldx = el.style.left, oldy = el.style.top; if (zk.opera) { //Opera: //1)we have to reset left/top. Or, the second call position wrong //test case: Tooltips and Popups //2)we cannot assing "", either //test case: menu el.style.left = el.style.top = "0"; } else { //IE/gecko fix: otherwise, zk.toStyleOffset won't correct if (el.style.left == "" || el.style.left == "auto") el.style.left = "0"; if (el.style.top == "" || el.style.top == "auto") el.style.top = "0"; } var ofs1 = Position.cumulativeOffset(el); var ofs2 = zk.getStyleOffset(el); ofs1 = [x - ofs1[0] + ofs2[0], y - ofs1[1] + ofs2[1]]; el.style.left = oldx; el.style.top = oldy; return ofs1;};/** Whether el1 and el2 are overlapped. */zk.isOverlapped = function (el1, el2) { return zk.isOffsetOverlapped( Position.cumulativeOffset(el1), [el1.offsetWidth, el1.offsetHeight], Position.cumulativeOffset(el2), [el2.offsetWidth, el2.offsetHeight]);};/** Whether ofs1/dim1 is overlapped with ofs2/dim2. */zk.isOffsetOverlapped = function (ofs1, dim1, ofs2, dim2) { var o1x1 = ofs1[0], o1x2 = dim1[0] + o1x1, o1y1 = ofs1[1], o1y2 = dim1[1] + o1y1; var o2x1 = ofs2[0], o2x2 = dim2[0] + o2x1, o2y1 = ofs2[1], o2y2 = dim2[1] + o2y1; return o2x1 <= o1x2 && o2x2 >= o1x1 && o2y1 <= o1y2 && o2y2 >= o1y1;};/** Whether an element is really visible. */zk.isRealVisible = function (e) { if (!e) return false; do { if (!$visible(e)) return false; //note: document is the top parent and has NO style } while (e = $parent(e)); //yes, assign return true;};zk.isVisible = $visible; //backward compatible/** Focus the specified element and any of its child. */zk.focusDown = function (el) { return zk._focusDown(el, ["INPUT", "SELECT", "BUTTON"], true) || zk._focusDown(el, ["A"], false);};/** checkA whether to check the A tag specially (i.e., focus if one ancestor * has z.type). */zk._focusDown = function (el, match, checkA) { if (!el) return false; if (el.focus) { var tn = $tag(el); if (match.contains(tn)) { try {el.focus();} catch (e) {} //IE throws exception when focus in some cases return true; } if (checkA && tn == "A") { for (var n = el; (n = $parent(n))/*yes, assign*/;) { if (getZKAttr(n, "type")) { try {el.focus();} catch (e) {} //IE throws exception when focus in some cases return true; } } } } for (el = el.firstChild; el; el = el.nextSibling) { if (zk._focusDown(el, match)) return true; } return false;};/** Focus the element with the specified ID and do it timeout later. */zk.asyncFocusDown = function (id, timeout) { ++zk.inAsyncFocus; setTimeout("--zk.inAsyncFocus; if (!zk.focusDown($e('"+id+"'))) window.focus();", timeout > 0? timeout: 0);};/** Focus the element without looking down, and do it timeout later. */zk.asyncFocus = function (id, timeout) { ++zk.inAsyncFocus; setTimeout("--zk.inAsyncFocus; zk.focus($e('"+id+"'));", timeout > 0? timeout: 0); //Workaround for an IE bug: we have to set focus twice since //the first one might fail (even we prolong the timeout to 1 sec)};zk.inAsyncFocus = 0;/** Focus to the specified component w/o throwing exception. */zk.focus = function (cmp) { if (cmp && cmp.focus) try { cmp.focus(); } catch (e) { setTimeout(function() { try {cmp.focus();} catch (e) {} }, 0); } //IE throws exception when focus in some cases};/** Select the text of the element, and do it timeout later. */zk.asyncSelect = function (id, timeout) { ++zk.inAsyncSelect;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -