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