mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
12 lines
10 KiB
JavaScript
12 lines
10 KiB
JavaScript
/*! tableSorter 2.3 widgets - updated 6/1/2012 */
|
|
;(function(b){
|
|
b.tablesorter.storage=function(c,a,e){var d,h=!1;d={};var l=c.id||b(".tablesorter").index(b(c)),g=window.location.pathname;try{h=!!localStorage.getItem}catch(f){}b.parseJSON&&(h?d=b.parseJSON(localStorage[a])||{}:(d=document.cookie.split(/[;\s|=]/),c=b.inArray(a,d)+1,d=0!==c?b.parseJSON(d[c])||{}:{}));if(e&&JSON&&JSON.hasOwnProperty("stringify")){if(!d[g]||!d[g][l])d[g]||(d[g]={});d[g][l]=e;h?localStorage[a]=JSON.stringify(d):(c=new Date,c.setTime(c.getTime()+31536E6),document.cookie= a+"="+JSON.stringify(d).replace(/\"/g,'"')+"; expires="+c.toGMTString()+"; path=/")}else return d&&d.hasOwnProperty(g)&&d[g].hasOwnProperty(l)?d[g][l]:{}};
|
|
b.tablesorter.AddWidget({id:"uitheme",format:function(c){var a,e,d,h,l,g=b(c),f=c.config,m=f.widgetOptions,i=["ui-icon-arrowthick-2-n-s","ui-icon-arrowthick-1-s","ui-icon-arrowthick-1-n"],i=f.widgetUitheme&&f.widgetUitheme.hasOwnProperty("css")?f.widgetUitheme.css||i:m&&m.hasOwnProperty("uitheme")?m.uitheme:i;d=i.join(" ");f.debug&&(a=new Date); g.hasClass("ui-theme")||(g.addClass("ui-widget ui-widget-content ui-corner-all ui-theme"),b.each(f.headerList,function(){b(this).addClass("ui-widget-header ui-corner-all ui-state-default").append('<span class="ui-icon"/>').wrapInner('<div class="tablesorter-inner"/>').hover(function(){b(this).addClass("ui-state-hover")},function(){b(this).removeClass("ui-state-hover")})}));b.each(f.headerList,function(a){h=b(this);if(this.sortDisabled)h.find("span.ui-icon").removeClass(d+" ui-icon");else{e=h.hasClass(f.cssAsc)? i[1]:h.hasClass(f.cssDesc)?i[2]:h.hasClass(f.cssHeader)?i[0]:"";l=g.hasClass("hasStickyHeaders")?g.find("tr."+(m.stickyHeaders||"tablesorter-stickyHeader")).find("th").eq(a).add(h):h;l[e===i[0]?"removeClass":"addClass"]("ui-state-active").find("span.ui-icon").removeClass(d).addClass(e)}});f.debug&&b.tablesorter.benchmark("Applying uitheme widget",a)}});
|
|
b.tablesorter.AddWidget({id:"columns",format:function(c){var a,e,d,h,l,g,f,m,i=c.config,o=b(c).children("tbody:not(."+i.cssInfoBlock+")"),p=i.sortList, r=p.length,q=["primary","secondary","tertiary"],q=i.widgetColumns&&i.widgetColumns.hasOwnProperty("css")?i.widgetColumns.css||q:i.widgetOptions&&i.widgetOptions.hasOwnProperty("columns")?i.widgetOptions.columns||q:q;l=q.length-1;g=q.join(" ");i.debug&&(h=new Date);for(m=0;m<o.length;m++)c=b(o[m]),a=c.addClass("tablesorter-hidden").children("tr"),a.each(function(){d=b(this);if("none"!==this.style.display&&(e=d.children().removeClass(g),p&&p[0]&&(e.eq(p[0][0]).addClass(q[0]),1<r)))for(f=1;f<r;f++)e.eq(p[f][0]).addClass(q[f]|| q[l])}),c.removeClass("tablesorter-hidden");i.debug&&b.tablesorter.benchmark("Applying Columns widget",h)}});
|
|
b.tablesorter.AddWidget({id:"filter",format:function(c){if(!b(c).hasClass("hasFilters")){var a,e,d,h,l,g,f,m,i,o,p,r,q,w,t,A,x,k=c.config,j=k.widgetOptions,s=j.filter_cssFilter||"tablesorter-filter",u=b(c).addClass("hasFilters"),y=u.children("tbody:not(."+k.cssInfoBlock+")"),B=k.parsers.length,n='<tr class="'+s+'">',C=/^\/((?:\\\/|[^\/])+)\/([mig]{0,3})?$/,F=RegExp(k.cssChildRow),v,D,z=function(){k.debug&& (v=new Date);g=u.find("thead").eq(0).children("tr").find("select."+s+", input."+s).map(function(){return b(this).val()||""}).get();l=g.join("");for(d=0;d<y.length;d++){w=b(y[d]);t=w.addClass("tablesorter-hidden").children("tr");h=t.length;for(e=0;e<h;e++)if(!F.test(t[e].className))if(""===l)t[e].style.display="";else{m=!0;q=t.eq(e).nextUntil("tr:not(."+k.cssChildRow+")");o=q.length&&(j&&j.hasOwnProperty("filter_childRows")&&"undefined"!==typeof j.filter_childRows?j.filter_childRows:1)?q.text():""; A=t.eq(e).children("td");for(a=0;a<B;a++)p=b.trim(A.eq(a).text()),r=j.filter_ignoreCase?p.toLocaleLowerCase():p,""!==g[a]&&(i=m,f=j.filter_ignoreCase?g[a].toLocaleLowerCase():g[a],j.filter_functions&&j.filter_functions[a]?!0===j.filter_functions[a]?i=j.filter_ignoreCase?f===r:g[a]===p:"function"===typeof j.filter_functions[a]?i=j.filter_functions[a](p,k.cache[d].normalized[e][a],g[a],a):"function"===typeof j.filter_functions[a][g[a]]&&(i=j.filter_functions[a][g[a]](p,k.cache[d].normalized[e][a],g[a], a)):C.test(f)?(x=C.exec(f),i=RegExp(x[1],x[2]).test(r)):/[\"|\']$/.test(f)&&r===f.replace(/(\"|\')/g,"")?m=m?!0:!1:/[\?|\*]/.test(f)?i=RegExp(f.replace(/\?/g,"\\S{1}").replace(/\*/g,"\\S*")).test(r):(p=(r+o).indexOf(f),m=!j.filter_startsWith&&0<=p||j.filter_startsWith&&0===p?m?!0:!1:!1),m=i?m?!0:!1:!1);t[e].style.display=m?"":"none";if(q.length)q[m?"show":"hide"]()}w.removeClass("tablesorter-hidden")}k.debug&&b.tablesorter.benchmark("Completed filter widget search",v);u.trigger("applyWidgets")},E= function(a){var c,f=[],a=parseInt(a,10);c='<option value="">'+(b(k.headerList[a]).attr("data-placeholder")||"")+"</option>";for(d=0;d<y.length;d++){h=k.cache[d].row.length;for(e=0;e<h;e++)o=k.cache[d].row[e][0].cells[a],f.push(k.supportsTextContent?o.textContent:b(o).text())}f=f.getUnique(!0);for(d=0;d<f.length;d++)c+='<option value="'+f[d]+'">'+f[d]+"</option>";u.find("thead").find("select."+s+'[data-col="'+a+'"]').append(c)};k.debug&&(v=new Date);for(a=0;a<B;a++)c=j.filter_functions&&j.filter_functions[a]&& "function"!==typeof j.filter_functions[a]||b(k.headerList[a]).hasClass("filter-select"),n+="<td>",n=c?n+('<select data-col="'+a+'" class="'+s):n+('<input type="search" placeholder="'+(b(k.headerList[a]).attr("data-placeholder")||"")+'" data-col="'+a+'" class="'+s),n=b.tablesorter.getData?n+("false"===b.tablesorter.getData(k.headerList[a],k.headers[a],"filter")?' disabled" disabled':'"'):n+(k.headers[a]&&k.headers[a].hasOwnProperty("filter")&&!1===k.headers[a].filter||b(k.headerList[a]).hasClass("filter-false")? ' disabled" disabled':'"'),n+=(c?"></select>":">")+"</td>";u.find("thead").eq(0).append(n+="</tr>").find("input."+s).bind("keyup search",function(a,b){32>a.which&&8!==a.which||37<=a.which&&40>=a.which||(!1===b?z():(clearTimeout(D),D=setTimeout(function(){z()},j.filter_searchDelay||300)))});if(j.filter_functions)for(a in j.filter_functions)if(o=b(k.headerList[a]),n="","string"===typeof a&&!0===j.filter_functions[a]&&!o.hasClass("filter-false"))E(a);else if("string"===typeof a&&!o.hasClass("filter-false")){for(e in j.filter_functions[a])"string"=== typeof e&&(n+=""===n?"<option>"+(o.attr("data-placeholder")||"")+"</option>":"",n+="<option>"+e+"</option>");u.find("thead").find("select."+s+'[data-col="'+a+'"]').append(n)}for(a=0;a<k.headerList.length;a++)o=b(k.headerList[a]),o.hasClass("filter-select")&&(!o.hasClass("filter-false")&&!(j.filter_functions&&!0===j.filter_functions[a]))&&E(a);u.find("select."+s).bind("change",function(){z()});k.debug&&b.tablesorter.benchmark("Applying Filter widget",v)}}});
|
|
b.tablesorter.AddWidget({id:"stickyHeaders", format:function(c){if(!b(c).hasClass("hasStickyHeaders")){var a=b(c).addClass("hasStickyHeaders"),e=c.config.widgetOptions,d=b(window),h=b(c).children("thead"),l=h.children("tr:not(.sticky-false)").children(),g=e.stickyHeaders||"tablesorter-stickyHeader",f=l.eq(0),m=a.find("tfoot"),i=h.find("tr.tablesorter-header:not(.sticky-false)").clone().removeClass("tablesorter-header").addClass(g).css({width:h.outerWidth(!0),position:"fixed",left:f.offset().left,margin:0,top:0,visibility:"hidden",zIndex:10}), o=i.children(),p="";a.bind("sortEnd",function(a,d){var c=b(d).find("thead tr"),e=c.filter("."+g).children();c.filter(":not(."+g+")").children().each(function(a){e.eq(a).attr("class",b(this).attr("class"))})}).bind("pagerComplete",function(){d.resize()});l.each(function(a){var d=b(this);o.eq(a).bind("mouseup",function(a){d.trigger(a,!0)}).bind("mousedown",function(){this.onselectstart=function(){return!1};return!1}).find(".tablesorter-header-inner").width(d.find(".tablesorter-header-inner").width())}); h.prepend(i);d.scroll(function(){var b=f.offset(),c=d.scrollTop(),e=a.height()-(f.height()+(m.height()||0)),c=c>b.top&&c<b.top+e?"visible":"hidden";i.css({left:b.left-d.scrollLeft(),visibility:c});c!==p&&(d.resize(),p=c)}).resize(function(){var a=0;i.css({left:f.offset().left-d.scrollLeft(),width:h.outerWidth()}).each(function(c){b(this).css("top",a);a+=h.find("tr").eq(c).outerHeight()});o.find(".tablesorter-header-inner").each(function(a){b(this).width(l.eq(a).find(".tablesorter-header-inner").width())})})}}});
|
|
b.tablesorter.AddWidget({id:"resizable",format:function(c){if(!b(c).hasClass("hasResizable")){b(c).addClass("hasResizable");var a,e,d=c.config,h=b(d.headerList).filter(":gt(0)"),l=0,g=null,f=null,m=function(){l=0;g=f=null;b(window).trigger("resize")};if(e=b.tablesorter.storage?b.tablesorter.storage(c,"tablesorter-resizable"):"")for(a in e)!isNaN(a)&&a<d.headerList.length&&b(d.headerList[a]).width(e[a]);h.each(function(){b(this).append('<div class="tablesorter-resizer" style="cursor:w-resize;position:absolute;height:100%;width:20px;left:-20px;top:0;z-index:1;"></div>').wrapInner('<div style="position:relative;height:100%;width:100%"></div>')}).bind("mousemove", function(a){if(0!==l&&g){var b=a.pageX-l;g.width()<-b||f&&f.width()<=b||(f.width(f.width()+b),l=a.pageX)}}).bind("mouseup",function(){e&&(b.tablesorter.storage&&g)&&(e[f.index()]=f.width(),b.tablesorter.storage(c,"tablesorter-resizable",e));m();return!1}).find(".tablesorter-resizer").bind("mousedown",function(a){g=b(a.target).closest("th");f=g.prev();l=a.pageX;return!1});b(c).find("thead").bind("mouseup mouseleave",function(){m()})}}});
|
|
b.tablesorter.AddWidget({id:"saveSort",init:function(b,a,e){e.format(b, !0)},format:function(c,a){var e,d,h=c.config;e={sortList:h.sortList};h.debug&&(d=new Date);b(c).hasClass("hasSaveSort")?c.hasInitialized&&b.tablesorter.storage&&(b.tablesorter.storage(c,"tablesorter-savesort",e),h.debug&&b.tablesorter.benchmark("saveSort widget: Saving last sort: "+h.sortList,d)):(b(c).addClass("hasSaveSort"),e="",b.tablesorter.storage&&(e=(e=b.tablesorter.storage(c,"tablesorter-savesort"))&&e.hasOwnProperty("sortList")&&b.isArray(e.sortList)?e.sortList:"",h.debug&&b.tablesorter.benchmark("saveSort: Last sort loaded: "+ e,d)),a&&e&&0<e.length?h.sortList=e:c.hasInitialized&&(e&&0<e.length)&&b(c).trigger("sorton",[e]))}})
|
|
})(jQuery);
|
|
Array.prototype.getUnique=function(b){var c,a=[],e={},d,h=0,l=this.length;for(d=0;d<l;++d)c=this[d],e[c]||(e[c]={},a[h++]=c);return b?a.sort():a};
|