📄 util.js
字号:
* 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.ltd.uk/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") { // Some browsers match names when looking for ids, so check names anyway. if (nodes == null) nodes = document.getElementsByName(orig); if (nodes != null && nodes.length > 1) { for (i = 0; i < nodes.length; i++) { node = nodes.item(i); if (node.type == "radio") node.checked = (node.value == val); } } else ele.checked = (val == true); } else if (ele.type == "checkbox") ele.checked = val; else ele.value = val; return; } if (dwr.util._isHTMLElement(ele, "textarea")) { ele.value = val; return; } // If the value to be set is a DOM object then we try importing the node // rather than serializing it out if (val.nodeType) { if (val.nodeType == 9 /*Node.DOCUMENT_NODE*/) val = val.documentElement; val = dwr.util._importNode(ele.ownerDocument, val, true); ele.appendChild(val); return; } // Fall back to innerHTML ele.innerHTML = val;};/** * @private Find multiple items in a select list and select them. Used by setValue() * @param ele The select list item * @param val The array of values to select */dwr.util._selectListItems = function(ele, val) { // We deal with select list elements by selecting the matching option // Begin by searching through the values var found = false; var i; var j; for (i = 0; i < ele.options.length; i++) { ele.options[i].selected = false; for (j = 0; j < val.length; j++) { if (ele.options[i].value == val[j]) { ele.options[i].selected = true; } } } // If that fails then try searching through the visible text if (found) return; for (i = 0; i < ele.options.length; i++) { for (j = 0; j < val.length; j++) { if (ele.options[i].text == val[j]) { ele.options[i].selected = true; } } }};/** * @private Find an item in a select list and select it. Used by setValue() * @param ele The select list item * @param val The value to select */dwr.util._selectListItem = function(ele, val) { // We deal with select list elements by selecting the matching option // Begin by searching through the values var found = false; var i; for (i = 0; i < ele.options.length; i++) { if (ele.options[i].value == val) { ele.options[i].selected = true; found = true; } else { ele.options[i].selected = false; } } // If that fails then try searching through the visible text if (found) return; for (i = 0; i < ele.options.length; i++) { if (ele.options[i].text == val) { ele.options[i].selected = true; } else { ele.options[i].selected = false; } }};/** * Read the current value for a given HTML element. * @see http://getahead.ltd.uk/dwr/browser/util/getvalue */dwr.util.getValue = function(ele, options) { if (options == null) options = {}; var orig = ele; ele = dwr.util.byId(ele); // We can work with names and need to sometimes for radio buttons, and IE has // an annoying bug where var nodes = document.getElementsByName(orig); if (ele == null && nodes.length >= 1) { ele = nodes.item(0); } if (ele == null) { dwr.util._debug("getValue() can't find an element with id/name: " + orig + "."); return ""; } if (dwr.util._isHTMLElement(ele, "select")) { // Using "type" property instead of "multiple" as "type" is an official // client-side property since JS 1.1 if (ele.type == "select-multiple") { var reply = new Array(); for (i = 0; i < ele.options.length; i++) { var item = ele.options[i]; if (item.selected) { if (item.value != null && item.value != "") { reply.push(item.value); } else { reply.push(item.text); } } } return reply; } else { var sel = ele.selectedIndex; if (sel != -1) { var item = ele.options[sel]; if (item.value != null && item.value != "") { return item.value; } return item.text; } else { return ""; } } } if (dwr.util._isHTMLElement(ele, "input")) { if (ele.type == "radio") { var node; for (i = 0; i < nodes.length; i++) { node = nodes.item(i); if (node.type == "radio") { if (node.checked) { if (nodes.length > 1) return node.value; else return true; } } } } switch (ele.type) { case "checkbox": case "check-box": case "radio": // if (ele.checked && ele.value != "") return ele.value; // else return ele.checked; default: return ele.value; } } if (dwr.util._isHTMLElement(ele, "textarea")) { return ele.value; } if (dwr.util._shouldEscapeHtml(options)) { if (ele.textContent) return ele.textContent; else if (ele.innerText) return ele.innerText; } return ele.innerHTML;};/** * getText() is like getValue() except that it reads the text (and not the value) from select elements * @see http://getahead.ltd.uk/dwr/browser/util/gettext */dwr.util.getText = function(ele) { ele = dwr.util._getElementById(ele, "getText()"); if (ele == null) return null; if (!dwr.util._isHTMLElement(ele, "select")) { dwr.util._debug("getText() can only be used with select elements. Attempt to use: " + dwr.util._detailedTypeOf(ele) + " from id: " + orig + "."); return ""; } // This is a bit of a scam because it assumes single select // but I'm not sure how we should treat multi-select. var sel = ele.selectedIndex; if (sel != -1) { return ele.options[sel].text; } else { return ""; }};/** * Given a map, call setValue() for all the entries in the map using the entry key as an element id * @see http://getahead.ltd.uk/dwr/browser/util/setvalues */dwr.util.setValues = function(map, options) { var prefixes = []; if (options && options.prefix) prefixes.push(options.prefix); dwr.util._getDataProperties(map, prefixes);};/** * @private retrieve values for the map and set the corresponding form fields. For object properties, recursively * read sub properties in order to matching nested form fields. */dwr.util._getDataProperties = function(map, prefixes) { for (var property in map) { // Only recurse into plain objects, ie not functions, arrays, etc if (dwr.util._isObject(map[property]) && !dwr.util._isArray(map[property])) { var prefixClone = []; for (var i = 0; i < prefixes.length; i++) { prefixClone.push(prefixes[i]); } prefixClone.push(property); dwr.util._getDataProperties(map[property], prefixClone); } else { var nestedProperty = property; if (prefixes.length > 0) { nestedProperty = (prefixes.join(".")) + "." + property; } // Are there any elements with that id or name if (dwr.util.byId(nestedProperty) != null || document.getElementsByName(nestedProperty).length >= 1) { dwr.util.setValue(nestedProperty, map[property]); } } }};/** * Given a map, call getValue() for all the entries in the map using the entry key as an element id. * Given a string or element that refers to a form, create an object from the elements of the form. * @see http://getahead.ltd.uk/dwr/browser/util/getvalues */dwr.util.getValues = function(data, options) { var ele; if (typeof data == "string") ele = dwr.util.byId(data); if (dwr.util._isHTMLElement(data)) ele = data; if (ele != null) { if (ele.elements == null) { alert("getValues() requires an object or reference to a form element."); return null; } var reply = {}; var value; for (var i = 0; i < ele.elements.length; i++) { if (ele[i].id != null) value = ele[i].id; else if (ele[i].value != null) value = ele[i].value; else value = "element" + i; reply[value] = dwr.util.getValue(ele[i]); } return reply; } else { var prefixes = []; if (options != null && options.prefix) prefixes.push(options.prefix); dwr.util._setDataProperties(data, prefixes); return data; }};/** * @private for each object property, set html field value if present. Recurse for object properties. */dwr.util._setDataProperties = function(data, prefixes) { for (var property in data) { // Only recurse into plain objects, ie not functions, arrays, etc if (dwr.util._isObject(data[property]) && !dwr.util._isArray(data[property])) { var prefixClone = []; for (var i = 0; i < prefixes.length; i++) { prefixClone.push(prefixes[i]); } prefixClone.push(property); dwr.util._setDataProperties(data[property], prefixClone); } else { var nestedProperty = property; if (prefixes.length > 0) { nestedProperty = (prefixes.join(".")) + "." + property; } // Are there any elements with that id or name if (dwr.util.byId(nestedProperty) != null || document.getElementsByName(nestedProperty).length >= 1) { data[property] = dwr.util.getValue(nestedProperty); } } }};/** * Add options to a list from an array or map. * @see http://getahead.ltd.uk/dwr/browser/lists */dwr.util.addOptions = function(ele, data/*, options*/) { ele = dwr.util._getElementById(ele, "addOptions()"); if (ele == null) return; // TODO: Restructure so we handle arguments to get proper options handling // if (options == null) options = {}; var useOptions = dwr.util._isHTMLElement(ele, "select"); var useLi = dwr.util._isHTMLElement(ele, ["ul", "ol"]); if (!useOptions && !useLi) { dwr.util._debug("addOptions() can only be used with select/ul/ol elements. Attempt to use: " + dwr.util._detailedTypeOf(ele)); return; } if (data == null) return; var text, value, li; if (dwr.util._isArray(data)) { // Loop through the data that we do have for (var i = 0; i < data.length; i++) { if (useOptions) { if (arguments[2] != null) { if (arguments[3] != null) { text = dwr.util._getValueFrom(data[i], arguments[3]); value = dwr.util._getValueFrom(data[i], arguments[2]); } else text = value = dwr.util._getValueFrom(data[i], arguments[2]); } else text = value = dwr.util._getValueFrom(data[i], arguments[3]); if (text != null || value) ele.options[ele.options.length] = new Option(text, value); } else { li = document.createElement("li"); value = dwr.util._getValueFrom(data[i], arguments[2]); if (value != null) { li.innerHTML = value; ele.appendChild(li); } } } } else if (arguments[3] != null) { if (!useOptions) { alert("dwr.util.addOptions can only create select lists from objects.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -