tablesorter/addons/pager/jquery.tablesorter.pager.min.js
2014-04-10 10:29:04 -05:00

3 lines
13 KiB
JavaScript

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