Browse Source

jst el() function now utilizes the arguments object instead of explicit parameter names.

dev-tmpl
Bryan Miller 5 years ago
parent
commit
f4e184e9de
1 changed files with 30 additions and 41 deletions
  1. +30
    -41
      jst/jst.js

+ 30
- 41
jst/jst.js View File

@@ -25,18 +25,6 @@ class ENode{

toString(){
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){
var attr = this.__attribs.reduce(function(a, at){
a += ((a !== "") ? " " : "") + at[0].trim() + "=\"" + at[1] + "\"";
@@ -64,39 +52,40 @@ class ENode{
}


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;
}


Loading…
Cancel
Save