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