// Thumbs.js 0.2.4 // // Copyright (c) 2014 Pollenware. // Distributed under MIT license. // // http://thumbsjs.com (function(){function defineThumbs(Backbone,_){var previousThumbs=root.Thumbs,Thumbs={},_extend=Backbone.Model.extend;return Backbone.Thumbs=Thumbs,Thumbs.MULTI_ARG_TOKEN=/ +/,Thumbs.KEY_VALUE_TOKEN=":",Thumbs.noConflict=function(){return root.Thumbs=previousThumbs,this},Thumbs.viewRegistry=function(){var a={},b=0,c={_hash:a,getEnclosingView:function(b){for(var d,e=b;e;){if(e!==b&&(d=1===e.nodeType&&e.getAttribute("thumbs-id")))return a[d];e=e.parentNode}return c._hash[b.getAttribute("thumbs-id")]},remove:function(c){a.hasOwnProperty(c)&&(delete a[c],b--)},add:function(c){var d=c.thumbsId;if(a.hasOwnProperty(d))throw new Error("Tried to register view with id "+d+" but that id is already registered");d&&(a[d]=c,b++)},get:function(b){return"string"==typeof b?a[b]:b},uniqueId:function(){return _.uniqueId("thumbs_view_")},getSubViews:function(b){function c(b){var c,e,f;for(e=b.firstChild;e;e=e.nextSibling)1===e.nodeType&&(c=e.getAttribute("thumbs-id"))&&(f=a[c])&&d.push(f)}var d=[];return c(b),d},getByNode:function(b){return b?a[b.thumbsId||b.getAttribute("thumbs-id")]:void 0},toArray:function(){return _.values(a)}};return c}(),Thumbs.viewByNode=Thumbs.viewRegistry.getByNode,Thumbs.viewById=Thumbs.viewRegistry.get,Thumbs.extend=function(){var a=_extend.apply(this,arguments);return a.prototype.__getConstructor=function(){return a},a},Backbone.Model.extend=Backbone.Collection.extend=Backbone.View.extend=Backbone.History.extend=Backbone.Router.extend=Thumbs.extend,Thumbs._super={_super:function(){function a(a,b){for(var c=b;c[a]===b[a]&&c.__getConstructor;){var d=c.__getConstructor();c=d.__super__}return c}return function(b,c){c=c||[],this._superCallObjects||(this._superCallObjects={});var d,e=this._superCallObjects[b]||this,f=a(b,e);this._superCallObjects[b]=f;var g=f[b];switch(c.length){case 0:d=g.call(this);break;case 1:d=g.call(this,c[0]);break;case 2:d=g.call(this,c[0],c[1]);break;case 3:d=g.call(this,c[0],c[1],c[2]);break;case 4:d=g.call(this,c[0],c[1],c[2],c[3]);break;default:d=g.apply(this,c)}return delete this._superCallObjects[b],d}}()},Thumbs.Events=Backbone.Events,Thumbs.sync=Backbone.sync,Thumbs.Class=function(){var a=function(){this.cid=_.uniqueId("class"),this.initialize.apply(this,arguments)};return _.extend(a.prototype,Backbone.Events,Thumbs._super,{initialize:function(){}}),a.extend=Thumbs.extend,a}(),Thumbs.Model=Backbone.Model.extend(Thumbs._super).extend({}),Thumbs.Collection=Backbone.Collection.extend(Thumbs._super).extend({}),Thumbs.History=function(){var a=Backbone.History.extend(Thumbs._super).extend({notFoundRedirect:null,notFoundCallback:null,checkPreRouteHandler:function(a,b,c){var d=!0,e=_.find(a,function(a){return a.route.test(b+c)});return e&&e.callback&&(d=e.callback(c)),d},loadUrl:function(a){var b=this.fragment=this.getFragment(a),c=this.checkPreRouteHandler(this.handlers,"pre-route_all",b);return c&&(c=this.checkPreRouteHandler(this.handlers,"pre-route_",b)),c&&(c=this._super("loadUrl",arguments),c||(this.options.notFoundRedirect?(this.navigate(this.options.notFoundRedirect,{trigger:!0}),c=!0):this.options.notFoundCallback&&(c=this.options.notFoundCallback()))),c}});return Thumbs.history=Backbone.history=new a,a}(),Thumbs.Router=Backbone.Router.extend(Thumbs._super).extend({preRoutes:null,_bindRoutes:function(){this.preBind&&_.isFunction(this.preBind)&&this.preBind(),this._super("_bindRoutes",arguments)},route:function(a){return Thumbs.history||(Thumbs.history=Backbone.history),this._super("route",arguments),this.setupPreRoute("all",a),this.setupPreRoute(null,a),this},setupPreRoute:function(a,b){var c,d,e;return a?e="pre-route_"+a+b:(a=b,e="pre-route_"+b),this.preRoutes&&this.preRoutes[a]&&(d=this.preRoutes[a],_.isString(d)?d=this[d]:_.isArray(d)&&(c=d,d=_.bind(function(){var a,b=!0;for(a=0;a1?b:e.text(),b=null!==b&&"undefined"!=typeof b?b:"",d=e.data("thumbs-format"),splitParts(d||"",function(a){2===a.length?(c=a[0],d=a[1]):d=a.pop()}),d&&"function"==typeof this[d]&&(b=this[d](b)),setElData(e,b,c),this},renderFormatters:function(){var a=this;return this.$("[data-thumbs-format]").not("[data-thumbs-view]").each(function(){a.checkFormatting(this)}),this},setElement:function(){this._super("setElement",arguments);return this.el?this.$el.attr("thumbs-id",this.thumbsId):viewRegistry.remove(this.thumbsId),this},addSubView:function(a,b){return b&&(this.removeSubView(a),this._subviews[a]=b,b.setElement(this.$(a)).render()),this},removeSubView:function(a){var b=this._subviews[a];return b&&(b.setElement(null),b.remove(),this.$(a).empty(),this._subviews[a]=null),this},removeSubViews:function(){return _.each(this._subviews,function(a,b){this.removeSubView(b)},this),this},checkForEvents:function(){var a=this;return this.$("[data-thumbs-delegate]").not("[data-thumbs-view]").each(function(){a._bindEvents(this)}),this},_bindEvents:function(a,b){var c=this,d=!1,e=b||viewRegistry.get($(a).attr("thumbs-id"));if(b||viewRegistry.getEnclosingView(a)===c){d=!0;var f=$(a),g=_.uniqueId("thumbs_");f.addClass(g),splitParts(f.data("thumbs-delegate"),function(a){var b=a[0],d=a[1];c.events[b+" ."+g]=d,e&&c.listenTo(e,b,c[d])})}return d},render:function(){return viewRegistry.get(this.thumbsId)||viewRegistry.add(this),this.checkForEvents().setupBinders().checkForIdentifiers().renderFormatters().checkForSubviews().updateIdentifiers().findEl().assign(this._subviews)._super("render",arguments),_.isEmpty(this.events)||this.delegateEvents(),this},assign:function(a,b){var c;return _.isObject(a)?c=a:a&&(c={},c[a]=b),c&&_.each(c,function(a,b){this.addSubView(b,a)},this),this},_parseViewArgs:function(args){var ret={};if(args)try{_.extend(ret,eval("with(this){({"+args+"})}"))}catch(e){throw new Error("Unable to parse data-thumbs-args : "+args+" : "+e.toString())}return ret},renderSubviewView:function(a){var b,c,d=null,e=$(a),f=e.data("thumbs-view");if(!f||!(d=this[f]))throw new Error("Unable to find "+f+" on view");var g=this._parseViewArgs(e.data("thumbs-args"));return _.extend(g,{el:a}),c=new d(g),c.render(),this.__subviews.push(c),(b=e.data("thumbs-id"))&&(this["$"+b]=c),c},checkForSubviews:function(){var a=this;return this.$("[data-thumbs-view]").each(function(){var b=a.renderSubviewView(this);$(this).is("[data-thumbs-delegate]")&&a._bindEvents(this,b),a.turnOffModelListeners(),$(this).is("[data-thumbs-bind]")&&a.setupBind(this),$(this).is("[data-thumbs-bind-event]")&&a.setupEventBind(this),$(this).is("[data-thumbs-bind-class]")&&a.setupClassBind(this),a.$el.is("[data-thumbs-view]")&&(a.$el.is("[data-thumbs-bind]")&&a.setupBind(a.el),a.$el.is("[data-thumbs-bind-class]")&&a.setupClassBind(a.el),a.$el.is("[data-thumbs-bind-event]")&&a.setupEventBind(a.el)),a.turnOnModelListeners(),a.model&&a.__setValues(a.model.attributes),$(this).is("[data-thumbs-format]")&&a.checkFormatting(this)}),this},remove:function(){return this.__subviews.length&&(_.each(this.__subviews,function(a){a.remove()}),this.__subviews=[]),this.stopListening(),this.removeSubViews(),this.turnOffModelListeners(),this.removeIdentifiers(),this.undelegateEvents(),viewRegistry.remove(this.thumbsId),this.__monitors={},this.__events={},this._super("remove",arguments),this}});return View}(),Thumbs.TemplateView=function(){Thumbs.templater=function(){var a=_.template;return function(b){return b?a=b:a}}();var a=Thumbs.View.extend({templater:null,template:null,initialize:function(){if(this._super("initialize",arguments),this.templater||(this.templater=Thumbs.templater()),_.isString(this.template))this._template=this.templater(this.template);else{if(!_.isFunction(this.template))throw"Unexpected template property type";this._template=this.template}},getTemplateData:function(){return this.model||this.collection?(this.model||this.collection).toJSON():{}},fillTemplate:function(a){return this._template?this._template(a||this.getTemplateData()):null},renderTemplate:function(){if(this._template){var a=this.fillTemplate();a&&this.$el.html(a)}return this},render:function(){return this.renderTemplate()._super("render",arguments),this}});return a}(),Thumbs}var root=this;if("object"==typeof exports){var underscore=require("undersore"),backbone=require("backbone");module.exports=defineThumbs(backbone,underscore)}else"function"==typeof define&&define.amd?define(["underscore","backbone"],function(a,b){return defineThumbs(b,a)}):root.Thumbs=root.thumbs=defineThumbs(root.Backbone,root._)}).call(this);