tabulator-tables
Version:
Interactive table generation JavaScript library
2 lines • 8.56 kB
JavaScript
/* Tabulator v4.9.3 (c) Oliver Folkerd */
var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},CalcComponent=function(t){this._row=t};CalcComponent.prototype.getData=function(t){return this._row.getData(t)},CalcComponent.prototype.getElement=function(){return this._row.getElement()},CalcComponent.prototype.getTable=function(){return this._row.table},CalcComponent.prototype.getCells=function(){var t=[];return this._row.getCells().forEach(function(o){t.push(o.getComponent())}),t},CalcComponent.prototype.getCell=function(t){var o=this._row.getCell(t);return!!o&&o.getComponent()},CalcComponent.prototype._getSelf=function(){return this._row};var ColumnCalcs=function(t){this.table=t,this.topCalcs=[],this.botCalcs=[],this.genColumn=!1,this.topElement=this.createElement(),this.botElement=this.createElement(),this.topRow=!1,this.botRow=!1,this.topInitialized=!1,this.botInitialized=!1,this.initialize()};ColumnCalcs.prototype.createElement=function(){var t=document.createElement("div");return t.classList.add("tabulator-calcs-holder"),t},ColumnCalcs.prototype.initialize=function(){this.genColumn=new Column({field:"value"},this)},ColumnCalcs.prototype.registerColumnField=function(){},ColumnCalcs.prototype.initializeColumn=function(t){var o=t.definition,e={topCalcParams:o.topCalcParams||{},botCalcParams:o.bottomCalcParams||{}};if(o.topCalc){switch(_typeof(o.topCalc)){case"string":this.calculations[o.topCalc]?e.topCalc=this.calculations[o.topCalc]:console.warn("Column Calc Error - No such calculation found, ignoring: ",o.topCalc);break;case"function":e.topCalc=o.topCalc}e.topCalc&&(t.modules.columnCalcs=e,this.topCalcs.push(t),"group"!=this.table.options.columnCalcs&&this.initializeTopRow())}if(o.bottomCalc){switch(_typeof(o.bottomCalc)){case"string":this.calculations[o.bottomCalc]?e.botCalc=this.calculations[o.bottomCalc]:console.warn("Column Calc Error - No such calculation found, ignoring: ",o.bottomCalc);break;case"function":e.botCalc=o.bottomCalc}e.botCalc&&(t.modules.columnCalcs=e,this.botCalcs.push(t),"group"!=this.table.options.columnCalcs&&this.initializeBottomRow())}},ColumnCalcs.prototype.removeCalcs=function(){var t=!1;this.topInitialized&&(this.topInitialized=!1,this.topElement.parentNode.removeChild(this.topElement),t=!0),this.botInitialized&&(this.botInitialized=!1,this.table.footerManager.remove(this.botElement),t=!0),t&&this.table.rowManager.adjustTableSize()},ColumnCalcs.prototype.initializeTopRow=function(){this.topInitialized||(this.table.columnManager.getElement().insertBefore(this.topElement,this.table.columnManager.headersElement.nextSibling),this.topInitialized=!0)},ColumnCalcs.prototype.initializeBottomRow=function(){this.botInitialized||(this.table.footerManager.prepend(this.botElement),this.botInitialized=!0)},ColumnCalcs.prototype.scrollHorizontal=function(t){this.botInitialized&&this.botRow&&(this.botRow.getElement().style.marginLeft=-t+"px")},ColumnCalcs.prototype.recalc=function(t){var o;if(this.topInitialized||this.botInitialized){if(this.rowsToData(t),this.topInitialized){for(this.topRow&&this.topRow.deleteCells(),o=this.generateRow("top",this.rowsToData(t)),this.topRow=o;this.topElement.firstChild;)this.topElement.removeChild(this.topElement.firstChild);this.topElement.appendChild(o.getElement()),o.initialize(!0)}if(this.botInitialized){for(this.botRow&&this.botRow.deleteCells(),o=this.generateRow("bottom",this.rowsToData(t)),this.botRow=o;this.botElement.firstChild;)this.botElement.removeChild(this.botElement.firstChild);this.botElement.appendChild(o.getElement()),o.initialize(!0)}this.table.rowManager.adjustTableSize(),this.table.modExists("frozenColumns")&&this.table.modules.frozenColumns.layout()}},ColumnCalcs.prototype.recalcRowGroup=function(t){this.recalcGroup(this.table.modules.groupRows.getRowGroup(t))},ColumnCalcs.prototype.recalcAll=function(){var t=this;if((this.topCalcs.length||this.botCalcs.length)&&("group"!==this.table.options.columnCalcs&&this.recalc(this.table.rowManager.activeRows),this.table.options.groupBy&&"table"!==this.table.options.columnCalcs)){table.modules.groupRows.getChildGroups().forEach(function(o){t.recalcGroup(o)})}},ColumnCalcs.prototype.recalcGroup=function(t){var o,e;t&&t.calcs&&(t.calcs.bottom&&(o=this.rowsToData(t.rows),e=this.generateRowData("bottom",o),t.calcs.bottom.updateData(e),t.calcs.bottom.reinitialize()),t.calcs.top&&(o=this.rowsToData(t.rows),e=this.generateRowData("top",o),t.calcs.top.updateData(e),t.calcs.top.reinitialize()))},ColumnCalcs.prototype.generateTopRow=function(t){return this.generateRow("top",this.rowsToData(t))},ColumnCalcs.prototype.generateBottomRow=function(t){return this.generateRow("bottom",this.rowsToData(t))},ColumnCalcs.prototype.rowsToData=function(t){var o=this,e=[];return t.forEach(function(t){if(e.push(t.getData()),o.table.options.dataTree&&o.table.options.dataTreeChildColumnCalcs&&t.modules.dataTree.open){var n=o.rowsToData(o.table.modules.dataTree.getFilteredTreeChildren(t));e=e.concat(n)}}),e},ColumnCalcs.prototype.generateRow=function(t,o){var e,n=this,i=this.generateRowData(t,o);return n.table.modExists("mutator")&&n.table.modules.mutator.disable(),e=new Row(i,this,"calc"),n.table.modExists("mutator")&&n.table.modules.mutator.enable(),e.getElement().classList.add("tabulator-calcs","tabulator-calcs-"+t),e.component=!1,e.getComponent=function(){return this.component||(this.component=new CalcComponent(this)),this.component},e.generateCells=function(){var o=[];n.table.columnManager.columnsByIndex.forEach(function(i){n.genColumn.setField(i.getField()),n.genColumn.hozAlign=i.hozAlign,i.definition[t+"CalcFormatter"]&&n.table.modExists("format")?n.genColumn.modules.format={formatter:n.table.modules.format.getFormatter(i.definition[t+"CalcFormatter"]),params:i.definition[t+"CalcFormatterParams"]||{}}:n.genColumn.modules.format={formatter:n.table.modules.format.getFormatter("plaintext"),params:{}},n.genColumn.definition.cssClass=i.definition.cssClass;var l=new Cell(n.genColumn,e);l.getElement(),l.column=i,l.setWidth(),i.cells.push(l),o.push(l),i.visible||l.hide()}),this.cells=o},e},ColumnCalcs.prototype.generateRowData=function(t,o){var e,n,i={},l="top"==t?this.topCalcs:this.botCalcs,a="top"==t?"topCalc":"botCalc";return l.forEach(function(t){var l=[];t.modules.columnCalcs&&t.modules.columnCalcs[a]&&(o.forEach(function(o){l.push(t.getFieldValue(o))}),n=a+"Params",e="function"==typeof t.modules.columnCalcs[n]?t.modules.columnCalcs[n](l,o):t.modules.columnCalcs[n],t.setFieldValue(i,t.modules.columnCalcs[a](l,o,e)))}),i},ColumnCalcs.prototype.hasTopCalcs=function(){return!!this.topCalcs.length},ColumnCalcs.prototype.hasBottomCalcs=function(){return!!this.botCalcs.length},ColumnCalcs.prototype.redraw=function(){this.topRow&&this.topRow.normalizeHeight(!0),this.botRow&&this.botRow.normalizeHeight(!0)},ColumnCalcs.prototype.getResults=function(){var t,o=this,e={};return this.table.options.groupBy&&this.table.modExists("groupRows")?(t=this.table.modules.groupRows.getGroups(!0),t.forEach(function(t){e[t.getKey()]=o.getGroupResults(t)})):e={top:this.topRow?this.topRow.getData():{},bottom:this.botRow?this.botRow.getData():{}},e},ColumnCalcs.prototype.getGroupResults=function(t){var o=this,e=t._getSelf(),n=t.getSubGroups(),i={};return n.forEach(function(t){i[t.getKey()]=o.getGroupResults(t)}),{top:e.calcs.top?e.calcs.top.getData():{},bottom:e.calcs.bottom?e.calcs.bottom.getData():{},groups:i}},ColumnCalcs.prototype.calculations={avg:function(t,o,e){var n=0,i=void 0!==e.precision?e.precision:2;return t.length&&(n=t.reduce(function(t,o){return Number(t)+Number(o)}),n/=t.length,n=!1!==i?n.toFixed(i):n),parseFloat(n).toString()},max:function(t,o,e){var n=null,i=void 0!==e.precision&&e.precision;return t.forEach(function(t){((t=Number(t))>n||null===n)&&(n=t)}),null!==n?!1!==i?n.toFixed(i):n:""},min:function(t,o,e){var n=null,i=void 0!==e.precision&&e.precision;return t.forEach(function(t){((t=Number(t))<n||null===n)&&(n=t)}),null!==n?!1!==i?n.toFixed(i):n:""},sum:function(t,o,e){var n=0,i=void 0!==e.precision&&e.precision;return t.length&&t.forEach(function(t){t=Number(t),n+=isNaN(t)?0:Number(t)}),!1!==i?n.toFixed(i):n},concat:function(t,o,e){var n=0;return t.length&&(n=t.reduce(function(t,o){return String(t)+String(o)})),n},count:function(t,o,e){var n=0;return t.length&&t.forEach(function(t){t&&n++}),n}},Tabulator.prototype.registerModule("columnCalcs",ColumnCalcs);