-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtreeTable.js
325 lines (270 loc) · 9.96 KB
/
treeTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'datatables.net'], function ($, dt) {
return factory(dt.$, window, document);
});
} else if (typeof exports === 'object') {
// CommonJS
module.exports = function (root, $) {
if (!root) {
root = window;
}
if (!$ || !$.fn.dataTable) {
$ = require('datatables.net')(root, $).$;
}
return factory($, root, root.document);
};
} else {
// Browser
factory(jQuery, window, document);
}
}(function ($) {
function compareObjectDesc(a, b) {
if (!a || !b) {
return 0
}
if (a.tt_key !== b.tt_key) {
return ((a.value < b.value) ? 1 : ((a.value > b.value) ? -1 : 0));
} else if (typeof a.child === 'undefined' && typeof b.child === 'undefined') {
return ((a.value < b.value) ? 1 : ((a.value > b.value) ? -1 : 0));
} else if (typeof a.child !== 'undefined' && typeof b.child !== 'undefined') {
return compareObjectDesc(a.child, b.child);
} else {
return typeof a.child !== 'undefined' ? 1 : -1;
}
}
function compareObjectAsc(a, b) {
if (!a || !b) {
return 0
}
if (a.tt_key !== b.tt_key) {
return ((a.value < b.value) ? -1 : ((a.value > b.value) ? 1 : 0));
} else if (typeof a.child === 'undefined' && typeof b.child === 'undefined') {
return ((a.value < b.value) ? -1 : ((a.value > b.value) ? 1 : 0));
} else if (typeof a.child !== 'undefined' && typeof b.child !== 'undefined') {
return compareObjectAsc(a.child, b.child);
} else {
return typeof a.child !== 'undefined' ? 1 : -1;
}
}
function level(self, key) {
if (key === 0) {
return 1
}
const parentKey = self.dataDict[key].tt_parent;
return 1 + level(self, parentKey);
}
function hasParent(self, key, parentRegex) {
const rowData = self.dataDict[key];
const p = rowData['tt_parent'];
if (p === 0) return false;
if (parentRegex.test(p.toString())) return true;
return hasParent(self, p, parentRegex);
}
function buildOrderObject(self, key, column) {
const rowData = self.dataDict[key];
const parentKey = rowData['tt_parent'];
let parent = {};
if (parentKey > 0) {
parent = buildOrderObject(self, parentKey, column);
}
let a = parent;
while (typeof a.child !== 'undefined') {
a = a.child;
}
a.child = {};
a.child.tt_key = rowData['tt_key'];
a.child.value = rowData[column];
return parent;
}
function buildSearchObject(self, key, col, data) {
const children = self.dataDict[key].children;
return [data].concat(children.map((c) => {
return buildSearchObject(self, c.tt_key, col, c[col])
}));
}
if (!$.fn.dataTable) throw new Error('treeTable requires datatables.net');
$.fn.dataTableExt.oSort['tt-asc'] = function (a, b) {
return compareObjectAsc(a, b);
};
$.fn.dataTableExt.oSort['tt-desc'] = function (a, b) {
return compareObjectDesc(a, b);
};
function createDataDict(data) {
const children = data.reduce(function (map, obj) {
if (obj.tt_parent) {
if (!map[obj.tt_parent]) {
map[obj.tt_parent] = [];
}
map[obj.tt_parent].push(obj);
}
return map;
}, {});
return data.reduce(function (map, obj) {
obj.children = children[obj.tt_key] || [];
obj.hasChild = obj.children.length > 0;
map[obj.tt_key] = obj;
return map;
}, {});
}
const TreeTable = function (element, options) {
const self = this;
this.$el = $(element);
if (options.data) {
this.init(options);
} else if (options.ajax) {
// here create a dummy DataTable using the provided ajax source so that we can use
// DataTables internal logic to retrieve json and handle any ajax errors
this.$dummy = $("<table></table>");
this.$dummyWrapper = $('<div id="dummy-wrapper" style="display:none"></div>');
$("body").append(this.$dummyWrapper);
this.$dummyWrapper.append(this.$dummy);
this.$dummy.DataTable({ajax: options.ajax, columns: options.columns});
this.$dummy.on('xhr.dt', function (e, settings, json, xhr) {
self.$dummy.DataTable().destroy();
self.$dummy.parent().remove();
if (json != null) {
// calling this internal method retrieves data from the json in accordance with
// provided DataTable ajax options - https://datatables.net/reference/option/ajax
options.data = $.fn.dataTableExt.internal._fnAjaxDataSrc(settings, json);
options.ajax = null;
self.init(options);
}
});
}
};
TreeTable.prototype.init = function (options) {
const self = this;
this.collapsed = new Set([]);
this.data = options.data;
this.dataDict = createDataDict(options.data);
this.displayedRows = [];
this.dt = null;
const initialOrder = options.order;
options.order = [];
options.columns = options.columns || [];
options.columns.map((col) => {
const oldRender = col.render;
col.render = function (data, type, full, meta) {
switch (type) {
case "sort":
return buildOrderObject(self, full["tt_key"], col["data"]).child;
case "filter":
return buildSearchObject(self, full['tt_key'], col["data"], data);
default:
return oldRender ? oldRender(data, type, full, meta) : data;
}
};
col.type = "tt";
});
options.rowId = "tt_key";
this.$el.find("thead tr").prepend("<th></th>");
options.columns = [{
"class": "tt-details-control",
"orderable": false,
"data": null,
"defaultContent": "<div class='expander'></div>",
"width": 50
}].concat(options.columns).concat([
{
"data": "tt_key",
"visible": false
},
{
"data": "tt_parent",
"visible": false
}
]);
options.createdRow = function (row, data) {
let cssClass = "";
if (self.dataDict[data.tt_key].hasChild) {
cssClass += " has-child ";
}
if (data.tt_parent > 0) {
cssClass += " has-parent";
}
cssClass += " level-" + (level(self, data.tt_key) - 2);
$(row).addClass(cssClass);
};
this.dt = this.$el.DataTable(options);
if (initialOrder) {
this.dt.order(initialOrder);
}
if (options.collapsed) {
this.collapseAllRows();
} else {
this.expandAllRows();
}
this.$el.find('tbody').on('click', 'tr.has-child td.tt-details-control', function () {
self.toggleChildRows($(this).parent("tr"))
});
this.redraw();
};
TreeTable.prototype.toggleChildRows = function ($tr) {
const row = this.dt.row($tr);
const key = row.data().tt_key;
if (this.collapsed.has(key)) {
this.collapsed.delete(key);
$tr.addClass('open');
} else {
this.collapsed.add(key);
$tr.removeClass('open');
}
this.redraw();
};
TreeTable.prototype.collapseAllRows = function () {
this.data.map((d) => {
if (this.dataDict[d.tt_key].hasChild) {
this.collapsed.add(d.tt_key);
}
});
this.$el.find("tbody tr.has-child").removeClass("open");
return this
};
TreeTable.prototype.expandAllRows = function () {
this.collapsed = new Set([]);
this.$el.one('draw.dt', () => {
this.$el.find("tbody tr.has-child").addClass("open");
});
return this
};
TreeTable.prototype.redraw = function () {
$.fn.dataTable.ext.search = $.fn.dataTable.ext.search.filter((it, i) => it.name !== "ttExpanded");
if (this.collapsed.size === 0) {
this.displayedRows = this.dt.rows().eq(0);
this.dt.draw();
return
}
let regex = "^(0";
this.collapsed.forEach(function (value) {
regex = regex + "|" + value;
});
regex = regex + ")$";
const parentRegex = new RegExp(regex);
this.displayedRows = this.dt.rows((idx, data) => {
return !hasParent(this, data["tt_key"], parentRegex)
}).eq(0);
const self = this;
const ttExpanded = function (settings, data, dataIndex) {
return self.displayedRows.indexOf(dataIndex) > -1
};
$.fn.dataTable.ext.search.push(ttExpanded);
this.dt.draw();
};
TreeTable.DEFAULTS = {};
const old = $.fn.treeTable;
$.fn.treeTable = function (option) {
return this.each(function () {
const $this = $(this);
let data = $this.data('treeTable');
const options = $.extend({}, TreeTable.DEFAULTS, typeof option === 'object' && option);
if (!data) $this.data('treeTable', (data = new TreeTable(this, options)));
});
};
$.fn.treeTable.Constructor = TreeTable;
$.fn.treeTable.noConflict = function () {
$.fn.treeTable = old;
return this;
};
}));