-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.js
322 lines (295 loc) · 10.1 KB
/
state.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
// 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
* Methods for manipulating the state and the DOM of the page
*/
/** @type {HTMLFormElement} Form containing options and filters */
const form = document.getElementById('options');
/** Utilities for working with the DOM */
const dom = {
/**
* Create a document fragment from the given nodes
* @param {Iterable<Node>} nodes
* @returns {DocumentFragment}
*/
createFragment(nodes) {
const fragment = document.createDocumentFragment();
for (const node of nodes) fragment.appendChild(node);
return fragment;
},
/**
* Removes all the existing children of `parent` and inserts
* `newChild` in their place
* @param {Node} parent
* @param {Node | null} newChild
*/
replace(parent, newChild) {
while (parent.firstChild) parent.removeChild(parent.firstChild);
if (newChild != null) parent.appendChild(newChild);
},
/**
* Builds a text element in a single statement.
* @param {string} tagName Type of the element, such as "span".
* @param {string} text Text content for the element.
* @param {string} [className] Class to apply to the element.
*/
textElement(tagName, text, className) {
const element = document.createElement(tagName);
element.textContent = text;
if (className) element.className = className;
return element;
},
};
/**
* We use a worker to keep large tree creation logic off the UI thread.
* This class is used to interact with the worker.
*/
class TreeWorker {
constructor(url) {
this._worker = new Worker(url);
/** ID counter used by `waitForResponse` */
this._requestId = 1;
/** @type {(data: TreeProgress) => void | null} callback for `loadTree` */
this._loadTreeCallback = null;
this._worker.addEventListener('message', event => {
if (event.data.id === 0) {
if (!this._loadTreeCallback) {
throw new Error('Got response to loadTree before listener was set');
}
this._loadTreeCallback(event.data);
}
});
}
/**
* Get data for a node with `idPath`. Loads information about the node and its
* direct children. Deeper children can be loaded by calling this function
* again.
* @param {string} idPath Path of the node to find
* @returns {Promise<TreeNode | null>}
*/
openNode(idPath) {
const id = ++this._requestId;
return new Promise((resolve, reject) => {
const handleResponse = event => {
if (event.data.id === id) {
this._worker.removeEventListener('message', handleResponse);
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data.result);
}
}
};
this._worker.addEventListener('message', handleResponse);
this._worker.postMessage({id, action: 'open', data: idPath});
});
}
/**
* Set callback used after `loadTree` is first called.
* @param {(data: TreeProgress) => void} callback Called when the worker
* has some data to display. Complete when `progress` is 1.
*/
setOnLoadHandler(callback) {
this._loadTreeCallback = callback;
}
/**
* Loads the tree data given on a worker thread and replaces the tree view in
* the UI once complete. Uses query string as state for the options.
* Use `onProgress` before calling `loadTree`.
*/
loadTree() {
this._worker.postMessage({
id: 0,
action: 'load',
data: location.search.slice(1),
});
}
}
/** Build utilities for working with the state. */
function _initState() {
/**
* State is represented in the query string and
* can be manipulated by this object. Keys in the query match with
* input names.
*/
let _filterParams = new URLSearchParams(location.search.slice(1));
const state = Object.freeze({
/**
* Returns a string from the current query string state.
* Can optionally restrict valid values for the query.
* Values not present in the query will return null, or the default
* value if supplied.
* @param {string} key
* @param {object} [options]
* @param {string} [options.default] Default to use if key is not present
* in the state
* @param {Set<string>} [options.valid] If provided, values must be in this
* set to be returned. Invalid values will return null or `defaultValue`.
* @returns {string | null}
*/
get(key, options = {}) {
const [val = null] = state.getAll(key, {
default: options.default ? [options.default] : null,
valid: options.valid,
});
return val;
},
/**
* Returns all string values for a key from the current query string state.
* Can optionally provide default values used if there are no values.
* @param {string} key
* @param {object} [options]
* @param {string[]} [options.default] Default to use if key is not present
* in the state.
* @param {Set<string>} [options.valid] If provided, values must be in this
* set to be returned. Invalid values will be omitted.
* @returns {string[]}
*/
getAll(key, options = {}) {
let vals = _filterParams.getAll(key);
if (options.valid != null) {
vals = vals.filter(val => options.valid.has(val));
}
if (options.default != null && vals.length === 0) {
vals = options.default;
}
return vals;
},
/**
* Checks if a key is present in the query string state.
* @param {string} key
* @returns {boolean}
*/
has(key) {
return _filterParams.has(key);
},
});
// Update form inputs to reflect the state from URL.
for (const element of form.elements) {
if (element.name) {
const input = /** @type {HTMLInputElement} */ (element);
const values = _filterParams.getAll(input.name);
const [value] = values;
if (value) {
switch (input.type) {
case 'checkbox':
input.checked = values.includes(input.value);
break;
case 'radio':
input.checked = value === input.value;
break;
default:
input.value = value;
break;
}
}
}
}
// Update the state when the form changes.
form.addEventListener('change', event => {
_filterParams = new URLSearchParams(new FormData(event.currentTarget));
history.replaceState(null, null, '?' + _filterParams.toString());
});
return state;
}
function _startListeners() {
const _SHOW_OPTIONS_STORAGE_KEY = 'show-options';
/** @type {HTMLSpanElement} */
const sizeHeader = document.getElementById('size-header');
/** @type {HTMLFieldSetElement} */
const typesFilterContainer = document.getElementById('types-filter');
/** @type {HTMLInputElement} */
const methodCountInput = form.elements.namedItem('method_count');
/** @type {HTMLFieldSetElement} */
const byteunit = form.elements.namedItem('byteunit');
/**
* The settings dialog on the side can be toggled on and off by elements with
* a 'toggle-options' class.
*/
function _toggleOptions() {
const openedOptions = document.body.classList.toggle('show-options');
localStorage.setItem(_SHOW_OPTIONS_STORAGE_KEY, openedOptions.toString());
}
for (const button of document.getElementsByClassName('toggle-options')) {
button.addEventListener('click', _toggleOptions);
}
if (localStorage.getItem(_SHOW_OPTIONS_STORAGE_KEY) === 'true') {
document.body.classList.add('show-options');
}
/**
* Disable some fields when method_count is set
*/
function setMethodCountModeUI() {
if (methodCountInput.checked) {
byteunit.setAttribute('disabled', '');
typesFilterContainer.setAttribute('disabled', '');
} else {
byteunit.removeAttribute('disabled');
typesFilterContainer.removeAttribute('disabled');
}
}
setMethodCountModeUI();
methodCountInput.addEventListener('change', setMethodCountModeUI);
}
function _makeIconTemplateGetter() {
const _icons = document.getElementById('icons');
/**
* @type {{[type:string]: SVGSVGElement}} Icon elements
* that correspond to each symbol type.
*/
const symbolIcons = {
D: _icons.querySelector('.foldericon'),
C: _icons.querySelector('.componenticon'),
F: _icons.querySelector('.fileicon'),
b: _icons.querySelector('.bssicon'),
d: _icons.querySelector('.dataicon'),
r: _icons.querySelector('.readonlyicon'),
t: _icons.querySelector('.codeicon'),
v: _icons.querySelector('.vtableicon'),
'*': _icons.querySelector('.generatedicon'),
x: _icons.querySelector('.dexicon'),
m: _icons.querySelector('.dexmethodicon'),
p: _icons.querySelector('.localpakicon'),
P: _icons.querySelector('.nonlocalpakicon'),
o: _icons.querySelector('.othericon'), // used as default icon
};
/** @type {Map<string, {color:string,description:string}>} */
const iconInfoCache = new Map();
/**
* Returns the SVG icon template element corresponding to the given type.
* @param {string} type Symbol type character.
* @param {boolean} readonly If true, the original template is returned.
* If false, a copy is returned that can be modified.
* @returns {SVGSVGElement}
*/
function getIconTemplate(type, readonly = false) {
const iconTemplate = symbolIcons[type] || symbolIcons.o;
return readonly ? iconTemplate : iconTemplate.cloneNode(true);
}
/**
* Returns style info about SVG icon template element corresponding to the
* given type.
* @param {string} type Symbol type character.
*/
function getIconStyle(type) {
let info = iconInfoCache.get(type);
if (info == null) {
const icon = getIconTemplate(type, true);
info = {
color: icon.getAttribute('fill'),
description: icon.querySelector('title').textContent,
};
iconInfoCache.set(type, info);
}
return info;
}
return {getIconTemplate, getIconStyle};
}
/** Utilities for working with the state */
const state = _initState();
const {getIconTemplate, getIconStyle} = _makeIconTemplateGetter();
_startListeners();