mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-12-05 05:04:20 +00:00
2 lines
7.1 KiB
JavaScript
2 lines
7.1 KiB
JavaScript
/*! Widget: grouping - updated 9/27/2017 (v2.29.0) */
|
|
!function(r){"use strict";var o=r.tablesorter,e=o.grouping={types:{number:function(r,e,u,p){var t,a=e.hasClass(o.css.sortAsc);return p>1&&""!==u?(t=a?Math.floor(parseFloat(u)/p)*p:Math.ceil(parseFloat(u)/p)*p,t+=" - "+(t+(p-1)*(a?1:-1))):t=parseFloat(u)||u,t},separator:function(o,e,u,p){var t=(u+"").split(o.widgetOptions.group_separator);return r.trim(t[p-1]||"")},text:function(r,o,e){return e},word:function(r,o,e,u){return((e+" ").match(/\w+/g)||[])[u-1]||""},letter:function(r,o,e,u){return e?(e+" ").substring(0,u):""},date:function(r,o,u,p){var t,a,s=r.widgetOptions,n=new Date(u||"");return n instanceof Date&&isFinite(n)?(t=n.getFullYear(),a=e.findMonth(s,n.getMonth()),"year"===p?t:"month"===p?a:"monthyear"===p?a+" "+t:"day"===p?a+" "+n.getDate():"week"===p?e.findWeek(s,n.getDay()):"time"===p?e.findTime(s,n):"hour"===p?e.findTime(s,n,"hour"):s.group_dateString(n,r,o)):s.group_dateInvalid}},findMonth:function(r,o){return r.group_months[o+(""===(r.group_months[0]||"")?1:0)]},findWeek:function(o,e){if(r.isArray(o.group_week))return o.group_week[e];if(!r.isEmptyObject(o.group_week)){var u=["sun","mon","tue","wed","thu","fri","sat"];return o.group_week[u[e]]}},findTime:function(r,o,e){var u,p=r.group_time.am&&r.group_time.pm,t=o.getHours(),a=t>=12?1:0,s=("00"+(r.group_time24Hour&&t>12?t-12:r.group_time24Hour&&0===t?t+12:t)).slice(-2),n=("00"+o.getMinutes()).slice(-2);return u=r.group_time[p?["am","pm"][a]:a],"hour"===e?s:s+":"+n+(r.group_time24Hour?"":" "+(u||""))},update:function(o){if(!r.isEmptyObject(o.config.cache)){var u=o.config,p=u.widgetOptions,t=void 0!==u.sortList[0],a={},s=r.isArray(p.group_forceColumn)&&void 0!==p.group_forceColumn[0]?p.group_enforceSort&&!t?-1:p.group_forceColumn[0]:t?u.sortList[0][0]:-1;u.$table.find("tr.group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove(),p.group_collapsible&&u.$table.data("pagerSavedHeight",0),s>=0&&s<u.columns&&!u.$headerIndexed[s].hasClass("group-false")&&(p.group_collapsedGroup="",p.group_collapsedGroups={},a.column=s,a.groupClass=(u.$headerIndexed[s].attr("class")||"").match(/(group-\w+(-\w+)?)/g),a.grouping=a.groupClass?a.groupClass[0].split("-"):["group","letter",1],a.savedGroup=e.saveCurrentGrouping(u,p,a),e.findColumnGroups(u,p,a),e.processHeaders(u,p,a),u.$table.triggerHandler(p.group_complete))}},processHeaders:function(o,e,u){var p,t,a,s,n,l,g=o.$table.find("tr.group-header"),i=g.length;for(g.bind("selectstart",!1),p=0;p<i;p++)n=(l=g.eq(p)).nextUntil("tr.group-header").filter(":visible"),(e.group_count||r.isFunction(e.group_callback))&&(a=l.find(".group-count")).length&&(e.group_count&&a.html(e.group_count.toString().replace(/\{num\}/g,n.length)),r.isFunction(e.group_callback)&&e.group_callback(l.find("td"),n,u.column,o.table)),e.group_saveGroups&&!r.isEmptyObject(e.group_collapsedGroups)&&e.group_collapsedGroups[e.group_collapsedGroup].length?(s=l.find(".group-name").text().toLowerCase()+l.attr("data-group-index"),t=r.inArray(s,e.group_collapsedGroups[e.group_collapsedGroup])>-1,l.toggleClass("collapsed",t),n.toggleClass("group-hidden",t)):e.group_collapsed&&e.group_collapsible&&(l.addClass("collapsed"),n.addClass("group-hidden"))},groupHeaderHTML:function(r,o,e){var u=(e.currentGroup||"").toString().replace(/</g,"<").replace(/>/g,">");return'<tr class="group-header '+r.selectorRemove.slice(1)+" "+(o.columnSelector_classHasSpan||"hasSpan")+'" unselectable="on" '+(r.tabIndex?'tabindex="0" ':"")+'data-group-index="'+e.groupIndex+'"><td colspan="'+r.columns+'">'+(o.group_collapsible?"<i/>":"")+'<span class="group-name">'+u+'</span><span class="group-count"></span></td></tr>'},saveCurrentGrouping:function(r,e,u){var p,t,a=!1;return e.group_collapsible&&e.group_saveGroups&&(e.group_collapsedGroups=o.storage&&o.storage(r.table,"tablesorter-groups")||{},t="dir"+r.sortList[0][1],p=e.group_collapsedGroup=""+r.sortList[0][0]+t+u.grouping.join(""),e.group_collapsedGroups[p]?a=!0:e.group_collapsedGroups[p]=[]),a},findColumnGroups:function(r,u,p){var t,a,s,n,l=o.hasWidget(r.table,"pager"),g=r.pager||{};for(p.groupIndex=0,t=0;t<r.$tbodies.length;t++)for(a=r.cache[t].normalized,p.group=void 0,s=l&&!g.ajax?g.startRow-1:0,n=l?g.endRow-(g.ajax?g.startRow:0):a.length;s<n;s++)p.rowData=a[s],p.rowData&&(p.$row=p.rowData[r.columns].$row,p.$row.is(":visible")&&e.types[p.grouping[1]]&&e.insertGroupHeader(r,u,p));o.hasWidget(r.table,"columnSelector")&&o.columnSelector.setUpColspan(r,u)},insertGroupHeader:function(o,u,p){var t=o.$headerIndexed[p.column],a=p.rowData[p.column],s=/date/.test(p.groupClass)?p.grouping[2]:parseInt(p.grouping[2]||1,10)||1;p.currentGroup=p.rowData?e.types[p.grouping[1]](o,t,a,s,p.group):p.currentGroup,p.group!==p.currentGroup&&(p.group=p.currentGroup,r.isFunction(u.group_formatter)&&(p.currentGroup=u.group_formatter((p.group||"").toString(),p.column,o.table,o,u,p)||p.group),p.$row.first().before(e.groupHeaderHTML(o,u,p)),u.group_saveGroups&&!p.savedGroup&&u.group_collapsed&&u.group_collapsible&&u.group_collapsedGroups[u.group_collapsedGroup].push(p.currentGroup+p.groupIndex),p.groupIndex++)},bindEvents:function(u,p,t){t.group_collapsible&&(t.group_collapsedGroups=[],p.$table.on("click toggleGroup keyup","tr.group-header",function(e){if(e.stopPropagation(),"keyup"!==e.type||13===e.which){var a,s,n=r(this),l=n.find(".group-name").text().toLowerCase()+n.attr("data-group-index");!e.shiftKey||"click"!==e.type&&"keyup"!==e.type||n.siblings(".group-header").trigger("toggleGroup"),n.toggleClass("collapsed"),n.nextUntil("tr.group-header").toggleClass("group-hidden",n.hasClass("collapsed")),!(a=n.hasClass("collapsed"))&&o.hasWidget(p.$table,"zebra")&&o.applyWidgetId(p.$table,"zebra"),t.group_saveGroups&&o.storage&&(t.group_collapsedGroups[t.group_collapsedGroup]||(t.group_collapsedGroups[t.group_collapsedGroup]=[]),a&&t.group_collapsedGroup?t.group_collapsedGroups[t.group_collapsedGroup].push(l):t.group_collapsedGroup&&(s=r.inArray(l,t.group_collapsedGroups[t.group_collapsedGroup]))>-1&&t.group_collapsedGroups[t.group_collapsedGroup].splice(s,1),o.storage(u,"tablesorter-groups",t.group_collapsedGroups))}})),r(t.group_saveReset).on("click",function(){e.clearSavedGroups(u)}),p.$table.on("pagerChange.tsgrouping",function(){e.update(u)})},clearSavedGroups:function(r){r&&o.storage&&(o.storage(r,"tablesorter-groups",""),e.update(r))}};o.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_forceColumn:[],group_enforceSort:!0,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_time24Hour:!1,group_dateInvalid:"Invalid Date",group_dateString:function(r){return r.toLocaleString()}},init:function(r,o,u,p){e.bindEvents(r,u,p)},format:function(r){e.update(r)},remove:function(r,o){o.$table.off("click","tr.group-header").off("pagerChange.tsgrouping").find(".group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove()}})}(jQuery); |