-
Notifications
You must be signed in to change notification settings - Fork 343
/
modules_service.ts
360 lines (320 loc) · 12 KB
/
modules_service.ts
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
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable:no-new-decorators
import {action, observable} from 'mobx';
import {LayoutSettings, type LitCanonicalLayout, LitComponentSpecifier, LitModuleClass, LitModuleConfig, LitTabGroupLayout, type ModelInfoMap, ResolvedModuleConfig, type Spec} from '../lib/types';
import {LitService} from './lit_service';
import {ModulesObservedByUrlService, UrlConfiguration} from './url_service';
/**
* An interface describing how to render a LIT module, specifying which module
* to render and whether it renders on a per-model basis.
*/
export interface RenderConfig {
key: string;
moduleType: LitModuleClass;
modelName?: string;
selectionServiceIndex?: number;
}
/**
* Layout for a single group of tabs, each containing multiple modules,
* optionally replicated.
*/
export interface LitTabGroupConfig {
[tabName: string]: RenderConfig[][];
}
/**
* A layout is defined by a set of main components that are always visible,
* (designated in the object by the "main" key)
* and a set of tabs that each contain a group other components.
*
* LitRenderConfig is a mapping of tab names arrays of component lists.
* Each component list includes multiple instances of the same component,
* duplicated for each model. This basically corresponds to the grid of
* components that will be rendered.
*/
export interface LitRenderConfig {
upper: LitTabGroupConfig;
lower: LitTabGroupConfig;
left: LitTabGroupConfig;
}
type RenderModulesCallback = () => void;
/**
* Look up any module names given as strings, and return the
* constructor object.
*/
function getModuleConstructor(moduleName: string): LitModuleClass {
const moduleClass = window.customElements.get(moduleName);
if (moduleClass === undefined) {
throw new Error(
`Malformed layout; unable to find element '${moduleName}'`);
}
return moduleClass as unknown as LitModuleClass;
}
/**
* Create a canonical module config from a specifier, which may just be a
* string.
* TODO(lit-dev): run this when canonicalizing layout?
*/
export function resolveModuleConfig(
specifier: LitComponentSpecifier): ResolvedModuleConfig {
const moduleConfig: LitModuleConfig =
typeof specifier === 'string' ? {module: specifier} : specifier;
const constructor = getModuleConstructor(moduleConfig.module);
return Object.assign(
{
constructor,
title: constructor.title,
requiredForTab: moduleConfig.requiredForTab ?? false
},
moduleConfig);
}
/**
* Singleton service responsible for maintaining which modules to render.
*/
export class ModulesService extends LitService implements
ModulesObservedByUrlService {
@observable declaredLayout: LitCanonicalLayout = {
upper: {}, lower: {}, left: {}, layoutSettings: {}, description: ''
};
@observable readonly selectedTabs = {upper: '', lower: '', left: ''};
@observable private renderLayout: LitRenderConfig = {
upper: {}, lower: {}, left: {}
};
@observable hiddenModuleKeys = new Set<string>();
@observable expandedModuleKey = '';
allModuleKeys = new Set<string>();
private renderModulesCallback: RenderModulesCallback = () => {};
// TODO(b/168201937): Remove imperative logic and use observables/reactions
// for all module logic.
/**
* We need to make the rendering of modules an explicit, callback-driven
* update because of difficulty ensuring the template results of the
* lit-modules don't trigger detach/reattach behavior of the module
* components.
*/
setRenderModulesCallback(callback: RenderModulesCallback) {
this.renderModulesCallback = callback;
}
/**
* Explicitly calls the render modules callback, which is set to rerender
* the lit-modules component.
*/
renderModules() {
this.renderModulesCallback();
}
/**
* In app initialization, we need to set the declared layout and compute the
* visible render layout based on the app config
*/
initializeLayout(
layout: LitCanonicalLayout, currentModelInfos: ModelInfoMap,
datasetSpec: Spec, compareExamples: boolean) {
this.declaredLayout = layout;
this.updateRenderLayout(currentModelInfos, datasetSpec, compareExamples);
}
@action
clearLayout() {
this.updateRenderLayout({}, {}, /* compareExamples */ false);
this.renderModules();
}
/**
* Recompute layout without clearing modules.
* Use this for comparison mode to provide a faster refresh, and avoid
* clearing module-level state such as datatable filters.
*/
@action
quickUpdateLayout(
currentModelInfos: ModelInfoMap, datasetSpec: Spec,
compareExamples: boolean) {
// Recompute layout
this.updateRenderLayout(currentModelInfos, datasetSpec, compareExamples);
this.renderModules();
}
setHiddenModules(keys: Set<string>|string[]) {
// Ensure we copy to a new set
const nextHiddenModuleKeys = new Set<string>(keys);
this.hiddenModuleKeys = nextHiddenModuleKeys;
}
setExpandedModule(key: string) {
this.expandedModuleKey = key;
}
setUrlConfiguration(urlConfiguration: UrlConfiguration) {
this.setHiddenModules(urlConfiguration.hiddenModules);
this.setExpandedModule(urlConfiguration.expandedModule ?? '');
this.selectedTabs.upper = urlConfiguration.selectedTabUpper ?? '';
this.selectedTabs.lower = urlConfiguration.selectedTabLower ?? '';
this.selectedTabs.left = urlConfiguration.selectedTabLeft ?? '';
}
isModuleGroupHidden(config: RenderConfig) {
return this.hiddenModuleKeys.has(config.key);
}
isModuleGroupExpanded(config: RenderConfig) {
return this.expandedModuleKey === config.key;
}
toggleHiddenModule(config: RenderConfig, isHidden: boolean) {
if (isHidden) {
this.hiddenModuleKeys.add(config.key);
} else {
this.hiddenModuleKeys.delete(config.key);
}
}
toggleExpandedModule(config: RenderConfig, isExpanded: boolean) {
if (isExpanded) {
this.expandedModuleKey = config.key;
} else {
this.expandedModuleKey = '';
}
}
getRenderLayout(): LitRenderConfig {
return this.renderLayout;
}
getSetting(settingName: keyof LayoutSettings) {
// settingName is guaranteed to be a keyof layoutSettings.
// tslint:disable-next-line:no-dict-access-on-struct-type
return this.declaredLayout?.layoutSettings?.[settingName];
}
/**
* Compute module configurations to render determining whether or not a module
* is visible for the selected models and user settings, and whether to render
* copies of a module per model based on the module behavior.
*/
computeTabGroupLayout(
groupContents: LitTabGroupLayout, currentModelInfos: ModelInfoMap,
datasetSpec: Spec, compareExamples: boolean) {
const tabGroupConfig: LitTabGroupConfig = {};
for (const [tabName, tabContents] of Object.entries(groupContents)) {
const moduleConfigs = tabContents.map(resolveModuleConfig);
// First, map all of the modules to render configs, filtering out those
// that are not visible.
const configs = this.getRenderConfigs(
moduleConfigs, currentModelInfos, datasetSpec, compareExamples,
tabName);
if (configs.length !== 0) {
tabGroupConfig[tabName] = configs;
}
}
return tabGroupConfig;
}
updateRenderLayout(
currentModelInfos: ModelInfoMap, datasetSpec: Spec,
compareExamples: boolean) {
const upper = this.computeTabGroupLayout(
this.declaredLayout.upper, currentModelInfos, datasetSpec,
compareExamples);
const lower = this.computeTabGroupLayout(
this.declaredLayout.lower, currentModelInfos, datasetSpec,
compareExamples);
const left = this.computeTabGroupLayout(
this.declaredLayout.left, currentModelInfos, datasetSpec,
compareExamples);
const renderLayout: LitRenderConfig = {upper, lower, left};
this.updateModuleKeys(renderLayout);
this.renderLayout = renderLayout;
}
private updateModuleKeys(renderLayout: LitRenderConfig) {
const allModuleKeys = new Set<string>();
for (const section of Object.values(renderLayout) as LitTabGroupConfig[]) {
for (const tabGroup of Object.values(section)) {
for (const configGroup of tabGroup) {
for (const config of configGroup) {
allModuleKeys.add(config.key);
if (config.moduleType.collapseByDefault) {
this.hiddenModuleKeys.add(config.key);
}
}
}
}
}
// Clean up any extraneous hidden module keys that are not part of the
// allModuleKeys set
for (const key of [...this.hiddenModuleKeys]) {
if (!allModuleKeys.has(key)) {
this.hiddenModuleKeys.delete(key);
}
}
this.allModuleKeys = allModuleKeys;
}
/**
* Process module configs for a single tab.
*
* Generates module renderConfig object or objects for a given LIT module
* depending on the model specs. Since some modules can render one copy per
* model, this method specifies the configurations for those multiple modules
* to render.
*/
private getRenderConfigs(
modules: ResolvedModuleConfig[], currentModelInfos: ModelInfoMap,
datasetSpec: Spec, compareExamples: boolean,
tabName: string): RenderConfig[][] {
const renderConfigs: RenderConfig[][] = [];
// Iterate over all modules to generate render config objects, expanding
// modules that display one per model.
for (const moduleConfig of modules) {
const moduleType = moduleConfig.constructor;
if (!moduleType.shouldDisplayModule(currentModelInfos, datasetSpec)) {
if (moduleConfig.requiredForTab) {
// Abort this tab if a required module is not compatible.
return [];
} else {
// Otherwise, skip this module and continue with others.
continue;
}
}
const configs: RenderConfig[] = [];
// Is compare examples mode on and does this module require duplication?
const compare =
compareExamples && moduleType.duplicateForExampleComparison;
const key = `${tabName}_${moduleType.title}`;
// model = undefined means the resulting module(s) will not be keyed to
// a specific model; they can access the list of active models via
// this.appState.currentModels.
let selectedModels: Array<string|undefined> = [undefined];
if (moduleType.duplicateForModelComparison) {
selectedModels = Object.keys(currentModelInfos);
}
for (const modelName of selectedModels) {
if (compare) {
// The 'reference' selection service is index 1, but we want this to
// render on top/left, so create this config first.
configs.push(this.makeRenderConfig(key, moduleType, modelName, 1));
configs.push(this.makeRenderConfig(key, moduleType, modelName, 0));
} else {
configs.push(this.makeRenderConfig(
key, moduleType, modelName, compareExamples ? 0 : undefined));
}
}
// Don't push empty configs, e.g. if there is no model selected.
if (configs.length > 0) {
renderConfigs.push(configs);
} else {
console.warn(`LIT layout: no compatible configs for module '${
moduleType.title}' in tab '${tabName}'`);
}
}
return renderConfigs;
}
private makeRenderConfig(
key: string, moduleType: LitModuleClass, modelName?: string,
selectionServiceIndex?: number): RenderConfig {
return {
key,
moduleType,
modelName,
selectionServiceIndex,
};
}
}