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; };
5 var DataTree =
function DataTree(table) {
9 this.collapseEl = null;
12 this.elementField =
false;
14 this.startOpen =
function () {};
16 this.displayIndex = 0;
19 DataTree.prototype.initialize =
function () {
21 firstCol = this.table.columnManager.getFirstVisibileColumn(),
22 options = this.table.options;
24 this.field = options.dataTreeChildField;
25 this.indent = options.dataTreeChildIndent;
26 this.elementField = options.dataTreeElementColumn || (firstCol ? firstCol.field :
false);
28 if (options.dataTreeBranchElement) {
30 if (options.dataTreeBranchElement ===
true) {
31 this.branchEl = document.createElement(
"div");
32 this.branchEl.classList.add(
"tabulator-data-tree-branch");
34 if (typeof options.dataTreeBranchElement ===
"string") {
35 dummyEl = document.createElement(
"div");
36 dummyEl.innerHTML = options.dataTreeBranchElement;
37 this.branchEl = dummyEl.firstChild;
39 this.branchEl = options.dataTreeBranchElement;
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;
50 this.collapseEl = options.dataTreeCollapseElement;
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>";
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;
65 this.expandEl = options.dataTreeExpandElement;
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>";
74 switch (_typeof(options.dataTreeStartExpanded)) {
76 this.startOpen =
function (row, index) {
77 return options.dataTreeStartExpanded;
82 this.startOpen = options.dataTreeStartExpanded;
86 this.startOpen =
function (row, index) {
87 return options.dataTreeStartExpanded[index];
93 DataTree.prototype.initializeRow =
function (row) {
94 var childArray = row.getData()[this.field];
95 var isArray = Array.isArray(childArray);
97 var children = isArray || !isArray && (typeof childArray ===
"undefined" ?
"undefined" : _typeof(childArray)) ===
"object" && childArray !== null;
99 row.modules.dataTree = {
101 open: children ? this.startOpen(row.getComponent(), 0) :
false,
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;
114 if (config.branchEl) {
115 config.branchEl.parentNode.removeChild(config.branchEl);
118 this.generateControlElement(row, el);
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";
126 el.style.paddingLeft = parseInt(window.getComputedStyle(el, null).getPropertyValue(
'padding-left')) + config.index *
this.indent +
"px";
131 DataTree.prototype.generateControlElement =
function (row, el) {
134 var config = row.modules.dataTree,
135 el = el || row.getCells()[0].getElement(),
136 oldControl = config.controlEl;
138 if (config.children !==
false) {
141 config.controlEl = this.collapseEl.cloneNode(
true);
142 config.controlEl.addEventListener(
"click",
function (e) {
144 _this.collapseRow(row);
147 config.controlEl = this.expandEl.cloneNode(
true);
148 config.controlEl.addEventListener(
"click",
function (e) {
150 _this.expandRow(row);
154 config.controlEl.addEventListener(
"mousedown",
function (e) {
158 if (oldControl && oldControl.parentNode === el) {
159 oldControl.parentNode.replaceChild(config.controlEl, oldControl);
161 el.insertBefore(config.controlEl, el.firstChild);
166 DataTree.prototype.setDisplayIndex =
function (index) {
167 this.displayIndex = index;
170 DataTree.prototype.getDisplayIndex =
function () {
171 return this.displayIndex;
174 DataTree.prototype.getRows =
function (rows) {
179 rows.forEach(
function (row, i) {
180 var config, children;
184 if (row instanceof Row) {
186 config = row.modules.dataTree.children;
188 if (!config.index && config.children !==
false) {
189 children = _this2.getChildren(row);
191 children.forEach(
function (child) {
201 DataTree.prototype.getChildren =
function (row) {
204 var config = row.modules.dataTree,
208 if (config.children !==
false && config.open) {
209 if (!Array.isArray(config.children)) {
210 config.children = this.generateChildren(row);
213 if (this.table.modExists(
"filter")) {
214 children = this.table.modules.filter.filter(config.children);
216 children = config.children;
219 if (this.table.modExists(
"sort")) {
220 this.table.modules.sort.sort(children);
223 children.forEach(
function (child) {
226 var subChildren = _this3.getChildren(child);
228 subChildren.forEach(
function (sub) {
237 DataTree.prototype.generateChildren =
function (row) {
242 var childArray = row.getData()[this.field];
244 if (!Array.isArray(childArray)) {
245 childArray = [childArray];
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);
255 children.push(childRow);
261 DataTree.prototype.expandRow =
function (row, silent) {
262 var config = row.modules.dataTree;
264 if (config.children !==
false) {
269 this.table.rowManager.refreshActiveData(
"tree",
false,
true);
271 this.table.options.dataTreeRowExpanded(row.getComponent(), row.modules.dataTree.index);
275 DataTree.prototype.collapseRow =
function (row) {
276 var config = row.modules.dataTree;
278 if (config.children !==
false) {
283 this.table.rowManager.refreshActiveData(
"tree",
false,
true);
285 this.table.options.dataTreeRowCollapsed(row.getComponent(), row.modules.dataTree.index);
289 DataTree.prototype.toggleRow =
function (row) {
290 var config = row.modules.dataTree;
292 if (config.children !==
false) {
294 this.collapseRow(row);
301 DataTree.prototype.getTreeParent =
function (row) {
302 return row.modules.dataTree.parent ? row.modules.dataTree.parent.getComponent() :
false;
305 DataTree.prototype.getTreeChildren =
function (row) {
306 var config = row.modules.dataTree,
309 if (config.children) {
311 if (!Array.isArray(config.children)) {
312 config.children = this.generateChildren(row);
315 config.children.forEach(
function (childRow) {
316 if (childRow instanceof Row) {
317 output.push(childRow.getComponent());
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();
333 DataTree.prototype.getChildField =
function () {
337 DataTree.prototype.redrawNeeded =
function (data) {
338 return (this.field ? typeof data[this.field] !==
"undefined" :
false) || (this.elementField ? typeof data[this.elementField] !==
"undefined" :
false);
341 Tabulator.prototype.registerModule(
"dataTree", DataTree);