forked from mbjorkegren/handlebones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebones.min.js
1 lines (1 loc) · 6.52 KB
/
handlebones.min.js
1
!function(e,t){if("function"==typeof define&&define.amd)define(["underscore","backbone","handlebars","jquery","exports"],function(i,n,s,r,l){e.Handlebones=t(e,l,i,n,s,r)});else if("undefined"!=typeof exports){var i=require("underscore"),n=require("backbone"),s=require("handlebars");t(e,exports,i,n,s)}else e.Handlebones=t(e,{},e._,e.Backbone,e.Handlebars,e.jQuery||e.Zepto||e.ender||e.$)}(this,function(e,t,i,n,s,r){function l(e){return new RegExp("(?:^|\\s+)"+e+"(?:\\s+|$)").test(this.className)}function h(e){l.call(this,e)||(this.className=this.className?[this.className,e].join(" "):e)}function a(e){if(l.call(this,e)){var t=this.className,i=new RegExp("(?:^|\\s+)"+e+"(?:\\s+|$)","g");this.className=t.replace(i,"")}}function o(e){e._isReady||e.trigger("ready")}function c(){this._isReady||(this._isReady=!0,i.each(this.children,o))}function d(){this._renderCount=0,this.children={},E[this.cid]=this,this.listenTo(this,"ready",c)}function u(){this._renderCount||this.render()}function f(e){if(e&&e.string&&(e=e.string),$){if(this._renderCount&&(T||r.fn&&r.fn.jquery))for(;this.el.hasChildNodes();)this.el.removeChild(this.el.childNodes[0]);this.$el.empty(),this.$el.append(e)}else this.el.innerHTML=e}function m(){this.emptyClassName&&a.call(this.el,this.emptyClassName),this._emptyViewInstance&&(this._emptyViewInstance.remove(),this.removeChild(this._emptyViewInstance)),f.call(this,"")}function p(){this.emptyClassName&&h.call(this.el,this.emptyClassName),f.call(this,""),this.emptyView&&(this._emptyViewInstance=new this.emptyView,this.addChild(this._emptyViewInstance),this._emptyViewInstance.appendTo(this.el))}function g(){this.modelFilter&&this.collection.forEach(function(e){v.call(this,e)},this)}function v(e,t){if(this.modelFilter){if(!t){var i="["+N+'="'+e.cid+'"]';t=this.$(i)[0]}t.style.display=w.call(this,e)?null:"none"}}function w(e){return this.modelFilter(e,this.collection.indexOf(e))}function y(e){var t=this.el;this.el=document.createDocumentFragment(),e.call(this),t.appendChild(this.el),this.el=t}function C(e,t,n){if(e.match(/^("|')/)&&e.match(/("|')$/))return e.replace(/(^("|')|('|")$)/g,"");for(var s=e.split("."),r=s.length,l=0;t&&r>l;l++)"this"!==s[l]&&(t=t[s[l]]);return n&&i.isString(t)?encodeURIComponent(t):t}function V(){var e=this.$("["+_+"]");i.each(e,function(e){var t=e.getAttribute(_),i=this.children[t];i&&(u.call(i),e.parentNode.replaceChild(i.el,e))},this)}t.VERSION="0.0.1";var b="data-view-name",x="data-view-cid",_="data-view-tmp",N="data-model-cid",E={},M=!!navigator.userAgent.match(/Trident\/7\./),T=M||/msie [\w.]+/.exec(navigator.userAgent.toLowerCase()),$="undefined"!=typeof r&&r.fn;return t.View=n.View.extend({template:s.VM.noop,render:function(e){var t;return t=i.isFunction(e)?e(this.context()):i.isString(e)?e:this.template(this.context()),f.call(this,t),V.call(this),++this._renderCount,this.trigger("render"),this},context:function(){return this},appendTo:function(e){return u.call(this),i.isFunction(e)?e():e.appendChild(this.el),this.trigger("ready"),this},addChild:function(e){return this.children[e.cid]?e:(e.parent=this,this.children[e.cid]=e,this._isReady&&o(e),e)},removeChild:function(e){return delete e.parent,delete this.children[e.cid],e},remove:function(){i.each(this.children,function(e){this.removeChild(e)},this),delete E[this.cid],this.name&&this.el.removeAttribute(b),this.el.removeAttribute(x);var e=n.View.prototype.remove.apply(this,arguments);return this.trigger("remove"),e},toString:function(){return"[object Handlebones.View."+(this.name||this.cid)+"]"},setElement:function(){var e=n.View.prototype.setElement.apply(this,arguments);return this.name&&this.el.setAttribute(b,this.name),this.el.setAttribute(x,this.cid),e},_ensureElement:function(){return d.call(this),n.View.prototype._ensureElement.call(this)}}),t.LayoutView=t.View.extend({render:function(){return++this._renderCount,this.trigger("render"),this},setView:function(e,t){var n,s,r=this._view;return e===r?this:(s=i.bind(function(){r&&(r.remove(),this.removeChild(r))},this),n=i.bind(function(){this._view=e,e&&(u.call(e),this.addChild(e),this._view.appendTo(this.el))},this),t?t(e,r,n,s):(s(),n()),this)},getView:function(){return this._view}}),t.ModelView=t.View.extend({initialize:function(){this.listenTo(this.model,"change",this.render)},context:function(){return this.model.attributes},setElement:function(){var e=t.View.prototype.setElement.apply(this,arguments);return this.el.setAttribute(N,this.model.cid),e}}),t.CollectionView=t.View.extend({modelView:t.ModelView,emptyView:!1,emptyClassName:"empty",modelFilter:!1,initialize:function(){this.listenTo(this.collection,{reset:this.render,sort:this.render,change:function(e){v.call(this,e)},add:function(e){1===this.collection.length&&m.call(this);var t=this.collection.indexOf(e);this.addModel(e,t)},remove:function(e){this.removeModel(e),0===this.collection.length&&p.call(this)}})},render:function(){return 0===this.collection.length?p.call(this):(m.call(this),y.call(this,function(){this.collection.forEach(function(e){this.addModel(e)},this)})),++this._renderCount,this.trigger("render"),this},addModel:function(e,t){var n;if(n=new this.modelView({model:e}),this.addChild(n),i.isUndefined(t))n.appendTo(this.el);else{var s,r=t>0?this.collection.at(t-1):!1;if(r){var l="["+N+'="'+r.cid+'"]';s=this.$(l)[0].nextSibling}else s=this.el.firstChild;n.appendTo(i.bind(function(){this.el.insertBefore(n.el,s)},this))}return v.call(this,e,n.el),this.trigger("addModel",e,n),this},removeModel:function(e){var t="["+N+'="'+e.cid+'"]',i=this.$(t)[0],n=i.getAttribute(x),s=this.children[n];return s.remove(),this.removeChild(s),this.trigger("removeModel",e,s),this},updateModelFilter:function(){g.call(this)}}),t.Util={tag:function(e,n,r){e.className&&(e["class"]=e.className,delete e.className);var l=i.omit(e,"tagName","tag"),h=e.tag||e.tagName||"div",a=i.keys(l).length>0?" ":"";return"<"+h+a+i.map(l,function(e,i){var n=e;return r&&(n=t.Util.expandToken(e,r)),n=s.Utils.escapeExpression(n),i+'="'+n+'"'}).join(" ")+">"+(i.isUndefined(n)?"":n)+"</"+h+">"},expandToken:function(e,t,n){if(e&&e.indexOf&&e.indexOf("{{")>=0){for(var r,l=/(?:\{?[^{]+)|(?:\{\{([^}]+)\}\})/g,h=[],a=function(e){return C(e,t,n)};r=l.exec(e);)if(r[1]){var o=r[1].split(/\s+/);if(o.length>1){var c=o.shift();o=i.map(o,a),h.push(s.helpers[c]?s.helpers[c].apply(t,o):r[0])}else h.push(C(o[0],t,n))}else h.push(r[0]);e=h.join("")}return e}},s.registerHelper("view",function(e){if(!e)return"";var i={tagName:e.el.tagName.toLowerCase()};i[_]=e.cid;var n=t.Util.tag(i,"",this);return new s.SafeString(n)}),$&&(r.fn.view=function(){var e="["+x+"]",t=r(this).closest(e);return t&&E[t.attr(x)]||!1}),t});