tablesorter/addons/pager/jquery.tablesorter.pager.min.js
2014-10-26 19:11:12 -05:00

3 lines
16 KiB
JavaScript

/* tablesorter pager plugin updated 10/26/2014 (v2.18.0) */
;(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 w=this,u=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))},p=function(c,a){var b=c.config,e=b.$table.hasClass("hasFilters");e&&!a.ajaxUrl?h.isEmptyObject(b.cache)?a.filteredRows=a.totalRows=b.$tbodies.eq(0).children("tr").not(a.countChildRows?"":"."+b.cssChildRow).length:(a.filteredRows=0,h.each(b.cache[0].normalized,function(c,e){a.filteredRows+=a.regexRows.test(e[b.columns].$row[0].className)?0:1})):e||(a.filteredRows=a.totalRows)},t=function(c,a,b){if(a.initialized){var e, f,g,l=c.config;e=a.size||10;a.countChildRows&&f.push(l.cssChildRow);a.totalPages=Math.ceil(a.totalRows/e);l.totalRows=a.totalRows;p(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,a.startRow=f?1:0===a.filteredRows?0:a.size*a.page+1,a.page=f?0:a.page,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}),g.length&&(g["INPUT"===g[0].tagName?"val":"html"](e),a.$goto.length&&(f="",h.each(H(a),function(a,b){f+='<option value="'+b+'">'+b+"</option>"}),a.$goto.html(f).val(a.page+1)),g.find(".ts-startRow, .ts-page").unbind("change").bind("change",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])})));u(a);C(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}))}},H=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})},C=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>'))},x=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);C(c,a);h.data(c,"pagerLastSize",a.size)},v=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,s=f.widgetOptions&& f.widgetOptions.filter_filteredRow||"filtered",r=0;for(b=0;b<h;b++)g[b].className.match(s)||(r===d&&g[b].className.match(f.cssChildRow)?g[b].style.display="none":(g[b].style.display=r>=d&&r<m?"":"none",r+=g[b].className.match(f.cssChildRow+"|"+f.selectorRemove.replace(/^(\w+\.)/g,""))&&!a.countChildRows?0:1,r===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= ""}},D=function(c,a){a.size=parseInt(a.$size.val(),10)||a.size;h.data(c,"pagerLastSize",a.size);u(a);a.removeRows||(v(c,a),h(c).bind("sortEnd.pager filterEnd.pager",function(){v(c,a)}))},E=function(c,a,b,e,f){if("function"===typeof b.ajaxProcessing){var g,l,d,m,s,r,q=a.config,p=q.$table,n="";g=b.ajaxProcessing(c,a,e)||[0,[]];c=p.find("thead th").length;k.showError(a);if(f)q.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+"]"),q.$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,q.totalRows=q.filteredRows=b.filteredRows=b.totalRows,e=0===b.totalRows?[""]:g[e?0:1]||[],s=g[2]):(b.ajaxData=g,q.totalRows=b.totalRows=g.total, q.filteredRows=b.filteredRows="undefined"!==typeof g.filteredRows?g.filteredRows:g.total,s=g.headers,e=g.rows);r=e&&e.length;if(e instanceof jQuery)b.processAjaxOnInit&&(q.$tbodies.eq(0).children("tr").detach(),q.$tbodies.eq(0).append(e));else if(r){for(g=0;g<r;g++){n+="<tr>";for(f=0;f<e[g].length;f++)n+=/^\s*<td/.test(e[g][f])?h.trim(e[g][f]):"<td>"+e[g][f]+"</td>";n+="</tr>"}b.processAjaxOnInit&&q.$tbodies.eq(0).html(n)}b.processAjaxOnInit=!0;s&&s.length===c&&(m=(l=p.hasClass("hasStickyHeaders"))? q.widgetOptions.$sticky.children("thead:first").children("tr").children():"",d=p.find("tfoot tr:first").children(),q.$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(s[a]).append(c),l&&m.length&&(c=m.eq(a).find("."+k.css.icon).clone(!0),m.eq(a).find(".tablesorter-header-inner").html(s[a]).append(c))):(b.find(".tablesorter-header-inner").html(s[a]),l&&m.length&&m.eq(a).find(".tablesorter-header-inner").html(s[a])); d.eq(a).html(s[a])}))}q.showProcessing&&k.isProcessing(a);b.totalPages=Math.ceil(b.totalRows/(b.size||10));b.last.totalRows=b.totalRows;b.last.currentFilters=b.currentFilters;b.last.sortList=(q.sortList||[]).join(",");t(a,b,!0);p.trigger("updateCache",[function(){b.initialized&&setTimeout(function(){p.trigger("applyWidgets").trigger("pagerChange",b)},0)}])}b.initialized||(b.initialized=!0,h(a).trigger("applyWidgets").trigger("pagerInitialized",b))},J=function(c,a){var b=I(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){E(null,c,a,d,g);e.unbind("ajaxError.pager")}),f=++a.ajaxCounter,a.ajaxObject.url=b,a.ajaxObject.success=function(b,d,g){f<a.ajaxCounter||(E(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))},I=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},z=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,r=b.size;if(1>(a&&a.length||0))e.debug&&k.log("Pager: no rows for pager to render");else{b.page>=b.totalPages&&y(c,b);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<r&&f<a.length;)m&&/filtered/.test(a[f][0].className)||(g++,g>p&&l<=r&&(l++,e.append(a[f]))),f++;k.processTbody(c,e,!1)}else v(c,b);t(c,b,!0);c.isUpdating&&d.trigger("updateComplete",[c,!0])}},F=function(c,a){a.ajax?u(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(),z(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})},A=function(c){var a=c.config,b=a.pager;a.$table.trigger("updateCache",[function(){var e,f=[],g=c.config.cache[0].normalized;b.totalRows=g.length;for(e=0;e<b.totalRows;e++)f.push(g[e][a.columns].$row);a.rowsCopy=f;n(c,b,!0)}])},n=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 A(c);p(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;if(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?J(c,a):a.ajax||z(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]))}},B=function(c,a,b){b.size=a||b.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);n(c,b)},K=function(c,a){a.page=0;n(c,a)},y=function(c,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;n(c,a)},L=function(c,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);n(c,a)},M=function(c,a){a.page--;0>=a.page&& (a.page=0);n(c,a)},G=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||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));x(c,a);b&&(f.$table.trigger("updateRows"),B(c,a.size,a),D(c,a),f.debug&&k.log("pager enabled"))};w.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||10,e.totalPages=Math.ceil(e.totalRows/e.size),z(c,a,e),t(c,e,!1))};w.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,p=d.$container=h(d.container).addClass("tablesorter-pager").show();g.debug&&k.log("Pager initializing");d.oldAjaxSuccess=d.oldAjaxSuccess||d.ajaxObject.success;g.appender=w.appender;k.filter&&0<=h.inArray("filter",g.widgets)&&(d.currentFilters=g.$table.data("lastSearch")||k.filter.setDefaults(f,g,g.widgetOptions)||[],k.setFilters(f,d.currentFilters,!1));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)||10,h.data(f,"pagerLastSize",d.size));d.regexRows=new RegExp("("+(l.filter_filteredRow||"filtered")+"|"+g.selectorRemove.replace(/^(\w+\.)/g,"")+"|"+g.cssChildRow+")");m.unbind("filterStart filterEnd sortEnd disable enable destroy updateComplete pageSize pageSet ".split(" ").join(".pager ")).bind("filterStart.pager",function(a,b){d.currentFilters=b;!1!==d.pageReset&&(g.lastCombinedFilter||"")!==(b||[]).join("")&&(d.page=d.pageReset)}).bind("filterEnd.pager sortEnd.pager", function(){d.initialized&&(g.delayInit&&g.rowsCopy&&0===g.rowsCopy.length&&A(f),n(f,d,!1),g.$table.trigger("applyWidgets"),t(f,d,!1))}).bind("disable.pager",function(a){a.stopPropagation();F(f,d)}).bind("enable.pager",function(a){a.stopPropagation();G(f,d,!0)}).bind("destroy.pager",function(a){a.stopPropagation();F(f,d);d.$container.hide();f.config.appender=null;d.initialized=!1;delete f.config.rowsCopy;h(f).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.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&&A(b),d.page>=d.totalPages&&y(b,d),v(b,d),x(b,d),t(b,d,!0))}).bind("pageSize.pager",function(a,b){a.stopPropagation();B(f,parseInt(b,10)||10,d);v(f,d);t(f,d,!1);d.$size.length&&d.$size.val(d.size)}).bind("pageSet.pager", function(a,b){a.stopPropagation();d.page=(parseInt(b,10)||1)-1;d.$goto.length&&d.$goto.val(d.size);n(f,d,!0);t(f,d,!1)});b=[d.cssFirst,d.cssPrev,d.cssNext,d.cssLast];e=[K,M,L,y];p.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=p.find(d.cssGoto);d.$goto.length&&d.$goto.unbind("change").bind("change",function(){d.page=h(this).val()- 1;n(f,d,!0);t(f,d,!1)});d.$size=p.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)||(B(f,parseInt(h(this).val(),10),d),x(f,d));return!1}));d.initialized=!1;m.trigger("pagerBeforeInitialized",d);G(f,d,!1);"string"===typeof d.ajaxUrl?(d.ajax=!0,g.widgetOptions.filter_serversideFiltering=!0,g.serverSideSorting=!0,n(f,d)):(d.ajax=!1,h(this).trigger("appendCache", !0),D(f,d));d.ajax||(d.initialized=!0,t(f,d,!0),h(f).trigger("pagerInitialized",d))}})}}});k.showError=function(k,u){h(k).each(function(){var k=this.config,t=k.pager&&k.pager.cssErrorRow||k.widgetOptions.pager_css&&k.widgetOptions.pager_css.errorRow||"tablesorter-errorRow";k&&("undefined"===typeof u?k.$table.find("thead").find(k.selectorRemove).remove():(/tr\>/.test(u)?h(u):h('<tr><td colspan="'+k.columns+'">'+u+"</td></tr>")).click(function(){h(this).remove()}).appendTo(k.$table.find("thead:first")).addClass(t+ " "+k.selectorRemove.replace(/^(\w+\.)/g,"")).attr({role:"alert","aria-live":"assertive"}))})};h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);