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 12KB

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