📄 util.js.svn-base
字号:
dwr.util._setValuesRecursive(data, prefix);};/** * @private Recursive helper for setValues() */dwr.util._setValuesRecursive = function(data, idpath) { // Array containing objects -> add "[n]" to prefix and make recursive call // for each item object if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) { for (var i = 0; i < data.length; i++) { dwr.util._setValuesRecursive(data[i], idpath+"["+i+"]"); } } // Object (not array) -> handle nested object properties else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) { for (var prop in data) { var subidpath = idpath ? idpath+"."+prop : prop; // Object (not array), or array containing objects -> call ourselves recursively if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop]) || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) { dwr.util._setValuesRecursive(data[prop], subidpath); } // Functions -> skip else if (typeof data[prop] == "function") { // NOP } // Only simple values left (or array of simple values, or empty array) // -> call setValue() else { // Are there any elements with that id or name if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) { dwr.util.setValue(subidpath, data[prop]); } } } }};/** * Given a map, or a recursive structure consisting of arrays and maps, call * getValue() for all leaf entries and use intermediate levels to form nested * element ids. * Given a string or element that refers to a form, create an object from the * elements of the form. * @see http://getahead.org/dwr/browser/util/getvalues */dwr.util.getValues = function(data, options) { if (typeof data == "string" || dwr.util._isHTMLElement(data)) { return dwr.util.getFormValues(data); } else { var prefix = ""; if (options != null && options.prefix) prefix = options.prefix; if (options != null && options.idPrefix) prefix = options.idPrefix; dwr.util._getValuesRecursive(data, prefix); return data; }};/** * Given a string or element that refers to a form, create an object from the * elements of the form. * @see http://getahead.org/dwr/browser/util/getvalues */dwr.util.getFormValues = function(eleOrNameOrId) { var ele = null; if (typeof eleOrNameOrId == "string") { ele = document.forms[eleOrNameOrId]; if (ele == null) ele = dwr.util.byId(eleOrNameOrId); } else if (dwr.util._isHTMLElement(eleOrNameOrId)) { ele = eleOrNameOrId; } if (ele != null) { if (ele.elements == null) { alert("getFormValues() requires an object or reference to a form element."); return null; } var reply = {}; var name; var value; for (var i = 0; i < ele.elements.length; i++) { if (ele[i].type in {button:0,submit:0,reset:0,image:0,file:0}) continue; if (ele[i].name) { name = ele[i].name; value = dwr.util.getValue(name); } else { if (ele[i].id) name = ele[i].id; else name = "element" + i; value = dwr.util.getValue(ele[i]); } reply[name] = value; } return reply; }};/** * @private Recursive helper for getValues(). */dwr.util._getValuesRecursive = function(data, idpath) { // Array containing objects -> add "[n]" to idpath and make recursive call // for each item object if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) { for (var i = 0; i < data.length; i++) { dwr.util._getValuesRecursive(data[i], idpath+"["+i+"]"); } } // Object (not array) -> handle nested object properties else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) { for (var prop in data) { var subidpath = idpath ? idpath+"."+prop : prop; // Object, or array containing objects -> call ourselves recursively if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop]) || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) { dwr.util._getValuesRecursive(data[prop], subidpath); } // Functions -> skip else if (typeof data[prop] == "function") { // NOP } // Only simple values left (or array of simple values, or empty array) // -> call getValue() else { // Are there any elements with that id or name if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) { data[prop] = dwr.util.getValue(subidpath); } } } }};/** * Add options to a list from an array or map. * @see http://getahead.org/dwr/browser/lists */dwr.util.addOptions = function(ele, data/*, options*/) { ele = dwr.util._getElementById(ele, "addOptions()"); if (ele == null) return; 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 argcount = arguments.length; var options = {}; var lastarg = arguments[argcount - 1]; if (argcount > 2 && dwr.util._isObject(lastarg)) { options = lastarg; argcount--; } var arg3 = null; if (argcount >= 3) arg3 = arguments[2]; var arg4 = null; if (argcount >= 4) arg4 = arguments[3]; if (!options.optionCreator && useOptions) options.optionCreator = dwr.util._defaultOptionCreator; if (!options.optionCreator && useLi) options.optionCreator = dwr.util._defaultListItemCreator; 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++) { options.data = data[i]; options.text = null; options.value = null; if (useOptions) { if (arg3 != null) { if (arg4 != null) { options.text = dwr.util._getValueFrom(data[i], arg4); options.value = dwr.util._getValueFrom(data[i], arg3); } else options.text = options.value = dwr.util._getValueFrom(data[i], arg3); } else options.text = options.value = dwr.util._getValueFrom(data[i]); if (options.text != null || options.value) { var opt = options.optionCreator(options); opt.text = options.text; opt.value = options.value; ele.options[ele.options.length] = opt; } } else { options.value = dwr.util._getValueFrom(data[i], arg3); if (options.value != null) { li = options.optionCreator(options); if (dwr.util._shouldEscapeHtml(options)) { options.value = dwr.util.escapeHtml(options.value); } li.innerHTML = options.value; ele.appendChild(li); } } } } else if (arg4 != null) { if (!useOptions) { alert("dwr.util.addOptions can only create select lists from objects."); return; } for (var prop in data) { options.data = data[prop]; options.value = dwr.util._getValueFrom(data[prop], arg3); options.text = dwr.util._getValueFrom(data[prop], arg4); if (options.text != null || options.value) { var opt = options.optionCreator(options); opt.text = options.text; opt.value = options.value; ele.options[ele.options.length] = opt; } } } else { if (!useOptions) { dwr.util._debug("dwr.util.addOptions can only create select lists from objects."); return; } for (var prop in data) { options.data = data[prop]; if (!arg3) { options.value = prop; options.text = data[prop]; } else { options.value = data[prop]; options.text = prop; } if (options.text != null || options.value) { var opt = options.optionCreator(options); opt.text = options.text; opt.value = options.value; ele.options[ele.options.length] = opt; } } } // All error routes through this function result in a return, so highlight now dwr.util.highlight(ele, options); };/** * @private Get the data from an array function for dwr.util.addOptions */dwr.util._getValueFrom = function(data, method) { if (method == null) return data; else if (typeof method == 'function') return method(data); else return data[method];};/** * @private Default option creation function */dwr.util._defaultOptionCreator = function(options) { return new Option();};/** * @private Default list item creation function */dwr.util._defaultListItemCreator = function(options) { return document.createElement("li");};/** * Remove all the options from a select list (specified by id) * @see http://getahead.org/dwr/browser/lists */dwr.util.removeAllOptions = function(ele) { ele = dwr.util._getElementById(ele, "removeAllOptions()"); if (ele == null) return; var useOptions = dwr.util._isHTMLElement(ele, "select"); var useLi = dwr.util._isHTMLElement(ele, ["ul", "ol"]); if (!useOptions && !useLi) { dwr.util._debug("removeAllOptions() can only be used with select, ol and ul elements. Attempt to use: " + dwr.util._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.org/dwr/browser/tables */dwr.util.addRows = function(ele, data, cellFuncs, options) { ele = dwr.util._getElementById(ele, "addRows()"); if (ele == null) return; if (!dwr.util._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) { dwr.util._debug("addRows() can only be used with table, tbody, thead and tfoot elements. Attempt to use: " + dwr.util._detailedTypeOf(ele)); return; } if (!options) options = {}; if (!options.rowCreator) options.rowCreator = dwr.util._defaultRowCreator; if (!options.cellCreator) options.cellCreator = dwr.util._defaultCellCreator; var tr, rowNum; if (dwr.util._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 = dwr.util._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 = dwr.util._addRowInner(cellFuncs, options); if (tr != null) ele.appendChild(tr); rowNum++; } } dwr.util.highlight(ele, options);};/** * @private Internal function to draw a single row of a table. */dwr.util._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]; if (typeof func == 'function') options.data = func(options.rowData, options); else options.data = func || ""; options.cellNum = cellNum; var td = options.cellCreator(options); if (td != null) { if (options.data != null) { if (dwr.util._isHTMLElement(options.data)) td.appendChild(options.data); else { if (dwr.util._shouldEscapeHtml(options) && typeof(options.data) == "string") { td.innerHTML = dwr.util.escapeHtml(options.data); } else { td.innerHTML = options.data; } } } tr.appendChild(td); } } return tr;};/** * @private Default row creation function */dwr.util._defaultRowCreator = function(options) { return document.createElement("tr");};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -