📄 util-js.js
字号:
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) {
// Are there any elements with that id or name
if ($(property) != null || document.getElementsByName(property).length >= 1) {
DWRUtil.setValue(property, 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
*/
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) {
// Are there any elements with that id or name
if ($(property) != null || document.getElementsByName(property).length >= 1) {
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) {
ele = DWRUtil._getElementById(ele, "addOptions()");
if (ele == null) 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 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) {
ele.options[ele.options.length] = new Option(text, value);
}
}
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) {
if (!useOptions) {
alert("DWRUtil.addOptions can only create select lists from objects.");
return;
}
for (var prop in data) {
value = DWRUtil._getValueFrom(data[prop], arguments[2]);
text = DWRUtil._getValueFrom(data[prop], arguments[3]);
if (text || value) {
ele.options[ele.options.length] = new Option(text, value);
}
}
}
else {
if (!useOptions) {
DWRUtil.debug("DWRUtil.addOptions can only create select lists from objects.");
return;
}
for (var prop in data) {
if (typeof data[prop] != "function") {
if (arguments[2]) {
ele.options[ele.options.length] = new Option(prop, data[prop]);
}
else {
ele.options[ele.options.length] = new Option(data[prop], prop);
}
}
}
}
};
/**
* @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) {
ele = DWRUtil._getElementById(ele, "removeAllOptions()");
if (ele == null) 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) {
ele = DWRUtil._getElementById(ele, "addRows()");
if (ele == null) 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 reply = func(options.rowData, options);
options.data = reply;
options.cellNum = cellNum;
var td = options.cellCreator(options);
if (td != null) {
if (reply != null) {
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) {
ele = DWRUtil._getElementById(ele, "removeAllRows()");
if (ele == null) 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);
}
};
/**
* $(ele).className = "X", that we can call from Java easily
*/
DWRUtil.setClassName = function(ele, className) {
ele = DWRUtil._getElementById(ele, "setClassName()");
if (ele == null) return;
ele.className = className;
};
/**
* $(ele).className += "X", that we can call from Java easily.
*/
DWRUtil.addClassName = function(ele, className) {
ele = DWRUtil._getElementById(ele, "addClassName()");
if (ele == null) return;
ele.className += " " + className;
};
/**
* $(ele).className -= "X", that we can call from Java easily
* From code originally by Gavin Kistner
*/
DWRUtil.removeClassName = function(ele, className) {
ele = DWRUtil._getElementById(ele, "removeClassName()");
if (ele == null) return;
var regex = new RegExp("(^|\\s)" + className + "(\\s|$)", 'g');
ele.className = ele.className.replace(regex, '');
};
/**
* $(ele).className |= "X", that we can call from Java easily.
*/
DWRUtil.toggleClassName = function(ele, className) {
ele = DWRUtil._getElementById(ele, "toggleClassName()");
if (ele == null) return;
var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
if (regex.test(element.className)) {
ele.className = ele.className.replace(regex, '');
}
else {
ele.className += " " + className;
}
};
/**
* Clone a node and insert it into the document just above the 'template' node
* @see http://getahead.ltd.uk/dwr/???
*/
DWRUtil.cloneNode = function(ele, options) {
ele = DWRUtil._getElementById(ele, "cloneNode()");
if (ele == null) return;
if (options == null) options = {};
var clone = ele.cloneNode(true);
if (options.idPrefix || options.idSuffix) {
DWRUtil._updateIds(clone, options);
}
else {
DWRUtil._removeIds(clone);
}
ele.parentNode.insertBefore(clone, ele);
return clone;
};
/**
* @private Update all of the ids in an element tree
*/
DWRUtil._updateIds = function(ele, options) {
if (options == null) options = {};
if (ele.id) {
ele.setAttribute("id", (options.idPrefix || "") + ele.id + (options.idSuffix || ""));
}
var children = ele.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children.item(i);
if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
DWRUtil._updateIds(child, options);
}
}
};
/**
* @private Remove all the Ids from an element
*/
DWRUtil._removeIds = function(ele) {
if (ele.id) ele.removeAttribute("id");
var children = ele.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children.item(i);
if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
DWRUtil._removeIds(child);
}
}
};
/**
* @private Helper to turn a string into an element with an error message
*/
DWRUtil._getElementById = function(ele, source) {
var orig = ele;
ele = $(ele);
if (ele == null) {
DWRUtil.debug(source + " can't find an element with id: " + orig + ".");
}
return ele;
};
/**
* @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;
};
/** The array of current debug items */
DWRUtil._debugDisplay = [];
/** What is the maximum length of the debug items array */
DWRUtil._debugMaxLength = 50;
/**
* Used internally when some message needs to get to the programmer
*/
DWRUtil.debug = function(message) {
var debug = $("dwr-debug");
if (debug) {
while (DWRUtil._debugDisplay.length >= DWRUtil._debugMaxLength) {
DWRUtil._debugDisplay.shift();
}
DWRUtil._debugDisplay.push(message);
var contents = "";
for (var i = 0; i < DWRUtil._debugDisplay.length; i++) {
contents += DWRUtil._debugDisplay[i] + "<br/>";
}
DWRUtil.setValue("dwr-debug", contents);
}
else if (window.console) window.console.log(message);
else if (window.opera && window.opera.postError) window.opera.postError(message);
//else if (window.navigator.product == "Gecko") window.dump(message + "\n");
alert(message);
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -