luci-base: luci.js: convert LuCI.dom to Class instance
authorJo-Philipp Wich <jo@mein.io>
Mon, 7 Jan 2019 14:40:20 +0000 (15:40 +0100)
committerJo-Philipp Wich <jo@mein.io>
Sun, 7 Jul 2019 13:25:49 +0000 (15:25 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/luci.js

index b86d499c6db4c6d001c896a3d91660506db2ac33..fb9ae2f7f6c0e8538fa6a5f411a810b7836c89e1 100644 (file)
                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',