-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbackbone.tableview.min.js
28 lines (28 loc) · 14.7 KB
/
backbone.tableview.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(function(){var t,u,v,w,x,y,z,c=function(c,a){return function(){return c.apply(a,arguments)}},A={}.hasOwnProperty,f=function(c,a){function g(){this.constructor=c}for(var b in a)A.call(a,b)&&(c[b]=a[b]);g.prototype=a.prototype;c.prototype=new g;c.__super__=a.prototype;return c},r=[].slice;Backbone.TableView=function(e){function a(){this.hideLoading=c(this.hideLoading,this);this.showLoadingNow=c(this.showLoadingNow,this);this.showLoading=c(this.showLoading,this);this.render=c(this.render,this);this.toggleSort=
c(this.toggleSort,this);this.nextPage=c(this.nextPage,this);this.prevPage=c(this.prevPage,this);this.toPage=c(this.toPage,this);this.changeSize=c(this.changeSize,this);this.renderData=c(this.renderData,this);this.refreshPagination=c(this.refreshPagination,this);this.update=c(this.update,this);this.updateUrl=c(this.updateUrl,this);this.updateSearch=c(this.updateSearch,this);this.createFilter=c(this.createFilter,this);this.setData=c(this.setData,this);return t=a.__super__.constructor.apply(this,arguments)}
f(a,e);a.prototype.tagName="div";a.prototype.titleTemplate=_.template('<div class="<%= classSize %>">\n <h4 class="<%= model.className || "" %>"><%= model.name || model %></h4>\n</div>');a.prototype.filtersTemplate=_.template('<div class="filters controls tableview-center <%= classSize %>" />');a.prototype.searchTemplate=_.template('<div class="<%= classSize %> form-group">\n <input type="text" class="search-query form-control input-block-level pull-right" placeholder="<%= model.detail || model %>" value="<%- data[model.query || "q"] || "" %>"></input>\n</div>');
a.prototype.paginationTemplate=_.template('<div class="row">\n <div class="span3 col-lg-3">\n <div class="tableview-info">Showing <%= from %> to <%= to %><%= total %></div>\n </div>\n <div class="span9 col-lg-9">\n <div class="pagination tableview-pagination">\n <ul class="pagination">\n <li class="pager-prev <%= prevDisabled %>"><a href="javascript:void(0)">\u2190 Previous</a></li>\n <% _.each(pages, function (page) { %>\n <li class="pager-page <%= page.active %>"><a href="javascript:void(0)"><%= page.value %></a></li>\n <% }) %>\n <li class="pager-next <%= nextDisabled %>"><a href="javascript:void(0)">Next \u2192 </a></li>\n </ul>\n </div>\n <div class="pagination tableview-size">\n <ul class="pagination">\n <li class="disabled"><a>Size</a></li>\n <% _.each(sizes, function (size) { %>\n <li class="pager-size <%= size.active %>"><a href="javascript:void(0)"><%= size.value %></a></li>\n <% }) %>\n </ul>\n </div>\n </div>\n</div>');
a.prototype.emptyTemplate=_.template('<tr><td colspan="10"><%= text %></td></tr>');a.prototype.columnsTemplate=_.template('<% _.each(model, function (col, key) { %>\n <% if (!col.noshow) { %>\n <th abbr="<%= key || col %>"\n class="<%= !col.nosort && !self.nosort ? "tableview-sorting" : "" %> <%= ((key || col) == data.sort_col) ? "tableview-sorting-" + data.sort_dir : "" %> <%= col.className || "" %>">\n <%= col.header || key %>\n </th>\n <% } %>\n<% }) %>');
a.prototype.template=_.template('<div class="tableview-container">\n <div class="row">\n <%= title %>\n\n <%= filters %>\n\n <%= search %>\n </div>\n\n <div class="tableview-table-wrapper">\n <span class="tableview-loading-spinner hide">Loading...</span>\n <table class="table table-striped tableview-table">\n <thead>\n <tr>\n <%= columns %>\n </tr>\n </thead>\n <tbody class="fade in" />\n </table>\n </div>\n\n <div id="pagination-main" />\n</div>');
a.prototype.search=!1;a.prototype.pagination=!1;a.prototype.loading=!0;a.prototype.fetch=!0;a.prototype.myEvents={"change .search-query":"updateSearch","click th":"toggleSort","click .pager-size:not(.active)":"changeSize","click .pager-page:not(.active)":"toPage","click .pager-prev:not(.disabled)":"prevPage","click .pager-next:not(.disabled)":"nextPage"};a.prototype.initialize=function(a){var b,d,c,h;for(b in a)d=a[b],this[b]=d;this.filterClasses=_.extend({option:Backbone.TableView.ButtonOptionFilter,
dropdown:Backbone.TableView.DropdownFilter,input:Backbone.TableView.InputFilter,button:Backbone.TableView.ButtonFilter,buttongroup:Backbone.TableView.ButtonGroupFilter},this.filterClasses);this.events=_.extend(_.clone(this.myEvents),this.events);this.data=_.extend({},this.initialData);this.router&&(this.data=_.extend(this.data,this.parseQueryString(Backbone.history.fragment)),this.on("updating",this.updateUrl));this.pagination&&(this.data.page=null!=(c=parseInt(this.data.page)||this.page)?c:1,this.data.size=
null!=(h=parseInt(this.data.size)||this.size)?h:10,this.on("updated",this.refreshPagination));this.loading&&(this.listenTo(this.collection,"request",this.showLoading),this.listenTo(this.collection,"sync",this.hideLoading));this.listenTo(this.collection,"sync",this.renderData);return this};a.prototype.prettyName=function(a){return a.charAt(0).toUpperCase()+a.substring(1).replace(/_(\w)/g,function(a,g){return" "+g.toUpperCase()})};a.prototype.parseQueryString=function(a){var b,d,c,h;c={};if(a&&0<=(b=
a.indexOf("?")))for(a=a.substring(b+1),h=/([^&=]+)=?([^&]*)/g,b=function(a){return decodeURIComponent(a.replace(/\+/g," "))};d=h.exec(a);)c[b(d[1])]=b(d[2]);return c};a.prototype.applyTemplate=function(a,b,d){null==d&&(d=12);return b&&d&&a({data:this.data,model:b,classSize:"span"+d+" col-lg-"+d,self:this})||""};a.prototype.setData=function(){var a,b,d;a=1<=arguments.length?r.call(arguments,0):[];this.pagination&&(this.data.page=1);for(;1<a.length;)b=a[0],d=a[1],a=3<=a.length?r.call(a,2):[],d||!1===
d||0===d?this.data[b]=d:delete this.data[b];return this.update()};a.prototype.createFilter=function(a,b){var d,c,h,e,k,m,l,n,p,q,s;return new this.filterClasses[b.type]({id:a,extraId:b.extraId,name:null!=(d=b.name)?d:this.prettyName(a),off:null!=(e=b.off)?e:"false",on:null!=(k=b.on)?k:"true",filterClass:null!=(m=b.className)?m:"",options:b.options,init:(null!=(s=b.set)?s:_.identity)(null!=(l=null!=(n=this.data[a])?n:b.init)?l:"",null!=(p=null!=(q=this.data[b.extraId])?q:b.extraInit)?p:""),setData:this.setData,
get:null!=(c=b.get)?c:_.identity,getExtraId:null!=(h=b.getExtraId)?h:_.identity})};a.prototype.updateSearch=function(a){return this.setData(this.search.query||"q",a.currentTarget.value)};a.prototype.updateUrl=function(a){var b,d;d=Backbone.history.fragment;0<=(b=d.indexOf("?"))&&(d=d.substring(0,b));(b=$.param(this.data))&&(d+="?"+b);this.router.navigate(d,{replace:a});return this};a.prototype.update=function(a){this.trigger("updating",a);a&&this.skipInitialFetch?this.renderData():this.fetch&&this.collection.fetch({data:("function"===
typeof this.filterData?this.filterData(_.clone(this.data)):void 0)||this.data});return this};a.prototype.refreshPagination=function(){var a,b,d,c,h,e,k,m,l;this.data.page=parseInt("function"===typeof(a=this.collection).getData?a.getData("page"):void 0)||this.data.page;this.data.size=parseInt("function"===typeof(d=this.collection).getData?d.getData("size"):void 0)||this.data.size;a=(this.data.page-1)*this.data.size;m=a+this.collection.size();0<this.collection.size()&&a++;d=null!=this.collection.count?
_.result(this.collection,"count"):-1;0>d?(c=1,e=h=this.data.page,l=""):(c=Math.ceil(d/this.data.size)||1,h=_.max([1,this.data.page-2-_.max([0,2+this.data.page-c])]),e=_.min([c,this.data.page+2+_.max([0,3-this.data.page])]),l=" of "+d+" entries");c=function(){var a,d,c,g;c=_.range(h,e+1);g=[];a=0;for(d=c.length;a<d;a++)b=c[a],g.push({value:b,active:b===this.data.page&&"active"||""});return g}.call(this);k=function(){var a,d,c,g;c=[10,20,50,200];g=[];a=0;for(d=c.length;a<d;a++)b=c[a],g.push({value:b,
active:b===this.data.size&&"active"||""});return g}.call(this);this.$("#pagination-main").html(this.paginationTemplate({from:a,to:m,total:l,prevDisabled:1===this.data.page?"disabled":"",nextDisabled:m===d?"disabled":"",pages:c,sizes:k}));return this};a.prototype.renderData=function(){var a,b,d,c,e,f,k,m,l,n,p,q;a=this.$("tbody");if(0===this.collection.models.length)a.html(this.emptyTemplate({text:null!=(b=this.empty)?b:"No records to show"}));else for(a.html(""),l=this.collection.models,k=0,m=l.length;k<
m;k++){c=l[k];f=$("<tr>");n=this.columns;for(e in n)d=n[e],d.noshow||(b=$("<td>").addClass(d.className).addClass(d.tdClass),null!=d.draw?b.html(d.draw(c,this)):b.text(null!=(p=c.get(e))?p:""),f.append(b));a.append(null!=(q="function"===typeof this.rowTransformer?this.rowTransformer(f,c):void 0)?q:f)}this.trigger("updated");return this};a.prototype.changeSize=function(a){return this.setData("size",parseInt(a.currentTarget.childNodes[0].text))};a.prototype.toPage=function(a){return this.setData("page",
parseInt(a.currentTarget.childNodes[0].text))};a.prototype.prevPage=function(){if(1<this.data.page)return this.setData("page",this.data.page-1)};a.prototype.nextPage=function(){return this.setData("page",this.data.page+1)};a.prototype.toggleSort=function(a){var b;a=a.currentTarget;b=a.className;if(0<=b.indexOf("tableview-sorting-asc"))b="desc";else if(0<=b.indexOf("tableview-sorting"))b="asc";else return this;this.$("th").removeClass("tableview-sorting-desc tableview-sorting-asc");this.$(a).addClass("tableview-sorting-"+
b);return this.setData("sort_col",a.abbr,"sort_dir",b)};a.prototype.render=function(){var a,b,d,c,e,f=this;d=3;a=6;b=3;this.search||(a+=b,b=0);null==this.title?(a+=d,d=0):null==this.filters&&(d+=a,a=0);this.$el.html(this.template({title:this.applyTemplate(this.titleTemplate,this.title,d),search:this.applyTemplate(this.searchTemplate,this.search,b),filters:this.applyTemplate(this.filtersTemplate,this.filters,a),columns:this.applyTemplate(this.columnsTemplate,this.columns)}));b=_.compact(_.map(this.filters,
function(a,b){return f.createFilter(b,a)}));d=this.$(".filters");c=0;for(e=b.length;c<e;c++)a=b[c],d.append(a.render().el);return this.update(!0)};a.prototype.showLoading=function(){this.showLoadingTimeout&&clearTimeout(this.showLoadingTimeout);return this.showLoadingTimeout=_.delay(this.showLoadingNow,500)};a.prototype.showLoadingNow=function(){this.showLoadingTimeout=void 0;this.$("tbody").removeClass("in");return this.$(".tableview-loading-spinner").removeClass("hide")};a.prototype.hideLoading=
function(){this.showLoadingTimeout&&clearTimeout(this.showLoadingTimeout);this.$("tbody").addClass("in");return this.$(".tableview-loading-spinner").addClass("hide")};return a}(Backbone.View);Backbone.TableView.Filter=function(e){function a(){this.render=c(this.render,this);return u=a.__super__.constructor.apply(this,arguments)}f(a,e);a.prototype.tagName="div";a.prototype.className="pull-left tableview-filterbox";a.prototype.initialize=function(a){this.options=a;this.id=this.options.id;this.extraId=
this.options.extraId;this.setData=this.options.setData;return this.options.options=_.map(_.result(this.options,"options"),function(a){_.isArray(a)?a={name:a[0],value:a[1]}:_.isObject(a)||(a={name:a,value:a});return a})};a.prototype.render=function(){this.$el.html(this.template(this.options));return this};return a}(Backbone.View);Backbone.TableView.InputFilter=function(e){function a(){this.update=c(this.update,this);return v=a.__super__.constructor.apply(this,arguments)}f(a,e);a.prototype.template=
_.template('<span class="input-group-addon add-on"><%= name %></span><input type="text" class="form-control filter <%= filterClass %>" value="<%= init %>"></input>');a.prototype.className="input-group col-lg-3 input-prepend pull-left tableview-filterbox";a.prototype.events={"change .filter":"update"};a.prototype.update=function(a){return this.extraId?this.setData(this.id,this.options.get(a.currentTarget.value),this.extraId,this.options.getExtraId(a.currentTarget.value)):this.setData(this.id,this.options.get(a.currentTarget.value))};
return a}(Backbone.TableView.Filter);Backbone.TableView.ButtonFilter=function(e){function a(){this.update=c(this.update,this);return w=a.__super__.constructor.apply(this,arguments)}f(a,e);a.prototype.template=_.template('<button type="button" class="filter btn <%= init == on ? "active" : "" %> <%= filterClass %>"><%= name %></button>');a.prototype.events={"click .filter":"update"};a.prototype.initialize=function(){a.__super__.initialize.apply(this,arguments);this.values=[this.options.off,this.options.on];
return this.current=this.options.init===this.options.on?1:0};a.prototype.update=function(a){this.$(a.currentTarget).toggleClass("active");this.current=1-this.current;return this.setData(this.id,this.values[this.current])};return a}(Backbone.TableView.Filter);Backbone.TableView.ButtonGroupFilter=function(e){function a(){this.update=c(this.update,this);return x=a.__super__.constructor.apply(this,arguments)}f(a,e);a.prototype.template=_.template('<% _.each(options, function (el, i) { %>\n <button type="button" class="btn <%= _.contains(init, el.value) ? "active" : "" %> <%= !_.isUndefined(el.className) ? el.className : "" %>" value="<%= el.value %>"><%= el.name %></button>\n<% }) %>');
a.prototype.className="btn-group pull-left tableview-filterbox";a.prototype.events={"click .btn":"update"};a.prototype.update=function(a){var b=this;this.$(a.currentTarget).toggleClass("active");a=_.compact(_.map(this.$(".btn"),function(a){return b.$(a).hasClass("active")?b.$(a).attr("value"):null}));return this.setData(this.id,this.options.get(a))};return a}(Backbone.TableView.Filter);Backbone.TableView.ButtonOptionFilter=function(e){function a(){this.update=c(this.update,this);return y=a.__super__.constructor.apply(this,
arguments)}f(a,e);a.prototype.template=_.template('<% _.each(options, function (el, i) { %>\n <button type="button" class="btn <%= init == el.value ? "active" : "" %>" value="<%= el.value %>"><%= el.name %></button>\n<% }) %>');a.prototype.className="btn-group pull-left tableview-filterbox";a.prototype.events={"click .btn":"update"};a.prototype.update=function(a){this.$(".btn").removeClass("active");this.$(a.currentTarget).addClass("active");return this.setData(this.id,this.options.get(a.currentTarget.value))};
return a}(Backbone.TableView.Filter);Backbone.TableView.DropdownFilter=function(e){function a(){this.update=c(this.update,this);return z=a.__super__.constructor.apply(this,arguments)}f(a,e);a.prototype.template=_.template('<select class="form-control filter <%= filterClass %>">\n <% _.each(options, function (el, i) { %>\n <option <%= init == el.value ? "selected=\'selected\'" : "" %> value="<%= el.value %>"><%= el.name %></option>\n <% }) %>\n</select>');a.prototype.events={"change .filter":"update"};
a.prototype.update=function(a){return this.setData(this.id,this.options.get(a.currentTarget.value))};return a}(Backbone.TableView.Filter)}).call(this);