tablesorter/dist/js/widgets/widget-sort2Hash.min.js
2015-10-31 11:37:55 -05:00

34 lines
4.4 KiB
JavaScript

/*! Widget: sort2Hash (BETA) - updated 10/31/2015 (v2.24.0) */
/* Requires tablesorter v2.8+ and jQuery 1.7+
* by Rob Garrison
*/
!function(a){"use strict";var b=a.tablesorter||{},c={init:function(d,e){var f,g,h,i,j=d.table,k=d.pager,l=b.hasWidget(j,"saveSort"),m=c.decodeHash(d,e,"sort");(m&&!l||m&&l&&e.sort2Hash_overrideSaveSort)&&c.convertString2Sort(d,e,m),b.hasWidget(d.table,"pager")&&(g=parseInt(c.decodeHash(d,e,"page"),10),h=k.page=(0>g?0:g>k.totalPages?k.totalPages-1:g)+1,i=k.size=parseInt(c.decodeHash(d,e,"size"),10)),b.hasWidget(j,"filter")?(f=c.decodeHash(d,e,"filter"),f&&(f=f.split(e.sort2Hash_separator),d.$table.one("tablesorter-ready",function(){setTimeout(function(){d.$table.one("filterEnd",function(){a(this).trigger("pageAndSize",[h,i])}),a.tablesorter.setFilters(j,f,!0)},100)}))):d.$table.trigger("pageAndSize",[h,i]),d.$table.on("sortEnd.sort2hash filterEnd.sort2hash pagerComplete.sort2Hash",function(){this.hasInitialized&&c.setHash(this.config,this.config.widgetOptions)})},getTableId:function(b,c){
// option > table id > table index on page
return c.sort2Hash_tableId||b.table.id||"table"+a("table").index(b.$table)},regexEscape:function(a){return a.replace(/([\.\^\$\*\+\-\?\(\)\[\]\{\}\\\|])/g,"\\$1")},
// convert 'first%20name,asc,last%20name,desc' into [[0,0], [1,1]]
convertString2Sort:function(a,b,d){for(var e,f,g,h,i,j,k=d.split(b.sort2Hash_separator),l=0,m=k.length,n=[];m>l;){
// ignore wo.sort2Hash_useHeaderText setting &
// just see if column contains a number
if(f=k[l++],h=parseInt(f,10),isNaN(h)||h>a.columns)for(e=new RegExp("("+c.regexEscape(f)+")","i"),i=0;i<a.columns;i++)j=a.$headerIndexed[i],e.test(j.attr(b.sort2Hash_headerTextAttr))&&(f=i,i=a.columns);g=k[l++],
// ignore unpaired values
"undefined"!=typeof f&&"undefined"!=typeof g&&(
// convert text to 0, 1
isNaN(g)&&(
// default to ascending sort
g=g.indexOf(b.sort2Hash_directionText[1])>-1?1:0),n.push([f,g]))}n.length&&(a.sortList=n)},
// convert [[0,0],[1,1]] to 'first%20name,asc,last%20name,desc'
convertSort2String:function(b,c){var d,e,f,g,h=[],i=b.sortList||[],j=i.length;for(d=0;j>d;d++)f=i[d][0],e=a.trim(b.$headerIndexed[f].attr(c.sort2Hash_headerTextAttr)),h.push(""!==e?encodeURIComponent(e):f),g=c.sort2Hash_directionText[i[d][1]],h.push(g);
// join with separator
return h.join(c.sort2Hash_separator)},convertFilter2String:function(b,c){var d,e,f,g,h=[],i=b.sortList||[],j=i.length;for(d=0;j>d;d++)f=i[d][0],e=a.trim(b.$headerIndexed[f].attr(c.sort2Hash_headerTextAttr)),f="undefined"!=typeof e?encodeURIComponent(e):f,h.push(f),g=c.sort2Hash_directionText[i[d][1]],h.push(g);
// join with separator
return h.join(c.sort2Hash_separator)},encodeHash:function(a,b,d,e,f){var g=!1,h=c.getTableId(a,b);return"function"==typeof b.sort2Hash_encodeHash&&(g=b.sort2Hash_encodeHash(a,h,d,e,f||e)),g===!1&&(g="&"+d+"["+h+"]="+e),g},decodeHash:function(a,b,d){var e,f=!1,g=c.getTableId(a,b);/*jshint -W030 */
return"function"==typeof b.sort2Hash_decodeHash&&(f=b.sort2Hash_decodeHash(a,g,d)),f===!1&&(e=new RegExp("[\\#&]"+d+"\\["+c.regexEscape(g)+"\\]=([^&]*)"),f=e.exec(window.location.hash)),f?decodeURIComponent(f[1]):""},cleanHash:function(a,b,d,e){var f,g,h,i,j=!1,k=c.getTableId(a,b);if("function"==typeof b.sort2Hash_cleanHash&&(j=b.sort2Hash_cleanHash(a,k,d,e)),j===!1)for(j=[],h=(e||"").slice(1).split("&"),g=h.length,i=new RegExp(d+"\\["+c.regexEscape(k)+"\\]=([^&]*)"),f=0;g>f;f++)i.test(h[f])||j.push(h[f]);return j.length?"#"+j.join("&"):""},setHash:function(d,e){var f="",g=window.location.hash,h=b.hasWidget(d.table,"pager"),i=b.hasWidget(d.table,"filter"),j=c.convertSort2String(d,e),k=i&&""!==d.lastSearch.join("")?d.lastSearch:[],l=encodeURIComponent(k.join(d.widgetOptions.sort2Hash_separator)),m={sort:j?c.encodeHash(d,e,"sort",j,d.sortList):"",page:h?c.encodeHash(d,e,"page",d.pager.page+1):"",size:h?c.encodeHash(d,e,"size",d.pager.size):"",filter:l?c.encodeHash(d,e,"filter",l,k):""};
// remove old hash
a.each(m,function(a,b){g=c.cleanHash(d,e,a,g),f+=b}),
// add updated hash
window.location.hash=((window.location.hash||"").replace("#","").length?g:e.sort2Hash_hash)+f}};b.addWidget({id:"sort2Hash",priority:60,// after saveSort & pager
options:{sort2Hash_hash:"#",// hash prefix
sort2Hash_separator:"-",// don't '#' or '=' here
sort2Hash_headerTextAttr:"data-header",// data attribute containing alternate header text
sort2Hash_directionText:[0,1],// [ 'asc', 'desc' ],
sort2Hash_overrideSaveSort:!1},init:function(a,b,d,e){c.init(d,e)},remove:function(a,b){b.$table.off(".sort2hash")}})}(jQuery);