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
|
'use strict';
'require view';
'require poll';
'require ui';
'require network';
'require view.dashboard.lib.widgets as widgets';
document.querySelector('head').appendChild(E('link', {
'rel': 'stylesheet',
'type': 'text/css',
'href': L.resource('view/dashboard/css/custom.css') + (L.env.resource_version ? '?v=' + L.env.resource_version : '')
}));
const ROWS = {
cards: { max: 5, shortFirst: false },
charts: { max: 3, shortFirst: true }
};
function invokeIncludesLoad(includes, layout) {
const tasks = [];
let has_load = false;
for (let i = 0; i < includes.length; i++) {
const declared = includes[i].widgets || [];
includes[i].failed = false;
includes[i].skipped = (declared.length > 0 && !declared.some(widget => layout[widget.slot]?.includes(widget.id)));
if (includes[i].skipped) {
tasks.push(null);
}
else if (typeof(includes[i].load) == 'function') {
tasks.push(includes[i].load().catch(function(e) {
console.error(e);
this.failed = true;
}.bind(includes[i])));
has_load = true;
}
else {
tasks.push(null);
}
}
return has_load ? Promise.all(tasks) : Promise.resolve(null);
}
function collectSections(includes, results, layout) {
const sections = { cards: [], charts: [], tabs: [], extra: [] };
const found = { cards: {}, charts: {}, tabs: {} };
for (let i = 0; i < includes.length; i++) {
let content = null;
if (includes[i].failed || includes[i].skipped)
continue;
try {
if (typeof(includes[i].render) == 'function')
content = includes[i].render(results ? results[i] : null);
else if (includes[i].content != null)
content = includes[i].content;
if (content == null)
continue;
if (content instanceof Node || Array.isArray(content)) {
sections.extra.push(content);
continue;
}
// Items may defer building their node or tab content until selected.
// Includes without widget declarations keep their original behavior.
for (const slot of widgets.slots)
(content[slot] || (slot == 'cards' ? content.kpi : null) || []).forEach(item => {
if (item instanceof Node)
sections[slot].push(item);
else if (includes[i].widgets == null)
sections[slot].push((slot == 'tabs') ? item : item.node);
else if (layout[slot].includes(item.id)) {
const value = (slot == 'tabs') ? item.content : item.node;
const node = (typeof(value) == 'function') ? value() : value;
found[slot][item.id] = (slot == 'tabs') ? Object.assign({}, item, { content: node }) : node;
}
});
}
catch (e) {
console.error(e);
continue;
}
}
for (const slot of widgets.slots)
sections[slot] = layout[slot].map(id => found[slot][id]).filter(item => item != null).concat(sections[slot]);
return sections;
}
// Split n items into rows of at most max items, as evenly as possible.
function rowSizes(n, max, shortFirst) {
const rows = Math.ceil(n / max);
const base = Math.floor(n / rows);
const extra = n % rows;
const sizes = [];
for (let i = 0; i < rows; i++)
sizes.push(base + ((shortFirst ? i >= rows - extra : i < extra) ? 1 : 0));
return sizes;
}
function fillRows(nodes, slot) {
let i = 0;
rowSizes(nodes.length, ROWS[slot].max, ROWS[slot].shortFirst).forEach(size => {
for (let n = 0; n < size; n++)
nodes[i++].style.setProperty('--dashboard-per-row', size);
});
return nodes;
}
function updateShell(root, sections, containers) {
const desired = [
sections.cards.length ? containers.cards : null,
sections.charts.length ? containers.charts : null,
sections.tabs.length ? containers.tabs : null,
sections.extra.length ? containers.extra : null
].filter(Boolean);
desired.forEach((node, i) => {
if (root.children[i] !== node)
root.insertBefore(node, root.children[i] || null);
});
while (root.children.length > desired.length)
root.removeChild(root.lastChild);
}
function updateCharts(container, chartNodes) {
const scrollPositions = {};
container.querySelectorAll('[data-chart]').forEach(el => {
if (el.dataset.chart)
scrollPositions[el.dataset.chart] = el.scrollLeft;
});
L.dom.content(container, fillRows(chartNodes, 'charts'));
container.querySelectorAll('[data-chart]').forEach(el => {
if (el.dataset.chart && scrollPositions[el.dataset.chart] != null)
el.scrollLeft = scrollPositions[el.dataset.chart];
});
}
function updateTabs(tabMap, tabGroup, items, form) {
const menu = tabMap.querySelector('.cbi-tabmenu');
const existingPanes = Array.from(tabGroup.children);
const existingIds = existingPanes.map(pane => pane.dataset.tab);
const itemIds = items.map(tab => tab.id);
const changed = !tabGroup.hasAttribute('data-initialized') ||
existingIds.length !== itemIds.length ||
existingIds.some((id, i) => id !== itemIds[i]);
if (changed) {
if (menu)
menu.remove();
tabGroup.removeAttribute('data-initialized');
const activePane = existingPanes.find(p => p.getAttribute('data-tab-active') === 'true');
const activeId = activePane ? activePane.dataset.tab : null;
while (tabGroup.firstChild)
tabGroup.removeChild(tabGroup.firstChild);
items.forEach(tab => {
const pane = (tab.id === 'layout' && form) ? form : E('div', {
'class': 'cbi-section',
'data-tab': tab.id,
'data-tab-title': (tab.count != null) ? '%s (%d)'.format(tab.title, tab.count) : tab.title
}, (typeof(tab.content) == 'function') ? tab.content() : tab.content);
if (tab.id === activeId)
pane.setAttribute('data-tab-active', 'true');
tabGroup.appendChild(pane);
});
ui.tabs.initTabGroup(tabGroup.childNodes);
}
else {
items.forEach(tab => {
const pane = tabGroup.querySelector(':scope > [data-tab="' + tab.id + '"]');
if (!pane)
return;
const newTitle = (tab.count != null) ? '%s (%d)'.format(tab.title, tab.count) : tab.title;
if (pane.dataset.tabTitle !== newTitle) {
pane.dataset.tabTitle = newTitle;
const link = menu ? menu.querySelector('li[data-tab="' + tab.id + '"] > a') : null;
if (link && link.textContent !== newTitle)
link.textContent = newTitle;
}
if (tab.id === 'layout')
return;
const contentNode = (typeof(tab.content) == 'function') ? tab.content() : tab.content;
if (contentNode) {
if (pane.childNodes.length !== 1 || pane.firstChild !== contentNode)
L.dom.content(pane, contentNode);
}
});
}
}
function startPolling(includes, layout, root, form) {
let loading = null;
const showForm = () => {
loading ??= L.require('view.dashboard.lib.layout')
.then(layoutForm => layoutForm.render(includes))
.then(node => L.dom.content(form, node));
};
if (form)
form.addEventListener('cbi-tab-active', showForm);
const cardsContainer = E('div', { 'class': 'dashboard-kpi-row' });
const chartsContainer = E('div', { 'class': 'dashboard-charts' });
const tabGroup = E('div', { 'class': 'cbi-map-tabbed' });
const tabMap = E('div', { 'class': 'cbi-map' }, [ tabGroup ]);
const extraContainer = E('div', {});
const containers = {
cards: cardsContainer,
charts: chartsContainer,
tabs: tabMap,
extra: extraContainer
};
const step = () => {
return network.flushCache().then(() => {
return invokeIncludesLoad(includes, layout);
}).then(results => {
const sections = collectSections(includes, results, layout);
if (form)
sections.tabs.push({ id: 'layout', title: _('Layout'), content: [] });
updateShell(root, sections, containers);
if (sections.cards.length)
L.dom.content(cardsContainer, fillRows(sections.cards, 'cards'));
if (sections.charts.length)
updateCharts(chartsContainer, sections.charts);
if (sections.tabs.length)
updateTabs(tabMap, tabGroup, sections.tabs, form);
if (sections.extra.length)
L.dom.content(extraContainer, sections.extra);
root.classList.add('fade-in');
});
};
return step().then(() => {
poll.add(step);
// Only resume what was stopped here, not a poll the user has paused.
let paused = false;
document.addEventListener('visibilitychange', () => {
if (document.hidden)
paused = poll.stop();
else if (paused)
paused = !poll.start();
});
});
}
return view.extend({
load() {
return widgets.load().then(includes => {
return widgets.layout(includes).then(layout => [ includes, layout ]);
});
},
render([ includes, layout ]) {
const root = E('div', { 'class': 'Dashboard' });
document.addEventListener('uci-applied', () => window.location.reload());
const form = L.hasViewPermission() ? E('div', { 'class': 'cbi-section', 'data-tab': 'layout', 'data-tab-title': _('Layout') }, [ E('em', { 'class': 'spinning' }, [ _('Loading view…') ]) ]) : null;
return startPolling(includes, layout, root, form).then(() => root);
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
|