tablesorter/dist/js/widgets/widget-columnSelector.min.js
Rob Garrison 8b1ba542db 2.30.0
2018-03-18 09:25:52 -05:00

1 line
10 KiB
JavaScript

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