tablesorter/dist/js/widgets/widget-sort2Hash.min.js
Rob Garrison 8b1ba542db 2.30.0
2018-03-18 09:25:52 -05:00

2 lines
4.3 KiB
JavaScript

/*! Widget: sort2Hash (BETA) - updated 9/27/2017 (v2.29.0) */
!function(e){"use strict";var t=e.tablesorter||{},a=t.sort2Hash={init:function(r,o){var s,n,i,h,l=r.table,d=r.pager,c=t.hasWidget(l,"saveSort"),H=a.decodeHash(r,o,"sort");(H&&!c||H&&c&&o.sort2Hash_overrideSaveSort)&&a.convertString2Sort(r,o,H),t.hasWidget(r.table,"pager")&&(n=parseInt(a.decodeHash(r,o,"page"),10),i=d.page=n<0?0:n>d.totalPages?d.totalPages-1:n,h=d.size=parseInt(a.decodeHash(r,o,"size"),10)),t.hasWidget(l,"filter")&&(s=a.decodeHash(r,o,"filter"))&&(s=s.split(o.sort2Hash_separator),r.$table.one("tablesorter-ready",function(){setTimeout(function(){r.$table.one("filterEnd",function(){e(this).triggerHandler("pageAndSize",[i,h])}),(n=t.filter.equalFilters?t.filter.equalFilters(r,r.lastSearch,s):(r.lastSearch||[]).join("")!==(s||[]).join(""))||e.tablesorter.setFilters(l,s,!0)},100)})),s||r.$table.one("tablesorter-ready",function(){r.$table.triggerHandler("pageAndSize",[i,h])}),r.$table.on("sortEnd.sort2hash filterEnd.sort2hash pagerComplete.sort2Hash",function(){this.hasInitialized&&a.setHash(this.config,this.config.widgetOptions)})},getTableId:function(t,a){return a.sort2Hash_tableId||t.table.id||"table"+e("table").index(t.$table)},regexEscape:function(e){return e.replace(/([\.\^\$\*\+\-\?\(\)\[\]\{\}\\\|])/g,"\\$1")},convertString2Sort:function(e,t,r){for(var o,s,n,i,h,l,d=r.split(t.sort2Hash_separator),c=0,H=d.length,g=[];c<H;){if(s=d[c++],i=parseInt(s,10),isNaN(i)||i>e.columns)for(o=new RegExp("("+a.regexEscape(s)+")","i"),h=0;h<e.columns;h++)l=e.$headerIndexed[h],o.test(l.attr(t.sort2Hash_headerTextAttr))&&(s=h,h=e.columns);n=d[c++],void 0!==s&&void 0!==n&&(isNaN(n)&&(n=n.indexOf(t.sort2Hash_directionText[1])>-1?1:0),g.push([s,n]))}g.length&&(e.sortList=g)},convertSort2String:function(t,a){var r,o,s,n,i=[],h=t.sortList||[],l=h.length;for(r=0;r<l;r++)s=h[r][0],o=e.trim(t.$headerIndexed[s].attr(a.sort2Hash_headerTextAttr)),i.push(""!==o?encodeURIComponent(o):s),n=a.sort2Hash_directionText[h[r][1]],i.push(n);return i.join(a.sort2Hash_separator)},convertFilter2String:function(t,a){var r,o,s,n,i=[],h=t.sortList||[],l=h.length;for(r=0;r<l;r++)s=h[r][0],s=void 0!==(o=e.trim(t.$headerIndexed[s].attr(a.sort2Hash_headerTextAttr)))?encodeURIComponent(o):s,i.push(s),n=a.sort2Hash_directionText[h[r][1]],i.push(n);return i.join(a.sort2Hash_separator)},getParam:function(e,t,r){t||(t=window.location.hash);var o=new RegExp("[\\?&]"+a.regexEscape(e)+"=([^&#]*)"),s=o.exec(t);return r?o:null===s?"":decodeURIComponent(s[1])},removeParam:function(e,t){t||(t=window.location.hash);var r,o=a.getParam(e,t,!0),s=[],n=t.split("&"),i=n.length;for(r=0;r<i;r++)o.test("&"+n[r])||s.push(n[r]);return s.length?s.join("&"):""},encodeHash:function(e,t,r,o,s){var n=!1,i=a.getTableId(e,t);return"function"==typeof t.sort2Hash_encodeHash&&(n=t.sort2Hash_encodeHash(e,i,r,o,s||o)),!1===n&&(n="&"+r+"["+i+"]="+o),n},decodeHash:function(e,t,r){var o=!1,s=a.getTableId(e,t);return"function"==typeof t.sort2Hash_decodeHash&&(o=t.sort2Hash_decodeHash(e,s,r)),!1===o&&(o=a.getParam(r+"["+s+"]")),o||""},cleanHash:function(e,t,r,o){var s=!1,n=a.getTableId(e,t);return"function"==typeof t.sort2Hash_cleanHash&&(s=t.sort2Hash_cleanHash(e,n,r,o)),!1===s&&(s=a.removeParam(r+"["+n+"]",o)),s||""},setHash:function(r,o){var s="",n=window.location.hash,i=t.hasWidget(r.table,"pager"),h=t.hasWidget(r.table,"filter"),l=a.convertSort2String(r,o),d=h&&""!==r.lastSearch.join("")?r.lastSearch:[],c=encodeURIComponent(d.join(r.widgetOptions.sort2Hash_separator)),H={sort:l?a.encodeHash(r,o,"sort",l,r.sortList):"",page:i?a.encodeHash(r,o,"page",r.pager.page+1):"",size:i?a.encodeHash(r,o,"size",r.pager.size):"",filter:c?a.encodeHash(r,o,"filter",c,d):""};e.each(H,function(e,t){n=a.cleanHash(r,o,e,n),s+=t});var g=o.sort2Hash_hash,p=((window.location.hash||"").replace(g,"").length?n:g)+s;if(o.sort2Hash_replaceHistory){var u=window.location.href.split(g)[0];p[0]!==g&&(p=g+p),window.location.replace(u+p)}else window.location.hash=p}};t.addWidget({id:"sort2Hash",priority:60,options:{sort2Hash_hash:"#",sort2Hash_separator:"-",sort2Hash_headerTextAttr:"data-header",sort2Hash_directionText:[0,1],sort2Hash_overrideSaveSort:!1,sort2Hash_replaceHistory:!1,sort2Hash_tableId:null,sort2Hash_encodeHash:null,sort2Hash_decodeHash:null,sort2Hash_cleanHash:null},init:function(e,t,r,o){a.init(r,o)},remove:function(e,t){t.$table.off(".sort2hash")}})}(jQuery);