otsdaq_utilities  v2_05_02_indev
html_table_import.js
1 var HtmlTableImport = function(table){
2  this.table = table; //hold Tabulator object
3  this.fieldIndex = [];
4  this.hasIndex = false;
5 };
6 
7 HtmlTableImport.prototype.parseTable = function(){
8  var self = this,
9  element = self.table.element,
10  options = self.table.options,
11  columns = options.columns,
12  headers = element.getElementsByTagName("th"),
13  rows = element.getElementsByTagName("tbody")[0],
14  data = [],
15  newTable;
16 
17  self.hasIndex = false;
18 
19  self.table.options.htmlImporting.call(this.table);
20 
21  rows = rows ? rows.getElementsByTagName("tr") : [];
22 
23  //check for tablator inline options
24  self._extractOptions(element, options);
25 
26  if(headers.length){
27  self._extractHeaders(headers, rows);
28  }else{
29  self._generateBlankHeaders(headers, rows);
30  }
31 
32 
33  //iterate through table rows and build data set
34  for(var index = 0; index < rows.length; index++){
35  var row = rows[index],
36  cells = row.getElementsByTagName("td"),
37  item = {};
38 
39  //create index if the dont exist in table
40  if(!self.hasIndex){
41  item[options.index] = index;
42  }
43 
44  for(var i = 0; i < cells.length; i++){
45  var cell = cells[i];
46  if(typeof this.fieldIndex[i] !== "undefined"){
47  item[this.fieldIndex[i]] = cell.innerHTML;
48  }
49  }
50 
51  //add row data to item
52  data.push(item);
53  }
54 
55  //create new element
56  var newElement = document.createElement("div");
57 
58  //transfer attributes to new element
59  var attributes = element.attributes;
60 
61  // loop through attributes and apply them on div
62 
63  for(var i in attributes){
64  if(typeof attributes[i] == "object"){
65  newElement.setAttribute(attributes[i].name, attributes[i].value);
66  }
67  }
68 
69  // replace table with div element
70  element.parentNode.replaceChild(newElement, element);
71 
72  options.data = data;
73 
74  self.table.options.htmlImported.call(this.table);
75 
76  // // newElement.tabulator(options);
77 
78  this.table.element = newElement;
79 };
80 
81 //extract tabulator attribute options
82 HtmlTableImport.prototype._extractOptions = function(element, options, defaultOptions){
83  var attributes = element.attributes;
84  var optionsArr = defaultOptions ? Object.assign([], defaultOptions) : Object.keys(options);
85  var optionsList = {};
86 
87  optionsArr.forEach(function(item){
88  optionsList[item.toLowerCase()] = item;
89  });
90 
91  for(var index in attributes){
92  var attrib = attributes[index];
93  var name;
94 
95  if(attrib && typeof attrib == "object" && attrib.name && attrib.name.indexOf("tabulator-") === 0){
96  name = attrib.name.replace("tabulator-", "");
97 
98  if(typeof optionsList[name] !== "undefined"){
99  options[optionsList[name]] = this._attribValue(attrib.value);
100  }
101  }
102  }
103 };
104 
105 //get value of attribute
106 HtmlTableImport.prototype._attribValue = function(value){
107  if(value === "true"){
108  return true;
109  }
110 
111  if(value === "false"){
112  return false;
113  }
114 
115  return value;
116 };
117 
118 //find column if it has already been defined
119 HtmlTableImport.prototype._findCol = function(title){
120  var match = this.table.options.columns.find(function(column){
121  return column.title === title;
122  });
123 
124  return match || false;
125 };
126 
127 //extract column from headers
128 HtmlTableImport.prototype._extractHeaders = function(headers, rows){
129  for(var index = 0; index < headers.length; index++){
130  var header = headers[index],
131  exists = false,
132  col = this._findCol(header.textContent),
133  width, attributes;
134 
135  if(col){
136  exists = true;
137  }else{
138  col = {title:header.textContent.trim()};
139  }
140 
141  if(!col.field) {
142  col.field = header.textContent.trim().toLowerCase().replace(" ", "_");
143  }
144 
145  width = header.getAttribute("width");
146 
147  if(width && !col.width) {
148  col.width = width;
149  }
150 
151  //check for tablator inline options
152  attributes = header.attributes;
153 
154  // //check for tablator inline options
155  this._extractOptions(header, col, Column.prototype.defaultOptionList);
156 
157  for(var i in attributes){
158  var attrib = attributes[i],
159  name;
160 
161  if(attrib && typeof attrib == "object" && attrib.name && attrib.name.indexOf("tabulator-") === 0){
162 
163  name = attrib.name.replace("tabulator-", "");
164 
165  col[name] = this._attribValue(attrib.value);
166  }
167  }
168 
169  this.fieldIndex[index] = col.field;
170 
171  if(col.field == this.table.options.index){
172  this.hasIndex = true;
173  }
174 
175  if(!exists){
176  this.table.options.columns.push(col);
177  }
178 
179  }
180 };
181 
182 //generate blank headers
183 HtmlTableImport.prototype._generateBlankHeaders = function(headers, rows){
184  for(var index = 0; index < headers.length; index++){
185  var header = headers[index],
186  col = {title:"", field:"col" + index};
187 
188  this.fieldIndex[index] = col.field;
189 
190  var width = header.getAttribute("width");
191 
192  if(width){
193  col.width = width;
194  }
195 
196  this.table.options.columns.push(col);
197  }
198 };
199 
200 Tabulator.prototype.registerModule("htmlTableImport", HtmlTableImport);