otsdaq_utilities  v2_05_02_indev
mutator.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 Mutator = function Mutator(table) {
6  this.table = table; //hold Tabulator object
7  this.allowedTypes = ["", "data", "edit", "clipboard"]; //list of muatation types
8  this.enabled = true;
9 };
10 
11 //initialize column mutator
12 Mutator.prototype.initializeColumn = function (column) {
13  var self = this,
14  match = false,
15  config = {};
16 
17  this.allowedTypes.forEach(function (type) {
18  var key = "mutator" + (type.charAt(0).toUpperCase() + type.slice(1)),
19  mutator;
20 
21  if (column.definition[key]) {
22  mutator = self.lookupMutator(column.definition[key]);
23 
24  if (mutator) {
25  match = true;
26 
27  config[key] = {
28  mutator: mutator,
29  params: column.definition[key + "Params"] || {}
30  };
31  }
32  }
33  });
34 
35  if (match) {
36  column.modules.mutate = config;
37  }
38 };
39 
40 Mutator.prototype.lookupMutator = function (value) {
41  var mutator = false;
42 
43  //set column mutator
44  switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
45  case "string":
46  if (this.mutators[value]) {
47  mutator = this.mutators[value];
48  } else {
49  console.warn("Mutator Error - No such mutator found, ignoring: ", value);
50  }
51  break;
52 
53  case "function":
54  mutator = value;
55  break;
56  }
57 
58  return mutator;
59 };
60 
61 //apply mutator to row
62 Mutator.prototype.transformRow = function (data, type, updatedData) {
63  var self = this,
64  key = "mutator" + (type.charAt(0).toUpperCase() + type.slice(1)),
65  value;
66 
67  if (this.enabled) {
68 
69  self.table.columnManager.traverse(function (column) {
70  var mutator, params, component;
71 
72  if (column.modules.mutate) {
73  mutator = column.modules.mutate[key] || column.modules.mutate.mutator || false;
74 
75  if (mutator && updatedData) {
76  value = column.getFieldValue(updatedData);
77 
78  if (type == "data" || typeof value !== "undefined") {
79  component = column.getComponent();
80  params = typeof mutator.params === "function" ? mutator.params(value, data, type, component) : mutator.params;
81  column.setFieldValue(data, mutator.mutator(value, data, type, params, component));
82  }
83  }
84  }
85  });
86  }
87 
88  return data;
89 };
90 
91 //apply mutator to new cell value
92 Mutator.prototype.transformCell = function (cell, value) {
93  var mutator = cell.column.modules.mutate.mutatorEdit || cell.column.modules.mutate.mutator || false,
94  tempData = {};
95 
96  if (mutator) {
97  tempData = Object.assign(tempData, cell.row.getData());
98  cell.column.setFieldValue(tempData, value);
99  return mutator.mutator(value, tempData, "edit", mutator.params, cell.getComponent());
100  } else {
101  return value;
102  }
103 };
104 
105 Mutator.prototype.enable = function () {
106  this.enabled = true;
107 };
108 
109 Mutator.prototype.disable = function () {
110  this.enabled = false;
111 };
112 
113 //default mutators
114 Mutator.prototype.mutators = {};
115 
116 Tabulator.prototype.registerModule("mutator", Mutator);