📄 util.js
字号:
if (nodes.item(i).type == "radio") { if (nodes.item(i).checked) { return nodes.item(i).value; } } } } switch (ele.type) { case "checkbox": case "check-box": case "radio": return ele.checked; default: return ele.value; } } if (DWRUtil._isHTMLElement(ele, "textarea")) { return ele.value; } 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 */DWRUtil.getText = function(ele) { var orig = ele; ele = $(ele); if (ele == null) { DWRUtil.debug("getText() can't find an element with id: " + orig + "."); return ""; } if (!DWRUtil._isHTMLElement(ele, "select")) { DWRUtil.debug("getText() can only be used with select elements. Attempt to use: " + DWRUtil._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 */DWRUtil.setValues = function(map) { for (var property in map) { var ele = $(property); if (ele != null) { var value = map[property]; DWRUtil.setValue(property, value); } }};/** * 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 */DWRUtil.getValues = function(data) { var ele; if (typeof data == "string") ele = $(data); if (DWRUtil._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] = DWRUtil.getValue(ele[i]); } return reply; } else { for (var property in data) { var ele = $(property); if (ele != null) { data[property] = DWRUtil.getValue(property); } } return data; }};/** * Add options to a list from an array or map. * @see http://getahead.ltd.uk/dwr/browser/lists */DWRUtil.addOptions = function(ele, data) { var orig = ele; ele = $(ele); if (ele == null) { DWRUtil.debug("addOptions() can't find an element with id: " + orig + "."); return; } var useOptions = DWRUtil._isHTMLElement(ele, "select"); var useLi = DWRUtil._isHTMLElement(ele, ["ul", "ol"]); if (!useOptions && !useLi) { DWRUtil.debug("addOptions() can only be used with select/ul/ol elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele)); return; } if (data == null) return; var text; var value; var opt; var li; if (DWRUtil._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 = DWRUtil._getValueFrom(data[i], arguments[3]); value = DWRUtil._getValueFrom(data[i], arguments[2]); } else { value = DWRUtil._getValueFrom(data[i], arguments[2]); text = value; } } else { text = DWRUtil._getValueFrom(data[i], arguments[3]); value = text; } if (text || value) { opt = new Option(text, value); ele.options[ele.options.length] = opt; } } else { li = document.createElement("li"); value = DWRUtil._getValueFrom(data[i], arguments[2]); if (value != null) { li.innerHTML = value; ele.appendChild(li); } } } } else if (arguments[3] != null) { for (var prop in data) { if (!useOptions) { alert("DWRUtil.addOptions can only create select lists from objects."); return; } value = DWRUtil._getValueFrom(data[prop], arguments[2]); text = DWRUtil._getValueFrom(data[prop], arguments[3]); if (text || value) { opt = new Option(text, value); ele.options[ele.options.length] = opt; } } } else { for (var prop in data) { if (!useOptions) { DWRUtil.debug("DWRUtil.addOptions can only create select lists from objects."); return; } if (typeof data[prop] == "function") { // Skip this one it's a function. text = null; value = null; } else if (arguments[2]) { text = prop; value = data[prop]; } else { text = data[prop]; value = prop; } if (text || value) { opt = new Option(text, value); ele.options[ele.options.length] = opt; } } }};/** * @private Get the data from an array function for DWRUtil.addOptions */DWRUtil._getValueFrom = function(data, method) { if (method == null) return data; else if (typeof method == 'function') return method(data); else return data[method];}/** * Remove all the options from a select list (specified by id) * @see http://getahead.ltd.uk/dwr/browser/lists */DWRUtil.removeAllOptions = function(ele) { var orig = ele; ele = $(ele); if (ele == null) { DWRUtil.debug("removeAllOptions() can't find an element with id: " + orig + "."); return; } var useOptions = DWRUtil._isHTMLElement(ele, "select"); var useLi = DWRUtil._isHTMLElement(ele, ["ul", "ol"]); if (!useOptions && !useLi) { DWRUtil.debug("removeAllOptions() can only be used with select, ol and ul elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele)); return; } if (useOptions) { ele.options.length = 0; } else { while (ele.childNodes.length > 0) { ele.removeChild(ele.firstChild); } }};/** * Create rows inside a the table, tbody, thead or tfoot element (given by id). * @see http://getahead.ltd.uk/dwr/browser/tables */DWRUtil.addRows = function(ele, data, cellFuncs, options) { var orig = ele; ele = $(ele); if (ele == null) { DWRUtil.debug("addRows() can't find an element with id: " + orig + "."); return; } if (!DWRUtil._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) { DWRUtil.debug("addRows() can only be used with table, tbody, thead and tfoot elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele)); return; } if (!options) options = {}; if (!options.rowCreator) options.rowCreator = DWRUtil._defaultRowCreator; if (!options.cellCreator) options.cellCreator = DWRUtil._defaultCellCreator; var tr, rowNum; if (DWRUtil._isArray(data)) { for (rowNum = 0; rowNum < data.length; rowNum++) { options.rowData = data[rowNum]; options.rowIndex = rowNum; options.rowNum = rowNum; options.data = null; options.cellNum = -1; tr = DWRUtil._addRowInner(cellFuncs, options); if (tr != null) ele.appendChild(tr); } } else if (typeof data == "object") { rowNum = 0; for (var rowIndex in data) { options.rowData = data[rowIndex]; options.rowIndex = rowIndex; options.rowNum = rowNum; options.data = null; options.cellNum = -1; tr = DWRUtil._addRowInner(cellFuncs, options); if (tr != null) ele.appendChild(tr); rowNum++; } }};/** * @private Internal function to draw a single row of a table. */DWRUtil._addRowInner = function(cellFuncs, options) { var tr = options.rowCreator(options); if (tr == null) return null; for (var cellNum = 0; cellNum < cellFuncs.length; cellNum++) { var func = cellFuncs[cellNum]; var td; //if (typeof func == "string") { // options.data = func; // options.cellNum = cellNum; // td = options.cellCreator(options); // td.appendChild(document.createTextNode(func)); //} //else { var reply = func(options.rowData); options.data = reply; options.cellNum = cellNum; td = options.cellCreator(options); if (DWRUtil._isHTMLElement(reply, "td")) td = reply; else if (DWRUtil._isHTMLElement(reply)) td.appendChild(reply); else td.innerHTML = reply; //} tr.appendChild(td); } return tr;};/** * @private Default row creation function */DWRUtil._defaultRowCreator = function(options) { return document.createElement("tr");};/** * @private Default cell creation function */DWRUtil._defaultCellCreator = function(options) { return document.createElement("td");};/** * Remove all the children of a given node. * @see http://getahead.ltd.uk/dwr/browser/tables */DWRUtil.removeAllRows = function(ele) { var orig = ele; ele = $(ele); if (ele == null) { DWRUtil.debug("removeAllRows() can't find an element with id: " + orig + "."); return; } if (!DWRUtil._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) { DWRUtil.debug("removeAllRows() can only be used with table, tbody, thead and tfoot elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele)); return; } while (ele.childNodes.length > 0) { ele.removeChild(ele.firstChild); }};/** * @private Is the given node an HTML element (optionally of a given type)? * @param ele The element to test * @param nodeName eg "input", "textarea" - check for node name (optional) * if nodeName is an array then check all for a match. */DWRUtil._isHTMLElement = function(ele, nodeName) { if (ele == null || typeof ele != "object" || ele.nodeName == null) { return false; } if (nodeName != null) { var test = ele.nodeName.toLowerCase(); if (typeof nodeName == "string") { return test == nodeName.toLowerCase(); } if (DWRUtil._isArray(nodeName)) { var match = false; for (var i = 0; i < nodeName.length && !match; i++) { if (test == nodeName[i].toLowerCase()) { match = true; } } return match; } DWRUtil.debug("DWRUtil._isHTMLElement was passed test node name that is neither a string or array of strings"); return false; } return true;};/** * @private Like typeOf except that more information for an object is returned other than "object" */DWRUtil._detailedTypeOf = function(x) { var reply = typeof x; if (reply == "object") { reply = Object.prototype.toString.apply(x); // Returns "[object class]" reply = reply.substring(8, reply.length-1); // Just get the class bit } return reply;};/** * @private Array detector. Work around the lack of instanceof in some browsers. */DWRUtil._isArray = function(data) { return (data && data.join) ? true : false;};/** * @private Date detector. Work around the lack of instanceof in some browsers. */DWRUtil._isDate = function(data) { return (data && data.toUTCString) ? true : false;};/** * @private Used by setValue. Gets around the missing functionallity in IE. */DWRUtil._importNode = function(doc, importedNode, deep) { var newNode; if (importedNode.nodeType == 1 /*Node.ELEMENT_NODE*/) { newNode = doc.createElement(importedNode.nodeName); for (var i = 0; i < importedNode.attributes.length; i++) { var attr = importedNode.attributes[i]; if (attr.nodeValue != null && attr.nodeValue != '') { newNode.setAttribute(attr.name, attr.nodeValue); } } if (typeof importedNode.style != "undefined") { newNode.style.cssText = importedNode.style.cssText; } } else if (importedNode.nodeType == 3 /*Node.TEXT_NODE*/) { newNode = doc.createTextNode(importedNode.nodeValue); } if (deep && importedNode.hasChildNodes()) { for (i = 0; i < importedNode.childNodes.length; i++) { newNode.appendChild(DWRUtil._importNode(doc, importedNode.childNodes[i], true)); } } return newNode;}/** * Used internally when some message needs to get to the programmer */DWRUtil.debug = function(message) { alert(message);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -