mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
!function(e){"use strict";var t=e.tablesorter;e.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,customAjaxUrl:function(e,t){return t},ajaxObject:{dataType:"json"},processAjaxOnInit:!0,ajaxProcessing:function(){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 a=this,s=function(e,t){var a="addClass",s="removeClass",i=e.cssDisabled,r=!!t,o=Math.min(e.totalPages,e.filteredPages);e.updateArrows&&(e.$container.find(e.cssFirst+","+e.cssPrev)[r||0===e.page?a:s](i),e.$container.find(e.cssNext+","+e.cssLast)[r||e.page===o-1||0===e.totalPages?a:s](i))},i=function(e,a,i){var r,o,n,g,l=e.config,d=l.$table.hasClass("hasFilters")&&!a.ajaxUrl,c=(l.widgetOptions&&l.widgetOptions.filter_filteredRow||"filtered")+","+l.selectorRemove+(a.countChildRows?"":",."+l.cssChildRow),p=a.size||10;if(a.totalPages=Math.ceil(a.totalRows/p),a.filteredRows=d?l.$tbodies.eq(0).children("tr").not("."+c).length:a.totalRows,a.filteredPages=d?Math.ceil(a.filteredRows/p)||1:a.totalPages,Math.min(a.totalPages,a.filteredPages)>=0&&(c=a.size*a.page>a.filteredRows,a.startRow=c?1:0===a.filteredRows?0:a.size*a.page+1,a.page=c?0:a.page,a.endRow=Math.min(a.filteredRows,a.totalRows,a.size*(a.page+1)),g=a.$container.find(a.cssPageDisplay),n=(a.ajaxData&&a.ajaxData.output?a.ajaxData.output||a.output:a.output).replace(/\{page([\-+]\d+)?\}/gi,function(e,t){return a.totalPages?a.page+(t?parseInt(t,10):1):0}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(e){var t=e.replace(/[{}\s]/g,""),s=t.split(":"),i=a.ajaxData,r=/(rows?|pages?)$/i.test(t)?0:"";return s.length>1&&i&&i[s[0]]?i[s[0]][s[1]]:a[t]||(i?i[t]:r)||r}),g.length&&(g["INPUT"===g[0].tagName?"val":"html"](n),a.$goto.length))){for(c="",o=Math.min(a.totalPages,a.filteredPages),r=1;o>=r;r++)c+="<option>"+r+"</option>";a.$goto.html(c).val(a.page+1)}s(a),a.initialized&&i!==!1&&(l.$table.trigger("pagerComplete",a),a.savePages&&t.storage&&t.storage(e,a.storageKey,{page:a.page,size:a.size}))},r=function(t,a){var s,i,r=t.config,o=r.$tbodies.eq(0);a.fixedHeight&&(o.find("tr.pagerSavedHeightSpacer").remove(),i=e.data(t,"pagerSavedHeight"),i&&(s=i-o.height(),s>5&&e.data(t,"pagerLastSize")===a.size&&o.children("tr:visible").length<a.size&&o.append('<tr class="pagerSavedHeightSpacer '+r.selectorRemove.replace(/(tr)?\./g,"")+'" style="height:'+s+'px;"></tr>')))},o=function(t,a){var s=t.config.$tbodies.eq(0);s.find("tr.pagerSavedHeightSpacer").remove(),e.data(t,"pagerSavedHeight",s.height()),r(t,a),e.data(t,"pagerLastSize",a.size)},n=function(e,t){if(!t.ajaxUrl){var a,s=e.config,i=s.$tbodies.eq(0).children(),r=i.length,o=t.page*t.size,n=o+t.size,g=s.widgetOptions&&s.widgetOptions.filter_filteredRow||"filtered",l=0;for(a=0;r>a;a++)i[a].className.match(g)||(i[a].style.display=l>=o&&n>l?"":"none",l+=i[a].className.match(s.cssChildRow+"|"+s.selectorRemove.slice(1))&&!t.countChildRows?0:1)}},g=function(t,a){a.size=parseInt(a.$size.val(),10)||a.size,e.data(t,"pagerLastSize",a.size),s(a),a.removeRows||(n(t,a),e(t).bind("sortEnd.pager filterEnd.pager",function(){n(t,a)}))},l=function(a,s,o,n,g){if("function"==typeof o.ajaxProcessing){var l,d,c,p,f,u,h,b,z,w,P,j=s.config,v=j.$table,x="",R=o.ajaxProcessing(a,s)||[0,[]],m=v.find("thead th").length;if(v.find("thead tr."+o.cssErrorRow).remove(),g)j.debug&&t.log("Ajax Error",n,g),w=e('<tr class="'+o.cssErrorRow+'"><td style="text-align:center;" colspan="'+m+'">'+(0===n.status?"Not connected, verify Network":404===n.status?"Requested page not found [404]":500===n.status?"Internal Server Error [500]":"parsererror"===g?"Requested JSON parse failed":"timeout"===g?"Time out error":"abort"===g?"Ajax Request aborted":"Uncaught error: "+n.statusText+" ["+n.status+"]")+"</td></tr>").click(function(){e(this).remove()}).appendTo(v.find("thead:first")),j.$tbodies.eq(0).empty();else{if(e.isArray(R)?(u=isNaN(R[0])&&!isNaN(R[1]),P=R[u?1:0],o.totalRows=isNaN(P)?o.totalRows||0:P,b=0===o.totalRows?[""]:R[u?0:1]||[],h=R[2]):(o.ajaxData=R,o.totalRows=R.total,h=R.headers,b=R.rows),z=b.length,b instanceof jQuery)j.$tbodies.eq(0).empty().append(b);else if(z){for(l=0;z>l;l++){for(x+="<tr>",d=0;d<b[l].length;d++)x+=/^\s*<td/.test(b[l][d])?e.trim(b[l][d]):"<td>"+b[l][d]+"</td>";x+="</tr>"}o.processAjaxOnInit?j.$tbodies.eq(0).html(x):o.processAjaxOnInit=!0}h&&h.length===m&&(c=v.hasClass("hasStickyHeaders"),f=c?j.widgetOptions.$sticky.children("thead:first").children().children():"",p=v.find("tfoot tr:first").children(),j.$headers.filter("th").each(function(a){var s,i=e(this);i.find("."+t.css.icon).length?(s=i.find("."+t.css.icon).clone(!0),i.find(".tablesorter-header-inner").html(h[a]).append(s),c&&f.length&&(s=f.eq(a).find("."+t.css.icon).clone(!0),f.eq(a).find(".tablesorter-header-inner").html(h[a]).append(s))):(i.find(".tablesorter-header-inner").html(h[a]),c&&f.length&&f.eq(a).find(".tablesorter-header-inner").html(h[a])),p.eq(a).html(h[a])}))}j.showProcessing&&t.isProcessing(s),o.last.totalPages=o.totalPages=Math.ceil(o.totalRows/(o.size||10)),o.last.currentFilters=o.currentFilters,o.last.sortList=(j.sortList||[]).join(","),i(s,o),r(s,o),v.trigger("applyWidgets"),v.trigger("updateRow",[!1,function(){o.initialized&&(v.trigger("updateComplete"),v.trigger("pagerChange",o))}])}o.initialized||(o.initialized=!0,e(s).trigger("pagerInitialized",o))},d=function(a,s){var i,r=c(a,s),o=e(document),n=a.config;""!==r&&(n.showProcessing&&t.isProcessing(a,!0),o.bind("ajaxError.pager",function(e,t,i,r){l(null,a,s,t,r),o.unbind("ajaxError.pager")}),i=++s.ajaxCounter,s.ajaxObject.url=r,s.ajaxObject.success=function(e){i<s.ajaxCounter||(l(e,a,s),o.unbind("ajaxError.pager"),"function"==typeof s.oldAjaxSuccess&&s.oldAjaxSuccess(e))},n.debug&&t.log("ajax initialized",s.ajaxObject),e.ajax(s.ajaxObject))},c=function(a,s){var i=a.config,r=s.ajaxUrl?s.ajaxUrl.replace(/\{page([\-+]\d+)?\}/,function(e,t){return s.page+(t?parseInt(t,10):0)}).replace(/\{size\}/g,s.size):"",o=i.sortList,n=s.currentFilters||e(a).data("lastSearch")||[],g=r.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),l=r.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),d=[];return g&&(g=g[1],e.each(o,function(e,t){d.push(g+"["+t[0]+"]="+t[1])}),r=r.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,d.length?d.join("&"):g),d=[]),l&&(l=l[1],e.each(n,function(e,t){t&&d.push(l+"["+e+"]="+encodeURIComponent(t))}),r=r.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,d.length?d.join("&"):l),s.currentFilters=n),"function"==typeof s.customAjaxUrl&&(r=s.customAjaxUrl(a,r)),i.debug&&t.log("Pager ajax url: "+r),r},p=function(a,s,o){var g,l,d=s&&s.length||0,c=o.page*o.size,p=c+o.size;if(!(1>d)){if(o.page>=o.totalPages&&z(a,o),o.isDisabled=!1,o.initialized&&e(a).trigger("pagerChange",o),o.removeRows){for(p>s.length&&(p=s.length),t.clearTableBody(a),l=t.processTbody(a,a.config.$tbodies.eq(0),!0),g=c;p>g;g++)l.append(s[g]);t.processTbody(a,l,!1)}else n(a,o);i(a,o),o.isDisabled||r(a,o),e(a).trigger("applyWidgets")}},f=function(a,i){i.ajax?s(i,!0):(i.isDisabled=!0,e.data(a,"pagerLastPage",i.page),e.data(a,"pagerLastSize",i.size),i.page=0,i.size=i.totalRows,i.totalPages=1,e(a).addClass("pagerDisabled").find("tr.pagerSavedHeightSpacer").remove(),p(a,a.config.rowsCopy,i),a.config.debug&&t.log("pager disabled")),i.$size.add(i.$goto).each(function(){e(this).addClass(i.cssDisabled)[0].disabled=!0})},u=function(a,s,i){if(!s.isDisabled){var r=a.config,o=s.last,n=Math.min(s.totalPages,s.filteredPages);s.page<0&&(s.page=0),s.page>n-1&&0!==n&&(s.page=n-1),(o.page!==s.page||o.size!==s.size||o.totalPages!==s.totalPages||(o.currentFilters||[]).join(",")!==(s.currentFilters||[]).join(",")||o.sortList!==(r.sortList||[]).join(","))&&(r.debug&&t.log("Pager changing to page "+s.page),s.last={page:s.page,size:s.size,sortList:(r.sortList||[]).join(","),totalPages:s.totalPages,currentFilters:s.currentFilters||[]},s.ajax?d(a,s):s.ajax||p(a,a.config.rowsCopy,s),e.data(a,"pagerLastPage",s.page),s.initialized&&i!==!1&&(r.$table.trigger("pageMoved",s),r.$table.trigger("applyWidgets")))}},h=function(t,a,s){s.size=a||s.size||10,s.$size.val(s.size),e.data(t,"pagerLastPage",s.page),e.data(t,"pagerLastSize",s.size),s.totalPages=Math.ceil(s.totalRows/s.size),u(t,s)},b=function(e,t){t.page=0,u(e,t)},z=function(e,t){t.page=Math.min(t.totalPages,t.filteredPages)-1,u(e,t)},w=function(e,t){t.page++,t.page>=Math.min(t.totalPages,t.filteredPages)-1&&(t.page=Math.min(t.totalPages,t.filteredPages)-1),u(e,t)},P=function(e,t){t.page--,t.page<=0&&(t.page=0),u(e,t)},j=function(a,s){f(a,s),s.$container.hide(),a.config.appender=null,s.initialized=!1,e(a).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager"),t.storage&&t.storage(a,s.storageKey,"")},v=function(a,s,i){var o=s.$size.removeClass(s.cssDisabled).removeAttr("disabled");s.$goto.removeClass(s.cssDisabled).removeAttr("disabled"),s.isDisabled=!1,s.page=e.data(a,"pagerLastPage")||s.page||0,s.size=e.data(a,"pagerLastSize")||parseInt(o.find("option[selected]").val(),10)||s.size||10,o.val(s.size),s.totalPages=Math.ceil(Math.min(s.totalRows,s.filteredRows)/s.size),i&&(e(a).trigger("updateRow"),h(a,s.size,s),g(a,s),r(a,s),a.config.debug&&t.log("pager enabled"))};a.appender=function(t,a){var s=t.config,i=s.pager;i.ajax||(s.rowsCopy=a,i.totalRows=i.countChildRows?s.$tbodies.eq(0).children().length:a.length,i.size=e.data(t,"pagerLastSize")||i.size||10,i.totalPages=Math.ceil(i.totalRows/i.size),p(t,a,i))},a.construct=function(s){return this.each(function(){if(this.config&&this.hasInitialized){var l,d,c,p=this,x=p.config,R=x.pager=e.extend({},e.tablesorterPager.defaults,s),m=x.$table,$=R.$container=e(R.container).addClass("tablesorter-pager").show();x.debug&&t.log("Pager initializing"),R.oldAjaxSuccess=R.oldAjaxSuccess||R.ajaxObject.success,x.appender=a.appender,t.filter&&e.inArray("filter",x.widgets)>=0&&(R.currentFilters=x.$table.data("lastSearch")||t.filter.setDefaults(p,x,x.widgetOptions)||[],t.setFilters(p,R.currentFilters,!1)),R.savePages&&t.storage&&(l=t.storage(p,R.storageKey)||{},R.page=isNaN(l.page)?R.page:l.page,R.size=(isNaN(l.size)?R.size:l.size)||10,e.data(p,"pagerLastSize",R.size)),m.unbind("filterStart filterEnd sortEnd disable enable destroy update updateRows updateAll addRows pageSize ".split(" ").join(".pager ")).bind("filterStart.pager",function(e,t){R.currentFilters=t,R.page=0}).bind("filterEnd.pager sortEnd.pager",function(){R.initialized&&(u(p,R,!1),i(p,R,!1),r(p,R))}).bind("disable.pager",function(e){e.stopPropagation(),f(p,R)}).bind("enable.pager",function(e){e.stopPropagation(),v(p,R,!0)}).bind("destroy.pager",function(e){e.stopPropagation(),j(p,R)}).bind("update updateRows updateAll addRows ".split(" ").join(".pager "),function(e){e.stopPropagation(),n(p,R)}).bind("pageSize.pager",function(e,t){e.stopPropagation(),h(p,parseInt(t,10)||10,R),n(p,R),i(p,R,!1),R.$size.length&&R.$size.val(R.size)}).bind("pageSet.pager",function(e,t){e.stopPropagation(),R.page=(parseInt(t,10)||1)-1,R.$goto.length&&R.$goto.val(R.size),u(p,R),i(p,R,!1)}),d=[R.cssFirst,R.cssPrev,R.cssNext,R.cssLast],c=[b,P,w,z],$.find(d.join(",")).unbind("click.pager").bind("click.pager",function(t){t.stopPropagation();var a,s=e(this),i=d.length;if(!s.hasClass(R.cssDisabled))for(a=0;i>a;a++)if(s.is(d[a])){c[a](p,R);break}}),R.$goto=$.find(R.cssGoto),R.$goto.length&&R.$goto.unbind("change").bind("change",function(){R.page=e(this).val()-1,u(p,R),i(p,R,!1)}),R.$size=$.find(R.cssPageSize),R.$size.length&&R.$size.unbind("change.pager").bind("change.pager",function(){return R.$size.val(e(this).val()),e(this).hasClass(R.cssDisabled)||(h(p,parseInt(e(this).val(),10),R),o(p,R)),!1}),R.initialized=!1,m.trigger("pagerBeforeInitialized",R),v(p,R,!1),"string"==typeof R.ajaxUrl?(R.ajax=!0,x.widgetOptions.filter_serversideFiltering=!0,x.serverSideSorting=!0,u(p,R)):(R.ajax=!1,e(this).trigger("appendCache",!0),g(p,R)),o(p,R),R.ajax||(R.initialized=!0,e(p).trigger("pagerInitialized",R))}})}}}),e.fn.extend({tablesorterPager:e.tablesorterPager.construct})}(jQuery); |