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