-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoldable-table.js
More file actions
164 lines (134 loc) · 4.23 KB
/
foldable-table.js
File metadata and controls
164 lines (134 loc) · 4.23 KB
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
(function () {
const addLeaf = (tree, item) => {
const { path, id } = item;
// console.log(id);
let o = tree;
const lastIndex = path.length - 1;
$.each(path, (i, token) => {
if (o[token] === undefined) {
const prefixPath = path.slice(0, i + 1);
const id = prefixPath.join('-');
o[token] = { branches: {}, path: prefixPath, id };
}
o = i === lastIndex ? o[token] : o[token].branches;
});
Object.assign(o, item);
};
const generateTree = (data) => {
const tree = { branches: {}, path: [] };
$.each(data, (i, item) => {
addLeaf(tree.branches, item);
});
return tree;
};
const parsePath = (path) => path.split('-');
const expandPaths = (data) => $.map(data, (item) => {
item.path = parsePath(item.id);
return item;
});
const hasBranches = (obj) => Boolean(obj.branches && Object.keys(obj.branches).length > 0);
const createBranch = ({ root, formatters, title }) => {
const { data, path, url, id } = root;
const pathStr = path.join('-');
const $self = $('<div>')
.addClass('branch')
.addClass('folded')
.toggleClass('has-branches', hasBranches(root))
const $row = $('<div>').addClass('row').appendTo($self);
const $main = $('<span>')
.addClass('main')
.appendTo($row);
const $fold = $('<a>')
.addClass('toggle-fold')
.attr('href', '#fold,' + id)
.appendTo($main)
.click(() => {
$self.toggleClass('folded');
return false;
});
if (hasBranches(root)) {
$('<span class="fold" />').text('-').appendTo($fold);
$('<span class="unfold" />').text('+').appendTo($fold);
}
const idText = id + (hasBranches(root) ? '-*' : '');
const label = title || (id && idText) || "";
const $head = $('<a class="link" />').attr('href', url).text(label).appendTo($main);
if (data) {
const formattedData = $.map(data, (v, i) => {
const fun = formatters[i];
return fun ? fun(v) : v;
});
const $cells = $.map(formattedData, (content, key) => $('<span>').text(content).addClass('col').addClass('col-' + key));
$row.append($cells)
}
$.each(root.branches, (key, branch) => {
createBranch({ root: branch, formatters }).appendTo($self);
});
return $self;
};
const renderHeader = (data) => {
const $row = $('<div>').addClass('header row');
const $main = $('<span>')
.addClass('main')
.appendTo($row);
if (data) {
const $cells = $.map(data, (content, key) => $('<span>').text(content).addClass('col-' + key).addClass('col'));
$row.append($cells)
}
return $row;
};
const renderTable = ({ root, formatters, title, header, icons }) => {
const $self = $('<div />').addClass('foldable-table');
if (icons)
$self.addClass("with-icons");
if (header) {
renderHeader(header).appendTo($self);
}
createBranch({ root, formatters, title }).appendTo($self).removeClass('folded')
return $self;
};
const fixAnonymousLeaves = (branch) => {
const { path, branches, data, id, url } = branch;
if (hasBranches(branch) && data) {
branches['_'] = { path, id, data, url };
delete branch.data;
delete branch.url;
}
$.each(branch.branches, (i, b) => fixAnonymousLeaves(b));
return branch;
};
const addAggregatedData = (branch) => {
const { path, branches, id } = branch;
if (hasBranches(branch)) {
let data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let maxIndex = 0;
$.each(branches, (i, b) => {
if (!b.data) {
addAggregatedData(b);
}
$.each(b.data, (i, item) => {
data[i] += item;
maxIndex = i > maxIndex ? i : maxIndex;
});
});
branch.data = data.slice(0, maxIndex + 1);
}
return branch;
};
$.fn.foldableTable = function ({ data, formatters, headers, icons = false }) {
return this.each(function () {
const $this = $(this);
const title = $this.attr('title');
$this.attr("title", "");
const tree = addAggregatedData(fixAnonymousLeaves(generateTree(expandPaths(data))));
const $table = renderTable({ root: tree, formatters, title, header: headers, icons });
$this.append($table);
const controller = {
expandAll: () => $table.find('.branch').removeClass('folded'),
collapseAll: () => $table.find('.branch').addClass('folded')
};
$this.data('foldableTable', controller);
return this;
})
};
}());