-
Notifications
You must be signed in to change notification settings - Fork 3
/
babajs-min.js
1 lines (1 loc) · 12.6 KB
/
babajs-min.js
1
var BabaJS={_stack:{},_cacheTemplates:!1,_cache:{},generateHTML:function(a,b){this._stack={};if("string"===typeof a){var c=this._compile(a);return this._replaceCompiledTags(c.chtml,c.ctags,b)}if(!a.ready){if(c=this._templates[a.templateName])return c.compiled||(c.compiled=this._compile(c.raw),delete c.raw),this._replaceCompiledTags(c.compiled.chtml,c.compiled.ctags,b);throw Error(a.templateName+" was not found in template manager");}this._asynchGenerateHTML(a,b)},_cfg:{fetcher:null,URLConvertor:null, context:this},setConfig:function(a){a.URLConvertor&&(this._cfg.URLConvertor=a.URLConvertor);a.fetcher&&(this._cfg.fetcher=a.fetcher);a.context&&(this._cfg.context=a.context);a.dep&&(this._dep=a.dep);this._cacheTemplates=a.cacheTemplates;a.flagCb&&(this._cfg.flagCb=a.flagCb)},_dep:{},includeTemplate:function(a,b){var c=this._templates[a];if(c)return c.compiled||(c.compiled=this._compile(c.raw),delete c.raw),this._replaceCompiledTags(c.compiled.chtml,c.compiled.ctags,b);throw Error(a+" was not found in template manager"); },removeTemplate:function(a){delete this._templates[a]},_getMissingTemplates:function(a){for(var b=[],c=0;c<a.length;c++)this._templates[a[c]]||b.push(a[c]);return b},_fetchTemplate:function(a,b,c,e,d){var f=a.fetcher?a.fetcher:this._cfg.fetcher;a=a.context?a.context:this._cfg.context;if(f){var g=this;f.call(a,b,function(a){g._cache[b]=!0;g._templates[b]={compiled:g._compile(a)};e.call(d,!0)})}else this._ajax(c,function(a){!1!==a?(this._cache[b]=!0,this._templates[b]={compiled:this._compile(a)},e.call(d, !0)):e.call(d,!1)},this)},_ajax:function(a,b,c){var e;"undefined"!==typeof ActiveXObject?e=new ActiveXObject("Microsoft.XMLHTTP"):"undefined"!==typeof XMLHttpRequest&&(e=new XMLHttpRequest);e.open("GET",a);e.onreadystatechange=function(){4==e.readyState&&(200==e.status||0==e.status?b.call(c,e.responseText):b.call(c,!1))};e.send()},ensureLocal:function(a,b,c,e){this._ensureLocal({URLConvertor:this._cfg.URLConvertor,fetcher:this._cfg.fetcher,context:this._cfg.context},a,b,c,function(a){e.call(this._cfg.context, a)},this)},_getNonCachedTemplates:function(a){for(var b=[],c=0;c<a.length;c++)this._cache[a[c]]||b.push(a[c]);return b},_ensureLocal:function(a,b,c,e,d,f){c=c?c:[];e=e?e:[];var g=0;b=this._getNonCachedTemplates(b);var l=a.URLConvertor?a.URLConvertor:this._cfg.URLConvertor,m=a.context?a.context:this._cfg.context,h=b.length+e.length+c.length;0==h&&d.call(f,!0);for(var k=0;k<b.length;k++){var r=l?l.call(m,b[k]):"";this._fetchTemplate(a,b[k],r,function(a){if(!1===a)throw Error("Failed to fetch template"+ r);g++;g==h&&d.call(f,!0)},this)}for(k=0;k<c.length;k++)this._fetchJS(c[k],function(){g++;g==h&&d.call(f,!0)},this);for(k=0;k<e.length;k++)this._fetchCSS(e[k],function(){g++;g==h&&d.call(f,!0)},this)},_fetchJS:function(a,b,c){this._js[a]?b.call(c,!0):this._ajax(a,function(e){if(!1===e)b.call(c,!1);else{var d=document.createElement("script");d.type="text/javascript";this._isIE()?d.text=e:d.innerHTML=e;document.getElementsByTagName("head")[0].appendChild(d);this._js[a]=!0;b.call(c,!0)}},this)},_ie:null, _isIE:function(){null===this._ie&&(this._ie=-1!=navigator.userAgent.indexOf("MSIE")?!0:!1);return this._ie},_fetchCSS:function(a,b,c){this._css[a]?b.call(c,!0):this._ajax(a,function(e){if(!1===e)b.call(c,!1);else{var d=document.createElement("style");d.type="text/css";this._isIE()?d.styleSheet.cssText=e:d.innerHTML=e;document.getElementsByTagName("head")[0].appendChild(d);this._css[a]=!0;b.call(c,!0)}},this)},_getDependencies:function(a,b,c){b=b?b:{templates:[],js:[],css:[]};c=c?c:{};if(c[a]||!this._dep[a])return b; c[a]=!0;if(this._dep[a].templates){b.templates=this._uniqueAdd(b.templates,this._dep[a].templates);for(var e=0;e<this._dep[a].templates.length;e++)this._getDependencies(this._dep[a].templates[e],b,c)}this._dep[a].js&&(b.js=this._uniqueAdd(b.js,this._dep[a].js));this._dep[a].css&&(b.css=this._uniqueAdd(b.css,this._dep[a].css));return b},_uniqueAdd:function(a,b){for(var c=[],e={},d=0;d<a.length;d++)c.push(a[d]),e[a[d]]=!0;for(d=0;d<b.length;d++)e[b[d]]||c.push(b[d]);return c},_asynchGenerateHTML:function(a, b){var c=a.context?a.context:this._cfg.context,e=[a.templateName];a.requires&&a.requires instanceof Array&&(e=e.concat(a.requires));a.requires&&a.requires.templates&&(e=e.concat(a.requires.templates));var d=this._getMissingTemplates(e),f=[];a.requires&&a.requires.js&&(f=a.requires.js);var g=[];a.requires&&a.requires.css&&(g=a.requires.css);var l=this._getDependencies(a.templateName),m={};m[a.templateName]=!0;for(var h=0;h<e.length;h++)this._getDependencies(e[h],l,m);d=this._uniqueAdd(d,l.templates); f=this._uniqueAdd(f,l.js);g=this._uniqueAdd(g,l.css);this._ensureLocal(a,d,f,g,function(){var d=this._templates[a.templateName];d.compiled||(d.compiled=this._compile(d.raw),delete d.raw);d=this._replaceCompiledTags(d.compiled.chtml,d.compiled.ctags,b);a.ready&&a.ready.call(c,d,a.templateName)},this)},_templates:{},_css:{},_js:{},addTemplates:function(a){for(var b in a)this.addTemplate(b,a[b])},addTemplate:function(a,b){if(!this._templates[a]){var c=this._compile(b);this._templates[a]={compiled:c}; this._cacheTemplates&&(this._cache[a]=!0)}},getTemplateObj:function(a){return!this._templates[a]?null:{_tplObj:this._templates[a],_babajs:this,_tplName:a,toString:function(){return this.compile()},compile:function(a){return this._babajs.includeTemplate(this._tplName,a)}}},_replaceCompiledTags:function(a,b,c){var e=/_{([\d]+)}_/g,d=/_{[\d]+}_/,f;for(e.lastIndex=0;f=e.exec(a);)a=a.replace(d,this._evalCtag(b,f[1],c)),e.lastIndex=0;return a},_evalCtag:function(a,b,c){b=a[b];switch(b.type){case "code":return this._evalCodeTag(c, b);case "assign":return this._evalCodeTag(c,b,!0);case "if":return this._evalConditionTag(c,a,b);case "loop":return this._evalLoopTag(c,a,b);default:return"Unknown tag"}},_evalLoopTag:function(a,b,c){var e="",d="",f;for(f in this._stack)e+="var "+f+"="+this._stringify(this._stack[f])+";\r\n";var g="";for(f in this._stack)g+="this._stack['"+f+"'] = "+f+";\r\n";for(f=0;f<c.vars.length;f++)g+="this._stack['"+c.vars[f]+"'] = "+c.vars[f]+";\r\n";f="var _retVal = '';\r\n"+c.code+"{\r\n\t "+g+"\r\n _retVal += this._replaceCompiledTags('"+ c.chtml1.replace(/[\r\n]/g,"")+"',ctags,data);} return _retVal;";try{d=(new Function("data","ctags","try{"+e+f+"}catch(_err){return _err;}finally{"+g+"}")).call(this,a,b),"string"==typeof d&&(d=d.replace(/[\$]/g,"$"))}catch(l){throw this._log("ERROR: ",l," Fn=_evalLoopTag ctag=",c," data=",a),l;}return d},_log:function(){"undefined"!==typeof console&&console.log.apply(console,arguments)},_evalConditionTag:function(a,b,c){var e="",d="",f;for(f in this._stack)e+="var "+f+"="+this._stringify(this._stack[f])+ ";\r\n";var g="";for(f in this._stack)g+="this._stack['"+f+"'] = "+f+";\r\n";for(f=0;f<c.vars.length;f++)g+="this._stack['"+c.vars[f]+"'] = "+c.vars[f]+";\r\n";e="try{"+e+("return ("+c.code+");")+"}catch(_err){return _err;}finally{"+g+"}";try{return(d=(new Function("data",e)).call(this,a))?this._replaceCompiledTags(c.chtml1,b,a):this._replaceCompiledTags(c.chtml2,b,a)}catch(l){throw this._log("ERROR: ",l," Fn=_evalConditionTag ctag=",c," data=",a),l;}},_evalCodeTag:function(a,b,c){var e="",d="",f; for(f in this._stack)e+="var "+f+"="+this._stringify(this._stack[f])+";\r\n";var g="";for(f in this._stack)g+="this._stack['"+f+"'] = "+f+";\r\n";for(f=0;f<b.vars.length;f++)g+="this._stack['"+b.vars[f]+"'] = "+b.vars[f]+";\r\n";c="try{"+e+((!0===c?"return ":"")+b.code+(!0===c?";":""))+"}catch(_err){return _err;}finally{"+g+" }";try{d=(new Function("data",c)).call(this,a);"undefined"===typeof d&&(d="");if(d instanceof Error)return this._log("BabaJS Error: ",d.message,d.href,d.lineNo,d.source),d.message; "string"==typeof d&&(d=d.replace(/[\$]/g,"$"))}catch(l){throw this._log("ERROR: ",l," Fn=_evalCodeTag ctag=",b," data=",a),l;}b.flags.length&&(this._cfg.flagCb?(a=this._cfg.flagCb.call(this._cfg.context,d,b.flags),d="string"==typeof a?a:this._processPredefinedFlags(b.flags,d)):d=this._processPredefinedFlags(b.flags,d));return d},_processPredefinedFlags:function(a,b){return"s"==a[0]?this._secureText(b,"low"):"S"==a[0]?this._secureText(b,"high"):b},secureText:function(a,b){return this._secureText(a, "undefined"==typeof b?"low":b)},_secureText:function(a,b){if(!a)return a;var c=this;return a.replace({low:/[\<\>\"\']/g,high:/[\<\>\&\"\'\`\,\!\@\$\%\(\)\[\]\{\}\=\+]/g}[b],function(a){return c._escaped[a]})},_escaped:{"'":"‘","<":"<",">":">","&":"&",'"':""","`":"`",",":",","!":"!","@":"@",$:"$","(":"(",")":")","[":"[","]":"]","{":"{","}":"}","=":"=","+":"+","%":"%"},_stringify:function(a){if("undefined"!==typeof JSON)return JSON.stringify(a); if("string"==typeof a||"number"==typeof a||"boolean"==typeof a)return a.toString();throw Error("Can't stringify an Object or an Array without JSON Support. please include a JSON object to the window object. Try https://github.com/douglascrockford/JSON-js");},_compile:function(a){for(var b=[],c=0,e={chtml:"",ctags:{}},d,f="",g=null,l="chtml",m=null,h="html",k=0,r=a.length;k<r;k++)if(d=a.charAt(k),"<"==d&&k<r-1&&"%"==a.charAt(k+1)){var p=this._getCtagType(a,k+2),m=p;if("code"==p||"assign"==p||"if"== p||"loop"==p){0<b.length&&(e.ctags[b[b.length-1]]._state=h);var q=this._getNewCtag(p);b.push(c);e.ctags[c]=q;this._addToCurrent(l,h,e,"_{"+c+"}_");l=c;c++}h="tag"}else if("flag"==h)if(","!=d&&" "!=d&&'"'!=d&&"'"!=d&&")"!=d)f+=d,"."==d&&"number"==g?g="float":"0"<=d&&"9">=d&&"string"!==g&&"float"!==g?g="number":"string"==g;else if(","==d||" "==d)f.length&&(d=f,"number"==g&&(d=parseInt(f)),"float"==g&&(d=parseFloat(f)),q.flags.push(d)),f="",g=null;else{if(")"==d&&(f.length&&(d=f,"number"==g&&(d=parseInt(f)), "float"==g&&(d=parseFloat(f)),q.flags.push(d)),f="",g=null," "==a.charAt(k+1)||"="==a.charAt(k+1)))h="code","="==a.charAt(k+1)&&(p=q.type="assign"),k++}else if("tag"==h)("code"==p||"assign"==p)&&"%"==d&&"f"==a.charAt(k+1)?(h="flag",k+=2,f="",g=null):"code"==p&&"%"==d&&"s"!=a.charAt(k+1).toLowerCase()&&"f"!=a.charAt(k+1)?h="code":"assign"==p&&"="==d?h="code":" "==d?h="code":"%"==d&&(k<r-1&&">"==a.charAt(k+1))&&("else"==m?h="chtml2":(b.pop(),0==b.length?(m=null,l="chtml",h="html"):(h=e.ctags[b[b.length- 1]],m=h.type,l=b[b.length-1],h=h._state)),k++);else if("%"==d&&k<r-1&&">"==a.charAt(k+1)){if("code"==m||"assign"==m)b.pop(),0==b.length?(m=null,l="chtml",h="html"):(h=e.ctags[b[b.length-1]],m=h.type,l=b[b.length-1],h=h._state);else if("if"==m||"loop"==m)h="chtml1";k++}else"tag"!=h&&this._addToCurrent(l,h,e,d);this._findVars(e);return e},_findVars:function(a){for(var b in a.ctags)a.ctags[b].vars=this._findVar(a.ctags[b].code)},_isWhiteSpace:function(a){return/[\s]/.test(a)},_findVar:function(a){for(var b= [],c=!1,e="",d=0,f=0,g=0,l=0,m=0,h=!1,k=!1,r=0,p=0,q=!1,s=!1,j=0,t=a.length;j<t;j++){var n=a.charAt(j);if("("==n){if(d++,!h&&(8<=j&&"n"==a.charAt(j-1)&&"o"==a.charAt(j-2)&&"i"==a.charAt(j-3)&&"t"==a.charAt(j-4)&&"c"==a.charAt(j-5)&&"n"==a.charAt(j-6)&&"u"==a.charAt(j-7)&&"f"==a.charAt(j-8))&&(h=!0),!s&&!h&&!q&&(3<=j&&"r"==a.charAt(j-1)&&"o"==a.charAt(j-2)&&"f"==a.charAt(j-3)||5<=j&&"e"==a.charAt(j-1)&&"l"==a.charAt(j-2)&&"i"==a.charAt(j-3)&&"h"==a.charAt(j-4)&&"w"==a.charAt(j-5)))s=!0}else if(")"== n)d--,0==d&&(!h&&!k&&s)&&(s=!1),h&&(0==f&&0==d)&&(h=!1);else if("{"==n)f++,q&&g++;else if("["==n)m++,q&&l++;else if("]"==n)m--,q&&l--;else if("}"==n)f--,q&&g--,h&&(0==f&&0==d)&&(h=!1);else if('"'==n)r=r?0:1;else if("'"==n)p=p?0:1;else if("="==n&&!h&&c&&!k){q=!0;e&&(b.push(e),e="");continue}k=p||r?!0:!1;if(!h&&!k&&(s||!d))if(!c&&"v"==n){if(0==j&&j<t-4&&"a"==a.charAt(j+1)&&"r"==a.charAt(j+2)&&this._isWhiteSpace(a.charAt(j+3))||(this._isWhiteSpace(a.charAt(j-1))||"{"==a.charAt(j-1)||"("==a.charAt(j- 1))&&j<t-4&&"a"==a.charAt(j+1)&&"r"==a.charAt(j+2)&&this._isWhiteSpace(a.charAt(j+3)))c=!0,j+=3}else c&&(","==n&&0==g&&0==l?(e&&(b.push(e),e=""),q=!1):" "==n&&s?(e&&(b.push(e),e=""),j<t-3&&("i"==a.charAt(j+1)&&"n"==a.charAt(j+2)&&" "==a.charAt(j+3))&&(q=c=!1)):";"==n?(e&&(b.push(e),e=""),q=c=!1):"i"==n&&j<t-3&&"n"==a.charAt(j+1)&&this._isWhiteSpace(a.charAt(j+2))?(e&&(b.push(e),e=""),c=!1):!this._isWhiteSpace(n)&&!q&&(e+=n))}return b},_addToCurrent:function(a,b,c,e){"chtml"==a?c.chtml+=e:c.ctags[a][b]+= e},_getCtagType:function(a,b){if("="==a.charAt(b))return"assign";if("I"==a.charAt(b).toUpperCase())return"if";if("L"==a.charAt(b).toUpperCase())return"loop";if("E"==a.charAt(b).toUpperCase()&&b<a.length-2){if("L"==a.charAt(b+1).toUpperCase())return"else";if("N"==a.charAt(b+1).toUpperCase()&&b<a.length-4){if("I"==a.charAt(b+3).toUpperCase())return"endif";if("L"==a.charAt(b+3).toUpperCase())return"endloop"}}return"code"},_getNewCtag:function(a){return{type:a,code:"",chtml1:"",chtml2:"",vars:[],flags:[]}}}; "undefined"!==typeof module&&module.exports&&(module.exports=BabaJS);