-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemples.min.js
8 lines (8 loc) · 5.26 KB
/
Temples.min.js
1
2
3
4
5
6
7
8
/**
* Temples.js
* ============
* Author: Eidolon Labs (zipang)
* Source : http://github.com/zipang/Temples
* Date: 2013-11-29
* v0.2.2
*/(function(e,t){function i(e){if(!e)return"undefined";var t=e.tagName,n=e.id,r=e.className;return t+(n?"#"+n:"")+(r?"."+r:"")}function s(e){return t("<!--"+e+"-->")}function o(e,t,n){var r,i,s=t,o=(e||"").trim().split(".");while(s&&(r=o.shift()))i=s,s=s[r];return typeof s=="function"?s.call(i,n):s||""}function u(){}function a(e,t){this.$root=e,this.bindings=[p.updater(e,t)],this.toString("AttributeRenderer("+i(e[0])+"["+t+"])")}function f(e,t){this.$root=e,this.toString("SimpleElementRenderer("+i(e[0])+"["+t+"])");var n=this.bindings=[],r,s=t.split(",");while(r=s.pop())n.push(p.updater(e,r.trim()))}function l(e,t,n){if(!arguments.length)return;this.$root=e,this.condition=t;var r=this.conditionalbindings=[];this.toString("ConditionalElementRenderer("+i(e[0])+"["+n+"] if "+t+")");if(n){var s=n.split(",");for(var u=0,a;a=s[u++];)r.push(p.updater(e,a.trim()))}var f=this;this.bindings=[function(e){if(o(t,e)){for(var n=0,i;i=r[n++];)i(e);f.reappear()}else f.disappear("Temples says: "+t+"=false")}]}function c(e,t,n,r){this.$root=e;var s=e.children().first();if(!s.length)throw"List Renderer "+i(e[0])+" for ("+t+") must have at least a child block!";var u=this.template=new h(this.toString(),s);this.toString("ListRenderer("+i(e[0])+"["+r+"] if "+n+")\n"+"iterate "+t+" with "+u);var a=t.replace("from",":"),f=a.split(":"),c=f[1]||f[0],p=this.seed=c.trim();f.length==2?this.varName=f[0].trim():(this.varName=this.seed.split(".").pop(),this.varName=this.varName.replace(/s*$/,""));var d=this.varName;l.call(this,e,n,r),this.conditionalbindings.push(function(t){var n=o(p,t);e.empty();for(var r=0,i=n.length;r<i;r++)t[d]=n[r],e.append(u.render(t).clone());delete t[d]})}function h(e,n){this.$root=n||e,n&&(this.name=e),this.toString("TemplateRenderer("+(n?this.name+", ":"")+i(n[0])+")");var r=this.bindings=[];n.each(function(e,n){var i=u.targets(t(n));i.each(function(e,n){var i=u.Factory(t(n));i&&Array.prototype.push.apply(r,i.bindings)})})}if(!t)throw"Temples relies on a jQuery compatible DOM search engine and DOM manipulator";var n=[];console||(window.console={log:function(){}});var r=String.prototype;r.startsWith=r.startsWith||function(e){return this.indexOf(e)===0},r.contains=r.contains||function(e){return this.indexOf(e)!==-1},r.trim=r.trim||function(){return this.replace(/^\s+|\s+$/g,"")},u.prototype={render:function(e){var t=this.bindings,n=t.length;for(var r=0;r<n;r++)t[r](e);return this.$root},output:function(e){this.render(e);var t=[];try{this.$root.each(function(e,n){t.push(n.outerHTML)})}catch(n){if(XMLSerializer){var r=new XMLSerializer;this.$root.each(function(e,n){t.push(r.serializeToString(n))})}}return t.join("")},toHtml:function(){return this.$root.html()},destroy:function(){this.bindings=this.$root=null,this.name&&(delete d[this.name],n.splice(n.indexOf(this.name),1))},toString:function(e){if(!e)return"Renderer";this.toString=function(){return e}}},a.prototype=new u,f.prototype=new u,t.extend(l.prototype=new u,{disappear:function(e){this.placeHolder||this.$root.replaceWith(this.placeHolder=s(e))},reappear:function(){this.placeHolder&&(this.placeHolder.replaceWith(this.$root),this.placeHolder=null)}}),c.prototype=new l,u.isListRenderer=c.loopExpression=function(e){try{return e.attr("data-iterate")||e.attr("data-each")}catch(t){return""}},h.prototype=new u,u.targets=function(e){var n=function(){return t(this).attr("data-bind")||t(this).attr("data-render-if")},r=function(){return t(this).parentsUntil(e,"[data-iterate], [data-each]").length==0};return u.isListRenderer(e)?e:e.filter(n).add(t("*[data-bind], *[data-render-if]",e).filter(r)).add(t("*[data-iterate], *[data-each]",e).filter(r))};var p={updater:function(e,t){console.log("Binding '"+t+"' for element "+i(e[0]));var n=t.split("=");if(n.length==1){var r=n[0],s=e[0].tagName.toLowerCase();return s=="input"||s=="select"?function(t){e.val(o(r,t,e))}:function(t){e.html(o(r,t,e))}}var u=n[0].toLowerCase(),r=n[1];if(u.startsWith("class")){var a=/\[([\w|\|]+)\]/g.exec(u);return a?(a=a[1].split("|").join(" "),function(t){e.removeClass(a),e.addClass(o(r,t,e))}):function(t){e.attr("class",o(r,t,e))}}return"text|html|value".contains(u)?function(t){e[u](o(r,t,e))}:function(t){e.attr(u,o(r,t,e))}}};u.Factory=function(e){var t=e.attr("data-bind"),n=e.attr("data-render-if"),r=e.attr("data-iterate")||e.attr("data-each");e.removeAttr("data-bind data-render-if data-iterate data-each");if(r)return new c(e,r,n,t);if(n)return new l(e,n,t);if(t&&t.contains(","))return new f(e,t);if(t)return new a(e,t)};var d={Renderer:h,prepare:function(e,r){if(!r){if(e.startsWith("#"))r=t(e);else if(e=="document")r=t("html");else if(e.startsWith("<")||e.contains(" "))r=e,e=undefined}else t.fetchDocument&&t.fetchDocument(r);var i=t(r),s;return e?(n.push(e),s=d[e]=new d.Renderer(e,i)):s=new d.Renderer(i),i.data("renderer",s),s},destroy:function(e){e?d[e]&&d[e].destroy():n.forEach(d.destroy)},render:function(e,t){!t&&typeof e=="object"&&(t=e,e="document"),(d[e]||d.prepare(e)).render(t)}};d.register=d.prepare,t.fn&&!t.fn.render&&(t.fn.render=function(e){var n=t(this).data("renderer");return n&&n.render(e),this}),e===window?e.Temples=d:module&&module.exports&&(module.exports=d)})(this,this.jQuery||this.ender||this.jquip||require("buck"));