// backbone.datagrid v0.3.2 // // Copyright (c) 2012 Loïc Frering // Distributed under the MIT license (function(){var e=Backbone.View.extend({initialize:function(){this.columns=this.options.columns,this.options=_.defaults(this.options,{paginated:!1,page:1,perPage:10,tableClassName:"table",emptyMessage:"

No results found.

"}),this.collection.on("reset",this.render,this),this._prepare()},render:function(){return this.$el.empty(),this.renderTable(),this.options.paginated&&this.renderPagination(),this},renderTable:function(){var e=$("
",{"class":this.options.tableClassName});this.$el.append(e);var n=new t({columns:this.columns,sorter:this.sorter});e.append(n.render().el),e.append(""),this.collection.isEmpty()?this.$el.append(this.options.emptyMessage):this.collection.forEach(this.renderRow,this)},renderPagination:function(){var e=new r({pager:this.pager});this.$el.append(e.render().el)},renderRow:function(e){var t={model:e,columns:this.columns},r=this.options.rowClassName;_.isFunction(r)&&(r=r(e)),t.className=r;var i=new n(t);this.$("tbody").append(i.render(this.columns).el)},refresh:function(e){this.options.paginated?this._page(e):this.options.inMemory?(this.collection.trigger("reset",this.collection),e&&e.success&&e.success()):this._request(e)},sort:function(e,t){this.sorter.sort(e,t)},page:function(e){this.pager.page(e)},perPage:function(e){this.pager.set("perPage",e)},_sort:function(){this.options.inMemory?this._sortInMemory():this._sortRequest()},_sortInMemory:function(){this.options.paginated?(this._originalCollection.comparator=_.bind(this._comparator,this),this._originalCollection.sort(),this.page(1)):(this.collection.comparator=_.bind(this._comparator,this),this.collection.sort())},_comparator:function(e,t){var n=this._comparatorForColumn(this.sorter.get("column")),r=n(e,t);return this.sorter.sortedASC()?r:-r},_comparatorForColumn:function(e){var t=_.find(this.columns,function(t){return t.property===e||t.index===e});return t?t.comparator:undefined},_sortRequest:function(){this._request()},_page:function(e){this.options.inMemory?this._pageInMemory(e):this._pageRequest(e)},_pageRequest:function(e){this._request(e)},_request:function(e){e=e||{};var t=e.success,n=e.silent;e.data=this._getRequestData(),e.success=_.bind(function(e){(!this.columns||_.isEmpty(this.columns))&&this._prepareColumns(),t&&t(),this.options.paginated&&this.pager.update(e),n||e.trigger("reset",e)},this),e.silent=!0,this.collection.fetch(e)},_getRequestData:function(){if(this.collection.data&&_.isFunction(this.collection.data))return this.collection.data(this.pager,this.sorter);if(this.collection.data&&typeof this.collection.data=="object"){var e={};return _.each(this.collection.data,function(t,n){_.isFunction(t)&&(t=t(this.pager,this.sorter)),e[n]=t},this),e}return this.options.paginated?{page:this.pager.get("currentPage"),per_page:this.pager.get("perPage")}:{}},_pageInMemory:function(e){this._originalCollection||(this._originalCollection=this.collection.clone());var t=this.pager.get("currentPage"),n=this.pager.get("perPage"),r=(t-1)*n,i=r+n;e&&e.success&&e.success(),this.pager.set("total",this._originalCollection.size()),this.collection.reset(this._originalCollection.slice(r,i),e)},_prepare:function(){this._prepareSorter(),this._preparePager(),this._prepareColumns(),this.refresh()},_prepareSorter:function(){this.sorter=new f,this.sorter.on("change",function(){this._sort(this.sorter.get("column"),this.sorter.get("order"))},this)},_preparePager:function(){this.pager=new a({currentPage:this.options.page,perPage:this.options.perPage}),this.pager.on("change:currentPage",function(){this._page()},this),this.pager.on("change:perPage",function(){this.page(1)},this)},_prepareColumns:function(){!this.columns||_.isEmpty(this.columns)?this._defaultColumns():_.each(this.columns,function(e,t){this.columns[t]=this._prepareColumn(e,t)},this)},_prepareColumn:function(e,t){_.isString(e)&&(e={property:e});if(_.isObject(e)){e.index=t;if(e.property)e.title=e.title||this._formatTitle(e.property);else if(!e.property&&!e.view)throw new Error("Column '"+e.title+"' has no property and must accordingly define a custom cell view.");if(this.options.inMemory&&e.sortable){if(!e.comparator&&!e.property&&!e.sortedProperty)throw new Error("Invalid column definition: a sortable column must have a comparator, property or sortedProperty defined.");e.comparator=e.comparator||this._defaultComparator(e.sortedProperty||e.property)}}return e},_formatTitle:function(e){return _.map(e.split(/\s|_/),function(e){return e.charAt(0).toUpperCase()+e.substr(1)}).join(" ")},_defaultColumns:function(){this.columns=[];var e=this.collection.first(),t=0;if(e)for(var n in e.toJSON())this.columns.push(this._prepareColumn(n,t++))},_defaultComparator:function(e){return function(t,n){var r=t.has(e)?t.get(e):"",i=n.has(e)?n.get(e):"";return r.localeCompare(i)}}}),t=e.Header=Backbone.View.extend({tagName:"thead",initialize:function(){this.columns=this.options.columns,this.sorter=this.options.sorter},render:function(){var e=new Backbone.Model,t,r=[];_.each(this.columns,function(n,i){t=_.clone(n),t.property=n.property||n.index,t.view=n.headerView||{type:u,sorter:this.sorter},e.set(t.property,n.title),r.push(t)},this);var i=new n({model:e,columns:r,header:!0});return this.$el.html(i.render().el),this}}),n=e.Row=Backbone.View.extend({tagName:"tr",initialize:function(){this.columns=this.options.columns,this.model.on("change",this.render,this)},render:function(){return this.$el.empty(),_.each(this.columns,this.renderCell,this),this},renderCell:function(e){var t=this._resolveCellView(e);this.$el.append(t.render().el)},_resolveCellView:function(e){var t={model:this.model,column:e};if(this.options.header||e.header)t.tagName="th";var n=e.cellClassName;_.isFunction(n)&&(n=n(this.model)),t.className=n;var r=e.view||i;if(typeof r!="object"&&(!r.prototype||!r.prototype.render))if(_.isString(r))t.callback=_.template(r),r=s;else{if(!_.isFunction(r)||!!r.prototype.render)throw new TypeError('Invalid view passed to column "'+e.title+'".');t.callback=r,r=s}else if(typeof r=="object"){_.extend(t,r),r=r.type;if(!r||!r.prototype||!r.prototype.render)throw new TypeError('Invalid view passed to column "'+e.title+'".')}return new r(t)}}),r=e.Pagination=Backbone.View.extend({className:"pagination pagination-centered",events:{"click li:not(.disabled) a":"page","click li.disabled a":function(e){e.preventDefault()}},initialize:function(){this.pager=this.options.pager},render:function(){var e=$(""),t;t=$(''),this.pager.hasPrev()||t.addClass("disabled"),e.append(t);if(this.pager.hasTotal())for(var n=1;n<=this.pager.get("totalPages");n++)t=$("
  • "),n===this.pager.get("currentPage")&&t.addClass("active"),t.append(''+n+""),e.append(t);return t=$(''),this.pager.hasNext()||t.addClass("disabled"),e.append(t),this.$el.append(e),this},page:function(e){var t=$(e.target),n;return t.parent().hasClass("prev")?this.pager.prev():t.parent().hasClass("next")?this.pager.next():this.pager.page(parseInt($(e.target).html(),10)),!1}}),i=e.Cell=Backbone.View.extend({tagName:"td",initialize:function(){this.column=this.options.column},render:function(){return this._prepareValue(),this.$el.html(this.value),this},_prepareValue:function(){this.value=this.model.get(this.column.property)}}),s=e.CallbackCell=i.extend({initialize:function(){s.__super__.initialize.call(this),this.callback=this.options.callback},_prepareValue:function(){this.value=this.callback(this.model.toJSON())}}),o=e.ActionCell=i.extend({initialize:function(){o.__super__.initialize.call(this)},action:function(){return this.options.action(this.model)},_prepareValue:function(){var e=$("");e.html(this.options.label),e.attr("href",this.options.href||"#"),this.options.actionClassName&&e.addClass(this.options.actionClassName),this.options.action&&this.delegateEvents({"click a":this.action}),this.value=e}}),u=e.HeaderCell=i.extend({initialize:function(){u.__super__.initialize.call(this),this.sorter=this.options.sorter,this.column.sortable&&this.delegateEvents({click:"sort"})},render:function(){this._prepareValue();var e=this.value,t;return this.column.sortable&&(this.$el.addClass("sortable"),this.sorter.sortedBy(this.column.sortedProperty||this.column.property)||this.sorter.sortedBy(this.column.index)?this.sorter.sortedASC()?t="icon-chevron-up":t="icon-chevron-down":t="icon-minus",e+=' '),this.$el.html(e),this},sort:function(){this.sorter.sort(this.column.sortedProperty||this.column.property)}}),a=e.Pager=Backbone.Model.extend({initialize:function(){this.on("change:perPage change:total",function(){this.totalPages(this.get("total"))},this),this.has("total")&&this.totalPages(this.get("total"))},update:function(e){_.each(["hasNext","hasPrev","total","totalPages","lastPage"],function(t){_.isUndefined(e[t])||this.set(t,e[t])},this)},totalPages:function(e){_.isNumber(e)?this.set("totalPages",Math.ceil(e/this.get("perPage"))):this.set("totalPages",undefined)},page:function(e){this.inBounds(e)&&(e===this.get("currentPage")?this.trigger("change:currentPage"):this.set("currentPage",e))},next:function(){this.page(this.get("currentPage")+1)},prev:function(){this.page(this.get("currentPage")-1)},hasTotal:function(){return this.has("totalPages")},hasNext:function(){return this.hasTotal()?this.get("currentPage")1},inBounds:function(e){return!this.hasTotal()||e>0&&e<=this.get("totalPages")},set:function(){var e=[];for(var t=0;t