tablesorter/dist/js/widgets/widget-grouping.min.js
2015-11-02 10:50:31 -06:00

79 lines
9.4 KiB
JavaScript

/*! Widget: grouping - updated 11/2/2015 (v2.24.1) */
/*
* Requires tablesorter v2.8+ and jQuery 1.7+
* by Rob Garrison
*/
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery: false */
!function(a){"use strict";var b=a.tablesorter,c=b.grouping={types:{number:function(a,c,d,e){var f,g=c.hasClass(b.css.sortAsc);
// show range
return e>1&&""!==d?(f=g?Math.floor(parseFloat(d)/e)*e:Math.ceil(parseFloat(d)/e)*e,f+=" - "+(f+(e-1)*(g?1:-1))):f=parseFloat(d)||d,f},separator:function(b,c,d,e){var f=(d+"").split(b.widgetOptions.group_separator);
// return $.trim(word && num > 0 && word.length >= num ? word[(num || 1) - 1] : '');
return a.trim(f[e-1]||"")},text:function(a,b,c){return c},word:function(a,b,c,d){var e=(c+" ").match(/\w+/g)||[];
// return word && word.length >= num ? word[num - 1] : txt || '';
return e[d-1]||""},letter:function(a,b,c,d){return c?(c+" ").substring(0,d):""},date:function(a,b,d,e,f){var g,h,i=a.widgetOptions,j=new Date(d||"");
// check for valid date
// check for valid date
return j instanceof Date&&isFinite(j)?(g=j.getFullYear(),h=c.findMonth(i,j.getMonth()),"year"===e?g:"month"===e?h:"monthyear"===e?h+" "+g:"day"===e?h+" "+j.getDate():"week"===e?c.findWeek(i,j.getDay()):"time"===e?c.findTime(i,j):i.group_dateString(j,a,b)):i.group_dateInvalid}},
// group date type functions to allow using this widget with Globalize
findMonth:function(a,b){
// CLDR returns an object { 1: "Jan", 2: "Feb", 3: "Mar", ..., 12: "Dec" }
return a.group_months[b+(""===(a.group_months[0]||"")?1:0)]},findWeek:function(b,c){if(a.isArray(b.group_week))return b.group_week[c];if(!a.isEmptyObject(b.group_week)){
// CLDR returns { sun: "Sun", mon: "Mon", tue: "Tue", wed: "Wed", thu: "Thu", ... }
var d=["sun","mon","tue","wed","thu","fri","sat"];return b.group_week[d[c]]}},findTime:function(a,b){var c,
// CLDR returns { am: "AM", pm: "PM", ... }
d=a.group_time.am&&a.group_time.pm,e=b.getHours(),f=e>=12?1:0,g=a.group_time24Hour&&e>12?e-12:a.group_time24Hour&&0===e?e+12:e,h=("00"+g).slice(-2),i=("00"+b.getMinutes()).slice(-2);return c=a.group_time[d?["am","pm"][f]:f],h+":"+i+(a.group_time24Hour?"":" "+(c||""))},update:function(b,d,e){if(!a.isEmptyObject(d.cache)){var f="undefined"!=typeof d.sortList[0],g={},h=a.isArray(e.group_forceColumn)&&"undefined"!=typeof e.group_forceColumn[0]?e.group_enforceSort&&!f?-1:e.group_forceColumn[0]:f?d.sortList[0][0]:-1;d.$table.find("tr.group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove(),e.group_collapsible&&
// clear pager saved spacer height (in case the rows are collapsed)
d.$table.data("pagerSavedHeight",0),h>=0&&h<d.columns&&!d.$headerIndexed[h].hasClass("group-false")&&(e.group_collapsedGroup="",// save current groups
e.group_collapsedGroups={},g.column=h,
// group class finds 'group-{word/separator/letter/number/date/false}-{optional:#/year/month/day/week/time}'
g.groupClass=(d.$headerIndexed[h].attr("class")||"").match(/(group-\w+(-\w+)?)/g),
// grouping = [ 'group', '{word/separator/letter/number/date/false}', '{#/year/month/day/week/time}' ]
g.grouping=g.groupClass?g.groupClass[0].split("-"):["group","letter",1],// default to letter 1
// save current grouping
g.savedGroup=c.saveCurrentGrouping(d,e,g),
// find column groups
c.findColumnGroups(d,e,g),c.processHeaders(d,e,g),d.$table.trigger(e.group_complete))}},processHeaders:function(b,c,d){var e,f,g,h,i,j,k=b.$table.find("tr.group-header"),l=k.length;for(k.bind("selectstart",!1),e=0;l>e;e++)j=k.eq(e),i=j.nextUntil("tr.group-header").filter(":visible"),(c.group_count||a.isFunction(c.group_callback))&&(g=j.find(".group-count"),g.length&&(c.group_count&&g.html(c.group_count.replace(/\{num\}/g,i.length)),a.isFunction(c.group_callback)&&c.group_callback(j.find("td"),i,d.column,b.table))),c.group_saveGroups&&!a.isEmptyObject(c.group_collapsedGroups)&&c.group_collapsedGroups[c.group_collapsedGroup].length?(h=j.find(".group-name").text().toLowerCase()+j.attr("data-group-index"),f=a.inArray(h,c.group_collapsedGroups[c.group_collapsedGroup])>-1,j.toggleClass("collapsed",f),i.toggleClass("group-hidden",f)):c.group_collapsed&&c.group_collapsible&&(j.addClass("collapsed"),i.addClass("group-hidden"))},groupHeaderHTML:function(a,b,c){return'<tr class="group-header '+a.selectorRemove.slice(1)+'" unselectable="on" '+(a.tabIndex?'tabindex="0" ':"")+'data-group-index="'+c.groupIndex++ +'"><td colspan="'+a.columns+'">'+(b.group_collapsible?"<i/>":"")+'<span class="group-name">'+c.currentGroup+'</span><span class="group-count"></span></td></tr>'},saveCurrentGrouping:function(a,c,d){
// save current grouping
var e,f,g=!1;
// include direction when saving groups (reversed numbers shows different range values)
// combine column, sort direction & grouping as save key
return c.group_collapsible&&c.group_saveGroups&&b.storage&&(c.group_collapsedGroups=b.storage(a.table,"tablesorter-groups")||{},f="dir"+a.sortList[0][1],e=c.group_collapsedGroup=""+a.sortList[0][0]+f+d.grouping.join(""),c.group_collapsedGroups[e]?g=!0:c.group_collapsedGroups[e]=[]),g},findColumnGroups:function(a,d,e){var f,g,h,i,j=b.hasWidget(a.table,"pager");for(e.groupIndex=0,f=0;f<a.$tbodies.length;f++)for(g=a.cache[f].normalized,e.group="",// clear grouping across tbodies
h=j?a.pager.startRow-1:0,i=j?a.pager.endRow:g.length;i>h;h++)e.rowData=g[h],e.$row=e.rowData[a.columns].$row,
// fixes #438
e.$row.is(":visible")&&c.types[e.grouping[1]]&&c.insertGroupHeader(a,d,e)},insertGroupHeader:function(b,d,e){var f=b.$headerIndexed[e.column],g=e.rowData[e.column],h=/date/.test(e.groupClass)?e.grouping[2]:parseInt(e.grouping[2]||1,10)||1;e.currentGroup=e.rowData?c.types[e.grouping[1]](b,f,g,h,e.group):e.currentGroup,e.group!==e.currentGroup&&(e.group=e.currentGroup,a.isFunction(d.group_formatter)&&(e.currentGroup=d.group_formatter((e.group||"").toString(),e.column,b.table,b,d)||e.group),e.$row.before(c.groupHeaderHTML(b,d,e)),d.group_saveGroups&&!e.savedGroup&&d.group_collapsed&&d.group_collapsible&&
// all groups start collapsed
d.group_collapsedGroups[d.group_collapsedGroup].push(e.currentGroup))},bindEvents:function(d,e,f){f.group_collapsible&&(f.group_collapsedGroups=[],
// .on() requires jQuery 1.7+
e.$table.on("click toggleGroup keyup","tr.group-header",function(c){
// pressing enter will toggle the group
if(c.stopPropagation(),"keyup"!==c.type||13===c.which){var g,h,i,j=a(this),k=j.find(".group-name").text().toLowerCase()+j.attr("data-group-index");
// use shift-click to toggle ALL groups
!c.shiftKey||"click"!==c.type&&"keyup"!==c.type||j.siblings(".group-header").trigger("toggleGroup"),j.toggleClass("collapsed"),
// nextUntil requires jQuery 1.4+
j.nextUntil("tr.group-header").toggleClass("group-hidden",j.hasClass("collapsed")),
// save collapsed groups
f.group_saveGroups&&b.storage&&(h=e.$table.find(".group-header"),g=j.hasClass("collapsed"),f.group_collapsedGroups[f.group_collapsedGroup]||(f.group_collapsedGroups[f.group_collapsedGroup]=[]),g&&f.group_collapsedGroup?f.group_collapsedGroups[f.group_collapsedGroup].push(k):f.group_collapsedGroup&&(i=a.inArray(k,f.group_collapsedGroups[f.group_collapsedGroup]),i>-1&&f.group_collapsedGroups[f.group_collapsedGroup].splice(i,1)),b.storage(d,"tablesorter-groups",f.group_collapsedGroups))}})),a(f.group_saveReset).on("click",function(){c.clearSavedGroups(d)}),e.$table.on("pagerChange.tsgrouping",function(){c.update(d,e,f)})},clearSavedGroups:function(a){a&&b.storage&&(b.storage(a,"tablesorter-groups",""),c.update(a,a.config,a.config.widgetOptions))}};b.addWidget({id:"group",priority:100,options:{group_collapsible:!0,// make the group header clickable and collapse the rows below it.
group_collapsed:!1,// start with all groups collapsed
group_saveGroups:!0,// remember collapsed groups
group_saveReset:null,// element to clear saved collapsed groups
group_count:" ({num})",// if not false, the '{num}' string is replaced with the number of rows in the group
group_separator:"-",// group name separator; used when group-separator-# class is used.
group_formatter:null,// function(txt, column, table, c, wo) { return txt; }
group_callback:null,// function($cell, $rows, column, table){}, callback allowing modification of the group header labels
group_complete:"groupingComplete",// event triggered on the table when the grouping widget has finished work
// apply the grouping widget only to selected column
group_forceColumn:[],// only the first value is used; set as an array for future expansion
group_enforceSort:!0,// only apply group_forceColumn when a sort is applied to the table
// checkbox parser text used for checked/unchecked values
group_checkbox:["checked","unchecked"],
// change these default date names based on your language preferences
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"],
// use 12 vs 24 hour time
group_time24Hour:!1,
// group header text added for invalid dates
group_dateInvalid:"Invalid Date",
// this function is used when 'group-date' is set to create the date string
// you can just return date, date.toLocaleString(), date.toLocaleDateString() or d.toLocaleTimeString()
// reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
group_dateString:function(a){return a.toLocaleString()}},init:function(a,b,d,e){c.bindEvents(a,d,e)},format:function(a,b,d){c.update(a,b,d)},remove:function(a,b,c){b.$table.off("click","tr.group-header").off("pagerChange.tsgrouping").find(".group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove()}})}(jQuery);