Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calculation_colums.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. 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; };
  2. /* Tabulator v4.8.2 (c) Oliver Folkerd */
  3. //public calc object
  4. var CalcComponent = function CalcComponent(row) {
  5. this._row = row;
  6. };
  7. CalcComponent.prototype.getData = function (transform) {
  8. return this._row.getData(transform);
  9. };
  10. CalcComponent.prototype.getElement = function () {
  11. return this._row.getElement();
  12. };
  13. CalcComponent.prototype.getTable = function () {
  14. return this._row.table;
  15. };
  16. CalcComponent.prototype.getCells = function () {
  17. var cells = [];
  18. this._row.getCells().forEach(function (cell) {
  19. cells.push(cell.getComponent());
  20. });
  21. return cells;
  22. };
  23. CalcComponent.prototype.getCell = function (column) {
  24. var cell = this._row.getCell(column);
  25. return cell ? cell.getComponent() : false;
  26. };
  27. CalcComponent.prototype._getSelf = function () {
  28. return this._row;
  29. };
  30. var ColumnCalcs = function ColumnCalcs(table) {
  31. this.table = table; //hold Tabulator object
  32. this.topCalcs = [];
  33. this.botCalcs = [];
  34. this.genColumn = false;
  35. this.topElement = this.createElement();
  36. this.botElement = this.createElement();
  37. this.topRow = false;
  38. this.botRow = false;
  39. this.topInitialized = false;
  40. this.botInitialized = false;
  41. this.initialize();
  42. };
  43. ColumnCalcs.prototype.createElement = function () {
  44. var el = document.createElement("div");
  45. el.classList.add("tabulator-calcs-holder");
  46. return el;
  47. };
  48. ColumnCalcs.prototype.initialize = function () {
  49. this.genColumn = new Column({ field: "value" }, this);
  50. };
  51. //dummy functions to handle being mock column manager
  52. ColumnCalcs.prototype.registerColumnField = function () {};
  53. //initialize column calcs
  54. ColumnCalcs.prototype.initializeColumn = function (column) {
  55. var def = column.definition;
  56. var config = {
  57. topCalcParams: def.topCalcParams || {},
  58. botCalcParams: def.bottomCalcParams || {}
  59. };
  60. if (def.topCalc) {
  61. switch (_typeof(def.topCalc)) {
  62. case "string":
  63. if (this.calculations[def.topCalc]) {
  64. config.topCalc = this.calculations[def.topCalc];
  65. } else {
  66. console.warn("Column Calc Error - No such calculation found, ignoring: ", def.topCalc);
  67. }
  68. break;
  69. case "function":
  70. config.topCalc = def.topCalc;
  71. break;
  72. }
  73. if (config.topCalc) {
  74. column.modules.columnCalcs = config;
  75. this.topCalcs.push(column);
  76. if (this.table.options.columnCalcs != "group") {
  77. this.initializeTopRow();
  78. }
  79. }
  80. }
  81. if (def.bottomCalc) {
  82. switch (_typeof(def.bottomCalc)) {
  83. case "string":
  84. if (this.calculations[def.bottomCalc]) {
  85. config.botCalc = this.calculations[def.bottomCalc];
  86. } else {
  87. console.warn("Column Calc Error - No such calculation found, ignoring: ", def.bottomCalc);
  88. }
  89. break;
  90. case "function":
  91. config.botCalc = def.bottomCalc;
  92. break;
  93. }
  94. if (config.botCalc) {
  95. column.modules.columnCalcs = config;
  96. this.botCalcs.push(column);
  97. if (this.table.options.columnCalcs != "group") {
  98. this.initializeBottomRow();
  99. }
  100. }
  101. }
  102. };
  103. ColumnCalcs.prototype.removeCalcs = function () {
  104. var changed = false;
  105. if (this.topInitialized) {
  106. this.topInitialized = false;
  107. this.topElement.parentNode.removeChild(this.topElement);
  108. changed = true;
  109. }
  110. if (this.botInitialized) {
  111. this.botInitialized = false;
  112. this.table.footerManager.remove(this.botElement);
  113. changed = true;
  114. }
  115. if (changed) {
  116. this.table.rowManager.adjustTableSize();
  117. }
  118. };
  119. ColumnCalcs.prototype.initializeTopRow = function () {
  120. if (!this.topInitialized) {
  121. // this.table.columnManager.headersElement.after(this.topElement);
  122. this.table.columnManager.getElement().insertBefore(this.topElement, this.table.columnManager.headersElement.nextSibling);
  123. this.topInitialized = true;
  124. }
  125. };
  126. ColumnCalcs.prototype.initializeBottomRow = function () {
  127. if (!this.botInitialized) {
  128. this.table.footerManager.prepend(this.botElement);
  129. this.botInitialized = true;
  130. }
  131. };
  132. ColumnCalcs.prototype.scrollHorizontal = function (left) {
  133. var hozAdjust = 0,
  134. scrollWidth = this.table.columnManager.getElement().scrollWidth - this.table.element.clientWidth;
  135. if (this.botInitialized && this.botRow) {
  136. this.botRow.getElement().style.marginLeft = -left + "px";
  137. }
  138. };
  139. ColumnCalcs.prototype.recalc = function (rows) {
  140. var data, row;
  141. if (this.topInitialized || this.botInitialized) {
  142. data = this.rowsToData(rows);
  143. if (this.topInitialized) {
  144. if (this.topRow) {
  145. this.topRow.deleteCells();
  146. }
  147. row = this.generateRow("top", this.rowsToData(rows));
  148. this.topRow = row;
  149. while (this.topElement.firstChild) {
  150. this.topElement.removeChild(this.topElement.firstChild);
  151. }this.topElement.appendChild(row.getElement());
  152. row.initialize(true);
  153. }
  154. if (this.botInitialized) {
  155. if (this.botRow) {
  156. this.botRow.deleteCells();
  157. }
  158. row = this.generateRow("bottom", this.rowsToData(rows));
  159. this.botRow = row;
  160. while (this.botElement.firstChild) {
  161. this.botElement.removeChild(this.botElement.firstChild);
  162. }this.botElement.appendChild(row.getElement());
  163. row.initialize(true);
  164. }
  165. this.table.rowManager.adjustTableSize();
  166. //set resizable handles
  167. if (this.table.modExists("frozenColumns")) {
  168. this.table.modules.frozenColumns.layout();
  169. }
  170. }
  171. };
  172. ColumnCalcs.prototype.recalcRowGroup = function (row) {
  173. this.recalcGroup(this.table.modules.groupRows.getRowGroup(row));
  174. };
  175. ColumnCalcs.prototype.recalcAll = function () {
  176. var _this = this;
  177. if (this.topCalcs.length || this.botCalcs.length) {
  178. if (this.table.options.columnCalcs !== "group") {
  179. this.recalc(this.table.rowManager.activeRows);
  180. }
  181. if (this.table.options.groupBy && this.table.options.columnCalcs !== "table") {
  182. var groups = table.modules.groupRows.getChildGroups();
  183. groups.forEach(function (group) {
  184. _this.recalcGroup(group);
  185. });
  186. }
  187. }
  188. };
  189. ColumnCalcs.prototype.recalcGroup = function (group) {
  190. var data, rowData;
  191. if (group) {
  192. if (group.calcs) {
  193. if (group.calcs.bottom) {
  194. data = this.rowsToData(group.rows);
  195. rowData = this.generateRowData("bottom", data);
  196. group.calcs.bottom.updateData(rowData);
  197. group.calcs.bottom.reinitialize();
  198. }
  199. if (group.calcs.top) {
  200. data = this.rowsToData(group.rows);
  201. rowData = this.generateRowData("top", data);
  202. group.calcs.top.updateData(rowData);
  203. group.calcs.top.reinitialize();
  204. }
  205. }
  206. }
  207. };
  208. //generate top stats row
  209. ColumnCalcs.prototype.generateTopRow = function (rows) {
  210. return this.generateRow("top", this.rowsToData(rows));
  211. };
  212. //generate bottom stats row
  213. ColumnCalcs.prototype.generateBottomRow = function (rows) {
  214. return this.generateRow("bottom", this.rowsToData(rows));
  215. };
  216. ColumnCalcs.prototype.rowsToData = function (rows) {
  217. var _this2 = this;
  218. var data = [];
  219. rows.forEach(function (row) {
  220. data.push(row.getData());
  221. if (_this2.table.options.dataTree && _this2.table.options.dataTreeChildColumnCalcs) {
  222. if (row.modules.dataTree.open) {
  223. var children = _this2.rowsToData(_this2.table.modules.dataTree.getFilteredTreeChildren(row));
  224. data = data.concat(children);
  225. }
  226. }
  227. });
  228. return data;
  229. };
  230. //generate stats row
  231. ColumnCalcs.prototype.generateRow = function (pos, data) {
  232. var self = this,
  233. rowData = this.generateRowData(pos, data),
  234. row;
  235. if (self.table.modExists("mutator")) {
  236. self.table.modules.mutator.disable();
  237. }
  238. row = new Row(rowData, this, "calc");
  239. if (self.table.modExists("mutator")) {
  240. self.table.modules.mutator.enable();
  241. }
  242. row.getElement().classList.add("tabulator-calcs", "tabulator-calcs-" + pos);
  243. row.component = false;
  244. row.getComponent = function () {
  245. if (!this.component) {
  246. this.component = new CalcComponent(this);
  247. }
  248. return this.component;
  249. };
  250. row.generateCells = function () {
  251. var cells = [];
  252. self.table.columnManager.columnsByIndex.forEach(function (column) {
  253. //set field name of mock column
  254. self.genColumn.setField(column.getField());
  255. self.genColumn.hozAlign = column.hozAlign;
  256. if (column.definition[pos + "CalcFormatter"] && self.table.modExists("format")) {
  257. self.genColumn.modules.format = {
  258. formatter: self.table.modules.format.getFormatter(column.definition[pos + "CalcFormatter"]),
  259. params: column.definition[pos + "CalcFormatterParams"] || {}
  260. };
  261. } else {
  262. self.genColumn.modules.format = {
  263. formatter: self.table.modules.format.getFormatter("plaintext"),
  264. params: {}
  265. };
  266. }
  267. //ensure css class defintion is replicated to calculation cell
  268. self.genColumn.definition.cssClass = column.definition.cssClass;
  269. //generate cell and assign to correct column
  270. var cell = new Cell(self.genColumn, row);
  271. cell.getElement();
  272. cell.column = column;
  273. cell.setWidth();
  274. column.cells.push(cell);
  275. cells.push(cell);
  276. if (!column.visible) {
  277. cell.hide();
  278. }
  279. });
  280. this.cells = cells;
  281. };
  282. return row;
  283. };
  284. //generate stats row
  285. ColumnCalcs.prototype.generateRowData = function (pos, data) {
  286. var rowData = {},
  287. calcs = pos == "top" ? this.topCalcs : this.botCalcs,
  288. type = pos == "top" ? "topCalc" : "botCalc",
  289. params,
  290. paramKey;
  291. calcs.forEach(function (column) {
  292. var values = [];
  293. if (column.modules.columnCalcs && column.modules.columnCalcs[type]) {
  294. data.forEach(function (item) {
  295. values.push(column.getFieldValue(item));
  296. });
  297. paramKey = type + "Params";
  298. params = typeof column.modules.columnCalcs[paramKey] === "function" ? column.modules.columnCalcs[paramKey](values, data) : column.modules.columnCalcs[paramKey];
  299. column.setFieldValue(rowData, column.modules.columnCalcs[type](values, data, params));
  300. }
  301. });
  302. return rowData;
  303. };
  304. ColumnCalcs.prototype.hasTopCalcs = function () {
  305. return !!this.topCalcs.length;
  306. };
  307. ColumnCalcs.prototype.hasBottomCalcs = function () {
  308. return !!this.botCalcs.length;
  309. };
  310. //handle table redraw
  311. ColumnCalcs.prototype.redraw = function () {
  312. if (this.topRow) {
  313. this.topRow.normalizeHeight(true);
  314. }
  315. if (this.botRow) {
  316. this.botRow.normalizeHeight(true);
  317. }
  318. };
  319. //return the calculated
  320. ColumnCalcs.prototype.getResults = function () {
  321. var self = this,
  322. results = {},
  323. groups;
  324. if (this.table.options.groupBy && this.table.modExists("groupRows")) {
  325. groups = this.table.modules.groupRows.getGroups(true);
  326. groups.forEach(function (group) {
  327. results[group.getKey()] = self.getGroupResults(group);
  328. });
  329. } else {
  330. results = {
  331. top: this.topRow ? this.topRow.getData() : {},
  332. bottom: this.botRow ? this.botRow.getData() : {}
  333. };
  334. }
  335. return results;
  336. };
  337. //get results from a group
  338. ColumnCalcs.prototype.getGroupResults = function (group) {
  339. var self = this,
  340. groupObj = group._getSelf(),
  341. subGroups = group.getSubGroups(),
  342. subGroupResults = {},
  343. results = {};
  344. subGroups.forEach(function (subgroup) {
  345. subGroupResults[subgroup.getKey()] = self.getGroupResults(subgroup);
  346. });
  347. results = {
  348. top: groupObj.calcs.top ? groupObj.calcs.top.getData() : {},
  349. bottom: groupObj.calcs.bottom ? groupObj.calcs.bottom.getData() : {},
  350. groups: subGroupResults
  351. };
  352. return results;
  353. };
  354. //default calculations
  355. ColumnCalcs.prototype.calculations = {
  356. "avg": function avg(values, data, calcParams) {
  357. var output = 0,
  358. precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : 2;
  359. if (values.length) {
  360. output = values.reduce(function (sum, value) {
  361. value = Number(value);
  362. return sum + value;
  363. });
  364. output = output / values.length;
  365. output = precision !== false ? output.toFixed(precision) : output;
  366. }
  367. return parseFloat(output).toString();
  368. },
  369. "max": function max(values, data, calcParams) {
  370. var output = null,
  371. precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
  372. values.forEach(function (value) {
  373. value = Number(value);
  374. if (value > output || output === null) {
  375. output = value;
  376. }
  377. });
  378. return output !== null ? precision !== false ? output.toFixed(precision) : output : "";
  379. },
  380. "min": function min(values, data, calcParams) {
  381. var output = null,
  382. precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
  383. values.forEach(function (value) {
  384. value = Number(value);
  385. if (value < output || output === null) {
  386. output = value;
  387. }
  388. });
  389. return output !== null ? precision !== false ? output.toFixed(precision) : output : "";
  390. },
  391. "sum": function sum(values, data, calcParams) {
  392. var output = 0,
  393. precision = typeof calcParams.precision !== "undefined" ? calcParams.precision : false;
  394. if (values.length) {
  395. values.forEach(function (value) {
  396. value = Number(value);
  397. output += !isNaN(value) ? Number(value) : 0;
  398. });
  399. }
  400. return precision !== false ? output.toFixed(precision) : output;
  401. },
  402. "concat": function concat(values, data, calcParams) {
  403. var output = 0;
  404. if (values.length) {
  405. output = values.reduce(function (sum, value) {
  406. return String(sum) + String(value);
  407. });
  408. }
  409. return output;
  410. },
  411. "count": function count(values, data, calcParams) {
  412. var output = 0;
  413. if (values.length) {
  414. values.forEach(function (value) {
  415. if (value) {
  416. output++;
  417. }
  418. });
  419. }
  420. return output;
  421. }
  422. };
  423. Tabulator.prototype.registerModule("columnCalcs", ColumnCalcs);