mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
e8bc2bef2e
The "refreshColumnSelector" method can now update the contents of the container in case the header text, column priorities, etc change
1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
!function(a){"use strict";var b=a.tablesorter,c=".tscolsel",d=b.columnSelector={queryAll:"@media only all { [columns] { display: none; } } ",queryBreak:"@media all and (min-width: [size]) { [columns] { display: table-cell; } } ",init:function(b,e,f){var g,h;return g=a(f.columnSelector_layout),g.find("input").add(g.filter("input")).length?(e.$table.addClass(e.namespace.slice(1)+"columnselector"),h=e.selector={$container:a(f.columnSelector_container||"<div>")},h.$style=a("<style></style>").prop("disabled",!0).appendTo("head"),h.$breakpoints=a("<style></style>").prop("disabled",!0).appendTo("head"),h.isInitializing=!0,d.setupSelector(e,f),f.columnSelector_mediaquery&&d.setupBreakpoints(e,f),h.isInitializing=!1,h.$container.length?d.updateCols(e,f):e.debug&&console.warn("ColumnSelector: >> container not found"),void e.$table.off("refreshColumnSelector"+c).on("refreshColumnSelector"+c,function(a,b,c){d.refreshColumns(this.config,b,c)})):void(e.debug&&console.error("ColumnSelector: >> ERROR: Column Selector aborting, no input found in the layout! ***"))},refreshColumns:function(b,c,e){var f,g,h=a.isArray(e||c),i=b.widgetOptions;if("undefined"!=typeof c&&b.selector.$container.length){if("selectors"===c&&(b.selector.$container.empty(),d.setupSelector(b,i),d.setupBreakpoints(b,i),"undefined"==typeof e&&(e=b.selector.auto)),h)for(g=e||c,a.each(g,function(a,b){g[a]=parseInt(b,10)}),f=0;f<b.columns;f++)b.selector.$container.find("input[data-column="+f+"]").prop("checked",a.inArray(f,g)>=0);d.updateAuto(b,i,b.selector.$container.find('input[data-column="auto"]').prop("checked",e===!0||c===!0||"auto"===c&&e!==!1))}else d.updateBreakpoints(b,i),d.updateCols(b,i)},setupSelector:function(c,e){var f,g=c.selector,h=g.$container,i=e.columnSelector_saveColumns&&b.storage,j=i?b.storage(c.table,"tablesorter-columnSelector"):[],k=i?b.storage(c.table,"tablesorter-columnSelector-auto"):{};g.auto=a.isEmptyObject(k)||"boolean"!==a.type(k.auto)?e.columnSelector_mediaqueryState:k.auto,g.states=[],g.$column=[],g.$wrapper=[],g.$checkbox=[],c.$table.children("thead").find("tr:first th",c.table).each(function(){var i=a(this),k=i.attr(e.columnSelector_priority)||1,l=i.attr("data-column"),m=b.getData(this,c.headers[l],"columnSelector");return isNaN(k)&&k.length>0||"disable"===m||e.columnSelector_columns[l]&&"disable"===e.columnSelector_columns[l]?!0:(g.states[l]=j&&"undefined"!=typeof j[l]?j[l]:"undefined"!=typeof e.columnSelector_columns[l]?e.columnSelector_columns[l]:"true"===m||"false"!==m,g.$column[l]=a(this),f=i.attr(e.columnSelector_name)||i.text(),void(h.length&&(g.$wrapper[l]=a(e.columnSelector_layout.replace(/\{name\}/g,f)).appendTo(h),g.$checkbox[l]=g.$wrapper[l].find("input").add(g.$wrapper[l].filter("input")).attr("data-column",l).toggleClass(e.columnSelector_cssChecked,g.states[l]).prop("checked",g.states[l]).on("change",function(){g.states[l]=this.checked,d.updateCols(c,e)}).change())))})},setupBreakpoints:function(b,e){var f=b.selector;e.columnSelector_mediaquery&&(f.lastIndex=-1,d.updateBreakpoints(b,e),b.$table.off("updateAll"+c).on("updateAll"+c,function(){d.updateBreakpoints(b,e),d.updateCols(b,e)})),f.$container.length&&(e.columnSelector_mediaquery&&(f.$auto=a(e.columnSelector_layout.replace(/\{name\}/g,e.columnSelector_mediaqueryName)).prependTo(f.$container),f.$auto.find("input").add(f.$auto.filter("input")).attr("data-column","auto").prop("checked",f.auto).toggleClass(e.columnSelector_cssChecked,f.auto).on("change",function(){d.updateAuto(b,e,a(this))}).change()),b.$table.off("update"+c).on("update"+c,function(){d.updateCols(b,e)}))},updateAuto:function(c,e,f){var g=c.selector;g.auto=f.prop("checked")||!1,a.each(g.$checkbox,function(a,b){b&&(b[0].disabled=g.auto,g.$wrapper[a].toggleClass("disabled",g.auto))}),e.columnSelector_mediaquery&&d.updateBreakpoints(c,e),d.updateCols(c,e),c.selector.$popup&&c.selector.$popup.find(".tablesorter-column-selector").html(g.$container.html()).find("input").each(function(){var b=a(this).attr("data-column");a(this).prop("checked","auto"===b?g.auto:g.states[b])}),e.columnSelector_saveColumns&&b.storage&&b.storage(c.$table[0],"tablesorter-columnSelector-auto",{auto:g.auto}),g.auto&&c.$table.trigger("columnUpdate")},updateBreakpoints:function(b,c){var e,f,g,h,i=b.selector,j=b.namespace+"columnselector",k=[],l="";if(c.columnSelector_mediaquery&&!i.auto)return i.$breakpoints.prop("disabled",!0),void i.$style.prop("disabled",!1);for(e=0;6>e;e++)g=[],b.$headers.filter("["+c.columnSelector_priority+"="+(e+1)+"]").each(function(){f=parseInt(a(this).attr("data-column"),10)+1,h=" col:nth-child("+f+")",g.push(j+h+","+j+"_extra_table"+h),h=" tr th:nth-child("+f+")",g.push(j+h+","+j+"_extra_table"+h),h=" tr td:nth-child("+f+")",g.push(j+h+","+j+"_extra_table"+h)}),g.length&&(k=k.concat(g),l+=d.queryBreak.replace(/\[size\]/g,c.columnSelector_breakpoints[e]).replace(/\[columns\]/g,g.join(",")));i.$style&&i.$style.prop("disabled",!0),k.length&&i.$breakpoints.prop("disabled",!1).html(d.queryAll.replace(/\[columns\]/g,k.join(","))+l)},updateCols:function(c,d){if(!(d.columnSelector_mediaquery&&c.selector.auto||c.selector.isInitializing)){var e,f,g=c.selector,h=[],i=c.namespace+"columnselector";g.$container.find("input[data-column]").filter('[data-column!="auto"]').each(function(){this.checked||(e=parseInt(a(this).attr("data-column"),10)+1,f=" col:nth-child("+e+")",h.push(i+f+","+i+"_extra_table"+f),f=" tr th:nth-child("+e+")",h.push(i+f+","+i+"_extra_table"+f),f=" tr td:nth-child("+e+")",h.push(i+f+","+i+"_extra_table"+f)),a(this).toggleClass(d.columnSelector_cssChecked,this.checked)}),d.columnSelector_mediaquery&&g.$breakpoints.prop("disabled",!0),g.$style&&g.$style.prop("disabled",!1).html(h.length?h.join(",")+" { display: none; }":""),d.columnSelector_saveColumns&&b.storage&&b.storage(c.$table[0],"tablesorter-columnSelector",g.states),c.$table.trigger("columnUpdate")}},attachTo:function(b,c){b=a(b)[0];var d,e,f,g=b.config,h=a(c);h.length&&g&&(h.find(".tablesorter-column-selector").length||h.append('<span class="tablesorter-column-selector"></span>'),d=g.selector,e=g.widgetOptions,h.find(".tablesorter-column-selector").html(d.$container.html()).find("input").each(function(){var b=a(this).attr("data-column"),c="auto"===b?d.auto:d.states[b];a(this).toggleClass(e.columnSelector_cssChecked,c).prop("checked",c)}),d.$popup=h.on("change","input",function(){f=a(this).toggleClass(e.columnSelector_cssChecked,this.checked).attr("data-column"),d.$container.find('input[data-column="'+f+'"]').prop("checked",this.checked).trigger("change")}))}};b.addWidget({id:"columnSelector",priority:10,options:{columnSelector_container:null,columnSelector_columns:{},columnSelector_saveColumns:!0,columnSelector_layout:'<label><input type="checkbox">{name}</label>',columnSelector_name:"data-selector-name",columnSelector_mediaquery:!0,columnSelector_mediaqueryName:"Auto: ",columnSelector_mediaqueryState:!0,columnSelector_breakpoints:["20em","30em","40em","50em","60em","70em"],columnSelector_priority:"data-priority",columnSelector_cssChecked:"checked"},init:function(a,b,c,e){d.init(a,c,e)},remove:function(a,b,d,e){if(!e){var f=b.selector;f.$container.empty(),f.$popup&&f.$popup.empty(),f.$style.remove(),f.$breakpoints.remove(),b.$table.off("updateAll"+c+" update"+c)}}})}(jQuery); |