| toString(){ | toString(){ | ||||
| var r = "<" + this.__tag.toUpperCase(); | var r = "<" + this.__tag.toUpperCase(); | ||||
| if (this.__classes.length > 0){ | |||||
| var cls = this.__classes.reduce(function(a, c){ | |||||
| c = c.split(" "); | |||||
| for (var i=0; i < c.length; i++){ | |||||
| c[i] = c[i].trim(); | |||||
| a += ((a !== "" && c[i].length > 0) ? " " : "") + c[i]; | |||||
| } | |||||
| return a; | |||||
| }, ""); | |||||
| r += " class=\"" + cls + "\""; | |||||
| } | |||||
| if (this.__attribs.length > 0){ | if (this.__attribs.length > 0){ | ||||
| var attr = this.__attribs.reduce(function(a, at){ | var attr = this.__attribs.reduce(function(a, at){ | ||||
| a += ((a !== "") ? " " : "") + at[0].trim() + "=\"" + at[1] + "\""; | a += ((a !== "") ? " " : "") + at[0].trim() + "=\"" + at[1] + "\""; | ||||
| } | } | ||||
| function appendCTX(el, ctx){ | |||||
| el.appendChild((ctx instanceof ENode) ? ctx : ctx.toString()); | |||||
| } | |||||
| function el(){ | |||||
| if (arguments.length < 1) | |||||
| throw new Error("Invalid number of arguments."); | |||||
| function el(ename, ctx, cls, attr){ | |||||
| var e = new ENode(ename); | |||||
| if (ctx){ | |||||
| if (ctx instanceof Array){ | |||||
| ctx.forEach(item => appendCTX(e, item)); | |||||
| } else { | |||||
| appendCTX(e, ctx); | |||||
| } | |||||
| } | |||||
| var ename = arguments[0]; | |||||
| var args = Array.prototype.slice.call(arguments, 1); | |||||
| if (cls){ | |||||
| var cl = cls; | |||||
| if (typeof(cl) === "String") | |||||
| cl = cl.split(" "); | |||||
| if (cl instanceof Array){ | |||||
| cl.forEach(c => e.addClass(c.trim())); | |||||
| } | |||||
| } | |||||
| if (attr){ | |||||
| var keys = Object.keys(attr); | |||||
| keys.forEach(function(key){ | |||||
| if (attr.hasOwnProperty(key)){ | |||||
| if (typeof(attr[key]) === "string"){ | |||||
| e.addAttribute(key.toString(), attr[key]); | |||||
| var e = new ENode(ename); | |||||
| args.forEach(function(item){ | |||||
| if (item === null){return;} | |||||
| // If given an array, assume it to be an array children. | |||||
| if (item instanceof Array){ | |||||
| item.forEach(itm => e.appendChild((itm instanceof ENode) ? itm : itm.toString())); | |||||
| // If given an ENode, then it IS a child | |||||
| } else if (item instanceof ENode){ | |||||
| e.appendChild(item); | |||||
| // All objects are assumed to be attributes. | |||||
| } else if (item instanceof Object){ | |||||
| var keys = Object.keys(item); | |||||
| keys.forEach(function(key){ | |||||
| if (item.hasOwnProperty(key)){ | |||||
| if (typeof(item[key]) === "string"){ | |||||
| e.addAttribute(key.toString(), item[key]); | |||||
| } | |||||
| } | } | ||||
| } | |||||
| }); | |||||
| } | |||||
| }); | |||||
| // Everything else is assumed to be a child. | |||||
| } else { | |||||
| e.appendChild(item.toString()); | |||||
| } | |||||
| }); | |||||
| return e; | return e; | ||||
| } | } | ||||