123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
-
- /* Tabulator v4.6.3 (c) Oliver Folkerd */
-
- var ColumnCalcs = function ColumnCalcs(table) {
- this.table = table; //hold Tabulator object
- this.topCalcs = [];
- this.botCalcs = [];
- this.genColumn = false;
- this.topElement = this.createElement();
- this.botElement = this.createElement();
- this.topRow = false;
- this.botRow = false;
- this.topInitialized = false;
- this.botInitialized = false;
-
- this.initialize();
- };
-
- ColumnCalcs.prototype.createElement = function () {
- var el = document.createElement("div");
- el.classList.add("tabulator-calcs-holder");
- return el;
- };
-
- ColumnCalcs.prototype.initialize = function () {
- this.genColumn = new Column({ field: "value" }, this);
- };
-
- //dummy functions to handle being mock column manager
- ColumnCalcs.prototype.registerColumnField = function () {};
-
- //initialize column calcs
- ColumnCalcs.prototype.initializeColumn = function (column) {
- var def = column.definition;
-
- var config = {
- topCalcParams: def.topCalcParams || {},
- botCalcParams: def.bottomCalcParams || {}
- };
-
- if (def.topCalc) {
-
- switch (_typeof(def.topCalc)) {
- case "string":
- if (this.calculations[def.topCalc]) {
- config.topCalc = this.calculations[def.topCalc];
- } else {
- console.warn("Column Calc Error - No such calculation found, ignoring: ", def.topCalc);
- }
- break;
-
- case "function":
- config.topCalc = def.topCalc;
- break;
-
- }
-
- if (config.topCalc) {
- column.modules.columnCalcs = config;
- this.topCalcs.push(column);
-
- if (this.table.options.columnCalcs != "group") {
- this.initializeTopRow();
- }
- }
- }
-
- if (def.bottomCalc) {
- switch (_typeof(def.bottomCalc)) {
- case "string":
- if (this.calculations[def.bottomCalc]) {
- config.botCalc = this.calculations[def.bottomCalc];
- } else {
- console.warn("Column Calc Error - No such calculation found, ignoring: ", def.bottomCalc);
- }
- break;
-
- case "function":
- config.botCalc = def.bottomCalc;
- break;
-
- }
-
- if (config.botCalc) {
- column.modules.columnCalcs = config;
- this.botCalcs.push(column);
-
- if (this.table.options.columnCalcs != "group") {
- this.initializeBottomRow();
- }
- }
- }
- };
-
- ColumnCalcs.prototype.removeCalcs = function () {
- var changed = false;
-
- if (this.topInitialized) {
- this.topInitialized = false;
- this.topElement.parentNode.removeChild(this.topElement);
- changed = true;
- }
-
- if (this.botInitialized) {
- this.botInitialized = false;
- this.table.footerManager.remove(this.botElement);
- changed = true;
- }
-
- if (changed) {
- this.table.rowManager.adjustTableSize();
- }
- };
-
- ColumnCalcs.prototype.initializeTopRow = function () {
- if (!this.topInitialized) {
- // this.table.columnManager.headersElement.after(this.topElement);
- this.table.columnManager.getElement().insertBefore(this.topElement, this.table.columnManager.headersElement.nextSibling);
- this.topInitialized = true;
- }
- };
-
- ColumnCalcs.prototype.initializeBottomRow = function () {
- if (!this.botInitialized) {
- this.table.footerManager.prepend(this.botElement);
- this.botInitialized = true;
- }
- };
-
- ColumnCalcs.prototype.scrollHorizontal = function (left) {
- var hozAdjust = 0,
- scrollWidth = this.table.columnManager.getElement().scrollWidth - this.table.element.clientWidth;
-
- if (this.botInitialized) {
- this.botRow.getElement().style.marginLeft = -left + "px";
- }
- };
-
- ColumnCalcs.prototype.recalc = function (rows) {
- var data, row;
-
- if (this.topInitialized || this.botInitialized) {
- data = this.rowsToData(rows);
-
- if (this.topInitialized) {
- if (this.topRow) {
- this.topRow.deleteCells();
- }
-
- row = this.generateRow("top", this.rowsToData(rows));
- this.topRow = row;
- while (this.topElement.firstChild) {
- this.topElement.removeChild(this.topElement.firstChild);
- }this.topElement.appendChild(row.getElement());
- row.initialize(true);
- }
-
- if (this.botInitialized) {
- if (this.botRow) {
- this.botRow.deleteCells();
- }
-
- row = this.generateRow("bottom", this.rowsToData(rows));
- this.botRow = row;
- while (this.botElement.firstChild) {
- this.botElement.removeChild(this.botElement.firstChild);
- }this.botElement.appendChild(row.getElement());
- row.initialize(true);
- }
-
- this.table.rowManager.adjustTableSize();
-
- //set resizable handles
- if (this.table.modExists("frozenColumns")) {
- this.table.modules.frozenColumns.layout();
- }
- }
- };
-
- ColumnCalcs.prototype.recalcRowGroup = function (row) {
- this.recalcGroup(this.table.modules.groupRows.getRowGroup(row));
- };
-
- ColumnCalcs.prototype.recalcAll = function () {
- var _this = this;
-
- if (this.topCalcs.length || this.botCalcs.length) {
- if (this.table.options.columnCalcs !== "group") {
- this.recalc(this.table.rowManager.activeRows);
- }
-
- if (this.table.options.groupBy && this.table.options.columnCalcs !== "table") {
-
- var groups = table.modules.groupRows.getChildGroups();
-
- groups.forEach(function (group) {
- _this.recalcGroup(group);
- });
- }
- }
- };
-
- ColumnCalcs.prototype.recalcGroup = function (group) {
- var data, rowData;
-
- if (group) {
- if (group.calcs) {
- if (group.calcs.bottom) {
- data = this.rowsToData(group.rows);
- rowData = this.generateRowData("bottom", data);
-
- group.calcs.bottom.updateData(rowData);
- group.calcs.bottom.reinitialize();
- }
-
- if (group.calcs.top) {
- data = this.rowsToData(group.rows);
- rowData = this.generateRowData("top", data);
-
- group.calcs.top.updateData(rowData);
- group.calcs.top.reinitialize();
- }
- }
- }
- };
-
- //generate top stats row
- ColumnCalcs.prototype.generateTopRow = function (rows) {
- return this.generateRow("top", this.rowsToData(rows));
- };
- //generate bottom stats row
- ColumnCalcs.prototype.generateBottomRow = function (rows) {
- return this.generateRow("bottom", this.rowsToData(rows));
- };
-
- ColumnCalcs.prototype.rowsToData = function (rows) {
- var _this2 = this;
-
- var data = [];
-
- rows.forEach(function (row) {
- data.push(row.getData());
-
- if (_this2.table.options.dataTree && _this2.table.options.dataTreeChildColumnCalcs) {
- if (row.modules.dataTree.open) {
- var children = _this2.rowsToData(_this2.table.modules.dataTree.getFilteredTreeChildren(row));
- data = data.concat(children);
- }
- }
- });
-
- return data;
- };
-
- //generate stats row
- ColumnCalcs.prototype.generateRow = function (pos, data) {
- var self = this,
- rowData = this.generateRowData(pos, data),
- row;
-
- if (self.table.modExists("mutator")) {
- self.table.modules.mutator.disable();
- }
-
- row = new Row(rowData, this, "calc");
-
- if (self.table.modExists("mutator")) {
- self.table.modules.mutator.enable();
- }
-
- row.getElement().classList.add("tabulator-calcs", "tabulator-calcs-" + pos);
-
- row.generateCells = function () {
-
- var cells = [];
-
- self.table.columnManager.columnsByIndex.forEach(function (column) {
-
- //set field name of mock column
- self.genColumn.setField(column.getField());
- self.genColumn.hozAlign = column.hozAlign;
-
- if (column.definition[pos + "CalcFormatter"] && self.table.modExists("format")) {
-
- self.genColumn.modules.format = {
- formatter: self.table.modules.format.getFormatter(column.definition[pos + "CalcFormatter"]),
- params: column.definition[pos + "CalcFormatterParams"]
- };
- } else {
- self.genColumn.modules.format = {
- formatter: self.table.modules.format.getFormatter("plaintext"),
- params: {}
- };
- }
-
- //ensure css class defintion is replicated to calculation cell
- self.genColumn.definition.cssClass = column.definition.cssClass;
-
- //generate cell and assign to correct column
- var cell = new Cell(self.genColumn, row);
- cell.column = column;
- cell.setWidth();
-
- column.cells.push(cell);
- cells.push(cell);
-
- if (!column.visible) {
- cell.hide();
- }
- });
-
- this.cells = cells;
- };
-
- return row;
- };
-
- //generate stats row
- ColumnCalcs.prototype.generateRowData = function (pos, data) {
- var rowData = {},
- calcs = pos == "top" ? this.topCalcs : this.botCalcs,
- type = pos == "top" ? "topCalc" : "botCalc",
- params,
- paramKey;
-
- calcs.forEach(function (column) {
- var values = [];
-
- if (column.modules.columnCalcs && column.modules.columnCalcs[type]) {
- data.forEach(function (item) {
- values.push(column.getFieldValue(item));
- });
-
- paramKey = type + "Params";
- params = typeof column.modules.columnCalcs[paramKey] === "function" ? column.modules.columnCalcs[paramKey](values, data) : column.modules.columnCalcs[paramKey];
-
- column.setFieldValue(rowData, column.modules.columnCalcs[type](values, data, params));
- }
- });
-
- return rowData;
- };
-
- ColumnCalcs.prototype.hasTopCalcs = function () {
- return !!this.topCalcs.length;
- };
-
- ColumnCalcs.prototype.hasBottomCalcs = function () {
- return !!this.botCalcs.length;
- };
-
- //handle table redraw
- ColumnCalcs.prototype.redraw = function () {
- if (this.topRow) {
- this.topRow.normalizeHeight(true);
- }
- if (this.botRow) {
- this.botRow.normalizeHeight(true);
- }
- };
-
- //return the calculated
- ColumnCalcs.prototype.getResults = function () {
- var self = this,
- results = {},
- groups;
-
- if (this.table.options.groupBy && this.table.modExists("groupRows")) {
- groups = this.table.modules.groupRows.getGroups(true);
-
- groups.forEach(function (group) {
- results[group.getKey()] = self.getGroupResults(group);
- });
- } else {
- results = {
- top: this.topRow ? this.topRow.getData() : {},
- bottom: this.botRow ? this.botRow.getData() : {}
- };
- }
-
- return results;
- };
-
- //get results from a group
- ColumnCalcs.prototype.getGroupResults = function (group) {
- var self = this,
- groupObj = group._getSelf(),
- subGroups = group.getSubGroups(),
- subGroupResults = {},
- results = {};
-
- subGroups.forEach(function (subgroup) {
- subGroupResults[subgroup.getKey()] = self.getGroupResults(subgroup);
- });
-
- results = {
- top: groupObj.calcs.top ? groupObj.calcs.top.getData() : {},
- bottom: groupObj.calcs.bottom ? groupObj.calcs.bottom.getData() : {},
- groups: subGroupResults
- };
-
- return results;
- };
-
- //default calculations
- ColumnCalcs.prototype.calculations = {
- "avg": function avg(values, data, calcParams) {
- var output = 0,
- precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : 2;
-
- if (values.length) {
- output = values.reduce(function (sum, value) {
- value = Number(value);
- return sum + value;
- });
-
- output = output / values.length;
-
- output = precision !== false ? output.toFixed(precision) : output;
- }
-
- return parseFloat(output).toString();
- },
- "max": function max(values, data, calcParams) {
- var output = null,
- precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
-
- values.forEach(function (value) {
-
- value = Number(value);
-
- if (value > output || output === null) {
- output = value;
- }
- });
-
- return output !== null ? precision !== false ? output.toFixed(precision) : output : "";
- },
- "min": function min(values, data, calcParams) {
- var output = null,
- precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
-
- values.forEach(function (value) {
-
- value = Number(value);
-
- if (value < output || output === null) {
- output = value;
- }
- });
-
- return output !== null ? precision !== false ? output.toFixed(precision) : output : "";
- },
- "sum": function sum(values, data, calcParams) {
- var output = 0,
- precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
-
- if (values.length) {
- values.forEach(function (value) {
- value = Number(value);
-
- output += !isNaN(value) ? Number(value) : 0;
- });
- }
-
- return precision !== false ? output.toFixed(precision) : output;
- },
- "concat": function concat(values, data, calcParams) {
- var output = 0;
-
- if (values.length) {
- output = values.reduce(function (sum, value) {
- return String(sum) + String(value);
- });
- }
-
- return output;
- },
- "count": function count(values, data, calcParams) {
- var output = 0;
-
- if (values.length) {
- values.forEach(function (value) {
- if (value) {
- output++;
- }
- });
- }
-
- return output;
- }
- };
-
- Tabulator.prototype.registerModule("columnCalcs", ColumnCalcs);
|