tablesorter/addons/pager/jquery.tablesorter.pager.min.js
2015-02-09 18:44:47 -06:00

3 lines
17 KiB
JavaScript

/* tablesorter (FORK) pager plugin updated 2/9/2015 (v2.19.1) */
;(function(h){var k=h.tablesorter;h.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,customAjaxUrl:function(c,a){return a},ajaxObject:{dataType:"json"},processAjaxOnInit:!0,ajaxProcessing:function(c){return[0,[],null]},output:"{startRow} to {endRow} of {totalRows} rows",updateArrows:!0,page:0,pageReset:0,size:10,maxOptionSize:20,savePages:!0,storageKey:"tablesorter-pager",fixedHeight:!1,countChildRows:!1,removeRows:!1,cssFirst:".first",cssPrev:".prev",cssNext:".next", cssLast:".last",cssGoto:".gotoPage",cssPageDisplay:".pagedisplay",cssPageSize:".pagesize",cssErrorRow:"tablesorter-errorRow",cssDisabled:"disabled",totalRows:0,totalPages:0,filteredRows:0,filteredPages:0,ajaxCounter:0,currentFilters:[],startRow:0,endRow:0,$size:null,last:{}};var y=this,v=function(c,a){var b=c.cssDisabled,e=!!a,f=e||0===c.page,g=Math.min(c.totalPages,c.filteredPages),e=e||c.page===g-1||0===g;c.updateArrows&&(c.$container.find(c.cssFirst+","+c.cssPrev)[f?"addClass":"removeClass"](b).attr("aria-disabled", f),c.$container.find(c.cssNext+","+c.cssLast)[e?"addClass":"removeClass"](b).attr("aria-disabled",e))},r=function(c,a){var b,e=c.config;(b=e.$table.hasClass("hasFilters"))&&!a.ajaxUrl?h.isEmptyObject(e.cache)?a.filteredRows=a.totalRows=e.$tbodies.eq(0).children("tr").not(a.countChildRows?"":"."+e.cssChildRow).length:(b=e.$table.children("tbody").index(e.$tbodies.eq(0)),a.filteredRows=0,h.each(e.cache[b].normalized,function(b,c){a.filteredRows+=a.regexRows.test(c[e.columns].$row[0].className)?0:1})): b||(a.filteredRows=a.totalRows)},q=function(c,a,b){if(!a.initializing){var e,f,g,l=c.config;e=a.size||a.settings.size||10;a.countChildRows&&f.push(l.cssChildRow);a.totalPages=Math.ceil(a.totalRows/e);l.totalRows=a.totalRows;r(c,a);l.filteredRows=a.filteredRows;a.filteredPages=Math.ceil(a.filteredRows/e)||0;0<=Math.min(a.totalPages,a.filteredPages)&&(f=a.size*a.page>a.filteredRows&&b,a.page=f?a.pageReset||0:a.page,a.startRow=f?a.size*a.page+1:0===a.filteredRows?0:a.size*a.page+1,a.endRow=Math.min(a.filteredRows, a.totalRows,a.size*(a.page+1)),g=a.$container.find(a.cssPageDisplay),e=(a.ajaxData&&a.ajaxData.output?a.ajaxData.output||a.output:a.output).replace(/\{page([\-+]\d+)?\}/gi,function(d,b){return a.totalPages?a.page+(b?parseInt(b,10):1):0}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(d){var b,c;b=d.replace(/[{}\s]/g,"");d=b.split(":");c=a.ajaxData;var e=/(rows?|pages?)$/i.test(b)?0:"";return/(startRow|page)/.test(d[0])&&"input"===d[1]?(b=(""+("page"===d[0]?a.totalPages:a.totalRows)).length,c="page"=== d[0]?a.page+1:a.startRow,'<input type="text" class="ts-'+d[0]+'" style="max-width:'+b+'em" value="'+c+'"/>'):1<d.length&&c&&c[d[0]]?c[d[0]][d[1]]:a[b]||(c?c[b]:e)||e}),a.$goto.length&&(f="",h.each(I(a),function(a,b){f+='<option value="'+b+'">'+b+"</option>"}),a.$goto.html(f).val(a.page+1)),g.length&&(g["INPUT"===g[0].tagName?"val":"html"](e),g.find(".ts-startRow, .ts-page").unbind("change.pager").bind("change.pager",function(){var b=h(this).val(),b=h(this).hasClass("ts-startRow")?Math.floor(b/a.size)+ 1:b;l.$table.trigger("pageSet.pager",[b])})));v(a);D(c,a);a.initialized&&!1!==b&&(l.$table.trigger("pagerComplete",a),a.savePages&&k.storage&&k.storage(c,a.storageKey,{page:a.page,size:a.size}))}},I=function(c){var a,b,e,f,g;g=Math.min(c.totalPages,c.filteredPages)||1;var l=5*Math.ceil(g/c.maxOptionSize/5);e=g>c.maxOptionSize;f=c.page+1;a=l;b=g-l;var d=[1];for(a=e?l:1;a<=g;)d.push(a),a+=e?l:1;d.push(g);if(e){e=[];b=Math.max(Math.floor(c.maxOptionSize/l)-1,5);a=f-b;1>a&&(a=1);b=f+b;for(b>g&&(b=g);a<= b;a++)e.push(a);d=h.grep(d,function(a,b){return h.inArray(a,d)===b});f=d.length;g=e.length;f-g>l/2&&f+g>c.maxOptionSize&&(c=Math.floor(f/2)-Math.floor(g/2),Array.prototype.splice.apply(d,[c,g]));d=d.concat(e)}return d=h.grep(d,function(a,b){return h.inArray(a,d)===b}).sort(function(a,b){return a-b})},D=function(c,a){var b,e=c.config,f=e.$tbodies.eq(0);f.find("tr.pagerSavedHeightSpacer").remove();a.fixedHeight&&!a.isDisabled&&(b=h.data(c,"pagerSavedHeight"))&&(b-=f.height(),5<b&&h.data(c,"pagerLastSize")=== a.size&&f.children("tr:visible").length<a.size&&f.append('<tr class="pagerSavedHeightSpacer '+e.selectorRemove.slice(1)+'" style="height:'+b+'px;"></tr>'))},z=function(c,a){var b;b=c.config;var e=b.$tbodies.eq(0);e.find("tr.pagerSavedHeightSpacer").remove();e.children("tr:visible").length||e.append('<tr class="pagerSavedHeightSpacer '+b.selectorRemove.slice(1)+'"><td>&nbsp</td></tr>');b=e.children("tr").eq(0).height()*a.size;h.data(c,"pagerSavedHeight",b);D(c,a);h.data(c,"pagerLastSize",a.size)}, w=function(c,a){if(!a.ajaxUrl){var b,e=0,f=c.config,g=f.$tbodies.eq(0).children("tr"),h=g.length,d=a.page*a.size,m=d+a.size,t=f.widgetOptions&&f.widgetOptions.filter_filteredRow||"filtered",u=0,n=0;a.cacheIndex=[];for(b=0;b<h;b++)g[b].className.match(t)||(n===d&&g[b].className.match(f.cssChildRow)?g[b].style.display="none":(g[b].style.display=n>=d&&n<m?"":"none",u!==n&&n>=d&&n<m&&(a.cacheIndex.push(b),u=n),n+=g[b].className.match(f.cssChildRow+"|"+f.selectorRemove.slice(1))&&!a.countChildRows?0:1, n===m&&"none"!==g[b].style.display&&g[b].className.match(k.css.cssHasChild)&&(e=b)));if(0<e&&g[e].className.match(k.css.cssHasChild))for(;++e<h&&g[e].className.match(f.cssChildRow);)g[e].style.display=""}},E=function(c,a){a.size=parseInt(a.$size.val(),10)||a.size||a.settings.size||10;h.data(c,"pagerLastSize",a.size);v(a);a.removeRows||(w(c,a),h(c).bind("sortEnd.pager filterEnd.pager",function(){w(c,a)}))},F=function(c,a,b,e,f){if("function"===typeof b.ajaxProcessing){var g,l,d,m,t,u,n=a.config,r= n.$table,p="";g=b.ajaxProcessing(c,a,e)||[0,[]];c=r.find("thead th").length;k.showError(a);if(f)n.debug&&k.log("Ajax Error",e,f),k.showError(a,0===e.status?"Not connected, verify Network":404===e.status?"Requested page not found [404]":500===e.status?"Internal Server Error [500]":"parsererror"===f?"Requested JSON parse failed":"timeout"===f?"Time out error":"abort"===f?"Ajax Request aborted":"Uncaught error: "+e.statusText+" ["+e.status+"]"),n.$tbodies.eq(0).children("tr").detach(),b.totalRows=0; else{h.isArray(g)?(e=isNaN(g[0])&&!isNaN(g[1]),f=g[e?1:0],b.totalRows=isNaN(f)?b.totalRows||0:f,n.totalRows=n.filteredRows=b.filteredRows=b.totalRows,e=0===b.totalRows?[""]:g[e?0:1]||[],t=g[2]):(b.ajaxData=g,n.totalRows=b.totalRows=g.total,n.filteredRows=b.filteredRows="undefined"!==typeof g.filteredRows?g.filteredRows:g.total,t=g.headers,e=g.rows);u=e&&e.length;if(e instanceof jQuery)b.processAjaxOnInit&&(n.$tbodies.eq(0).children("tr").detach(),n.$tbodies.eq(0).append(e));else if(u){for(g=0;g<u;g++){p+= "<tr>";for(f=0;f<e[g].length;f++)p+=/^\s*<td/.test(e[g][f])?h.trim(e[g][f]):"<td>"+e[g][f]+"</td>";p+="</tr>"}b.processAjaxOnInit&&n.$tbodies.eq(0).html(p)}b.processAjaxOnInit=!0;t&&t.length===c&&(m=(l=r.hasClass("hasStickyHeaders"))?n.widgetOptions.$sticky.children("thead:first").children("tr").children():"",d=r.find("tfoot tr:first").children(),n.$headers.filter("th").each(function(a){var b=h(this),c;b.find("."+k.css.icon).length?(c=b.find("."+k.css.icon).clone(!0),b.find(".tablesorter-header-inner").html(t[a]).append(c), l&&m.length&&(c=m.eq(a).find("."+k.css.icon).clone(!0),m.eq(a).find(".tablesorter-header-inner").html(t[a]).append(c))):(b.find(".tablesorter-header-inner").html(t[a]),l&&m.length&&m.eq(a).find(".tablesorter-header-inner").html(t[a]));d.eq(a).html(t[a])}))}n.showProcessing&&k.isProcessing(a);b.totalPages=Math.ceil(b.totalRows/(b.size||b.settings.size||10));b.last.totalRows=b.totalRows;b.last.currentFilters=b.currentFilters;b.last.sortList=(n.sortList||[]).join(",");q(a,b,!1);r.trigger("updateCache", [function(){b.initialized&&setTimeout(function(){r.trigger("applyWidgets").trigger("pagerChange",b);q(a,b,!0)},0)}])}b.initialized||(b.initialized=!0,b.initializing=!1,h(a).trigger("applyWidgets").trigger("pagerInitialized",b),q(a,b))},K=function(c,a){var b=J(c,a),e=h(document),f,g=c.config;""!==b&&(g.showProcessing&&k.isProcessing(c,!0),e.bind("ajaxError.pager",function(b,d,f,g){F(null,c,a,d,g);e.unbind("ajaxError.pager")}),f=++a.ajaxCounter,a.last.ajaxUrl=b,a.ajaxObject.url=b,a.ajaxObject.success= function(b,d,g){f<a.ajaxCounter||(F(b,c,a,g),e.unbind("ajaxError.pager"),"function"===typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(b))},g.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},J=function(c,a){var b=c.config,e=a.ajaxUrl?a.ajaxUrl.replace(/\{page([\-+]\d+)?\}/,function(b,c){return a.page+(c?parseInt(c,10):0)}).replace(/\{size\}/g,a.size):"",f=b.sortList,g=a.currentFilters||h(c).data("lastSearch")||[],l=e.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),d=e.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/), m=[];l&&(l=l[1],h.each(f,function(a,b){m.push(l+"["+b[0]+"]="+b[1])}),e=e.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):l),m=[]);d&&(d=d[1],h.each(g,function(a,b){b&&m.push(d+"["+a+"]="+encodeURIComponent(b))}),e=e.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):d),a.currentFilters=g);"function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(c,e));b.debug&&k.log("Pager ajax url: "+e);return e},B=function(c,a,b){var e,f,g,l,d=h(c);e=c.config;var m=e.$table.hasClass("hasFilters"), p=b.page*b.size,u=b.size;if(1>(a&&a.length||0))e.debug&&k.log("Pager: no rows for pager to render");else{b.page>=b.totalPages&&A(c,b);b.cacheIndex=[];b.isDisabled=!1;b.initialized&&d.trigger("pagerChange",b);if(b.removeRows){k.clearTableBody(c);e=k.processTbody(c,e.$tbodies.eq(0),!0);f=m?0:p;g=m?0:p;for(l=0;l<u&&f<a.length;)m&&/filtered/.test(a[f][0].className)||(g++,g>p&&l<=u&&(l++,b.cacheIndex.push(f),e.append(a[f]))),f++;k.processTbody(c,e,!1)}else w(c,b);q(c,b);c.isUpdating&&d.trigger("updateComplete", [c,!0])}},G=function(c,a){a.ajax?v(a,!0):(a.isDisabled=!0,h.data(c,"pagerLastPage",a.page),h.data(c,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,h(c).addClass("pagerDisabled").removeAttr("aria-describedby").find("tr.pagerSavedHeightSpacer").remove(),B(c,c.config.rowsCopy,a),h(c).trigger("applyWidgets"),c.config.debug&&k.log("pager disabled"));a.$size.add(a.$goto).add(a.$container.find(".ts-startRow, .ts-page")).each(function(){h(this).attr("aria-disabled","true").addClass(a.cssDisabled)[0].disabled= !0})},C=function(c){var a=c.config,b=a.pager;a.$table.trigger("updateCache",[function(){var e,f=[];e=a.$table.children("tbody").index(a.$tbodies.eq(0));var g=c.config.cache[e].normalized;b.totalRows=g.length;for(e=0;e<b.totalRows;e++)f.push(g[e][a.columns].$row);a.rowsCopy=f;p(c,b,!0)}])},p=function(c,a,b){if(!a.isDisabled){var e,f=c.config,g=h(c),l=a.last;if(!1!==b&&a.initialized&&h.isEmptyObject(f.cache))return C(c);if(!a.ajax||!k.hasWidget(c,"filter")||f.widgetOptions.filter_initialized)if(r(c, a),e=Math.min(a.totalPages,a.filteredPages),0>a.page&&(a.page=0),a.page>e-1&&0!==e&&(a.page=e-1),l.currentFilters=""===(l.currentFilters||[]).join("")?[]:l.currentFilters,a.currentFilters=""===(a.currentFilters||[]).join("")?[]:a.currentFilters,l.page!==a.page||l.size!==a.size||l.totalRows!==a.totalRows||(l.currentFilters||[]).join(",")!==(a.currentFilters||[]).join(",")||(l.ajaxUrl||"")!==(a.ajaxObject.url||"")||(l.optAjaxUrl||"")!==(a.ajaxUrl||"")||l.sortList!==(f.sortList||[]).join(","))f.debug&& k.log("Pager changing to page "+a.page),a.last={page:a.page,size:a.size,sortList:(f.sortList||[]).join(","),totalRows:a.totalRows,currentFilters:a.currentFilters||[],ajaxUrl:a.ajaxObject.url||"",optAjaxUrl:a.ajaxUrl||""},a.ajax?K(c,a):a.ajax||B(c,f.rowsCopy,a),h.data(c,"pagerLastPage",a.page),a.initialized&&!1!==b&&(g.trigger("pageMoved",a).trigger("applyWidgets"),c.isUpdating&&g.trigger("updateComplete",[c,!0]))}},x=function(c,a,b){b.size=a||b.size||b.settings.size||10;b.$size.val(b.size);h.data(c, "pagerLastPage",b.page);h.data(c,"pagerLastSize",b.size);b.totalPages=Math.ceil(b.totalRows/b.size);b.filteredPages=Math.ceil(b.filteredRows/b.size);p(c,b)},L=function(c,a){a.page=0;p(c,a)},A=function(c,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;p(c,a)},M=function(c,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);p(c,a)},N=function(c,a){a.page--;0>=a.page&&(a.page=0);p(c,a)},H=function(c,a,b){var e,f=c.config;a.$size.add(a.$goto).add(a.$container.find(".ts-startRow, .ts-page")).removeClass(a.cssDisabled).removeAttr("disabled").attr("aria-disabled", "false");a.isDisabled=!1;a.page=h.data(c,"pagerLastPage")||a.page||0;a.size=h.data(c,"pagerLastSize")||parseInt(a.$size.find("option[selected]").val(),10)||a.size||a.settings.size||10;a.$size.val(a.size);a.totalPages=Math.ceil(Math.min(a.totalRows,a.filteredRows)/a.size);c.id&&(e=c.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",e),f.$table.attr("aria-describedby",e));z(c,a);b&&(f.$table.trigger("updateRows"),x(c,a.size,a),E(c,a),f.debug&&k.log("pager enabled"))};y.appender=function(c, a){var b=c.config,e=b.pager;e.ajax||(b.rowsCopy=a,e.totalRows=e.countChildRows?b.$tbodies.eq(0).children("tr").length:a.length,e.size=h.data(c,"pagerLastSize")||e.size||e.settings.size||10,e.totalPages=Math.ceil(e.totalRows/e.size),B(c,a,e),q(c,e,!1))};y.construct=function(c){return this.each(function(){if(this.config&&this.hasInitialized){var a,b,e,f=this,g=f.config,l=g.widgetOptions,d=g.pager=h.extend(!0,{},h.tablesorterPager.defaults,c),m=g.$table,r=d.$container=h(d.container).addClass("tablesorter-pager").show(); d.settings=h.extend(!0,{},h.tablesorterPager.defaults,c);g.debug&&k.log("Pager initializing");d.oldAjaxSuccess=d.oldAjaxSuccess||d.ajaxObject.success;g.appender=y.appender;d.initializing=!0;d.savePages&&k.storage&&(a=k.storage(f,d.storageKey)||{},d.page=isNaN(a.page)?d.page:a.page,d.size=(isNaN(a.size)?d.size:a.size)||d.settings.size||10,h.data(f,"pagerLastSize",d.size));d.regexRows=new RegExp("("+(l.filter_filteredRow||"filtered")+"|"+g.selectorRemove.slice(1)+"|"+g.cssChildRow+")");m.unbind(h.trim("filterInit filterStart filterEnd sortEnd disable enable destroy updateComplete pageSize pageSet pageAndSize pagerUpdate ".split(" ").join(".pager "))).bind("filterInit.pager filterStart.pager", function(a,b){d.currentFilters=h.isArray(b)?b:g.$table.data("lastSearch");"filterStart"===a.type&&!1!==d.pageReset&&(g.lastCombinedFilter||"")!==(d.currentFilters||[]).join("")&&(d.page=d.pageReset)}).bind("filterEnd.pager sortEnd.pager",function(){d.currentFilters=g.$table.data("lastSearch");if(d.initialized||d.initializing)g.delayInit&&g.rowsCopy&&0===g.rowsCopy.length&&C(f),q(f,d,!1),p(f,d,!1),g.$table.trigger("applyWidgets")}).bind("disable.pager",function(a){a.stopPropagation();G(f,d)}).bind("enable.pager", function(a){a.stopPropagation();H(f,d,!0)}).bind("destroy.pager",function(a){a.stopPropagation();G(f,d);d.$container.hide();f.config.appender=null;d.initialized=!1;delete f.config.rowsCopy;h(f).unbind(h.trim("filterInit filterStart filterEnd sortEnd disable enable destroy updateComplete pageSize pageSet pageAndSize pagerUpdate ".split(" ").join(".pager ")));k.storage&&k.storage(f,d.storageKey,"")}).bind("updateComplete.pager",function(a,b,c){a.stopPropagation();b&&!c&&(a=g.$tbodies.eq(0).children("tr").not(g.selectorRemove), d.totalRows=a.length-(d.countChildRows?0:a.filter("."+g.cssChildRow).length),d.totalPages=Math.ceil(d.totalRows/d.size),a.length&&g.rowsCopy&&0===g.rowsCopy.length&&C(b),d.page>=d.totalPages&&A(b,d),w(b,d),z(b,d),q(b,d,!0))}).bind("pageSize.pager refreshComplete.pager",function(a,b){a.stopPropagation();x(f,parseInt(b,10)||d.settings.size||10,d);w(f,d);q(f,d,!1)}).bind("pageSet.pager pagerUpdate.pager",function(a,b){a.stopPropagation();d.page=(parseInt(b,10)||1)-1;"pagerUpdate"===a.type&&(d.last.page= !0);p(f,d,!0);q(f,d,!1)}).bind("pageAndSize.pager",function(a,b,c){a.stopPropagation();d.page=(parseInt(b,10)||1)-1;x(f,parseInt(c,10)||d.settings.size||10,d);p(f,d,!0);w(f,d);q(f,d,!1)});b=[d.cssFirst,d.cssPrev,d.cssNext,d.cssLast];e=[L,N,M,A];r.find(b.join(",")).attr("tabindex",0).unbind("click.pager").bind("click.pager",function(a){a.stopPropagation();var c=h(this),g=b.length;if(!c.hasClass(d.cssDisabled))for(a=0;a<g;a++)if(c.is(b[a])){e[a](f,d);break}});d.$goto=r.find(d.cssGoto);d.$goto.length&& d.$goto.unbind("change.pager").bind("change.pager",function(){d.page=h(this).val()-1;p(f,d,!0);q(f,d,!1)});d.$size=r.find(d.cssPageSize);d.$size.length&&(d.$size.find("option").removeAttr("selected"),d.$size.unbind("change.pager").bind("change.pager",function(){d.$size.val(h(this).val());h(this).hasClass(d.cssDisabled)||(x(f,parseInt(h(this).val(),10),d),z(f,d));return!1}));d.initialized=!1;m.trigger("pagerBeforeInitialized",d);H(f,d,!1);"string"===typeof d.ajaxUrl?(d.ajax=!0,g.widgetOptions.filter_serversideFiltering= !0,g.serverSideSorting=!0,p(f,d)):(d.ajax=!1,h(this).trigger("appendCache",!0),E(f,d));d.ajax||d.initialized||(d.initializing=!1,d.initialized=!0,p(f,d),h(f).trigger("pagerInitialized",d),g.widgetOptions.filter_initialized&&k.hasWidget(f,"filter")||q(f,d,!1))}})}}});k.showError=function(k,v){h(k).each(function(){var k=this.config,q=k.pager&&k.pager.cssErrorRow||k.widgetOptions.pager_css&&k.widgetOptions.pager_css.errorRow||"tablesorter-errorRow";k&&("undefined"===typeof v?k.$table.find("thead").find(k.selectorRemove).remove(): (/tr\>/.test(v)?h(v):h('<tr><td colspan="'+k.columns+'">'+v+"</td></tr>")).click(function(){h(this).remove()}).appendTo(k.$table.find("thead:first")).addClass(q+" "+k.selectorRemove.slice(1)).attr({role:"alert","aria-live":"assertive"}))})};h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);