📄 util.js
字号:
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * Declare an object to which we can add real functions. */if (dwr == null) var dwr = {};if (dwr.util == null) dwr.util = {};if (DWRUtil == null) var DWRUtil = dwr.util;/** @private The flag we use to decide if we should escape html */dwr.util._escapeHtml = true;/** * Set the global escapeHtml flag */dwr.util.setEscapeHtml = function(escapeHtml) { dwr.util._escapeHtml = escapeHtml;}/** @private Work out from an options list and global settings if we should be esccaping */dwr.util._shouldEscapeHtml = function(options) { if (options && options.escapeHtml != null) { return options.escapeHtml; } return dwr.util._escapeHtml;}/** * Return a string with &, <, >, ' and " replaced with their entities * @see TODO */dwr.util.escapeHtml = function(original) { var div = document.createElement('div'); var text = document.createTextNode(original); div.appendChild(text); return div.innerHTML;}/** * Replace common XML entities with characters (see dwr.util.escapeHtml()) * @see TODO */dwr.util.unescapeHtml = function(original) { var div = document.createElement('div'); div.innerHTML = original.replace(/<\/?[^>]+>/gi, ''); return div.childNodes[0] ? div.childNodes[0].nodeValue : '';}/** * Replace characters dangerous for XSS reasons with visually similar characters * @see TODO */dwr.util.replaceXmlCharacters = function(original) { original = original.replace("&", "+"); original = original.replace("<", "\u2039"); original = original.replace(">", "\u203A"); original = original.replace("\'", "\u2018"); original = original.replace("\"", "\u201C"); return original;}/** * Return true iff the input string contains any XSS dangerous characters * @see TODO */dwr.util.containsXssRiskyCharacters = function(original) { return (original.indexOf('&') != -1 && original.indexOf('<') != -1 && original.indexOf('>') != -1 && original.indexOf('\'') != -1 && original.indexOf('\"') != -1);}/** * Enables you to react to return being pressed in an input * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.onReturn = function(event, action) { if (!event) event = window.event; if (event && event.keyCode && event.keyCode == 13) action();};/** * Select a specific range in a text box. Useful for 'google suggest' type functions. * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.selectRange = function(ele, start, end) { ele = dwr.util._getElementById(ele, "selectRange()"); if (ele == null) return; if (ele.setSelectionRange) { ele.setSelectionRange(start, end); } else if (ele.createTextRange) { var range = ele.createTextRange(); range.moveStart("character", start); range.moveEnd("character", end - ele.value.length); range.select(); } ele.focus();};/** * Find the element in the current HTML document with the given id or ids * @see http://getahead.org/dwr/browser/util/$ */if (document.getElementById) { dwr.util.byId = function() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') { element = document.getElementById(element); } if (arguments.length == 1) { return element; } elements.push(element); } return elements; };}else if (document.all) { dwr.util.byId = function() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') { element = document.all[element]; } if (arguments.length == 1) { return element; } elements.push(element); } return elements; };}/** * Alias $ to dwr.util.byId * @see http://getahead.org/dwr/browser/util/$ */var $;if (!$) { $ = dwr.util.byId;}/** * Like toString but aimed at debugging * @see http://getahead.org/dwr/browser/util/todescriptivestring */dwr.util.toDescriptiveString = function(data, level, depth) { var reply = ""; var i = 0; var value; var obj; if (level == null) level = 0; if (depth == null) depth = 0; if (data == null) return "null"; if (dwr.util._isArray(data)) { if (data.length == 0) reply += "[]"; else { if (level != 0) reply += "[\n"; else reply = "["; for (i = 0; i < data.length; i++) { try { obj = data[i]; if (obj == null || typeof obj == "function") { continue; } else if (typeof obj == "object") { if (level > 0) value = dwr.util.toDescriptiveString(obj, level - 1, depth + 1); else value = dwr.util._detailedTypeOf(obj); } else { value = "" + obj; value = value.replace(/\/n/g, "\\n"); value = value.replace(/\/t/g, "\\t"); } } catch (ex) { value = "" + ex; } if (level != 0) { reply += dwr.util._indent(level, depth + 2) + value + ", \n"; } else { if (value.length > 13) value = value.substring(0, 10) + "..."; reply += value + ", "; if (i > 5) { reply += "..."; break; } } } if (level != 0) reply += dwr.util._indent(level, depth) + "]"; else reply += "]"; } return reply; } if (typeof data == "string" || typeof data == "number" || dwr.util._isDate(data)) { return data.toString(); } if (typeof data == "object") { var typename = dwr.util._detailedTypeOf(data); if (typename != "Object") reply = typename + " "; if (level != 0) reply += "{\n"; else reply = "{"; var isHtml = dwr.util._isHTMLElement(data); for (var prop in data) { if (isHtml) { // HTML nodes have far too much stuff. Chop out the constants if (prop.toUpperCase() == prop || prop == "title" || prop == "lang" || prop == "dir" || prop == "className" || prop == "form" || prop == "name" || prop == "prefix" || prop == "namespaceURI" || prop == "nodeType" || prop == "firstChild" || prop == "lastChild" || prop.match(/^offset/)) { continue; } } value = ""; try { obj = data[prop]; if (obj == null || typeof obj == "function") { continue; } else if (typeof obj == "object") { if (level > 0) { value = "\n"; value += dwr.util._indent(level, depth + 2); value = dwr.util.toDescriptiveString(obj, level - 1, depth + 1); } else { value = dwr.util._detailedTypeOf(obj); } } else { value = "" + obj; value = value.replace(/\/n/g, "\\n"); value = value.replace(/\/t/g, "\\t"); } } catch (ex) { value = "" + ex; } if (level == 0 && value.length > 13) value = value.substring(0, 10) + "..."; var propStr = prop; if (propStr.length > 30) propStr = propStr.substring(0, 27) + "..."; if (level != 0) reply += dwr.util._indent(level, depth + 1); reply += prop + ":" + value + ", "; if (level != 0) reply += "\n"; i++; if (level == 0 && i > 5) { reply += "..."; break; } } reply += dwr.util._indent(level, depth); reply += "}"; return reply; } return data.toString();};/** * @private Indenting for dwr.util.toDescriptiveString */dwr.util._indent = function(level, depth) { var reply = ""; if (level != 0) { for (var j = 0; j < depth; j++) { reply += "\u00A0\u00A0"; } reply += " "; } return reply;};/** * Setup a GMail style loading message. * @see http://getahead.org/dwr/browser/util/useloadingmessage */dwr.util.useLoadingMessage = function(message) { var loadingMessage; if (message) loadingMessage = message; else loadingMessage = "Loading"; dwr.engine.setPreHook(function() { var disabledZone = dwr.util.byId('disabledZone'); if (!disabledZone) { disabledZone = document.createElement('div'); disabledZone.setAttribute('id', 'disabledZone'); disabledZone.style.position = "absolute"; disabledZone.style.zIndex = "1000"; disabledZone.style.left = "0px"; disabledZone.style.top = "0px"; disabledZone.style.width = "100%"; disabledZone.style.height = "100%"; document.body.appendChild(disabledZone); var messageZone = document.createElement('div'); messageZone.setAttribute('id', 'messageZone'); messageZone.style.position = "absolute"; messageZone.style.top = "0px"; messageZone.style.right = "0px"; messageZone.style.background = "red"; messageZone.style.color = "white"; messageZone.style.fontFamily = "Arial,Helvetica,sans-serif"; messageZone.style.padding = "4px"; disabledZone.appendChild(messageZone); var text = document.createTextNode(loadingMessage); messageZone.appendChild(text); dwr.util._disabledZoneUseCount = 1; } else { dwr.util.byId('messageZone').innerHTML = loadingMessage; disabledZone.style.visibility = 'visible'; dwr.util._disabledZoneUseCount++; } }); dwr.engine.setPostHook(function() { dwr.util._disabledZoneUseCount--; if (dwr.util._disabledZoneUseCount == 0) { dwr.util.byId('disabledZone').style.visibility = 'hidden'; } });};/** * Set a global highlight handler */dwr.util.setHighlightHandler = function(handler) { dwr.util._highlightHandler = handler;};/** * An example highlight handler */dwr.util.yellowFadeHighlightHandler = function(ele) { dwr.util._yellowFadeProcess(ele, 0);};dwr.util._yellowFadeSteps = [ "d0", "b0", "a0", "90", "98", "a0", "a8", "b0", "b8", "c0", "c8", "d0", "d8", "e0", "e8", "f0", "f8" ];dwr.util._yellowFadeProcess = function(ele, colorIndex) { ele = dwr.util.byId(ele); if (colorIndex < dwr.util._yellowFadeSteps.length) { ele.style.backgroundColor = "#ffff" + dwr.util._yellowFadeSteps[colorIndex]; setTimeout("dwr.util._yellowFadeProcess('" + ele.id + "'," + (colorIndex + 1) + ")", 200); } else { ele.style.backgroundColor = "transparent"; }};/** * An example highlight handler */dwr.util.borderFadeHighlightHandler = function(ele) { ele.style.borderWidth = "2px"; ele.style.borderStyle = "solid"; dwr.util._borderFadeProcess(ele, 0);};dwr.util._borderFadeSteps = [ "d0", "b0", "a0", "90", "98", "a0", "a8", "b0", "b8", "c0", "c8", "d0", "d8", "e0", "e8", "f0", "f8" ];dwr.util._borderFadeProcess = function(ele, colorIndex) { ele = dwr.util.byId(ele); if (colorIndex < dwr.util._borderFadeSteps.length) { ele.style.borderColor = "#ff" + dwr.util._borderFadeSteps[colorIndex] + dwr.util._borderFadeSteps[colorIndex]; setTimeout("dwr.util._borderFadeProcess('" + ele.id + "'," + (colorIndex + 1) + ")", 200); } else { ele.style.backgroundColor = "transparent"; }};/** * A focus highlight handler */dwr.util.focusHighlightHandler = function(ele) { try { ele.focus(); } catch (ex) { /* ignore */ }};/** @private the current global highlight style */dwr.util._highlightHandler = null;/** * Highlight that an element has changed */dwr.util.highlight = function(ele, options) { if (options && options.highlightHandler) { options.highlightHandler(dwr.util.byId(ele)); } else if (dwr.util._highlightHandler != null) { dwr.util._highlightHandler(dwr.util.byId(ele)); }};/** * Set the value an HTML element to the specified value. * @see http://getahead.org/dwr/browser/util/setvalue */dwr.util.setValue = function(ele, val, options) { if (val == null) val = ""; if (options == null) options = {}; if (dwr.util._shouldEscapeHtml(options) && typeof(val) == "string") { val = dwr.util.escapeHtml(val); } var orig = ele; var nodes, node, i; ele = dwr.util.byId(ele); // We can work with names and need to sometimes for radio buttons if (ele == null) { nodes = document.getElementsByName(orig); if (nodes.length >= 1) ele = nodes.item(0); } if (ele == null) { dwr.util._debug("setValue() can't find an element with id/name: " + orig + "."); return; } // All paths now lead to some update so we highlight a change dwr.util.highlight(ele, options); if (dwr.util._isHTMLElement(ele, "select")) { if (ele.type == "select-multiple" && dwr.util._isArray(val)) dwr.util._selectListItems(ele, val); else dwr.util._selectListItem(ele, val); return; } if (dwr.util._isHTMLElement(ele, "input")) { if (ele.type == "radio") {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -