UNPKG

khoaijs-template

Version:

![http://i.imgur.com/RmD50NR.jpg](http://i.imgur.com/RmD50NR.jpg)

3 lines (2 loc) 4.57 kB
!function(t,e){if("function"==typeof define&&define.amd){var n=["lodash","khoaijs","khoaijs-event-emitter"];define(n,function(n,r,s){var o=e(n,r,s);return r.Template=o,t.Template=o,o})}else{var r=e(t._,t.Khoai,t.Khoai.EventEmitter||t.EventEmitter);t.Khoai.Template=r,t.Template=r}}(this,function(t,e,n){"use strict";function r(e,r){this.type_prefix="template",n.call(this),this.dataSource=null,this.options={},this._layout="",this._sections={},t.isUndefined(r)||self.setLayout(r),t.isObject(e)&&t.each(e,function(t,e){self.setSection(t,e)})}function s(t){for(var e,n=new RegExp("@([A-Z0-9_-]+)@","gi"),r=[];null!==(e=n.exec(t));)r.push(e[1]);return r}function o(e,n,r,i){var c="",u=[];c=t.isFunction(e)?e(n,n.dataSource,i):e+"",u=s(c);var a=t.difference(u,Object.keys(r));return t.isEmpty(a)||(console.warn("Missing subsections:",a),t.each(a,function(t){r[t]="",u[t]=""})),u=t.zipObject(u,t.fill(new Array(u.length),"")),t.isEmpty(u)||(t.each(u,function(t,e){r[e]=o(r[e],n,r,i),u[e]=r[e]}),t.each(u,function(t,e){c=c.replace(new RegExp("@"+e.toUpperCase()+"@","gi"),t)})),c}r.prototype=Object.create(n.prototype),r.prototype.constructor=r,r.prototype.option=function(n,r){return this.options=e.util.setup.apply(null,[this.options].concat(t.toArray(arguments))),this},r.prototype.connect=function(t){return null!==this.dataSource&&this.disconnect(),this.dataSource=t,t instanceof n&&this.listen(t),this.emitEvent("connected"),this},r.prototype.disconnect=function(){return null!==this.dataSource&&(this.emitEvent("before_disconnect"),this.dataSource instanceof n&&this.unlisten(this.dataSource),this.dataSource=null,this.emitEvent("disconnected"),!0)},r.prototype.getDataSource=function(){return this.dataSource},r.prototype.isConnected=function(){return null!==this.dataSource},r.prototype.setLayout=function(t){return this._layout=t,this},r.prototype.getLayout=function(){return this._layout},r.prototype.setSection=function(e,n){return t.isObject(e)?t.each(e,function(t,e){this._sections[e.toUpperCase()]=t}.bind(this)):this._sections[e.toUpperCase()]=n,this},r.prototype.currentDraw=function(){return e.util.currentID(this.id+"_draw",!1)},r.prototype.getDOMID=function(){return this.id},r.prototype.prepareData=function(){return{}},r.prototype.render=function(n){var r,s=this,i=t.extend({},{option:this.options,data_source:this.getDataSource()}),c=t.extend({},this._sections);return e.util.nextID(this.id+"_draw"),i.draw=this.currentDraw(),i.dom_id=this.getDOMID(),t.extend(i,this.prepareData(i),t.isObject(n)?n:{}),r=t.isFunction(this._layout)?this._layout(s,this.dataSource,i):this._layout+"",r=o(r,this,c,i),t.template(r)(i)},r.prototype.rendered=function(){},r.prototype.reDraw=function(t){var e=this.getDOM();if(e){var n=this.render(t);return this.emitEvent("re-draw"),e.replaceWith(n),this.emitEvent("drawn",n),this.rendered(),!0}return!1},r.prototype.getDOM=function(){return document.getElementById(this.getDOMID())};var i={compilers:{},types:{}};return r.hasCompiler=function(e){return t.has(i.compilers,e)},r.compiler=function(t,e){i.compilers[t]=e},r.compilers=function(){return Object.keys(i.compilers)},r.render=function(t,e){return!!this.hasCompiler(t)&&(1==arguments.length&&(e={}),i.compilers[t](e))},r.register=function(t,e,n){return i.types.hasOwnProperty(t)||(i.types[t]={_default:null,constructors:{}}),!i.types[t].hasOwnProperty(e)&&(i.types[t].constructors[e]=n,i.types[t]._default||(i.types[t]._default=e),!0)},r.types=function(e){return e?t.mapObject(i.types,function(t){return Object.keys(t.constructors)}):Object.keys(i.types)},r.hasType=function(t){return i.types.hasOwnProperty(t)},r.templates=function(e,n){return n?Object.keys(i.types[e].constructors):t.clone(i.types[e].constructors)},r.hasTemplate=function(t,e){return i.types.hasOwnProperty(t)&&i.types[t].constructors.hasOwnProperty(e)},r.defaultTemplate=function(e,n){if(!t.isEmpty(i.types[e].constructors)){if(n){if(i.types[e].constructors.hasOwnProperty(n))return i.types[e]._default=n,!0;throw new Error("Set default template with invalid template name")}if(i.types[e]._default&&this.hasTemplate(e,i.types[e]._default))return i.types[e]._default;var r=Object.keys(i.types[e].constructors)[0];return i.types[e]._default=r,new i.types[e].constructors[r]}return!1},r.templateInstance=function(t,e){if(arguments.length<2){var n=this.defaultTemplate(t);if(!1!==n)return new i.types[t].constructors[n]}else if(this.hasTemplate(t,e))return new i.types[t].constructors[e];throw new Error("Template with name isn't exists or invalid type")},r}); //# sourceMappingURL=template.min.js.map