mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-12-05 05:04:20 +00:00
1 line
10 KiB
JavaScript
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(t,l,a){var n,c;(n=e(a.columnSelector_layout)).find("input").add(n.filter("input")).length?(l.$table.addClass(l.namespace.slice(1)+"columnselector"),(c=l.selector={$container:e(a.columnSelector_container||"<div>")}).$style=e("<style></style>").prop("disabled",!0).appendTo("head"),c.$breakpoints=e("<style></style>").prop("disabled",!0).appendTo("head"),c.isInitializing=!0,o.setUpColspan(l,a),o.setupSelector(l,a),a.columnSelector_mediaquery&&o.setupBreakpoints(l,a),c.isInitializing=!1,c.$container.length?o.updateCols(l,a):l.debug&&console.warn("ColumnSelector: >> container not found"),l.$table.off("refreshColumnSelector.tscolsel").on("refreshColumnSelector.tscolsel",function(e,t,l){o.refreshColumns(this.config,t,l)})):l.debug&&console.error("ColumnSelector: >> ERROR: Column Selector aborting, no input found in the layout! ***")},refreshColumns:function(t,l,a){var n,c,r,s,u=t.selector,i=e.isArray(a||l),d=t.widgetOptions;if(void 0!==l&&null!==l&&u.$container.length){if("selectors"===l&&(u.$container.empty(),o.setupSelector(t,d),o.setupBreakpoints(t,d),void 0===a&&null!==a&&(a=u.auto)),i)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=u.$container.find("input[data-column="+n+"]")).length&&(r.prop("checked",s),u.states[n]=s);s=!0===a||!0===l||"auto"===l&&!1!==a,r=u.$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,u,i,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,i=r.attr("data-column"),u=t.getColumnData(l.table,l.headers,i),S=t.getData(r,u,"columnSelector"),isNaN(s)&&s.length>0||"disable"===S||a.columnSelector_columns[i]&&"disable"===a.columnSelector_columns[i]?p.states[i]=null:(p.states[i]=f&&void 0!==f[i]&&null!==f[i]?f[i]:void 0!==a.columnSelector_columns[i]&&null!==a.columnSelector_columns[i]?a.columnSelector_columns[i]:"true"===S||"false"!==S,p.$column[i]=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(i,10))),p.$wrapper[i]=e(a.columnSelector_layout.replace(/\{name\}/g,c)).appendTo(m),p.$checkbox[i]=p.$wrapper[i].find("input").add(p.$wrapper[i].filter("input")).attr("data-column",i).toggleClass(a.columnSelector_cssChecked,p.states[i]).prop("checked",p.states[i]).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,u=[],i=l.selector,d=l.namespace+"columnselector",p=[],m="";if(a.columnSelector_mediaquery&&!i.auto)return i.$breakpoints.prop("disabled",!0),void i.$style.prop("disabled",!1);if(a.columnSelector_mediaqueryHidden)for(r=0;r<l.columns;r++)c=t.getColumnData(l.table,l.headers,r),u[r+1]="false"===t.getData(l.$headerIndexed[r],c,"columnSelector"),u[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,u[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(",")));i.$style&&i.$style.prop("disabled",!0),p.length&&i.$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),u=!1,i=l.$table.add(e(l.namespace+"_extra_table")).children().children("tr").children("th, td"),d=i.length;for(n=0;n<d;n++)(c=i[n].colSpan)>1&&(u=!0,i.eq(n).addClass(l.namespace.slice(1)+"columnselector"+a.columnSelector_classHasSpan).attr("data-col-span",c),t.computeColumnIndex(i.eq(n).parent().addClass(a.columnSelector_classHasSpan)));u&&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,u=t.selector,i=o.filter_filteredRow||"filtered",d=o.columnSelector_mediaquery&&u.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===u.states[a]||d&&t.$headerIndexed[a]&&!t.$headerIndexed[a].is(":visible"))&&c--;c?s.removeClass(i)[0].colSpan=c:s.addClass(i)}else void 0!==u.states[n]&&null!==u.states[n]&&s.toggleClass(i,!d&&!u.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); |