/
treeIt.min.js
6 lines (6 loc) · 2.08 KB
/
treeIt.min.js
1
2
3
4
5
6
/*!
* treeIt: Convert your JavaScript objects to HTML, no additional hassle
* https://github.com/alessioalex/treeIt
* License MIT (c) Alexandru Vladutu
*/
(function(){function g(a){b["is"+a]=function(b){return Object.prototype.toString.call(b)=="[object "+a+"]"}}"use strict";var a,b,c,d,e,f;a=this,c=a.treeIt,b={},d=["Object","Array","Arguments","Function","String","Number","Date","RegExp"];for(f=0,e=d.length;f<e;f++)g(d[f]);b.getKeys=Object.keys||function(){var a,b,c,d;return a=Object.prototype.hasOwnProperty,b=!{toString:null}.propertyIsEnumerable("toString"),c=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],d=c.length,function(e){var f,g,h;f=[];if(typeof e!="object"&&typeof e!="function"||e===null)throw new TypeError("Object.keys called on non-object");for(g in e)a.call(e,g)&&f.push(g);if(b)for(h=0;h<d;h++)a.call(e,c[h])&&f.push(c[h]);return f}}(),b.hasBranches=function(a){return typeof a!="undefined"&&a!==null&&(b.isObject(a)||b.isArray(a))},b.getBranches=function(a){return b.hasBranches(a)?b.getKeys(a):[]},b.getDefaultTemplates=function(){return{root:"<ul>{{data}}</ul>",branch:'<li class="branch">{{data}}</li>',branchRoot:'<span class="branchRoot">{{data}}</span>',leaf:'<ul><li class="leaf">{{data}}</li></ul>'}},b.tmpl=function(a,b){return typeof b!="undefined"&&b!==null?a.replace("{{data}}",b):""},b.renderSingleLeaf=function(a){return b.isString(a)&&(a="'"+a+"'"),b.tmpl(b.templates.leaf,a)},b.traverse=function(a){var c,d,e,f,g;c="",d=b.getBranches(a);if(!d.length)c+=b.renderSingleLeaf(a);else{for(e=0,f=d.length;e<f;e++)g=b.tmpl(b.templates.branchRoot,d[e]),b.hasBranches(a[d[e]])?g+=b.traverse(a[d[e]]):g+=b.renderSingleLeaf(a[d[e]]),c+=b.tmpl(b.templates.branch,g);c=b.tmpl(b.templates.root,c)}return c},b.generate=function(a,c){var d,e,f,g;b.templates=b.getDefaultTemplates();if(b.isObject(c)){d=b.getBranches(c);for(e=0,f=d.length;e<f;e++)b.templates[d[e]]=c[d[e]]}return b.traverse(a)},b.noConflict=function(){return a.treeIt=c,b},typeof module!="undefined"&&module.exports?exports=module.exports=b:a.treeIt=b}).call(this)