tablesorter/addons/pager/jquery.tablesorter.pager.min.js
2014-03-18 18:13:27 -05:00

3 lines
13 KiB
JavaScript

/* tablesorter pager plugin updated 3/18/2014 (v2.15.11) */
;(function(h){var k=h.tablesorter;h.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,customAjaxUrl:function(b,a){return a},ajaxObject:{dataType:"json"},processAjaxOnInit:!0,ajaxProcessing:function(b){return[0,[],null]},output:"{startRow} to {endRow} of {totalRows} rows",updateArrows:!0,page:0,size:10,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 u=this,l=function(b,a){var d=b.cssDisabled,e=!!a,f=e||0===b.page,g=Math.min(b.totalPages,b.filteredPages),e=e||b.page===g-1||0===b.totalPages;b.updateArrows&&(b.$container.find(b.cssFirst+","+b.cssPrev)[f?"addClass":"removeClass"](d).attr("aria-disabled",f), b.$container.find(b.cssNext+","+b.cssLast)[e?"addClass":"removeClass"](d).attr("aria-disabled",e))},q=function(b,a,d){var e,f,g,c=b.config;e=c.$table.hasClass("hasFilters")&&!a.ajaxUrl;g=(c.widgetOptions&&c.widgetOptions.filter_filteredRow||"filtered")+","+c.selectorRemove+(a.countChildRows?"":",."+c.cssChildRow);f=a.size||10;a.totalPages=Math.ceil(a.totalRows/f);a.filteredRows=e?c.$tbodies.eq(0).children("tr").not("."+g).length:a.totalRows;a.filteredPages=e?Math.ceil(a.filteredRows/f)||1:a.totalPages; if(0<=Math.min(a.totalPages,a.filteredPages)&&(g=a.size*a.page>a.filteredRows,a.startRow=g?1:0===a.filteredRows?0:a.size*a.page+1,a.page=g?0:a.page,a.endRow=Math.min(a.filteredRows,a.totalRows,a.size*(a.page+1)),e=a.$container.find(a.cssPageDisplay),g=(a.ajaxData&&a.ajaxData.output?a.ajaxData.output||a.output:a.output).replace(/\{page([\-+]\d+)?\}/gi,function(c,b){return a.totalPages?a.page+(b?parseInt(b,10):1):0}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(c){c=c.replace(/[{}\s]/g,"");var b=c.split(":"), d=a.ajaxData,e=/(rows?|pages?)$/i.test(c)?0:"";return 1<b.length&&d&&d[b[0]]?d[b[0]][b[1]]:a[c]||(d?d[c]:e)||e}),e.length&&(e["INPUT"===e[0].tagName?"val":"html"](g),a.$goto.length))){g="";f=Math.min(a.totalPages,a.filteredPages);for(e=1;e<=f;e++)g+="<option>"+e+"</option>";a.$goto.html(g).val(a.page+1)}l(a);a.initialized&&!1!==d&&(c.$table.trigger("pagerComplete",a),a.savePages&&k.storage&&k.storage(b,a.storageKey,{page:a.page,size:a.size}))},s=function(b,a){var d,e=b.config,f=e.$tbodies.eq(0);a.fixedHeight&& (f.find("tr.pagerSavedHeightSpacer").remove(),d=h.data(b,"pagerSavedHeight"))&&(d-=f.height(),5<d&&h.data(b,"pagerLastSize")===a.size&&f.children("tr:visible").length<a.size&&f.append('<tr class="pagerSavedHeightSpacer '+e.selectorRemove.replace(/(tr)?\./g,"")+'" style="height:'+d+'px;"></tr>'))},z=function(b,a){var d=b.config.$tbodies.eq(0);d.find("tr.pagerSavedHeightSpacer").remove();h.data(b,"pagerSavedHeight",d.height());s(b,a);h.data(b,"pagerLastSize",a.size)},t=function(b,a){if(!a.ajaxUrl){var d, e=b.config,f=e.$tbodies.eq(0).children(),g=f.length,c=a.page*a.size,h=c+a.size,k=e.widgetOptions&&e.widgetOptions.filter_filteredRow||"filtered",p=0;for(d=0;d<g;d++)f[d].className.match(k)||(f[d].style.display=p>=c&&p<h?"":"none",p+=f[d].className.match(e.cssChildRow+"|"+e.selectorRemove.slice(1))&&!a.countChildRows?0:1)}},A=function(b,a){a.size=parseInt(a.$size.val(),10)||a.size;h.data(b,"pagerLastSize",a.size);l(a);a.removeRows||(t(b,a),h(b).bind("sortEnd.pager filterEnd.pager",function(){t(b,a)}))}, B=function(b,a,d,e,f){if("function"===typeof d.ajaxProcessing){var g,c,y,m,p,v,r=a.config,n=r.$table,l="";g=d.ajaxProcessing(b,a)||[0,[]];b=n.find("thead th").length;k.showError(a);if(f)r.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+"]"),r.$tbodies.eq(0).empty(),d.totalRows=0;else{h.isArray(g)?(e=isNaN(g[0])&&!isNaN(g[1]),f=g[e?1:0],d.totalRows=isNaN(f)?d.totalRows||0:f,e=0===d.totalRows?[""]:g[e?0:1]||[],p=g[2]):(d.ajaxData=g,d.totalRows=g.total,p=g.headers,e=g.rows);v=e.length;if(e instanceof jQuery)r.$tbodies.eq(0).empty().append(e);else if(v){for(g=0;g<v;g++){l+="<tr>";for(f=0;f<e[g].length;f++)l+=/^\s*<td/.test(e[g][f])?h.trim(e[g][f]):"<td>"+e[g][f]+"</td>";l+="</tr>"}d.processAjaxOnInit?r.$tbodies.eq(0).html(l): d.processAjaxOnInit=!0}p&&p.length===b&&(m=(c=n.hasClass("hasStickyHeaders"))?r.widgetOptions.$sticky.children("thead:first").children().children():"",y=n.find("tfoot tr:first").children(),r.$headers.filter("th").each(function(a){var b=h(this),d;b.find("."+k.css.icon).length?(d=b.find("."+k.css.icon).clone(!0),b.find(".tablesorter-header-inner").html(p[a]).append(d),c&&m.length&&(d=m.eq(a).find("."+k.css.icon).clone(!0),m.eq(a).find(".tablesorter-header-inner").html(p[a]).append(d))):(b.find(".tablesorter-header-inner").html(p[a]), c&&m.length&&m.eq(a).find(".tablesorter-header-inner").html(p[a]));y.eq(a).html(p[a])}))}r.showProcessing&&k.isProcessing(a);d.totalPages=Math.ceil(d.totalRows/(d.size||10));d.last.totalRows=d.totalRows;d.last.currentFilters=d.currentFilters;d.last.sortList=(r.sortList||[]).join(",");q(a,d);s(a,d);n.trigger("updateCache",[function(){d.initialized&&n.trigger("applyWidgets").trigger("pagerChange",d)}])}d.initialized||(d.initialized=!0,h(a).trigger("applyWidgets").trigger("pagerInitialized",d))},G=function(b, a){var d=F(b,a),e=h(document),f,g=b.config;""!==d&&(g.showProcessing&&k.isProcessing(b,!0),e.bind("ajaxError.pager",function(c,d,f,g){B(null,b,a,d,g);e.unbind("ajaxError.pager")}),f=++a.ajaxCounter,a.ajaxObject.url=d,a.ajaxObject.success=function(c){f<a.ajaxCounter||(B(c,b,a),e.unbind("ajaxError.pager"),"function"===typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(c))},g.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},F=function(b,a){var d=b.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=d.sortList,g=a.currentFilters||h(b).data("lastSearch")||[],c=e.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),q=e.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),m=[];c&&(c=c[1],h.each(f,function(a,b){m.push(c+"["+b[0]+"]="+b[1])}),e=e.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):c),m=[]);q&&(q=q[1],h.each(g,function(a,b){b&&m.push(q+"["+a+"]="+encodeURIComponent(b))}),e=e.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, m.length?m.join("&"):q),a.currentFilters=g);"function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(b,e));d.debug&&k.log("Pager ajax url: "+e);return e},w=function(b,a,d){var e,f,g=h(b);f=b.config;e=d.page*d.size;var c=e+d.size;if(!(1>(a&&a.length||0))){d.page>=d.totalPages&&C(b,d);d.isDisabled=!1;d.initialized&&g.trigger("pagerChange",d);if(d.removeRows){c>a.length&&(c=a.length);k.clearTableBody(b);for(f=k.processTbody(b,f.$tbodies.eq(0),!0);e<c;e++)f.append(a[e]);k.processTbody(b,f,!1)}else t(b, d);q(b,d);d.isDisabled||s(b,d);g.trigger("applyWidgets");b.isUpdating&&g.trigger("updateComplete",b)}},D=function(b,a){a.ajax?l(a,!0):(a.isDisabled=!0,h.data(b,"pagerLastPage",a.page),h.data(b,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,h(b).addClass("pagerDisabled").removeAttr("aria-describedby").find("tr.pagerSavedHeightSpacer").remove(),w(b,b.config.rowsCopy,a),b.config.debug&&k.log("pager disabled"));a.$size.add(a.$goto).each(function(){h(this).attr("aria-disabled","true").addClass(a.cssDisabled)[0].disabled= !0})},n=function(b,a,d){if(!a.isDisabled){var e=b.config,f=h(b),g=a.last,c=Math.min(a.totalPages,a.filteredPages);0>a.page&&(a.page=0);a.page>c-1&&0!==c&&(a.page=c-1);g.currentFilters=""===(g.currentFilters||[]).join("")?[]:g.currentFilters;a.currentFilters=""===(a.currentFilters||[]).join("")?[]:a.currentFilters;if(g.page!==a.page||g.size!==a.size||g.totalRows!==a.totalRows||(g.currentFilters||[]).join(",")!==(a.currentFilters||[]).join(",")||g.sortList!==(e.sortList||[]).join(","))e.debug&&k.log("Pager changing to page "+ a.page),a.last={page:a.page,size:a.size,sortList:(e.sortList||[]).join(","),totalRows:a.totalRows,currentFilters:a.currentFilters||[]},a.ajax?G(b,a):a.ajax||w(b,e.rowsCopy,a),h.data(b,"pagerLastPage",a.page),a.initialized&&!1!==d&&(f.trigger("pageMoved",a).trigger("applyWidgets"),b.isUpdating&&f.trigger("updateComplete"))}},x=function(b,a,d){d.size=a||d.size||10;d.$size.val(d.size);h.data(b,"pagerLastPage",d.page);h.data(b,"pagerLastSize",d.size);d.totalPages=Math.ceil(d.totalRows/d.size);d.filteredPages= Math.ceil(d.filteredRows/d.size);n(b,d)},H=function(b,a){a.page=0;n(b,a)},C=function(b,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;n(b,a)},I=function(b,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);n(b,a)},J=function(b,a){a.page--;0>=a.page&&(a.page=0);n(b,a)},E=function(b,a,d){var e,f=b.config;a.$size.add(a.$goto).removeClass(a.cssDisabled).removeAttr("disabled").attr("aria-disabled","false");a.isDisabled=!1;a.page=h.data(b, "pagerLastPage")||a.page||0;a.size=h.data(b,"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);b.id&&(e=b.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",e),f.$table.attr("aria-describedby",e));d&&(f.$table.trigger("updateRows"),x(b,a.size,a),A(b,a),s(b,a),f.debug&&k.log("pager enabled"))};u.appender=function(b,a){var d=b.config,e=d.pager;e.ajax||(d.rowsCopy=a,e.totalRows= e.countChildRows?d.$tbodies.eq(0).children().length:a.length,e.size=h.data(b,"pagerLastSize")||e.size||10,e.totalPages=Math.ceil(e.totalRows/e.size),w(b,a,e),q(b,e,!1))};u.construct=function(b){return this.each(function(){if(this.config&&this.hasInitialized){var a,d,e,f=this,g=f.config,c=g.pager=h.extend({},h.tablesorterPager.defaults,b),l=g.$table,m=c.$container=h(c.container).addClass("tablesorter-pager").show();g.debug&&k.log("Pager initializing");c.oldAjaxSuccess=c.oldAjaxSuccess||c.ajaxObject.success; g.appender=u.appender;k.filter&&0<=h.inArray("filter",g.widgets)&&(c.currentFilters=g.$table.data("lastSearch")||k.filter.setDefaults(f,g,g.widgetOptions)||[],k.setFilters(f,c.currentFilters,!1));c.savePages&&k.storage&&(a=k.storage(f,c.storageKey)||{},c.page=isNaN(a.page)?c.page:a.page,c.size=(isNaN(a.size)?c.size:a.size)||10,h.data(f,"pagerLastSize",c.size));l.unbind("filterStart filterEnd sortEnd disable enable destroy update updateRows updateAll addRows pageSize ".split(" ").join(".pager ")).bind("filterStart.pager", function(a,b){c.currentFilters=b;c.page=0}).bind("filterEnd.pager sortEnd.pager",function(){c.initialized&&(n(f,c,!1),q(f,c,!1),s(f,c))}).bind("disable.pager",function(a){a.stopPropagation();D(f,c)}).bind("enable.pager",function(a){a.stopPropagation();E(f,c,!0)}).bind("destroy.pager",function(a){a.stopPropagation();D(f,c);c.$container.hide();f.config.appender=null;c.initialized=!1;h(f).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager");k.storage&&k.storage(f,c.storageKey, "")}).bind("update.pager updateRows.pager updateAll.pager addRows.pager ",function(a){a.stopPropagation();t(f,c)}).bind("pageSize.pager",function(a,b){a.stopPropagation();x(f,parseInt(b,10)||10,c);t(f,c);q(f,c,!1);c.$size.length&&c.$size.val(c.size)}).bind("pageSet.pager",function(a,b){a.stopPropagation();c.page=(parseInt(b,10)||1)-1;c.$goto.length&&c.$goto.val(c.size);n(f,c);q(f,c,!1)});d=[c.cssFirst,c.cssPrev,c.cssNext,c.cssLast];e=[H,J,I,C];m.find(d.join(",")).attr("tabindex",0).unbind("click.pager").bind("click.pager", function(a){a.stopPropagation();var b=h(this),g=d.length;if(!b.hasClass(c.cssDisabled))for(a=0;a<g;a++)if(b.is(d[a])){e[a](f,c);break}});c.$goto=m.find(c.cssGoto);c.$goto.length&&c.$goto.unbind("change").bind("change",function(){c.page=h(this).val()-1;n(f,c);q(f,c,!1)});c.$size=m.find(c.cssPageSize);c.$size.length&&c.$size.unbind("change.pager").bind("change.pager",function(){c.$size.val(h(this).val());h(this).hasClass(c.cssDisabled)||(x(f,parseInt(h(this).val(),10),c),z(f,c));return!1});c.initialized= !1;l.trigger("pagerBeforeInitialized",c);E(f,c,!1);"string"===typeof c.ajaxUrl?(c.ajax=!0,g.widgetOptions.filter_serversideFiltering=!0,g.serverSideSorting=!0,n(f,c)):(c.ajax=!1,h(this).trigger("appendCache",!0),A(f,c));z(f,c);c.ajax||(c.initialized=!0,h(f).trigger("pagerInitialized",c))}})}}});k.showError=function(k,l){h(k).each(function(){var k=this.config,s=k.pager&&k.pager.cssErrorRow||k.widgetOptions.pager_css&&k.widgetOptions.pager_css.errorRow||"tablesorter-errorRow";k&&("undefined"===typeof l? k.$table.find("thead").find(k.selectorRemove).remove():(/tr\>/.test(l)?h(l):h('<tr><td colspan="'+k.columns+'">'+l+"</td></tr>")).click(function(){h(this).remove()}).appendTo(k.$table.find("thead:first")).addClass(s+" "+k.selectorRemove.replace(/^[.#]/,"")).attr({role:"alert","aria-live":"assertive"}))})};h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);