This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tTable.min.js
2 lines (2 loc) · 12.8 KB
/
tTable.min.js
1
2
/*! tTable v0.5.0 */
!function(a){var b={container:null,titles:[],data:[],row_numbers:!1,pager:null,page:1,show_pages:!0,page_size:10,page_sizes:[10,25,50],nav_arrows:!0,"goto":!0,sort_by:0,sorting:!0,sort_type:"asc",prefix:{},suffix:{},formatter:null,hover_cols:!1,hidden_cols:[],search_container:null,search:!1,search_auto:!0,search_sensitive:!1,search_value:"",ajax:null},c=function(a){if(!(this instanceof c))return new c(a);var d=this;return d.config=_.clone(b),d.init(a)},d=c.prototype;d.cache=[],d.tpl={top:"<table class='tTable'>",header:"<tr><%= data.colls || '' %></tr>",colgroup:"<colgroup></colgroup>",row:"<tr class='<%= className %>'><%= colls %></tr>",coll:"<td><%= data.html || '' %></td>",sorting:'<div class="table-sorting" data-sort_type="<%= sort_type %>" data-sort_by="<%= sort_by %>" <%= is_sort_column ? "data-sort" : "" %>><div class="table-sorting-asc"></div><div class="table-sorting-desc"></div></div>',pager:{wrap_top:'<div class="table-pager">',arrows:'<span class="table-pager-arrows"><a href="#" class="table-pager-arrows-prev <%= prev_disabled %>">prev</a><a href="#" class="table-pager-arrows-next <%= next_disabled %>">next</a></span>',pages_top:'<span class="table-pager-pages">',pages:'<a href="#<%= page %>" class="table-pager-pages-item <%= current == page ? "table-pager-pages-item__on" : ""%>" data-goto="<%= page %>"><%= page %></a>',dots:'<span class="table-pager-pages-item">...</span>',pages_bottom:"</span>","goto":'<input type="text" name="table-goto" class="table-pager-goto" />',page_size:'<select class="table-pager-page_size"><% _.each(sizes, function(item){ %><option value="<%= item %>" <%= current == item ? "selected" : "" %>><%= item %></option><% }); %></select>',wrap_bottom:"</div>"},loading:'<div class="table-loading">Loading data...</div>',no_data:'<div class="table-loading">No data...</div>',search:'<input type="text" name="table_search" class="table-search-input" placeholder="Search" value="<%= value %>">',bottom:"</table>"},d.html={},d.init=function(a){var b,c=this;return c.config=_.extend(c.config,a),c.$el=$(c.get("container")),c.$pager=$(c.get("pager")),c.xhr={},c.xhr_key="",c.xhr_data={},c.xhr_data_size={},c.loading=!1,b=c.get("ajax"),b&&"string"==typeof b.url&&!c.ajax_data_size&&c.getAJAXData(),c.get("search")&&(c.search=c.get("search_value"),c.$search=$(c.get("search_container")),c.renderSearch(),c.search&&c.searchData()),c.goto(c.get("page")),c},d.destroy=function(){var a=this;return a.$search&&(a.$search.off("input keypress blur",".table-search-input"),a.$search.empty()),a.$el&&(a.$el.off("click",".table-sorting"),a.$el.undelegate("td","mouseover mouseleave"),a.$el.empty()),a.$pager&&(a.$pager.off("click",".table-pager-arrows-prev").off("click",".table-pager-arrows-next").off("click",".table-pager-pages-item").off("keypress",".table-pager-goto").off("change",".table-pager-page_size"),a.$pager.empty()),a},d.get=function(a){var b,c=this;return b=c.config[a]},d.set=function(a){var b=this;for(var c in a)b.config[c]=a[c];return b},d.renderSearch=function(){var a=this,b=a.get("search_value");return a.$search.html(_.template(a.tpl.search,{value:b})),a},d.renderTable=function(){var a,b=this,c={};return c.top=b.html.top||_.template(b.tpl.top,{}),c.header=b.__tableHeadHTML(),c.body=b.__tableBodyHTML(),c.bottom=b.html.bottom||_.template(b.tpl.bottom,{}),a=b.loading?c.top+c.header+c.bottom+b.tpl.loading:0==b.dataSize()?c.top+c.header+c.bottom+b.tpl.no_data:c.top+c.header+c.body+c.bottom,b.$el.html(a),b.updatePager(),b.bindEvents(),b},d.__tableHeadHTML=function(){var a=this,b=a.get("row_numbers"),c=a.get("titles"),d=[{title:"#",type:"number"}],e=a.get("sorting"),f=a.get("sort_by"),g=a.get("sort_type"),h=a.html.header||"",i=a.get("hover_cols"),j=a.get("hidden_cols"),k=0;return h||b&&(k+=1,h+=_.template(a.tpl.coll,{data:{html:d[0].title}})),_.each(c,function(b,c){var d="",i=c+1;j&&-1!==j.indexOf(i)||(k+=1,(e===!0||_.contains(e,i))&&(d=_.template(a.tpl.sorting,{sort_by:i,sort_type:g,is_sort_column:f==i})),h+=_.template(a.tpl.coll,{data:{html:b.title+d}}))}),h=_.template(a.tpl.row,{className:"table-head",colls:h}),i&&(h=Array(k+1).join(a.tpl.colgroup)+h),h},d.__tableBodyHTML=function(){var a=this,b="",c=a.get("page_size"),d=a.get("page"),e=(a.get("sort_by"),a.get("prefix")),f=a.get("suffix"),g=c*(d-1)+1,h=a.get("row_numbers"),i=a.getPageData(),j=a.get("hidden_cols");return _.each(i,function(c){var d="";h&&(c=[g].concat(c),g++),_.each(c,function(b,c){var g=_.isObject(b)?b.formatted:b,i=c+(h?0:1);j&&-1!==j.indexOf(i)||(e[i]&&(g=e[i]+g),f[i]&&(g+=f[i]),d+=_.template(a.tpl.coll,{data:{html:g}}))}),b+=_.template(a.tpl.row,{className:"",colls:d})}),b},d.updatePager=function(){var a,b,c,d,e=this,f=e.tpl.pager,g=parseInt(e.get("page_size"),10),h=e.get("page_sizes"),i=!!h,j=e.get("nav_arrows"),k=e.get("goto"),l=e.get("show_pages"),m=parseInt(e.get("page"),10);return g?(b=e.countPages(),c=function(){var a,c=2,d=[m-c,m+c],e=f.dots,g=0,h=function(a){return _.template(f.pages,{page:a,current:m})};return d[1]>b&&(g=d[1]-b,d[1]=b,d[0]=d[0]-g),d[0]<1&&(g=0-d[0],d[0]=1,d[1]=d[1]+g+1),d[1]>b&&(d[1]=b),a=function(){for(var a="",b=d[0];b<=d[1];b++)a+=h(b);return a}(),2==d[0]&&(a=h(1)+a),3==d[0]&&(a=h(1)+h(2)+a),d[0]>3&&(a=h(1)+e+a),d[1]+2==b&&(a=a+h(b-1)+h(b)),d[1]+1==b&&(a+=h(b)),d[1]+3<=b&&(a=a+e+h(b)),a},d={top:f.wrap_top,arrows:j&&b>1?_.template(f.arrows,{prev_disabled:1==m?"table-pager-arrows-prev__disabled":"",next_disabled:m==b?"table-pager-arrows-next__disabled":""}):"",pages:l&&b>1?c():"","goto":k&&b>1?_.template(f.goto,{}):"",page_size:i?_.template(f.page_size,{sizes:h,current:g}):"",bottom:f.wrap_bottom},a=d.top+d.arrows+d.pages+d.page_size+d.goto+d.bottom,e.$pager.html(a),e):(e.$pager.empty(),e)},d.bindEvents=function(){var a=this,b={nav_arrows:function(){a.$pager.off("click",".table-pager-arrows-prev").on("click",".table-pager-arrows-prev",function(b){return b.preventDefault(),a.goto(a.get("page")-1),!1}),a.$pager.off("click",".table-pager-arrows-next").on("click",".table-pager-arrows-next",function(b){return b.preventDefault(),a.goto(a.get("page")+1),!1})},pagination:function(){a.$pager.off("click",".table-pager-pages-item").on("click",".table-pager-pages-item",function(b){return b.preventDefault(),$(b.target).hasClass("table-pager-pages-item__on")?!1:(a.goto($(this).data("goto")),!1)})},"goto":function(){a.$pager.off("keypress",".table-pager-goto").on("keypress",".table-pager-goto",function(b){return 13==b.keyCode?(b.preventDefault(),a.goto($(this).val()),!1):void 0})},page_sizes:function(){a.$pager.off("change",".table-pager-page_size").on("change",".table-pager-page_size",function(b){return b.preventDefault(),a.set({page_size:parseInt($(this).val(),10)}).goto(1),!1})},sorting:function(){a.$el.off("click",".table-sorting").on("click",".table-sorting",function(b){b.preventDefault();var c=$(this),d=a.get("sort_by"),e=c.data("sort_by"),f=c.data("sort_type"),g="asc"==f?"desc":"asc";return a.set({sort_by:e,sort_type:d==e?g:f}).goto(1),!1})},search:function(){var b=a.get("search_auto"),c=b?"input keypress":"keypress",d=a.get("search_sensitive");a.$search.off(c,".table-search-input").on(c,".table-search-input",function(c){var e=this.value;d||(e=e.toLowerCase()),!b&&13!=c.keyCode||a.search==e||(a.search=e,a.set({search_value:e}).goto(1))}),a.$search.off("blur",".table-search-input").on("blur",".table-search-input",function(){var b=this.value;d||(b=b.toLowerCase()),a.search!=b&&(a.search=b,a.set({search_value:b}).goto(1))})},hover_cols:function(){var b=a.$el.find("colgroup"),c=a.get("row_numbers"),d=a.get("hover_cols");a.$el.undelegate("td","mouseover mouseleave").delegate("td","mouseover mouseleave",function(a){var e=$(this),f=e.index();c&&0===f||!(_.isArray(d)&&-1!==d.indexOf(f+(c?0:1))||d===!0)||("mouseover"==a.type?b.eq(f).addClass("table-col-hover"):b.eq(f).removeClass("table-col-hover"))})}};return a.get("nav_arrows")&&b.nav_arrows(),a.get("show_pages")&&b.pagination(),a.get("goto")&&b.goto(),a.get("page_sizes")&&b.page_sizes(),a.get("sorting")&&b.sorting(),a.get("search")&&b.search(),a.get("hover_cols")&&b.hover_cols(),a},d.countPages=function(){var a=this,b=a.dataSize(),c=a.get("page_size"),d=b/c,e=Math.floor(d);return d=d>e?e+1:d,a.pages_count=d,d>0?d:1},d.dataSize=function(){var a=this,b=a.get("ajax")||{},c="function"==typeof b.url,d=_.size(a.get("filter")),e=0;return c?(a.getData(),e=a.ajax_data_size):e=a.search?_.size(a.search_data):d?_.size(a.filtered_data):_.size(a.get("data")),e>0?e:0},d.getAJAXData=function(){var a,b=this,c=(b.get("titles"),b.getSortKey()),d=b.getSortType(),e=b.get("page_size"),f=b.get("page"),g=(f-1)*e,h=b.get("ajax"),i="function"==typeof h.url,j=b.search,k=b.get("search_sensitive").toString(),l=g.toString()+e.toString()+c.toString()+d.toString()+j,m=_.cloneDeep(h);return g=0>g?0:g,i&&(m.url=h.url(g,e,c,d,j,k)),m.success=function(a){var c=h.prepare_data(a),d=i?h.full_size(a):c.length;b.set({data:c}),b.ajax_data_size=d,b.data=c,b.xhr_data[l]=c,b.xhr_data_size[l]=d,b.loading=!1,b.countPages(d),b.goto(f)},b.xhr_data[l]?(i&&(b.ajax_data_size=b.xhr_data_size[l]),i?b.xhr_data[l]:b.data):(b.xhr_key!==l&&(b.xhr.abort&&b.xhr.abort(),b.loading=!0,b.xhr=$.ajax(m),b.xhr_key=l),a)},d.getData=function(){var a,b,c,d=this,e=d.get("sort_by"),f=d.get("data"),g=d.get("sort_type"),h=d.get("ajax"),i=h&&"function"==typeof h.url;return!h||!i&&_.size(d.data)==d.ajax_data_size?(a=d.sortData(f,e,g),b=d.filterData(a),c=d.searchData(b,d.search)):c=d.getAJAXData(),c},d.searchData=function(a,b){a=a||this.getData(),b=b||this.search;var c,d=this,e=d.get("search_sensitive");return b?(c=_.filter(a,function(a){var c=0;return _.each(a,function(a){e?(_.isObject(a)&&_.contains(a.value,b)||_.contains(a,b))&&(c+=1):_.isObject(a)?-1!==a.value.toLowerCase().indexOf(b)&&(c+=1):-1!==a.indexOf(b)&&(c+=1)}),c>0}),d.search_data=c,c):a},d.filterData=function(a){var b=this,c=[],d=b.get("filter"),e=_.size(d),f=_.size(a);if(_.size(d)>0)for(var g=0;f>g;g++){var h=0;_.each(d,function(b,c){var d=a[g][c-1];_.isObject(d)?d.value==b&&(h+=1):d==b&&(h+=1)}),h==e&&c.push(a[g])}else c=a;return b.filtered_data=c,c},d.__prepareDataFormat=function(a,b){var c=(a||[]).length;if(b)for(var d in b)for(var e=parseInt(d,10)-1,f=0;c>f;f++){var g=a[f][e];_.isObject(g)||(a[f][e]={value:g,formatted:b[d](g)})}return a},d.getSortKey=function(){var a=this,b=a.get("titles"),c=parseInt(a.get("sort_by"),10);return c>0?b[c-1].key||c:c},d.getSortType=function(){return this.get("sort_type")},d.getSortDataType=function(){var a=this,b=a.get("titles"),c=b.length,d=a.get("sort_by");return(d>0&&c>=d?b[d-1].type:"").toLowerCase()},d.sortData=function(a,b,c){a=a||this.get("data"),b=b||this.get("sort_by"),c=c||this.getSortType();var d=this,e=d.get("titles"),f=e.length,g=d.getSortDataType(),h=d.get("ajax")||{},i=h&&"function"==typeof h.url,j=i?"":d.dataSize(),k=b.toString()+c.toString()+j.toString(),l=[];return d.data_cache_key==k?d.data:(b>0&&f>=b?"string"==g?l=a.sort(function(a,d){var e=a[b-1],f=d[b-1];return e=_.isObject(e)?e.value.toString():e.toString(),f=_.isObject(f)?f.value.toString():f.toString(),"asc"==c?e.localeCompare(f):"desc"==c?f.localeCompare(e):void 0}):"number"==g&&(l=a.sort(function(a,d){var e=a[b-1],f=d[b-1];return e=_.isObject(e)?e.value:e,f=_.isObject(f)?f.value:f,"asc"==c?e-f:"desc"==c?f-e:void 0})):l=a,d.data_cache_key!=k&&(d.data_cache_key=k,d.data=l),l)},d.getTotal=function(a){var b=this,c=0,d=b.get("titles"),e=d.length,f=(a>0&&e>=a?d[a-1].type:"").toLowerCase(),g=b.getData();return"number"==f&&_.each(g,function(b){c+=parseInt(b[a-1])}),c},d.getPageData=function(){var a,b,c=this,d=c.get("page_size"),e=c.get("page"),f=c.get("formatter"),g=c.get("ajax"),h=g&&"function"==typeof g.url;return a=g?4==c.xhr.readyState?c.getData():[]:c.getData(),b=h?a:function(a){var b;return b="number"==typeof d&&a?a.slice((e-1)*d,e*d):a}(a),b=c.__prepareDataFormat(b,f)},d.addRow=function(a){var b,c=this,d=c.get("ajax"),e=d&&"function"==typeof d.url;return e?(console.error("Error while adding row to ajax per page driven table"),c):(b=c.get("data"),b.push(a),c.goto(c.get("page")),c)},d.updateRow=function(a,b){var c,d,e=this,f=e.get("ajax"),g=f&&"function"==typeof f.url,h=_.size(b);if(g)return console.error("Error while deleting rows from ajax per page driven table"),e;c=e.get("data"),d=e.dataSize();for(var i=0;d>i;i++){var j=0;for(var k in b){var l=parseInt(k,10)-1,m=c[i][l];_.isObject(m)?m.value==b[k]&&(j+=1):m==b[k]&&(j+=1)}j===h&&_.each(a,function(a,b){c[i][b-1]=a})}return e.goto(e.get("page")),e},d.delRow=function(a){var b,c=this,d=c.get("ajax"),e=d&&"function"==typeof d.url,f=[],g=_.size(a);return e?(console.error("Error while deleting rows from ajax per page driven table"),c):(b=c.get("data"),_.each(b,function(b){var c=0;_.each(a,function(a,d){_.isObject(b[d-1])?b[d-1].value==a&&(c+=1):b[d-1]==a&&(c+=1)}),g>c&&f.push(b)}),b=f,c.set({data:f}),c.data=f,c.goto(c.get("page")),c)},d.goto=function(a){if(!a)return this;a=parseInt(a,10);var b=this,c=(b.get("page_size"),b.countPages()),d={},e="page";return d[e]=c>=a&&a>0?a:0>=a?1:c,b.set(d).renderTable(),b},a.tTable=c}(window,document);