otsdaq_utilities  v2_05_02_indev
accessor.js
1 var Accessor = function(table){
2  this.table = table; //hold Tabulator object
3  this.allowedTypes = ["", "data", "download", "clipboard"] //list of accessor types
4 };
5 
6 
7 //initialize column accessor
8 Accessor.prototype.initializeColumn = function(column){
9  var self = this,
10  match = false,
11  config = {};
12 
13  this.allowedTypes.forEach(function(type){
14  var key = "accessor" + (type.charAt(0).toUpperCase() + type.slice(1)),
15  accessor;
16 
17  if(column.definition[key]){
18  accessor = self.lookupAccessor(column.definition[key]);
19 
20  if(accessor){
21  match = true;
22 
23  config[key] = {
24  accessor:accessor,
25  params: column.definition[key + "Params"] || {},
26  }
27  }
28  }
29  });
30 
31  if(match){
32  column.modules.accessor = config;
33  }
34 },
35 
36 Accessor.prototype.lookupAccessor = function(value){
37  var accessor = false;
38 
39  //set column accessor
40  switch(typeof value){
41  case "string":
42  if(this.accessors[value]){
43  accessor = this.accessors[value]
44  }else{
45  console.warn("Accessor Error - No such accessor found, ignoring: ", value);
46  }
47  break;
48 
49  case "function":
50  accessor = value;
51  break;
52  }
53 
54  return accessor;
55 }
56 
57 
58 //apply accessor to row
59 Accessor.prototype.transformRow = function(dataIn, type){
60  var self = this,
61  key = "accessor" + (type.charAt(0).toUpperCase() + type.slice(1));
62 
63  //clone data object with deep copy to isolate internal data from returned result
64  var data = Tabulator.prototype.helpers.deepClone(dataIn || {});
65 
66  self.table.columnManager.traverse(function(column){
67  var value, accessor, params, component;
68 
69  if(column.modules.accessor){
70 
71  accessor = column.modules.accessor[key] || column.modules.accessor.accessor || false;
72 
73  if(accessor){
74  value = column.getFieldValue(data);
75 
76  if(value != "undefined"){
77  component = column.getComponent();
78  params = typeof accessor.params === "function" ? accessor.params(value, data, type, component) : accessor.params;
79  column.setFieldValue(data, accessor.accessor(value, data, type, params, component));
80  }
81  }
82  }
83  });
84 
85  return data;
86 },
87 
88 //default accessors
89 Accessor.prototype.accessors = {};
90 
91 
92 
93 Tabulator.prototype.registerModule("accessor", Accessor);