tablesorter/dist/js/widgets/widget-columnSelector.min.js
Rob Garrison f006d8aa5f v2.28.6
2017-04-02 06:34:02 -05:00

1 line
9.9 KiB
JavaScript

!function(a){"use strict";var b=a.tablesorter,c=b.columnSelector={queryAll:"@media only all { [columns] { display: none; } } ",queryBreak:"@media all and (min-width: [size]) { [columns] { display: table-cell; } } ",init:function(b,d,e){var f,g;if(f=a(e.columnSelector_layout),!f.find("input").add(f.filter("input")).length)return void(d.debug&&console.error("ColumnSelector: >> ERROR: Column Selector aborting, no input found in the layout! ***"));d.$table.addClass(d.namespace.slice(1)+"columnselector"),g=d.selector={$container:a(e.columnSelector_container||"<div>")},g.$style=a("<style></style>").prop("disabled",!0).appendTo("head"),g.$breakpoints=a("<style></style>").prop("disabled",!0).appendTo("head"),g.isInitializing=!0,c.setUpColspan(d,e),c.setupSelector(d,e),e.columnSelector_mediaquery&&c.setupBreakpoints(d,e),g.isInitializing=!1,g.$container.length?c.updateCols(d,e):d.debug&&console.warn("ColumnSelector: >> container not found"),d.$table.off("refreshColumnSelector.tscolsel").on("refreshColumnSelector.tscolsel",function(a,b,d){c.refreshColumns(this.config,b,d)})},refreshColumns:function(b,d,e){var f,g,h,i,j=b.selector,k=a.isArray(e||d),l=b.widgetOptions;if(void 0!==d&&null!==d&&j.$container.length){if("selectors"===d&&(j.$container.empty(),c.setupSelector(b,l),c.setupBreakpoints(b,l),void 0===e&&null!==e&&(e=j.auto)),k)for(g=e||d,a.each(g,function(a,b){g[a]=parseInt(b,10)}),f=0;f<b.columns;f++)i=a.inArray(f,g)>=0,h=j.$container.find("input[data-column="+f+"]"),h.length&&(h.prop("checked",i),j.states[f]=i);i=e===!0||d===!0||"auto"===d&&e!==!1,h=j.$container.find('input[data-column="auto"]').prop("checked",i),c.updateAuto(b,l,h)}else c.updateBreakpoints(b,l),c.updateCols(b,l);c.saveValues(b,l),c.adjustColspans(b,l)},setupSelector:function(d,e){var f,g,h,i,j,k,l,m=d.selector,n=m.$container,o=e.columnSelector_saveColumns&&b.storage,p=o?b.storage(d.table,"tablesorter-columnSelector"):[],q=o?b.storage(d.table,"tablesorter-columnSelector-auto"):{};for(m.auto=a.isEmptyObject(q)||"boolean"!==a.type(q.auto)?e.columnSelector_mediaqueryState:q.auto,m.states=[],m.$column=[],m.$wrapper=[],m.$checkbox=[],f=0;f<d.columns;f++)h=d.$headerIndexed[f],i=h.attr(e.columnSelector_priority)||1,k=h.attr("data-column"),j=b.getColumnData(d.table,d.headers,k),q=b.getData(h,j,"columnSelector"),isNaN(i)&&i.length>0||"disable"===q||e.columnSelector_columns[k]&&"disable"===e.columnSelector_columns[k]?m.states[k]=null:(m.states[k]=p&&void 0!==p[k]&&null!==p[k]?p[k]:void 0!==e.columnSelector_columns[k]&&null!==e.columnSelector_columns[k]?e.columnSelector_columns[k]:"true"===q||"false"!==q,m.$column[k]=a(this),n.length&&(g=h.attr(e.columnSelector_name)||h.text().trim(),"function"==typeof e.columnSelector_layoutCustomizer&&(l=h.find("."+b.css.headerIn),g=e.columnSelector_layoutCustomizer(l.length?l:h,g,parseInt(k,10))),m.$wrapper[k]=a(e.columnSelector_layout.replace(/\{name\}/g,g)).appendTo(n),m.$checkbox[k]=m.$wrapper[k].find("input").add(m.$wrapper[k].filter("input")).attr("data-column",k).toggleClass(e.columnSelector_cssChecked,m.states[k]).prop("checked",m.states[k]).on("change",function(){if(!m.isInitializing){var b=a(this).attr("data-column");if(!c.checkChange(d,this.checked))return this.checked=!this.checked,!1;d.selector.states[b]=this.checked,c.updateCols(d,e)}}).change()))},checkChange:function(a,b){for(var c=a.widgetOptions,d=c.columnSelector_maxVisible,e=c.columnSelector_minVisible,f=a.selector.states,g=f.length,h=0;g-- >=0;)f[g]&&h++;return!(b&null!==d&&h>=d||!b&&null!==e&&h<=e)},setupBreakpoints:function(b,d){var e=b.selector;d.columnSelector_mediaquery&&(e.lastIndex=-1,c.updateBreakpoints(b,d),b.$table.off("updateAll.tscolsel").on("updateAll.tscolsel",function(){c.setupSelector(b,d),c.setupBreakpoints(b,d),c.updateBreakpoints(b,d),c.updateCols(b,d)})),e.$container.length&&(d.columnSelector_mediaquery&&(e.$auto=a(d.columnSelector_layout.replace(/\{name\}/g,d.columnSelector_mediaqueryName)).prependTo(e.$container),e.$auto.find("input").add(e.$auto.filter("input")).attr("data-column","auto").prop("checked",e.auto).toggleClass(d.columnSelector_cssChecked,e.auto).on("change",function(){c.updateAuto(b,d,a(this))}).change()),b.$table.off("update.tscolsel").on("update.tscolsel",function(){c.updateCols(b,d)}))},updateAuto:function(b,d,e){var f=b.selector;f.auto=e.prop("checked")||!1,a.each(f.$checkbox,function(a,b){b&&(b[0].disabled=f.auto,f.$wrapper[a].toggleClass("disabled",f.auto))}),d.columnSelector_mediaquery&&c.updateBreakpoints(b,d),c.updateCols(b,d),b.selector.$popup&&b.selector.$popup.find(".tablesorter-column-selector").html(f.$container.html()).find("input").each(function(){var b=a(this).attr("data-column");a(this).prop("checked","auto"===b?f.auto:f.states[b])}),c.saveValues(b,d),c.adjustColspans(b,d),f.auto&&b.$table.triggerHandler(d.columnSelector_updated)},addSelectors:function(a,b){var c=[],d=" col:nth-child("+b+")";return c.push(a+d+","+a+"_extra_table"+d),d=' tr:not(.hasSpan) th[data-column="'+(b-1)+'"]',c.push(a+d+","+a+"_extra_table"+d),d=" tr:not(.hasSpan) td:nth-child("+b+")",c.push(a+d+","+a+"_extra_table"+d),d=" tr td:not("+a+'HasSpan)[data-column="'+(b-1)+'"]',c.push(a+d+","+a+"_extra_table"+d),c},updateBreakpoints:function(d,e){var f,g,h,i,j=[],k=d.selector,l=d.namespace+"columnselector",m=[],n="";if(e.columnSelector_mediaquery&&!k.auto)return k.$breakpoints.prop("disabled",!0),void k.$style.prop("disabled",!1);if(e.columnSelector_mediaqueryHidden)for(h=0;h<d.columns;h++)g=b.getColumnData(d.table,d.headers,h),j[h+1]="false"===b.getData(d.$headerIndexed[h],g,"columnSelector"),j[h+1]&&(m=m.concat(c.addSelectors(l,h+1)));for(f=0;f<e.columnSelector_maxPriorities;f++)i=[],d.$headers.filter("["+e.columnSelector_priority+"="+(f+1)+"]").each(function(){h=parseInt(a(this).attr("data-column"),10)+1,j[h]||(i=i.concat(c.addSelectors(l,h)))}),i.length&&(m=m.concat(i),n+=c.queryBreak.replace(/\[size\]/g,e.columnSelector_breakpoints[f]).replace(/\[columns\]/g,i.join(",")));k.$style&&k.$style.prop("disabled",!0),m.length&&k.$breakpoints.prop("disabled",!1).text(c.queryAll.replace(/\[columns\]/g,m.join(","))+n)},updateCols:function(b,d){if(!(d.columnSelector_mediaquery&&b.selector.auto||b.selector.isInitializing)){var e,f=b.selector,g=[],h=b.namespace+"columnselector";f.$container.find("input[data-column]").filter('[data-column!="auto"]').each(function(){this.checked||(e=parseInt(a(this).attr("data-column"),10)+1,g=g.concat(c.addSelectors(h,e))),a(this).toggleClass(d.columnSelector_cssChecked,this.checked)}),d.columnSelector_mediaquery&&f.$breakpoints.prop("disabled",!0),f.$style&&f.$style.prop("disabled",!1).text(g.length?g.join(",")+" { display: none; }":""),c.saveValues(b,d),c.adjustColspans(b,d),b.$table.triggerHandler(d.columnSelector_updated)}},setUpColspan:function(d,e){var f,g,h,i=a(window),j=!1,k=d.$table.add(a(d.namespace+"_extra_table")).children().children("tr").children("th, td"),l=k.length;for(f=0;f<l;f++)(g=k[f].colSpan)>1&&(j=!0,k.eq(f).addClass(d.namespace.slice(1)+"columnselectorHasSpan").attr("data-col-span",g),b.computeColumnIndex(k.eq(f).parent().addClass("hasSpan")));j&&e.columnSelector_mediaquery&&(h=d.namespace+"columnselector",i.off(h).on("resize"+h,b.window_resize).on("resizeEnd"+h,function(){i.off("resize"+h,b.window_resize),c.adjustColspans(d,e),i.on("resize"+h,b.window_resize)}))},adjustColspans:function(b,c){var d,e,f,g,h,i,j=b.selector,k=c.filter_filteredRow||"filtered",l=c.columnSelector_mediaquery&&j.auto,m=b.$table.children("thead, tfoot").children().children().add(a(b.namespace+"_extra_table").children("thead, tfoot").children().children()),n=m.length;for(d=0;d<n;d++)if(i=m.eq(d),f=parseInt(i.attr("data-column"),10)||i[0].cellIndex,g=parseInt(i.attr("data-col-span"),10)||1,h=f+g,g>1){for(e=f;e<h;e++)(!l&&j.states[e]===!1||l&&b.$headerIndexed[e]&&!b.$headerIndexed[e].is(":visible"))&&g--;g?i.removeClass(k)[0].colSpan=g:i.addClass(k)}else void 0!==j.states[f]&&null!==j.states[f]&&i.toggleClass(k,!j.states[f])},saveValues:function(a,c){if(c.columnSelector_saveColumns&&b.storage){var d=a.selector;b.storage(a.$table[0],"tablesorter-columnSelector-auto",{auto:d.auto}),b.storage(a.$table[0],"tablesorter-columnSelector",d.states)}},attachTo:function(b,d){b=a(b)[0];var e,f,g,h=b.config,i=a(d);i.length&&h&&(i.find(".tablesorter-column-selector").length||i.append('<span class="tablesorter-column-selector"></span>'),e=h.selector,f=h.widgetOptions,i.find(".tablesorter-column-selector").html(e.$container.html()).find("input").each(function(){var b=a(this).attr("data-column"),c="auto"===b?e.auto:e.states[b];a(this).toggleClass(f.columnSelector_cssChecked,c).prop("checked",c)}),e.$popup=i.on("change","input",function(){if(!e.isInitializing){if(!c.checkChange(h,this.checked))return this.checked=!this.checked,!1;g=a(this).toggleClass(f.columnSelector_cssChecked,this.checked).attr("data-column"),e.$container.find('input[data-column="'+g+'"]').prop("checked",this.checked).trigger("change")}}))}};b.window_resize=function(){b.timer_resize&&clearTimeout(b.timer_resize),b.timer_resize=setTimeout(function(){a(window).trigger("resizeEnd")},250)},b.addWidget({id:"columnSelector",priority:10,options:{columnSelector_container:null,columnSelector_columns:{},columnSelector_saveColumns:!0,columnSelector_layout:'<label><input type="checkbox">{name}</label>',columnSelector_layoutCustomizer:null,columnSelector_name:"data-selector-name",columnSelector_mediaquery:!0,columnSelector_mediaqueryName:"Auto: ",columnSelector_mediaqueryState:!0,columnSelector_mediaqueryHidden:!1,columnSelector_maxVisible:null,columnSelector_minVisible:null,columnSelector_breakpoints:["20em","30em","40em","50em","60em","70em"],columnSelector_maxPriorities:6,columnSelector_priority:"data-priority",columnSelector_cssChecked:"checked",columnSelector_updated:"columnUpdate"},init:function(a,b,d,e){c.init(a,d,e)},remove:function(b,c,d,e){var f=c.selector;f&&f.$container.empty(),!e&&f&&(f.$popup&&f.$popup.empty(),f.$style.remove(),f.$breakpoints.remove(),a(c.namespace+"columnselectorHasSpan").removeClass(d.filter_filteredRow||"filtered"),c.$table.off("updateAll.tscolsel update.tscolsel"))}})}(jQuery);