Advertisement
SReject

cHTML.js

Jan 29th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (root, factory) {
  2.    
  3.     // requirejs|amd loader
  4.     if (typeof define === 'function' && define.amd) {
  5.         define(factory);
  6.        
  7.     // node js
  8.     } else if (typeof module === 'object' && module.exports) {
  9.         module.exports = factory();
  10.    
  11.     // global
  12.     } else {
  13.         root.cHTML = factory();
  14.     }
  15. })(
  16. typeof self !== 'undefined' ? self : this,
  17. function () {
  18.  
  19.     const clsMethodTest = /^(?:add|(?:rem(?:ove)?)|(?:tog(?:gle)?))$/
  20.     class cHTMLNode {
  21.         constructor(node) {
  22.             if (node instanceof cHTMLNode) {
  23.                 return node;
  24.             }
  25.             this.node = node;
  26.             this._eventhandlers = {};
  27.         }
  28.         set(attrs) {
  29.             let node = this.node;
  30.             Object.keys(attrs).forEach(function (key) {
  31.                 node.setAttribute(key, attrs[key]);
  32.             });
  33.             return this;
  34.         }
  35.         unset(attrs) {
  36.             let node = this.node;
  37.             Object.keys(attrs).forEach(function (key) {
  38.                 if (this.node.hasAttribute(key)) {
  39.                     this.node.removeAttribute(key, attrs[key]);
  40.                 }
  41.             });
  42.             return this;
  43.         }
  44.         cls(classes, method) {
  45.             if (typeof classes === 'string') {
  46.                 classes = String(classes).trim().split(/\s+/g);
  47.  
  48.             } else if (typeof classes === "object" && !Array.isArray(classes)) {
  49.                 classes = [classes];
  50.  
  51.             } else if (!Array.isArray(classes)) {
  52.                 throw new TypeError('invalid class list');
  53.             }
  54.             method = method || 'add';
  55.             if (typeof method !== 'string' || !clsMethodTest.test(method = method.toLowerCase())) {
  56.                 throw new Error('Invalid method');
  57.             }
  58.             let seen = {},
  59.                 className = this.node.className.trim();
  60.             if (className === "") {
  61.                 className = [];
  62.             } else {
  63.                 className = className.split(/\s+/g).filter(function (item) {
  64.                     return !seen.hasOwnProperty(item) && (seen[item] = true);
  65.                 });
  66.             }
  67.             classes.forEach(function (cls) {
  68.                 if (typeof cls === '') {
  69.                     return
  70.                 }
  71.                 let action = method;
  72.                 if (typeof cls === 'object') {
  73.                     action = cls.action || method;
  74.                     cls = cls.name;
  75.                     if (typeof action !== "string" || clsMethodTest.test(action = action.toLowerCase())) {
  76.                         throw new TypeError('Invalid method');
  77.                     }
  78.                 } else if (typeof cls !== 'string') {
  79.                     throw new TypeError('invalid class');
  80.                 }
  81.                 action = action.substring(0,3);
  82.                 let idx = className.indexOf(cls);
  83.                 if (idx === -1 && (action === 'tog' || action === 'add')) {
  84.                     className.push(cls);
  85.                 } else if (idx > -1 && (action === 'tog' || action === 'rem')) {
  86.                     className.splice(idx, 1);
  87.                 }
  88.             });
  89.             this.node.className = className.join(" ");
  90.             return this;
  91.         }
  92.         text(text, overwrite) {
  93.             if (overwrite) {
  94.                 this.empty();
  95.             }
  96.             this.node.appendChild(document.createTextNode(text));
  97.             return this;
  98.         }
  99.         html(html, overwrite) {
  100.             if (overwrite) {
  101.                 this.empty();
  102.             }
  103.             this.node.innerHTML += html;
  104.             return this;
  105.         }
  106.         empty() {
  107.             let node = this.node, child;
  108.             while (child = node.lastChild) {
  109.                 node.removeChild(child);
  110.             }
  111.             return this;
  112.         }
  113.         on(name, handler, capture, once) {
  114.             if (typeof name !== 'string' || name == null) {
  115.                 throw new TypeError('invalid event name');
  116.             }
  117.             if (typeof handler !== 'function') {
  118.                 throw new TypeError('invalid handler');
  119.             }
  120.             if (capture != null && typeof capture !== 'boolean') {
  121.                 throw new TypeError('invalid capture parameter');
  122.             }
  123.             if (once != null && typeof once !== 'boolean') {
  124.                 throw new TypeError('invalid once parameter');
  125.             }
  126.             const self = this,
  127.                 evthandler = {
  128.                     handler: handler,
  129.                     capture: capture,
  130.                     once: once,
  131.                     func: function callback(evt) {
  132.                         if (once) {
  133.                             self.off(name, handler, capture, once);
  134.                         }
  135.                         handler.call(self.node, evt || event);
  136.                     }
  137.                 };
  138.             if (!this._eventhandlers.hasOwnProperty(name)) {
  139.                 this._eventhandlers[name] = [];
  140.             }
  141.             this._eventhandlers[name].push(evthandler);
  142.             this.node.addEventListener(name, evthandler.func, capture == null ? false : capture);
  143.             return this;
  144.         }
  145.         off(name, handler, capture, once) {
  146.             if (typeof name !== 'string' || name == null) {
  147.                 throw new TypeError('invalid event name');
  148.             }
  149.             if (typeof handler !== 'function') {
  150.                 throw new TypeError('invalid handler');
  151.             }
  152.             if (capture != null && typeof capture !== 'boolean') {
  153.                 throw new TypeError('invalid bubble parameter');
  154.             }
  155.             if (this._eventhandlers.hasOwnProperty(name)) {
  156.                 let handlers = this._eventhandlers[name],
  157.                     idx = handlers.findIndex(function (item, idx) {
  158.                         return item.handler === handler && item.capture === capture && item.once === once;
  159.                     });
  160.                 if (idx > -1) {
  161.                     handler = handlers[idx].func;
  162.                     if (handlers.length === 1) {
  163.                         delete this._eventhandlers[name]
  164.                     } else {
  165.                         handlers.splice(idx, 1);
  166.                     }
  167.                     return this;
  168.                 }
  169.             }
  170.             this.node.removeEventListener(name, handler, capture == null ? false : capture)
  171.             return this;
  172.         }
  173.         once(name, handler, capture) {
  174.             return this.on(name, handler, capture, true);
  175.         }
  176.         onceoff(name, handler, capture) {
  177.             return this.off(name, handler, capture, true);
  178.         }
  179.         attach(target, method) {
  180.             if (target instanceof cHTMLNode) {
  181.                 target = target.node;
  182.             } else if (typeof target === 'string') {
  183.                 target = document.querySelector(target);
  184.             }
  185.             if (!(target instanceof Node)) {
  186.                 throw new TypeError('attach target not a node');
  187.             }
  188.             if (method == null) {
  189.                 method = 'to';
  190.             }
  191.             if (typeof method !== 'string' || !/^(?:first|before|after|to|last)$/.test(method = String(method).toLowerCase())) {
  192.                 throw new TypeError('invalid attachment method');
  193.             }
  194.             if (!target.parent && (method === 'before' || method === 'after')) {
  195.                 throw new Error('method not applicable for target');
  196.             }
  197.             this.detach();
  198.             if (method === 'first' && target.firstChild) {
  199.                 target.insertBefore(this.node, target.firstChild);
  200.  
  201.             } else if (method === 'before') {
  202.                 target.parent.insertBefore(this.node, target);
  203.  
  204.             } else if (method === 'after') {
  205.                 if (target.nextSibling) {
  206.                     target.parent.insertBefore(this.node, target.nextSibling);
  207.  
  208.                 } else {
  209.                     target.parent.appendChild(this.node);
  210.                 }
  211.             } else {
  212.                 target.appendChild(this.node);
  213.             }
  214.             return this;
  215.         }
  216.         detach() {
  217.             if (this.node.parentNode) {
  218.                 this.node.parentNode.removeChild(this.node);
  219.             }
  220.             return this;
  221.         }
  222.     }
  223.     return function cHTML(target) {
  224.         if (target instanceof cHTMLNode) {
  225.             return target;
  226.         }
  227.         if (typeof target === "string") {
  228.             target = String(target);
  229.             if (/^<.*>$/.test(target)) {
  230.                 target = document.createElement(target.substring(1, target.length -1));
  231.             } else {
  232.                 target = document.querySelector(target);
  233.             }
  234.         }
  235.         if (target instanceof Node) {
  236.             return new cHTMLNode(target);
  237.         }
  238.         throw new TypeError('invald target')
  239.     }
  240. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement