-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamoeba.min.js
4 lines (4 loc) · 14.5 KB
/
amoeba.min.js
1
2
3
4
/*! amoeba - v0.2.0 - 2013-03-11
* Copyright (c) 2013 Amoeba Consulting <[email protected]> (http://amoe.ba); Licensed MIT
* http://github.com/AmoebaConsulting/amoeba-js */
(function(){Object.keys||(Object.keys=function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push(n);return t}),Function.prototype.bind||(Function.prototype.bind=function(e){var t=this;return function(){return t.apply(e,arguments)}})})(),function(){window.Amoeba={version:"0.2.0",Collection:{}}}.call(this),function(){Amoeba.log=function(e){return Amoeba.DEBUG?(console.log(e),!0):!1}}.call(this),function(){Amoeba.Util={appendQueryParam:function(e,t){var n;return n=~e.indexOf("?")?"&":"?",""+e+n+t}}}.call(this),function(){var e;e={create:"POST",update:"PUT",patch:"PATCH","delete":"DELETE",read:"GET"},Backbone.originalSync=Backbone.sync,Backbone.sync=function(e,t,n){var r,i;return n==null&&(n={}),t&&(e==="create"||e==="update"||e==="patch")&&((i=n.data)==null&&(n.data={}),r={},t.paramRoot?r[t.paramRoot]=t.toJSON(n):r=t.toJSON(n),n.contentType="application/json",n.data=JSON.stringify(_.extend(n.data,r))),Backbone.originalSync(e,t,n)}}.call(this),function(){Amoeba.Helpers=function(){function e(){var e,t,n,r;r=this.constructor.helpers;for(t=0,n=r.length;t<n;t++)e=r[t],this.extractHelper(e)}return e.helpers=[],e.prototype.extractHelper=function(e){var t,n,r;r=[];for(n in e)t=e[n],r.push(this[n]=t.bind(this));return r},e.register=function(e){return this.helpers.push(e)},e}()}.call(this),function(){var e={}.hasOwnProperty,t=function(t,n){function i(){this.constructor=t}for(var r in n)e.call(n,r)&&(t[r]=n[r]);return i.prototype=n.prototype,t.prototype=new i,t.__super__=n.prototype,t};Amoeba.Router=function(e){function n(){return n.__super__.constructor.apply(this,arguments)}return t(n,e),n.prototype.render=function(e,t){var n;return t==null&&(t={}),n=Amoeba.app.lookupContext.find(e),Amoeba.app.currentView=this.currentView=new n(t)},n.prototype.removeCurrentView=function(){var e;return(e=this.currentView)!=null&&typeof e.remove=="function"&&e.remove(),this.currentView=void 0},n}(Backbone.Router)}.call(this),function(){var e=function(e,t){return function(){return e.apply(t,arguments)}};Amoeba.Template=function(){function t(t){var n=this;return this.template=t,this.render=e(this.render,this),function(e){return e==null&&(e={}),n.template(_.extend(e,{render:n.render,helpers:Amoeba.app.helpers}))}}return t.prototype.render=function(e,t){var n,r,i;return t==null&&(t={}),(i=t.locals)==null&&(t.locals={}),t.collection?(r=function(){var r,i,s,o,u=this;s=t.collection,o=[];for(r=0,i=s.length;r<i;r++)n=s[r],o.push(function(n){var r;return r={},r[e]=n,_.extend(r,t.locals),u._render(e,r)}(n));return o}.call(this),r.join("")):this._render(e,t.locals)},t.prototype._render=function(e,t){e=JST[""+Amoeba.app.templatePath+"/"+e];if(!e)throw""+Amoeba.app.templatePath+"/"+e+" does not exist";return(new Amoeba.Template(e))(t)},t}()}.call(this),function(){var e={}.hasOwnProperty,t=function(t,n){function i(){this.constructor=t}for(var r in n)e.call(n,r)&&(t[r]=n[r]);return i.prototype=n.prototype,t.prototype=new i,t.__super__=n.prototype,t};Amoeba.View=function(e){function n(e){e==null&&(e={}),this.locals=e.locals||{},this.parent=e.parent,this.template&&(this.template=new Amoeba.Template(this.template)),this.helpers=Amoeba.app.helpers,n.__super__.constructor.apply(this,arguments)}return t(n,e),n.prototype._render=function(e,t){var n;return t==null&&(t={}),t.parent=this,n=Amoeba.app.lookupContext.find(e),new n(t)},n}(Backbone.View)}.call(this),function(){var e=function(e,t){return function(){return e.apply(t,arguments)}},t={}.hasOwnProperty,n=function(e,n){function i(){this.constructor=e}for(var r in n)t.call(n,r)&&(e[r]=n[r]);return i.prototype=n.prototype,e.prototype=new i,e.__super__=n.prototype,e};Amoeba.View.Collection=function(t){function r(t){var n;t==null&&(t={}),this.removeModel=e(this.removeModel,this),this.addModel=e(this.addModel,this),this.extractSubView=e(this.extractSubView,this),this.render=e(this.render,this),this.subviews=[],(n=t.subView)==null&&(t.subView={}),this.subView=t.subView.partial||this.subView,r.__super__.constructor.call(this,t),this.listenTo(this.collection,"add",this.addModel),this.listenTo(this.collection,"remove",this.removeModel)}return n(r,t),r.prototype.subView=Amoeba.View,r.prototype.render=function(){if(this.rendered)return;return this.extractSubViews(),this.subviews.length&&this.$el.html(this.renderSubViews()),this.rendered=!0,this.trigger("render"),this},r.prototype.refresh=function(){return this.subviews=[],this.rendered=!1,this.collection.fetch({success:this.render,silent:!0}),this},r.prototype.extractSubViews=function(){return this.collection.each(this.extractSubView)},r.prototype.extractSubView=function(e){var t;return t=this._render(this.subView,_.extend(this.options.subView,{model:e})),this.subviews.push(t),t},r.prototype.renderSubViews=function(){var e,t=this;return e=document.createDocumentFragment(),_.each(this.subviews,function(t){return e.appendChild(t.render().el)}),e},r.prototype.addModel=function(e){var t;t=this.extractSubView(e);if(this.rendered)return this.$el.append(t.render().el),this.trigger("render")},r.prototype.removeModel=function(e){var t;t=_.select(this.subviews,function(t){return t.model.id===e.id})[0];if(t){this.subviews=_.without(this.subviews,t);if(this.rendered)return t.remove()}},r}(Amoeba.View)}.call(this),function(){var e=function(e,t){return function(){return e.apply(t,arguments)}},t={}.hasOwnProperty,n=function(e,n){function i(){this.constructor=e}for(var r in n)t.call(n,r)&&(e[r]=n[r]);return i.prototype=n.prototype,e.prototype=new i,e.__super__=n.prototype,e};Amoeba.View.PaginatedCollection=function(t){function r(t){t==null&&(t={}),this.removeModel=e(this.removeModel,this),this.addModel=e(this.addModel,this),this.removePage=e(this.removePage,this),this.renderPage=e(this.renderPage,this),this.pages={},this.currentPage=t.page||1,this.collectionView=t.collectionView||this.collectionView,this.container=t.container,this.listenTo(this.container,"removePage",this.removePage),this.listenTo(this.container,"remove",this.removeModel),this.listenTo(this.container,"add",this.addModel),r.__super__.constructor.call(this,t)}return n(r,t),r.prototype.collectionView=Amoeba.View.Collection,r.prototype.renderPage=function(e,t){var n;this.pages[e]||this.createPage(e,t);if(this.pages[e].rendered)return;return n=this.getPageEl(e),n.length?n.removeClass("hide"):(n=this.pages[e].view.render().$el.addClass("page-"+e),this.$el.append(n)),e!==this.currentPage&&(this.pages[this.currentPage]&&(this.getPageEl(this.currentPage).addClass("hide"),this.pages[this.currentPage].rendered=!1),this.currentPage=e),this.pages[e].rendered=!0,this.trigger("renderPage",e),this},r.prototype.refresh=function(e,t){var n,r=this;return e==null&&(e=this.currentPage),t==null&&(t={}),this.pages[e]&&(this.pages[e].rendered=!1,this.pages[e].collection.dirty&&this.removePage(e)),n=t.success,_.extend(t,{silent:!0,success:function(t){return r.renderPage(e,t),typeof n=="function"?n(e):void 0}}),this.container.fetch(e,t),this},r.prototype.removePage=function(e){return this.pages[e]?(this.pages[e].view.remove(),delete this.pages[e],this):this},r.prototype.addModel=function(e,t){},r.prototype.removeModel=function(e,t){},r.prototype.createPage=function(e,t){return this.pages[e]={collection:t,view:this._render(this.collectionView,_.extend(this.options,{collection:t})),rendered:!1}},r.prototype.getPageEl=function(e){return this.$(".page-"+e)},r}(Amoeba.View)}.call(this),function(){var e=function(e,t){return function(){return e.apply(t,arguments)}},t={}.hasOwnProperty,n=function(e,n){function i(){this.constructor=e}for(var r in n)t.call(n,r)&&(e[r]=n[r]);return i.prototype=n.prototype,e.prototype=new i,e.__super__=n.prototype,e};Amoeba.View.ScrollableCollection=function(t){function r(){this.onLoad=e(this.onLoad,this),this.onScroll=e(this.onScroll,this),r.__super__.constructor.apply(this,arguments),$(window).on("scroll."+this.cid,this.onScroll)}return n(r,t),r.prototype.loading=!1,r.prototype.padding=100,r.prototype.onScroll=function(){if(!this.rendered||this.loading)return!0;if(this.needsToLoad()&&this.collection.hasMorePages())return this.loading=!0,this.collection.fetchNextPage({success:this.onLoad,error:this.onLoad})},r.prototype.needsToLoad=function(){var e,t,n,r,i,s;return s=$(window).height(),r=$(window).scrollTop(),i=s+r,t=this.$el.height(),n=this.$el.offset().top,e=t+n,e+this.padding<i},r.prototype.onLoad=function(){return this.loading=!1},r}(Amoeba.View.Collection)}.call(this),function(){Backbone.History.prototype.hasUrl=function(e){return _.any(this.handlers,function(t){return t.route.test(e)})}}.call(this),function(){Amoeba.LookupContext=function(){function e(e){this.viewPath=e}return e.prototype.getNamespaces=function(e){return e.split(".")},e.prototype.recurse=function(e,t){var n,r,i,s;if(!e)return void 0;if(!t.length)return e;r=this.getNamespaces(t);for(i=0,s=r.length;i<s;i++){n=r[i];if(!e[n]){e=void 0;break}e=e[n]}return e},e.prototype.findFromViewPath=function(e,t){var n,r;return t==null&&(t=window),r=this.getNamespaces(e),n=r.shift(),this.recurse(t[n],r.join("."))},e.prototype.find=function(e,t){var n;if(!e)throw"A template is required";if(typeof e!="string")return e;this.viewPath?n=this.recurse(this.viewPath,e):n=this.findFromViewPath(e,t);if(!n)throw"Cannot find template "+e;return n},e}()}.call(this),function(){var e,t=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};e=["extended","included"],Amoeba.Module=function(){function n(){}return n.extend=function(n){var r,i,s;for(r in n)i=n[r],t.call(e,r)<0&&(this[r]=i);return(s=n.extended)!=null&&s.apply(this),this},n.include=function(n){var r,i,s;for(r in n)i=n[r],t.call(e,r)<0&&(this.prototype[r]=i);return(s=n.included)!=null&&s.apply(this),this},n}()}.call(this),function(){var e=function(e,t){return function(){return e.apply(t,arguments)}},t={}.hasOwnProperty,n=function(e,n){function i(){this.constructor=e}for(var r in n)t.call(n,r)&&(e[r]=n[r]);return i.prototype=n.prototype,e.prototype=new i,e.__super__=n.prototype,e};Amoeba.App=function(t){function r(t){t==null&&(t={}),this.unbindRequestListener=e(this.unbindRequestListener,this),this.bindRequestListner=e(this.bindRequestListner,this),this.requestHandler=e(this.requestHandler,this),_.defaults(t,this.constructor.defaults),this.helpers=new Amoeba.Helpers,this.lookupContext=new Amoeba.LookupContext(t.viewPath),t.templatePath&&(this.templatePath=t.templatePath),this.hijackRequests=t.hijackRequests,this.hijackRequests&&this.bindRequestListner(),this}return n(r,t),r.include(Backbone.Events),r.defaults={hijackRequests:!0},r.settings={linkSelector:"a"},r.start=function(e){var t;return e==null&&(e={}),Amoeba.app=new this(e),Amoeba.app.initialize(e),t=_.pick(e,"pushState","hashChange","silent","root"),Backbone.history.start(t),Amoeba.app},r.prototype.initialize=function(){},r.prototype.requestHandler=function(e){var t;return t=$(e.target).closest("a").attr("href").replace(/^\//,""),Amoeba.log("Initiating route to '"+t+"'"),Backbone.history.hasUrl(t)?(Backbone.history.navigate(t,{trigger:!0}),!1):!0},r.prototype.bindRequestListner=function(){return $(document).on("click",this.constructor.settings.linkSelector,this.requestHandler)},r.prototype.unbindRequestListener=function(){return $(document).off("click",this.constructor.settings.linkSelector,this.requestHandler)},r}(Amoeba.Module)}.call(this),function(){var e={}.hasOwnProperty,t=function(t,n){function i(){this.constructor=t}for(var r in n)e.call(n,r)&&(t[r]=n[r]);return i.prototype=n.prototype,t.prototype=new i,t.__super__=n.prototype,t};Amoeba.Collection.Container=function(e){function n(){this.pages={},this.initialize.apply(this,arguments)}return t(n,e),n.include(Backbone.Events),n.prototype.urlPageQuery="page",n.prototype.collection=Backbone.Collection,n.prototype.initialize=function(){},n.prototype.sync=function(){return Backbone.sync.apply(this,arguments)},n.prototype.parse=function(e){return e},n.prototype.resetPage=function(e,t,n){var r;return n==null&&(n={}),n=_.clone(n),n.parse=!0,this.pages[e]?this.pages[e].reset(this.parse(t),n):(r=new this.collection,r.page=e,this.model&&(r.model=this.model),r.reset(this.parse(t),n),r.on("all",this._onCollectionEvent,this),this.pages[e]=r),this.pages[e].dirty=!1,this.pages[e]},n.prototype._onCollectionEvent=function(e,t,n){if(e==="add")return this.trigger("add",n.page,t,this);if(e==="remove")return this.shift(n.page),this.trigger("remove",n.page,t,this)},n.prototype.reset=function(){var e,t,n;n=this.pages;for(t in n)e=n[t],this.remove(t);return this.pages={},this},n.prototype.shift=function(e){var t,n,r,i,s=this;if(this.perPage&&this.pages[e].length>=this.perPage)return this;n=e+1,this.pages[n]?this._shift(e,n):this.fetch(n,{success:function(){if(s.pages[n])return s._shift(e,n)}}),i=this.pages;for(r in i)t=i[r],r>n&&this.remove(r);return this},n.prototype._shift=function(e,t){var n,r;return n=this.pages[t],r=n.first(),r&&(n.remove(r,{silent:!0}),this.pages[e].add(r)),n.length===0?this.remove(t):n.dirty=!0},n.prototype.remove=function(e){return this.pages[e]?(this.pages[e].reset(),this.pages[e].off("all",this._onCollectionEvent,this),delete this.pages[e],this.trigger("removePage",e,this),this):this},n.prototype.fetch=function(e,t){var n,r,i=this;t==null&&(t={}),t=_.clone(t),e=parseInt(e);if(this.pages[e]&&!this.pages[e].dirty&&!t.force)return typeof t.success=="function"?t.success(this.pages[e],this,this.pages[e].toJSON(),t):void 0;n=t.success,t.success=function(r,s){var o;return o=i.resetPage(e,s,t),typeof n=="function"?n(o,i,s,t):void 0},r=_.result(this,"url");if(!r)throw"No url specified";return t.url=Amoeba.Util.appendQueryParam(r,""+this.urlPageQuery+"="+e),this.sync("read",this,t)},n.prototype.toJSON=function(e){return _.map(this.pages,function(t,n){var r;return r={},r[n]=t.toJSON(e),r})},n}(Amoeba.Module)}.call(this),function(){var e={}.hasOwnProperty,t=function(t,n){function i(){this.constructor=t}for(var r in n)e.call(n,r)&&(t[r]=n[r]);return i.prototype=n.prototype,t.prototype=new i,t.__super__=n.prototype,t};Amoeba.Collection.Growable=function(e){function n(){return n.__super__.constructor.apply(this,arguments)}return t(n,e),n.prototype.urlPageQuery="page",n.prototype.reset=function(){return this.nextPage=void 0,n.__super__.reset.apply(this,arguments)},n.prototype.hasMorePages=function(){return _.result(this,"nextPage")!=null},n.prototype.fetchNextPage=function(e){var t,n;e==null&&(e={}),n=_.result(this,"url");if(!n)throw"No url specified";return t=""+this.urlPageQuery+"="+_.result(this,"nextPage"),_.extend(e,{url:Amoeba.Util.appendQueryParam(n,t),update:!0,remove:!1}),this.fetch(e)},n}(Backbone.Collection)}.call(this),function(){}.call(this);