tablesorter/dist/js/widgets/widget-grouping.min.js
2015-03-05 10:10:57 -06:00

2 lines
5.5 KiB
JavaScript

/*! tablesorter Grouping widget - updated 3/5/2015 (v2.21.0) */
!function(a){"use strict";var b=a.tablesorter;b.grouping={types:{number:function(a,c,d,e,f){var g,h;return e>1&&""!==d?c.hasClass(b.css.sortAsc)?(g=Math.floor(parseFloat(d)/e)*e,g>parseFloat(f||0)?g:parseFloat(f||0)):(g=Math.ceil(parseFloat(d)/e)*e,g<parseFloat(f||e)-g?parseFloat(f||e)-g:g):(h=(d+"").match(/\d+/g),h&&h.length>=e?h[e-1]:d||"")},separator:function(b,c,d,e){var f=(d+"").split(b.widgetOptions.group_separator);return a.trim(f&&e>0&&f.length>=e?f[(e||1)-1]:"")},word:function(a,b,c,d){var e=(c+" ").match(/\w+/g);return e&&e.length>=d?e[d-1]:c||""},letter:function(a,b,c,d){return c?(c+" ").substring(0,d):""},date:function(a,b,c,d){var e=a.widgetOptions,f=new Date(c||""),g=f.getHours();return"year"===d?f.getFullYear():"month"===d?e.group_months[f.getMonth()]:"monthyear"===d?e.group_months[f.getMonth()]+" "+f.getFullYear():"day"===d?e.group_months[f.getMonth()]+" "+f.getDate():"week"===d?e.group_week[f.getDay()]:"time"===d?("00"+(g>12?g-12:0===g?g+12:g)).slice(-2)+":"+("00"+f.getMinutes()).slice(-2)+" "+("00"+e.group_time[g>=12?1:0]).slice(-2):e.group_dateString(f)}},update:function(c,d,e){if(!a.isEmptyObject(d.cache)){var f,g,h,i,j,k,l,m,n,o=e.grouping_language,p="",q=!1,r=d.sortList[0]?d.sortList[0][0]:-1;if(d.$table.find("tr.group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove(),e.group_collapsible&&d.$table.data("pagerSavedHeight",0),r>=0&&!d.$headerIndexed[r].hasClass("group-false")){for(e.group_currentGroup="",e.group_currentGroups={},j=(d.$headerIndexed[r].attr("class")||"").match(/(group-\w+(-\w+)?)/g),k=j?j[0].split("-"):["group","letter",1],e.group_collapsible&&e.group_saveGroups&&b.storage&&(e.group_currentGroups=b.storage(c,"tablesorter-groups")||{},n="number"===k[1]&&k[2]>1?"dir"+d.sortList[0][1]:"",m=e.group_currentGroup=""+r+n+k.join(""),e.group_currentGroups[m]?q=!0:e.group_currentGroups[m]=[]),g=0;g<d.$tbodies.length;g++)for(l=d.cache[g].normalized,p="",i=d.$tbodies.eq(g).children("tr").not("."+d.cssChildRow),f=0;f<i.length;f++)i.eq(f).is(":visible")&&b.grouping.types[k[1]]&&(h=l[f]?b.grouping.types[k[1]](d,d.$headerIndexed[r],l[f][r],/date/.test(j)?k[2]:parseInt(k[2]||1,10)||1,p,o):h,p!==h&&(p=h,"number"===k[1]&&k[2]>1&&""!==h&&(h+=" - "+(parseInt(h,10)+(parseInt(k[2],10)-1)*(d.$headerIndexed[r].hasClass(b.css.sortAsc)?1:-1))),a.isFunction(e.group_formatter)&&(h=e.group_formatter((h||"").toString(),r,c,d,e)||h),i.eq(f).before('<tr class="group-header '+d.selectorRemove.slice(1)+'" unselectable="on"><td colspan="'+d.columns+'">'+(e.group_collapsible?"<i/>":"")+'<span class="group-name">'+h+'</span><span class="group-count"></span></td></tr>'),e.group_saveGroups&&!q&&e.group_collapsed&&e.group_collapsible&&e.group_currentGroups[e.group_currentGroup].push(h)));d.$table.find("tr.group-header").bind("selectstart",!1).each(function(){var b,d,f,g=a(this),h=g.nextUntil("tr.group-header").filter(":visible");(e.group_count||a.isFunction(e.group_callback))&&(d=g.find(".group-count"),d.length&&(e.group_count&&d.html(e.group_count.replace(/\{num\}/g,h.length)),a.isFunction(e.group_callback)&&e.group_callback(g.find("td"),h,r,c))),e.group_saveGroups&&e.group_currentGroups.length&&e.group_currentGroups[e.group_currentGroup].length?(f=g.find(".group-name").text().toLowerCase(),b=a.inArray(f,e.group_currentGroups[e.group_currentGroup])>-1,g.toggleClass("collapsed",b),h.toggleClass("group-hidden",b)):e.group_collapsed&&e.group_collapsible&&(g.addClass("collapsed"),h.addClass("group-hidden"))}),d.$table.trigger(e.group_complete)}}},bindEvents:function(c,d,e){e.group_collapsible&&(e.group_currentGroups=[],d.$table.on("click toggleGroup","tr.group-header",function(f){f.stopPropagation();var g,h,i,j=a(this),k=j.find(".group-name").text().toLowerCase();"click"===f.type&&f.shiftKey&&j.siblings(".group-header").trigger("toggleGroup"),j.toggleClass("collapsed"),j.nextUntil("tr.group-header").toggleClass("group-hidden",j.hasClass("collapsed")),e.group_saveGroups&&b.storage&&(h=d.$table.find(".group-header"),g=j.hasClass("collapsed"),e.group_currentGroups[e.group_currentGroup]||(e.group_currentGroups[e.group_currentGroup]=[]),g&&e.group_currentGroup?e.group_currentGroups[e.group_currentGroup].push(k):e.group_currentGroup&&(i=a.inArray(k,e.group_currentGroups[e.group_currentGroup]),i>-1&&e.group_currentGroups[e.group_currentGroup].splice(i,1)),b.storage(c,"tablesorter-groups",e.group_currentGroups))})),a(e.group_saveReset).on("click",function(){b.grouping.clearSavedGroups(c)}),d.$table.on("pagerChange.tsgrouping",function(){b.grouping.update(c,d,e)})},clearSavedGroups:function(a){a&&b.storage&&(b.storage(a,"tablesorter-groups",""),b.grouping.update(a,a.config,a.config.widgetOptions))}},b.addWidget({id:"group",priority:100,options:{group_collapsible:!0,group_collapsed:!1,group_saveGroups:!0,group_saveReset:null,group_count:" ({num})",group_separator:"-",group_formatter:null,group_callback:null,group_complete:"groupingComplete",group_checkbox:["checked","unchecked"],group_months:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],group_week:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],group_time:["AM","PM"],group_dateString:function(a){return a.toLocaleString()}},init:function(a,c,d,e){b.grouping.bindEvents(a,d,e)},format:function(a,c,d){b.grouping.update(a,c,d)},remove:function(a,b){b.$table.off("click","tr.group-header").off("pagerChange.tsgrouping").find(".group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove()}})}(jQuery);