-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjtmpl.min.js
2 lines (2 loc) · 12.1 KB
/
jtmpl.min.js
1
2
/*! jtmpl 09-10-2013 */
!function(){var a;a=this,a.jtmpl=function(a,b,c,d){var e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;if(s=/^\#[\w-] $/,e=Array.prototype,(null===a||"string"==typeof a)&&null==b){if("undefined"==typeof document||null===document)throw":( this API is only available in a browser";return e.slice.call(document.querySelectorAll(a))}if("string"!=typeof a||"number"!=(w=typeof b)&&"string"!==w&&"boolean"!==w&&"object"!==w||void 0!==c||(d=c,c=b,b=a,a=void 0),"string"==typeof a&&a.match(s)&&(a=document.getElementById(a.substring(1))),null==c)throw":( no model";return b.match&&b.match(s)&&(b=document.getElementById(b.substring(1)).innerHTML),q=function(a){return(a "").replace(/([.?* ^$[\]\\(){}|-])/g,"\\$1")},d=d||{},d.delimiters=(d.delimiters||"{{ }}").split(" "),d.compiledDelimiters=(d.compiledDelimiters||"<<< >>>").split(" "),d.defaultSection=d.defaultSectionTag||"div",d.defaultSectionItem=d.defaultSectionItem||"div",d.defaultVar=d.defaultVar||"span",d.defaultTargetTag=d.defaultTargetTag||"div",u=/(\{)?(\#|\^|\/)?([\w\.\-_] )(\})?/,r=new RegExp(q(d.delimiters[0]) u.source q(d.delimiters[1]),"g"),l=/(<\s*[\w-_] )(?:\s ([\w-\{\}]*)(=)?("[^">]*"?)?)*?\s*(>)?\s*(?:<!--.*?-->\s*)*$/,n=/^(\s*<([\w-_] ))(?:(\s*data-jt="[^"]*)")?[^>]*>[\s\S]*?<\/\2>\s*$/,j=function(a){return(null!=a&&a||"").toString().replace(/[&\"<>\\]/g,function(a){switch(a){case"&":return"&";case"\\":return"\\\\";case'"':return'"';case"<":return"<";case">":return">";default:return a}})},p=function(a){return[function(){switch(a[2]){case"/":return"end";case"#":return"section";case"^":return"inverted_section";case void 0:return"{"===a[1]?"unescaped_var":"var";default:throw":( internal error, tag " a[0]}}(),a[3],a[0],(a[2]||"") a[3]]},g=function(a,b,c){return b.addEventListener?b.addEventListener(a,c,!1):b.attachEvent?b.attachEvent("on" a,c):b[a]=c},v=function(a,b){var c;return"undefined"!=typeof Event&&null!==Event?b.dispatchEvent(new Event(a)):document.createEvent?(c=document.createEvent("Event"),c.initEvent(a,!0,!0),b.dispatchEvent(c)):(c=document.createEventObject(),b.fireEvent("on" a,c))},k=function(a,b){return new RegExp("(\\s|^)" b "(\\s|$)").test(a.className)},f=function(a,b){return k(a,b)?void 0:a.className =(a.className&&" "||"") b},t=function(a,b){return k(a,b)?a.className=a.className.replace(new RegExp("(\\s|^)" b "(\\s|$)"),"").replace(/^\s |\s $/g,""):void 0},i=function(a,b,c,e,f){var g,h,k,m,o,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L;for(D=c||0,B=C="",F=w=A=null,s=function(){return B =a.slice(D,r.lastIndex-(t||"").length),D=r.lastIndex},v=function(a,b){return b=b||"",w[3]&&!w[5]&&w[2] w[3] b a b||a},k=function(){return i(a,b,D,G,!0)},y=function(){var a,b,c;return b=w.index w[1].length (C!==B&&B.length-C.length||0),c="" v(u),(a=B.match(new RegExp("[\\s\\S]{" b '}(\\sdata-jt="([^"]*))"')))?(b =a[1].length,B="" B.slice(0,b) (a[2].length&&" "||"") c B.slice(b)):B="" B.slice(0,b) ' data-jt="' c '"' B.slice(b)},m=function(){var c,e;return c=r.lastIndex,e=i(a,b,D,G,!0).trim().replace(new RegExp(q(d.delimiters[0]),"g"),d.compiledDelimiters[0]).replace(new RegExp(q(d.delimiters[1]),"g"),d.compiledDelimiters[1]),B ="<!-- " F[2] " " e " -->",r.lastIndex=c},h=function(a){var b,c;return a=a.trim(),b=a.match(n),B =b?(c=b[1].length (b[3]&&b[3].length||0),"" a.slice(0,c) (!b[3]&&' data-jt="."'||" .") a.slice(c)):"<" d.defaultSectionItem ' data-jt=".">' a "</" d.defaultSectionItem ">"},g=function(a,b){var c,e;return a=a.trim(),c=a.match(n),B =c?(e=c[1].length,"" a.slice(0,e) ' data-jt="' u '"' (b&&' style="display:none"'||"") a.slice(e)):"<" d.defaultSection ' data-jt="' u '"' (b&&' style="display:none"'||"") ">" a "</" d.defaultSectionItem ">"};F=r.exec(a);)switch(L=p(F),H=L[0],G=L[1],t=L[2],u=L[3],s(),C=B.length>300&&B.slice(-300)||B,w=C.match(l),H){case"end":if(G!==e)throw e?":( expected {{/" e "}}, got " t:":( unexpected {{/" G "}}";return B;case"var":case"unescaped_var":if(f)B =t;else if(I="."===G?b:b[G],o="unescaped_var"===H&&I||j(I),w)if(y(),"function"==typeof I)B=B.replace(/[\w-_] =$/,"");else if("class"!==w[2]||w[5])w[3]&&!w[5]?null==I||null===I?B=B.replace(/[\w-_] =$/,""):"boolean"==typeof I?B=B.replace(/[\w-_] =$/,"") (I&&w[2]||""):B ='"' I '"':B =I;else{if("boolean"!=typeof I)throw"" G " is not boolean";I&&(B =G)}else B ="<" d.defaultVar ' data-jt="' u '">' o "</" d.defaultVar ">";break;case"section":if(f)B ="" t i(a,b,D,G,!0) "{{/" G "}}";else if(I=b[G],"object"!=typeof I)s(),E=i(a,b,D,G),g(E,!I);else if(Array.isArray(I)){if(w?y():G!==A&&(B ="<" d.defaultSection ' data-jt="' u '">'),m(),I.length)for(x=J=0,K=I.length;K>J;x= J)z=I[x],s(),h(i(a,I&&"object"==typeof I?z:b,D,G)),x<I.length-1&&(r.lastIndex=D);else k();w||G===A||(B ="</" d.defaultSection ">"),A=G}else s(),B =i(a,I,D,G);D=r.lastIndex;break;case"inverted_section":f?B ="" t i(a,b,D,G,!0) "{{/" G "}}":(I=b[G],Array.isArray(I)?(w?y():G!==A&&(B ="<" d.defaultSection ' data-jt="' u '">'),m(),I.length?k():B =i(a,b,D,G),w||G===A||(B ="</" d.defaultSection ">"),A=G):g(i(a,b,D,G),I)),D=r.lastIndex}return B =a.slice(D)},b=b.replace(new RegExp("<!--\\s*(" r.source ")\\s*-->"),"$1"),b=b.replace(new RegExp("([\\w-_] )='(" r.source ")'","g"),"$1=$2"),b=b.replace(new RegExp('([\\w-_] )="(' r.source ')"',"g"),"$1=$2"),b=b.replace(new RegExp("\\n\\s*(" r.source ")\\s*\\n","g"),"\n$1\n"),m=i(b,c),m=m.replace(/data-jt="\.(\s\.) "/g,'data-jt="."'),null==a?m:("SCRIPT"===a.nodeName&&(o=document.createElement(d.defaultTargetTag),a.parentNode.replaceChild(o,a),a=o),a.innerHTML=m,h=function(b,i){var j,k,l,m,n,o,p,r,s,u,w,x,y,z,A,B;for(r=0,u=null,n=function(a,b,c){return function(){return a[c]=this[b]}},x=function(a,b,c){return function(){var d,e,f,g;if(this[b])for(g=jtmpl("input[type=radio][name=" this.name "]"),e=0,f=g.length;f>e;e )d=g[e],d!==this&&v("change",d);return a[c]=this[b]}},w=function(a,b,c){return function(){var b,d,e,f,g,h;for(b=0,g=this.children,h=[],e=0,f=g.length;f>e;e )d=g[e],"OPTION"===d.nodeName?(a[b][c]=d.selected,h.push(b )):h.push(void 0);return h}},p=function(a,b){return a["__" b "_bindings"]?void 0:(Object.defineProperty(a,"__" b "_bindings",{enumerable:!1,writable:!0,value:[]}),Object.defineProperty(a,"__" b,{enumerable:!1,writable:!0,value:a[b]}),Object.defineProperty(a,b,{get:function(){return this["__" b]},set:function(a){var c,d,e,f,g;for(this["__" b]=a,f=this["__" b "_bindings"],g=[],d=0,e=f.length;e>d;d )c=f[d],g.push(c.call(this,a));return g}}))},o=function(a,b){var d;return d=document.createElement("body"),d.innerHTML=jtmpl(a.getAttribute("data-jt-1")||"",b),d=d.children[0],jtmpl(d,d.innerHTML,b,{rootModel:c}),d},l=function(a,b){var c,d,f,g,h;if(!a.__values)for(a.__garbageCollectNodes=function(){var a,b;for(a=this.__nodes.length,b=[];--a;)this.__nodes[a].parentNode?b.push(void 0):b.push(this.__nodes.splice(a,1));return b},a.__removeEmpty=function(){return this.length?void 0:b.innerHTML=""},a.__addEmpty=function(){return this.length?void 0:b.innerHTML=jtmpl(b.getAttribute("data-jt-0")||"",{})},a.pop=function(){var a,c,d;for(this.__removeEmpty(),this.__garbageCollectNodes(),d=this.__nodes,a=0,c=d.length;c>a;a )b=d[a],b.removeChild(b.children[b.children.length-1]);return e.pop.apply(this,arguments),e.pop.apply(this.__values,arguments),this.__addEmpty()},a.push=function(a){var d,f,g,h,i;for(this.__removeEmpty(),this.__garbageCollectNodes(),i=this.__nodes,g=0,h=i.length;h>g;g )b=i[g],b.appendChild(o(b,a));return e.push.apply(this,arguments),d=this.__values.length,f=e.push.apply(this.__values,arguments),c(a,d),f},a.reverse=function(){var a,d,f,g,h,i,j,k,l;for(this.__removeEmpty(),this.__garbageCollectNodes(),f=e.reverse.apply(this.__values,arguments),k=this.__nodes,g=0,i=k.length;i>g;g )for(b=k[g],b.innerHTML="",l=this.__values,a=h=0,j=l.length;j>h;a= h)d=l[a],b.appendChild(o(b,d)),c(d,a);return this.__addEmpty(),f},a.shift=function(){var a,d,f,g,h,i,j,k,l;for(this.__removeEmpty(),this.__garbageCollectNodes(),e.shift.apply(this,arguments),f=e.shift.apply(this.__values,arguments),k=this.__nodes,g=0,i=k.length;i>g;g )b=k[g],b.removeChild(b.children[0]);for(l=this.__values,a=h=0,j=l.length;j>h;a= h)d=l[a],c(d,a);return this.__addEmpty(),f},a.unshift=function(){var a,d,f,g,h,i,j,k,l,m,n,p;for(this.__removeEmpty(),this.__garbageCollectNodes(),m=e.slice.call(arguments).reverse(),g=0,j=m.length;j>g;g )for(d=m[g],n=this.__nodes,h=0,k=n.length;k>h;h )b=n[h],b.insertBefore(o(b,d),b.children[0]);for(e.unshift.apply(this,arguments),f=e.unshift.apply(this.__values,arguments),p=this.__values,a=i=0,l=p.length;l>i;a= i)d=p[a],c(d,a);return this.__addEmpty(),f},a.sort=function(){var d,f,g,h,i,j,k,l,m,n,p;for(this.__removeEmpty(),this.__garbageCollectNodes(),e.sort.apply(this,arguments),g=e.sort.apply(this.__values,arguments),n=this.__nodes,h=0,k=n.length;k>h;h )for(b=n[h],b.innerHTML="",d=i=0,l=a.length;l>i;d= i){for(f=a[d],p=this.__nodes,j=0,m=p.length;m>j;j )b=p[j],b.appendChild(o(b,f));c(f,d)}return this.__addEmpty(),g},a.splice=function(a,d){var f,g,h,i,j,k,l,m,n;for(this.__removeEmpty(),this.__garbageCollectNodes(),m=this.__nodes,h=0,k=m.length;k>h;h ){for(b=m[h],f=i=0;d>=0?d>i:i>d;f=d>=0? i:--i)b.removeChild(b.children[a]);for(n=e.slice.call(arguments,2),j=0,l=n.length;l>j;j )g=n[j],b.insertBefore(o(b,g),b.children[a]),c(g,a)}return e.splice.apply(this,arguments),e.splice.apply(this.__values,arguments),this.__addEmpty()},c=function(c,d){return a.__values[d]=c,Object.defineProperty(a,d,{get:function(){return this.__values[d]},set:function(a){var c,e,f,g;for(this.__garbageCollectNodes(),this.__values[d]=a,f=this.__nodes,g=[],c=0,e=f.length;e>c;c )b=f[c],g.push(b.replaceChild(o(b,a),b.children[d]));return g}})},Object.defineProperty(a,"__nodes",{enumerable:!1,writable:!0,value:[]}),Object.defineProperty(a,"__values",{enumerable:!1,writable:!0,value:[]}),d=g=0,h=a.length;h>g;d= g)f=a[d],c(f,d);return-1===a.__nodes.indexOf(b)&&a.__nodes.push(b),a},j=function(a,b,c,d){return"object"==typeof a?(p(a,c),d?"class"===d?a["__" c "_bindings"].push(function(a,b){return function(c){return(c&&f||t)(a,b)}}(b,c)):a["__" c "_bindings"].push(function(a,b,c){return function(b){return"value"!==c&&"checked"!==c&&"selected"!==c?"boolean"==typeof b&&!b||null===b?a.removeAttribute(c):a.setAttribute(c,b):a[c]!==b?a[c]=b:void 0}}(b,c,d)):a["__" c "_bindings"].push(function(a){return function(b){return a.innerHTML=b}}(b))):void 0},k=function(a,b,d,e){return p(a,d),a["__" d "_bindings"].push(function(a,b,d,e){return function(a){var d,f,g,h,i;if(Array.isArray(a)){for(l(a,b),b.innerHTML=a.length?"":jtmpl(b.getAttribute("data-jt-0")||"",{}),i=[],d=g=0,h=a.length;h>g;d= g)f=a[d],p(a,d),i.push(b.appendChild(o(b,f)));return i}return"object"==typeof a?(b.innerHTML=jtmpl(b.getAttribute("data-jt-1")||"",a),jtmpl(b,b.innerHTML,a,{rootModel:c})):b.style.display=!!a===e&&"none"||""}}(a,b,d,e))},m=function(a){var b,e,f,h,m,o,p,q,s,t,v,y,z;if(b=a.getAttribute("data-jt")){for(h=b.trim().split(" ").reverse(),z=[],t=0,v=h.length;v>t;t )if(f=h[t],p=f.slice(0,1),"#"===p||"^"===p)o=f.slice(1),u=u||i[o],Array.isArray(u)?(k(i,a,o,"^"===p),z.push(l(u,a))):"object"==typeof u?z.push(k(u,a,o,"^"===p)):z.push(k(i,a,o,"^"===p));else if("."===f)z.push(u=i[r ]);else if(y=f.match(/(?:\/|#)?([\w-.] )(?:\=([\w-.] ))?/),q=y[0],m=y[1],s=y[2],m&&0===m.indexOf("on")){if(e=null!=d.rootModel&&d.rootModel[s]||c[s],"function"!=typeof e)throw":( " s " is not a function, cannot attach event handler";z.push(g(m.slice(2),a,e.bind(i)))}else s?(u&&!Array.isArray(u)?j(u,a,s,m):j(i,a,s,m),"value"===m||"checked"===m||"selected"===m?("OPTION"===a.nodeName&&a.parentNode.querySelectorAll("option")[0]===a&&g("change",a.parentNode,w(i,m,s).bind(a.parentNode)),"radio"===a.type&&a.name&&g("change",a,x(i,m,s).bind(a)),"text"===a.type?z.push(g("input",a,n(i,m,s).bind(a))):z.push(g("change",a,n(i,m,s).bind(a)))):z.push(void 0)):u&&!Array.isArray(u)?z.push(j(u,a,m)):z.push(j(i,a,m));return z}},b===a&&m(b,i),B=b.childNodes,z=0,A=B.length;A>z;z )switch(s=B[z],s.nodeType){case s.ELEMENT_NODE:m(s),h(s,"object"==typeof u&&u||i),u=null;break;case s.COMMENT_NODE:(y=s.nodeValue.trim().match(/^(#|\^)\s([\s\S]*)$/))&&(y[2]=y[2].replace(new RegExp(q(d.compiledDelimiters[0]),"g"),d.delimiters[0]).replace(new RegExp(q(d.compiledDelimiters[1]),"g"),d.delimiters[1]),"#"===y[1]?b.setAttribute("data-jt-1",y[2]):b.setAttribute("data-jt-0",y[2]))}return s},h(a,c))}}.call(this);