-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree-ui.js
418 lines (377 loc) · 13.5 KB
/
tree-ui.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-check
'use strict';
/**
* @fileoverview
* UI classes and methods for the Tree View in the
* Binary Size Analysis HTML report.
*/
const worker = new TreeWorker('tree-worker.js');
{
// Templates for tree nodes in the UI.
/** @type {HTMLTemplateElement} Template for leaves in the tree */
const _leafTemplate = document.getElementById('treenode-symbol');
/** @type {HTMLTemplateElement} Template for trees */
const _treeTemplate = document.getElementById('treenode-container');
const _symbolTree = document.getElementById('symboltree');
/**
* @type {HTMLCollectionOf<HTMLAnchorElement | HTMLSpanElement>}
* HTMLCollection of all nodes. Updates itself automatically.
*/
const _liveNodeList = document.getElementsByClassName('node');
/**
* @type {HTMLCollectionOf<HTMLSpanElement>}
* HTMLCollection of all size elements. Updates itself automatically.
*/
const _liveSizeSpanList = document.getElementsByClassName('size');
/**
* @type {WeakMap<HTMLElement, Readonly<TreeNode>>}
* Associates UI nodes with the corresponding tree data object
* so that event listeners and other methods can
* query the original data.
*/
const _uiNodeData = new WeakMap();
/**
* Create the contents for the size element of a tree node.
* If in method count mode, size instead represents the amount of methods in
* the node. In this case, don't append a unit at the end.
* @param {number} methodCount Number of methods to use for the count text
* @returns {{title:string,element:Node}} Object with hover text title and
* size element body. Can be consumed by `_applySizeFunc()`
*/
function _getMethodCountContents(methodCount) {
const methodStr = methodCount.toLocaleString(undefined, {
useGrouping: true,
});
return {
element: document.createTextNode(methodStr),
title: `${methodStr} method${methodCount === 1 ? '' : 's'}`,
};
}
/**
* Create the contents for the size element of a tree node.
* The unit to use is selected from the current state, and the original number
* of bytes will be displayed as hover text over the element.
* @param {number} bytes Number of bytes to use for the size text
* @param {string} unit Byte unit to use, such as 'MiB'.
* @returns {{title:string,element:Node}} Object with hover text title and
* size element body. Can be consumed by `_applySizeFunc()`
*/
function _getSizeContents(bytes, unit) {
// Format the bytes as a number with 2 digits after the decimal point
const text = (bytes / _BYTE_UNITS[unit]).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const textNode = document.createTextNode(`${text} `);
// Display the suffix with a smaller font
const suffixElement = document.createElement('small');
suffixElement.textContent = unit;
return {
element: dom.createFragment([textNode, suffixElement]),
title: bytes.toLocaleString(undefined, {useGrouping: true}) + ' bytes',
};
}
/**
* Replace the contexts of the size element for a tree node, using a
* predefined function which returns a title string and DOM element.
* @param {GetSize} sizeFunc
* @param {HTMLElement} sizeElement Element that should display the size
* @param {number} bytes Number of bytes to use for the size text
*/
function _applySizeFunc(sizeFunc, sizeElement, bytes, unit) {
const {title, element} = sizeFunc(bytes, unit);
// Replace the contents of '.size' and change its title
dom.replace(sizeElement, element);
sizeElement.title = title;
if (bytes < 0) {
sizeElement.classList.add('negative');
} else {
sizeElement.classList.remove('negative');
}
}
/**
* Sets focus to a new tree element while updating the element that last had
* focus. The tabindex property is used to avoid needing to tab through every
* single tree item in the page to reach other areas.
* @param {number | HTMLElement} el Index of tree node in `_liveNodeList`
*/
function _focusTreeElement(el) {
const lastFocused = document.activeElement;
if (_uiNodeData.has(lastFocused)) {
// Update DOM
lastFocused.tabIndex = -1;
}
const element = typeof el === 'number' ? _liveNodeList[el] : el;
if (element != null) {
// Update DOM
element.tabIndex = 0;
element.focus();
}
}
/**
* Click event handler to expand or close the child group of a tree.
* @param {Event} event
*/
async function _toggleTreeElement(event) {
event.preventDefault();
/** @type {HTMLAnchorElement | HTMLSpanElement} */
const link = event.currentTarget;
const element = link.parentElement;
const group = link.nextElementSibling;
const isExpanded = element.getAttribute('aria-expanded') === 'true';
if (isExpanded) {
// Update DOM
element.setAttribute('aria-expanded', 'false');
dom.replace(group, null);
} else {
let data = _uiNodeData.get(link);
if (data == null || data.children == null) {
const idPath = link.querySelector('.symbol-name').title;
data = await worker.openNode(idPath);
_uiNodeData.set(link, data);
}
const newElements = data.children.map(child => newTreeElement(child));
if (newElements.length === 1) {
// Open the inner element if it only has a single child.
// Ensures nodes like "java"->"com"->"google" are opened all at once.
newElements[0].querySelector('.node').click();
}
const newElementsFragment = dom.createFragment(newElements);
// Update DOM
requestAnimationFrame(() => {
group.appendChild(newElementsFragment);
element.setAttribute('aria-expanded', 'true');
});
}
}
/**
* Keydown event handler to move focus for the given element
* @param {KeyboardEvent} event
*/
function _handleKeyNavigation(event) {
/** @type {HTMLAnchorElement | HTMLSpanElement} */
const link = event.target;
const focusIndex = Array.prototype.indexOf.call(_liveNodeList, link);
/** Focus the tree element immediately following this one */
function _focusNext() {
if (focusIndex > -1 && focusIndex < _liveNodeList.length - 1) {
event.preventDefault();
_focusTreeElement(focusIndex + 1);
}
}
/** Open or close the tree element */
function _toggle() {
event.preventDefault();
link.click();
}
/**
* Focus the tree element at `index` if it starts with `char`
* @param {string} char
* @param {number} index
*/
function _focusIfStartsWith(char, index) {
const data = _uiNodeData.get(_liveNodeList[index]);
if (shortName(data).startsWith(char)) {
event.preventDefault();
_focusTreeElement(index);
return true;
} else {
return false;
}
}
switch (event.key) {
// Space should act like clicking or pressing enter & toggle the tree.
case ' ':
_toggle();
break;
// Move to previous focusable node
case 'ArrowUp':
if (focusIndex > 0) {
event.preventDefault();
_focusTreeElement(focusIndex - 1);
}
break;
// Move to next focusable node
case 'ArrowDown':
_focusNext();
break;
// If closed tree, open tree. Otherwise, move to first child.
case 'ArrowRight': {
const data = _uiNodeData.get(link);
if (!data.children || data.children.length !== 0) {
const isExpanded =
link.parentElement.getAttribute('aria-expanded') === 'true';
if (isExpanded) {
_focusNext();
} else {
_toggle();
}
}
break;
}
// If opened tree, close tree. Otherwise, move to parent.
case 'ArrowLeft':
{
const isExpanded =
link.parentElement.getAttribute('aria-expanded') === 'true';
if (isExpanded) {
_toggle();
} else {
const groupList = link.parentElement.parentElement;
if (groupList.getAttribute('role') === 'group') {
event.preventDefault();
_focusTreeElement(groupList.previousElementSibling);
}
}
}
break;
// Focus first node
case 'Home':
event.preventDefault();
_focusTreeElement(0);
break;
// Focus last node on screen
case 'End':
event.preventDefault();
_focusTreeElement(_liveNodeList.length - 1);
break;
// Expand all sibling nodes
case '*':
const groupList = link.parentElement.parentElement;
if (groupList.getAttribute('role') === 'group') {
event.preventDefault();
for (const li of groupList.children) {
if (li.getAttribute('aria-expanded') !== 'true') {
li.querySelector('.node').click();
}
}
}
break;
// If a letter was pressed, find a node starting with that character.
default:
if (event.key.length === 1 && event.key.match(/\S/)) {
for (let i = focusIndex + 1; i < _liveNodeList.length; i++) {
if (_focusIfStartsWith(event.key, i)) return;
}
for (let i = 0; i < focusIndex; i++) {
if (_focusIfStartsWith(event.key, i)) return;
}
}
break;
}
}
/**
* Inflate a template to create an element that represents one tree node.
* The element will represent a tree or a leaf, depending on if the tree
* node object has any children. Trees use a slightly different template
* and have click event listeners attached.
* @param {TreeNode} data Data to use for the UI.
* @returns {DocumentFragment}
*/
function newTreeElement(data) {
const isLeaf = data.children && data.children.length === 0;
const template = isLeaf ? _leafTemplate : _treeTemplate;
const element = document.importNode(template.content, true);
// Associate clickable node & tree data
/** @type {HTMLAnchorElement | HTMLSpanElement} */
const link = element.querySelector('.node');
_uiNodeData.set(link, Object.freeze(data));
// Icons are predefined in the HTML through hidden SVG elements
const type = data.type[0];
const icon = getIconTemplate(type);
if (_CONTAINER_TYPE_SET.has(type)) {
const symbolStyle = getIconStyle(data.type[1]);
icon.setAttribute('fill', symbolStyle.color);
icon.querySelector('title').textContent += ` - Mostly ${
symbolStyle.description
}`;
}
// Insert an SVG icon at the start of the link to represent type
link.insertBefore(icon, link.firstElementChild);
// Set the symbol name and hover text
/** @type {HTMLSpanElement} */
const symbolName = element.querySelector('.symbol-name');
symbolName.textContent = shortName(data);
symbolName.title = data.idPath;
// Set the byte size and hover text
const _getSizeLabels = state.has('method_count')
? _getMethodCountContents
: _getSizeContents;
_applySizeFunc(
_getSizeLabels,
element.querySelector('.size'),
data.size,
state.get('byteunit', {default: 'MiB', valid: _BYTE_UNITS_SET})
);
link.addEventListener('mouseover', event =>
displayInfocard(_uiNodeData.get(event.currentTarget), _getSizeLabels)
);
if (!isLeaf) {
link.addEventListener('click', _toggleTreeElement);
}
return element;
}
// When the `byteunit` state changes, update all .size elements in the page
form.elements.namedItem('byteunit').addEventListener('change', event => {
const unit = event.currentTarget.value;
// Update existing size elements with the new unit
for (const sizeElement of _liveSizeSpanList) {
const data = _uiNodeData.get(sizeElement.parentElement);
_applySizeFunc(_getSizeContents, sizeElement, data.size, unit);
}
});
_symbolTree.addEventListener('keydown', _handleKeyNavigation);
_symbolTree.addEventListener('focusin', event =>
displayInfocard(
_uiNodeData.get(event.target),
state.has('method_count') ? _getMethodCountContents : _getSizeContents
)
);
self.newTreeElement = newTreeElement;
}
{
/** @type {HTMLUListElement} */
const _symbolTree = document.getElementById('symboltree');
/** @type {HTMLProgressElement} */
const _progress = document.getElementById('progress');
/** @type {HTMLSpanElement} */
const _sizeHeader = document.getElementById('size-header');
/** Displays the given data as a tree view */
worker.setOnLoadHandler(({root, percent, sizeHeader, error}) => {
let rootElement = null;
if (root) {
/** @type {DocumentFragment} */
rootElement = newTreeElement(root);
/** @type {HTMLAnchorElement} */
const link = rootElement.querySelector('.node');
// Expand the root UI node
link.click();
link.tabIndex = 0;
}
requestAnimationFrame(() => {
_progress.value = percent;
_sizeHeader.textContent = sizeHeader;
if (error) {
_progress.classList.add('error');
} else {
_progress.classList.remove('error');
}
dom.replace(_symbolTree, rootElement);
});
});
worker.loadTree();
form.addEventListener('change', event => {
if (event.target.dataset.dynamic == null) {
_progress.value = 0;
worker.loadTree();
}
});
form.addEventListener('submit', event => {
event.preventDefault();
_progress.value = 0;
worker.loadTree();
});
}