diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-01-07 15:40:20 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-07-07 15:25:49 +0200 |
commit | 26e54bb01f62c2bcc6ac9ff96afe1e653838d45f (patch) | |
tree | de7559bc9e743843897e118292b37f1f95376e14 /modules/luci-base/htdocs | |
parent | 344c4c511936062ec4521d42d1c1ba51577daee0 (diff) |
luci-base: luci.js: convert LuCI.dom to Class instance
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/htdocs')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 225 |
1 files changed, 119 insertions, 106 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index b86d499c6..fb9ae2f7f 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -653,144 +653,157 @@ halt: function() { return Request.poll.stop() }, run: function() { return Request.poll.start() }, - Class: Class, - Request: Request - }); - - /* DOM manipulation */ - LuCI.prototype.dom = { - elem: function(e) { - return (typeof(e) === 'object' && e !== null && 'nodeType' in e); - }, + /* DOM manipulation */ + dom: Class.singleton({ + __name__: 'LuCI.DOM', - parse: function(s) { - var elem; + elem: function(e) { + return (e != null && typeof(e) == 'object' && 'nodeType' in e); + }, - try { - domParser = domParser || new DOMParser(); - elem = domParser.parseFromString(s, 'text/html').body.firstChild; - } - catch(e) {} + parse: function(s) { + var elem; - if (!elem) { try { - dummyElem = dummyElem || document.createElement('div'); - dummyElem.innerHTML = s; - elem = dummyElem.firstChild; + domParser = domParser || new DOMParser(); + elem = domParser.parseFromString(s, 'text/html').body.firstChild; } - catch (e) {} - } + catch(e) {} - return elem || null; - }, + if (!elem) { + try { + dummyElem = dummyElem || document.createElement('div'); + dummyElem.innerHTML = s; + elem = dummyElem.firstChild; + } + catch (e) {} + } - matches: function(node, selector) { - var m = this.elem(node) ? node.matches || node.msMatchesSelector : null; - return m ? m.call(node, selector) : false; - }, + return elem || null; + }, - parent: function(node, selector) { - if (this.elem(node) && node.closest) - return node.closest(selector); + matches: function(node, selector) { + var m = this.elem(node) ? node.matches || node.msMatchesSelector : null; + return m ? m.call(node, selector) : false; + }, - while (this.elem(node)) - if (this.matches(node, selector)) - return node; - else - node = node.parentNode; + parent: function(node, selector) { + if (this.elem(node) && node.closest) + return node.closest(selector); - return null; - }, + while (this.elem(node)) + if (this.matches(node, selector)) + return node; + else + node = node.parentNode; - append: function(node, children) { - if (!this.elem(node)) return null; + }, - if (Array.isArray(children)) { - for (var i = 0; i < children.length; i++) - if (this.elem(children[i])) - node.appendChild(children[i]); - else if (children !== null && children !== undefined) - node.appendChild(document.createTextNode('' + children[i])); + append: function(node, children) { + if (!this.elem(node)) + return null; - return node.lastChild; - } - else if (typeof(children) === 'function') { - return this.append(node, children(node)); - } - else if (this.elem(children)) { - return node.appendChild(children); - } - else if (children !== null && children !== undefined) { - node.innerHTML = '' + children; - return node.lastChild; - } + if (Array.isArray(children)) { + for (var i = 0; i < children.length; i++) + if (this.elem(children[i])) + node.appendChild(children[i]); + else if (children !== null && children !== undefined) + node.appendChild(document.createTextNode('' + children[i])); - return null; - }, + return node.lastChild; + } + else if (typeof(children) === 'function') { + return this.append(node, children(node)); + } + else if (this.elem(children)) { + return node.appendChild(children); + } + else if (children !== null && children !== undefined) { + node.innerHTML = '' + children; + return node.lastChild; + } - content: function(node, children) { - if (!this.elem(node)) return null; + }, - while (node.firstChild) - node.removeChild(node.firstChild); + content: function(node, children) { + if (!this.elem(node)) + return null; - return this.append(node, children); - }, + while (node.firstChild) + node.removeChild(node.firstChild); - attr: function(node, key, val) { - if (!this.elem(node)) - return null; + return this.append(node, children); + }, - var attr = null; + attr: function(node, key, val) { + if (!this.elem(node)) + return null; + + var attr = null; - if (typeof(key) === 'object' && key !== null) - attr = key; - else if (typeof(key) === 'string') - attr = {}, attr[key] = val; + if (typeof(key) === 'object' && key !== null) + attr = key; + else if (typeof(key) === 'string') + attr = {}, attr[key] = val; - for (key in attr) { - if (!attr.hasOwnProperty(key) || attr[key] === null || attr[key] === undefined) - continue; + for (key in attr) { + if (!attr.hasOwnProperty(key) || attr[key] == null) + continue; - switch (typeof(attr[key])) { - case 'function': - node.addEventListener(key, attr[key]); - break; + switch (typeof(attr[key])) { + case 'function': + node.addEventListener(key, attr[key]); + break; - case 'object': - node.setAttribute(key, JSON.stringify(attr[key])); - break; + case 'object': + node.setAttribute(key, JSON.stringify(attr[key])); + break; - default: - node.setAttribute(key, attr[key]); + default: + node.setAttribute(key, attr[key]); + } } - } - }, + }, - create: function() { - var html = arguments[0], - attr = (arguments[1] instanceof Object && !Array.isArray(arguments[1])) ? arguments[1] : null, - data = attr ? arguments[2] : arguments[1], - elem; + create: function() { + var html = arguments[0], + attr = arguments[1], + data = arguments[2], + elem; - if (this.elem(html)) - elem = html; - else if (html.charCodeAt(0) === 60) - elem = this.parse(html); - else - elem = document.createElement(html); + if (!(attr instanceof Object) || Array.isArray(attr)) + data = attr, attr = null; - if (!elem) - return null; + if (Array.isArray(html)) { + elem = document.createDocumentFragment(); + for (var i = 0; i < html.length; i++) + elem.appendChild(this.create(html[i])); + } + else if (this.elem(html)) { + elem = html; + } + else if (html.charCodeAt(0) === 60) { + elem = this.parse(html); + } + else { + elem = document.createElement(html); + } - this.attr(elem, attr); - this.append(elem, data); + if (!elem) + return null; - return elem; - } - }; + this.attr(elem, attr); + this.append(elem, data); + + return elem; + } + }), + + Class: Class, + Request: Request + }); XHR = Class.extend({ __name__: 'LuCI.XHR', |