otsdaq_utilities  v2_05_02_indev
data_tree.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 DataTree = function DataTree(table) {
6  this.table = table;
7  this.indent = 10;
8  this.field = "";
9  this.collapseEl = null;
10  this.expandEl = null;
11  this.branchEl = null;
12  this.elementField = false;
13 
14  this.startOpen = function () {};
15 
16  this.displayIndex = 0;
17 };
18 
19 DataTree.prototype.initialize = function () {
20  var dummyEl = null,
21  firstCol = this.table.columnManager.getFirstVisibileColumn(),
22  options = this.table.options;
23 
24  this.field = options.dataTreeChildField;
25  this.indent = options.dataTreeChildIndent;
26  this.elementField = options.dataTreeElementColumn || (firstCol ? firstCol.field : false);
27 
28  if (options.dataTreeBranchElement) {
29 
30  if (options.dataTreeBranchElement === true) {
31  this.branchEl = document.createElement("div");
32  this.branchEl.classList.add("tabulator-data-tree-branch");
33  } else {
34  if (typeof options.dataTreeBranchElement === "string") {
35  dummyEl = document.createElement("div");
36  dummyEl.innerHTML = options.dataTreeBranchElement;
37  this.branchEl = dummyEl.firstChild;
38  } else {
39  this.branchEl = options.dataTreeBranchElement;
40  }
41  }
42  }
43 
44  if (options.dataTreeCollapseElement) {
45  if (typeof options.dataTreeCollapseElement === "string") {
46  dummyEl = document.createElement("div");
47  dummyEl.innerHTML = options.dataTreeCollapseElement;
48  this.collapseEl = dummyEl.firstChild;
49  } else {
50  this.collapseEl = options.dataTreeCollapseElement;
51  }
52  } else {
53  this.collapseEl = document.createElement("div");
54  this.collapseEl.classList.add("tabulator-data-tree-control");
55  this.collapseEl.tabIndex = 0;
56  this.collapseEl.innerHTML = "<div class='tabulator-data-tree-control-collapse'></div>";
57  }
58 
59  if (options.dataTreeExpandElement) {
60  if (typeof options.dataTreeExpandElement === "string") {
61  dummyEl = document.createElement("div");
62  dummyEl.innerHTML = options.dataTreeExpandElement;
63  this.expandEl = dummyEl.firstChild;
64  } else {
65  this.expandEl = options.dataTreeExpandElement;
66  }
67  } else {
68  this.expandEl = document.createElement("div");
69  this.expandEl.classList.add("tabulator-data-tree-control");
70  this.expandEl.tabIndex = 0;
71  this.expandEl.innerHTML = "<div class='tabulator-data-tree-control-expand'></div>";
72  }
73 
74  switch (_typeof(options.dataTreeStartExpanded)) {
75  case "boolean":
76  this.startOpen = function (row, index) {
77  return options.dataTreeStartExpanded;
78  };
79  break;
80 
81  case "function":
82  this.startOpen = options.dataTreeStartExpanded;
83  break;
84 
85  default:
86  this.startOpen = function (row, index) {
87  return options.dataTreeStartExpanded[index];
88  };
89  break;
90  }
91 };
92 
93 DataTree.prototype.initializeRow = function (row) {
94  var childArray = row.getData()[this.field];
95  var isArray = Array.isArray(childArray);
96 
97  var children = isArray || !isArray && (typeof childArray === "undefined" ? "undefined" : _typeof(childArray)) === "object" && childArray !== null;
98 
99  row.modules.dataTree = {
100  index: 0,
101  open: children ? this.startOpen(row.getComponent(), 0) : false,
102  controlEl: false,
103  branchEl: false,
104  parent: false,
105  children: children
106  };
107 };
108 
109 DataTree.prototype.layoutRow = function (row) {
110  var cell = this.elementField ? row.getCell(this.elementField) : row.getCells()[0],
111  el = cell.getElement(),
112  config = row.modules.dataTree;
113 
114  if (config.branchEl) {
115  config.branchEl.parentNode.removeChild(config.branchEl);
116  }
117 
118  this.generateControlElement(row, el);
119 
120  if (config.index) {
121  if (this.branchEl) {
122  config.branchEl = this.branchEl.cloneNode(true);
123  el.insertBefore(config.branchEl, el.firstChild);
124  config.branchEl.style.marginLeft = (config.branchEl.offsetWidth + config.branchEl.style.marginRight) * (config.index - 1) + config.index * this.indent + "px";
125  } else {
126  el.style.paddingLeft = parseInt(window.getComputedStyle(el, null).getPropertyValue('padding-left')) + config.index * this.indent + "px";
127  }
128  }
129 };
130 
131 DataTree.prototype.generateControlElement = function (row, el) {
132  var _this = this;
133 
134  var config = row.modules.dataTree,
135  el = el || row.getCells()[0].getElement(),
136  oldControl = config.controlEl;
137 
138  if (config.children !== false) {
139 
140  if (config.open) {
141  config.controlEl = this.collapseEl.cloneNode(true);
142  config.controlEl.addEventListener("click", function (e) {
143  e.stopPropagation();
144  _this.collapseRow(row);
145  });
146  } else {
147  config.controlEl = this.expandEl.cloneNode(true);
148  config.controlEl.addEventListener("click", function (e) {
149  e.stopPropagation();
150  _this.expandRow(row);
151  });
152  }
153 
154  config.controlEl.addEventListener("mousedown", function (e) {
155  e.stopPropagation();
156  });
157 
158  if (oldControl && oldControl.parentNode === el) {
159  oldControl.parentNode.replaceChild(config.controlEl, oldControl);
160  } else {
161  el.insertBefore(config.controlEl, el.firstChild);
162  }
163  }
164 };
165 
166 DataTree.prototype.setDisplayIndex = function (index) {
167  this.displayIndex = index;
168 };
169 
170 DataTree.prototype.getDisplayIndex = function () {
171  return this.displayIndex;
172 };
173 
174 DataTree.prototype.getRows = function (rows) {
175  var _this2 = this;
176 
177  var output = [];
178 
179  rows.forEach(function (row, i) {
180  var config, children;
181 
182  output.push(row);
183 
184  if (row instanceof Row) {
185 
186  config = row.modules.dataTree.children;
187 
188  if (!config.index && config.children !== false) {
189  children = _this2.getChildren(row);
190 
191  children.forEach(function (child) {
192  output.push(child);
193  });
194  }
195  }
196  });
197 
198  return output;
199 };
200 
201 DataTree.prototype.getChildren = function (row) {
202  var _this3 = this;
203 
204  var config = row.modules.dataTree,
205  children = [],
206  output = [];
207 
208  if (config.children !== false && config.open) {
209  if (!Array.isArray(config.children)) {
210  config.children = this.generateChildren(row);
211  }
212 
213  if (this.table.modExists("filter")) {
214  children = this.table.modules.filter.filter(config.children);
215  } else {
216  children = config.children;
217  }
218 
219  if (this.table.modExists("sort")) {
220  this.table.modules.sort.sort(children);
221  }
222 
223  children.forEach(function (child) {
224  output.push(child);
225 
226  var subChildren = _this3.getChildren(child);
227 
228  subChildren.forEach(function (sub) {
229  output.push(sub);
230  });
231  });
232  }
233 
234  return output;
235 };
236 
237 DataTree.prototype.generateChildren = function (row) {
238  var _this4 = this;
239 
240  var children = [];
241 
242  var childArray = row.getData()[this.field];
243 
244  if (!Array.isArray(childArray)) {
245  childArray = [childArray];
246  }
247 
248  childArray.forEach(function (childData) {
249  var childRow = new Row(childData || {}, _this4.table.rowManager);
250  childRow.modules.dataTree.index = row.modules.dataTree.index + 1;
251  childRow.modules.dataTree.parent = row;
252  if (childRow.modules.dataTree.children) {
253  childRow.modules.dataTree.open = _this4.startOpen(childRow.getComponent(), childRow.modules.dataTree.index);
254  }
255  children.push(childRow);
256  });
257 
258  return children;
259 };
260 
261 DataTree.prototype.expandRow = function (row, silent) {
262  var config = row.modules.dataTree;
263 
264  if (config.children !== false) {
265  config.open = true;
266 
267  row.reinitialize();
268 
269  this.table.rowManager.refreshActiveData("tree", false, true);
270 
271  this.table.options.dataTreeRowExpanded(row.getComponent(), row.modules.dataTree.index);
272  }
273 };
274 
275 DataTree.prototype.collapseRow = function (row) {
276  var config = row.modules.dataTree;
277 
278  if (config.children !== false) {
279  config.open = false;
280 
281  row.reinitialize();
282 
283  this.table.rowManager.refreshActiveData("tree", false, true);
284 
285  this.table.options.dataTreeRowCollapsed(row.getComponent(), row.modules.dataTree.index);
286  }
287 };
288 
289 DataTree.prototype.toggleRow = function (row) {
290  var config = row.modules.dataTree;
291 
292  if (config.children !== false) {
293  if (config.open) {
294  this.collapseRow(row);
295  } else {
296  this.expandRow(row);
297  }
298  }
299 };
300 
301 DataTree.prototype.getTreeParent = function (row) {
302  return row.modules.dataTree.parent ? row.modules.dataTree.parent.getComponent() : false;
303 };
304 
305 DataTree.prototype.getTreeChildren = function (row) {
306  var config = row.modules.dataTree,
307  output = [];
308 
309  if (config.children) {
310 
311  if (!Array.isArray(config.children)) {
312  config.children = this.generateChildren(row);
313  }
314 
315  config.children.forEach(function (childRow) {
316  if (childRow instanceof Row) {
317  output.push(childRow.getComponent());
318  }
319  });
320  }
321 
322  return output;
323 };
324 
325 DataTree.prototype.checkForRestyle = function (cell) {
326  if (!cell.row.cells.indexOf(cell)) {
327  if (cell.row.modules.dataTree.children !== false) {
328  cell.row.reinitialize();
329  }
330  }
331 };
332 
333 DataTree.prototype.getChildField = function () {
334  return this.field;
335 };
336 
337 DataTree.prototype.redrawNeeded = function (data) {
338  return (this.field ? typeof data[this.field] !== "undefined" : false) || (this.elementField ? typeof data[this.elementField] !== "undefined" : false);
339 };
340 
341 Tabulator.prototype.registerModule("dataTree", DataTree);