cornfield 0.46.1 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/explorer/capped.js +3 -2
- package/explorer/engine/feedback.js +49 -0
- package/explorer/engine/flow-ui.js +39 -0
- package/explorer/engine/fly.js +54 -0
- package/explorer/engine/hint.js +98 -0
- package/explorer/engine/keyboard.js +156 -0
- package/explorer/engine/layers.js +280 -0
- package/explorer/engine/layout.js +378 -0
- package/explorer/engine/map-style.js +552 -0
- package/explorer/engine/piece.js +142 -0
- package/explorer/engine/render.js +331 -0
- package/explorer/engine/search-status.js +59 -0
- package/explorer/engine/selection.js +42 -0
- package/explorer/engine/sheets.js +417 -0
- package/explorer/engine/strings.js +63 -0
- package/explorer/feedback.js +11 -45
- package/explorer/flow-build.js +88 -0
- package/explorer/flow-style.js +28 -0
- package/explorer/flows.js +11 -132
- package/explorer/fly.js +11 -43
- package/explorer/hint.js +15 -88
- package/explorer/index.html +40 -82
- package/explorer/input.js +197 -177
- package/explorer/instance-state.js +172 -0
- package/explorer/instance.js +267 -0
- package/explorer/keyboard.js +14 -206
- package/explorer/labels.js +3 -3
- package/explorer/layers.js +34 -445
- package/explorer/layout.js +14 -372
- package/explorer/main.js +4 -226
- package/explorer/map-style.js +27 -525
- package/explorer/mockups-model.js +3 -3
- package/explorer/page-pieces.js +66 -0
- package/explorer/page.js +223 -0
- package/explorer/panels.js +29 -409
- package/explorer/pieces/adopt-tokens.js +38 -0
- package/explorer/pieces/layers.js +325 -0
- package/explorer/pieces/map-stage.css +14 -0
- package/explorer/pieces/map.js +332 -0
- package/explorer/pieces/node-panel.js +536 -0
- package/explorer/pieces/search.js +454 -0
- package/explorer/proposal-model.js +1 -1
- package/explorer/proposal.js +1 -0
- package/explorer/render.js +37 -267
- package/explorer/renderer-canvas2d.js +4 -4
- package/explorer/renderer-webgl.js +21 -8
- package/explorer/renderer.js +12 -9
- package/explorer/scene.js +3 -2
- package/explorer/search-status.js +8 -47
- package/explorer/search.js +26 -324
- package/explorer/selection-view.js +17 -19
- package/explorer/sheets.js +31 -379
- package/explorer/state.js +38 -173
- package/explorer/strings.js +17 -60
- package/explorer/view.js +4 -5
- package/package.json +1 -1
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/map
|
|
3
|
+
*
|
|
4
|
+
* The map as a piece (spec 0011 D3): its two canvases, the help for its keys and its announcer, made, wired and ended in one place, for cornfield's
|
|
5
|
+
* own page (explorer/page.js mounts it where index.html keeps `<!--corn-piece:map/stage-->`) and for the `<corn-map>` component (components/src,
|
|
6
|
+
* which mounts it in its shadow root). There is one code path: the keys are engine/keyboard.js's, a tap selects through engine/selection.js, and
|
|
7
|
+
* the page and a component differ only in who listens to the instance's events (the page's node panel follows `node:show`; a component turns
|
|
8
|
+
* `selection` into `cornSelect`).
|
|
9
|
+
*
|
|
10
|
+
* const piece = mount(root, inst, options) -> { update, destroy, ... }
|
|
11
|
+
*
|
|
12
|
+
* root where the piece's elements go when `options.slots` names no place: an element or a shadow root (they go in a stage of their own there)
|
|
13
|
+
* inst the instance (instance.js) the map runs on; null makes one over `root`, which `destroy()` then ends too
|
|
14
|
+
* options see below; `update(next)` takes the same keys and applies what changed
|
|
15
|
+
*
|
|
16
|
+
* Options, all optional:
|
|
17
|
+
* topology {nodes, edges}: the map to draw (the object topology.json holds). The page does not pass it: it fetches and fills the state itself (view.js)
|
|
18
|
+
* view 'map' | 'dataflow'
|
|
19
|
+
* node the id of the selected node, or null/'' for none. A node the map has not got yet is selected when a topology brings it
|
|
20
|
+
* theme 'light' | 'dark' | 'auto': the ground the map is drawn for. 'auto' (and nothing) follows the document's own `data-theme`, else prefers-color-scheme
|
|
21
|
+
* renderer 'auto' | 'canvas2d'
|
|
22
|
+
* base, fetch, strings for a piece that makes its own instance
|
|
23
|
+
* slots { stage: Node }: the place the elements go before (cornfield's page: the marker in its stage). Without it they go in a stage of their own in `root`
|
|
24
|
+
* keysHelp the words the canvas is described by; default KEYS_HELP. cornfield's page, which has the node panel and Freeform, says KEYS_HELP_WITH_PANEL
|
|
25
|
+
* tokens the default `--corn-*` tokens (CSS text) to add, once, to the document, and to the shadow root the map's host is in if it is in one (adopt-tokens.js)
|
|
26
|
+
* measureCovered () => px of the bottom of the map something covers (the page's phone sheets); default none
|
|
27
|
+
* onSelect ({ id, kind, source, from?, to? }) once per change of the selection, from the instance's `selection` event, whoever made it:
|
|
28
|
+
* kind 'node' (id: the node's), 'edge' (id: 'from->to', and from, to), or null with id null (cleared); source as the event says
|
|
29
|
+
* onReady () once, after the first frame with a map in it is drawn
|
|
30
|
+
*
|
|
31
|
+
* What it does for a root that is not the document (the traps in test/explorer/piece-map.test.js):
|
|
32
|
+
* a drag released outside the root ends (pointer capture on the canvas), theme icons are fetched with CORS when the instance has a base,
|
|
33
|
+
* a change of theme reads the tokens again (`refresh()`), freeform positions are never stored, and `destroy()` always ends the instance's map.
|
|
34
|
+
*
|
|
35
|
+
* It imports the instance and the engine, and none of the bridges (state.js, render.js, map-style.js, layout.js): see instance-tripwires.test.js.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import { createInstance } from '../instance.js';
|
|
39
|
+
import { createMap } from '../engine/render.js';
|
|
40
|
+
import { mapStyleOf } from '../engine/map-style.js';
|
|
41
|
+
import { computeDegreeCentrality, computeLaneLayout, layoutOrbit } from '../engine/layout.js';
|
|
42
|
+
import { initKeyboard, resetKeyboard } from '../engine/keyboard.js';
|
|
43
|
+
import { selectNode, clearSelection } from '../engine/selection.js';
|
|
44
|
+
import { stopFlight } from '../engine/fly.js';
|
|
45
|
+
import { scopeOf, place } from '../engine/piece.js';
|
|
46
|
+
import { createRenderer } from '../renderer.js';
|
|
47
|
+
import { bindCanvasInput } from '../input.js';
|
|
48
|
+
import { buildDataFlow } from '../flow-build.js';
|
|
49
|
+
import { resolveTheme } from '../theme-model.js';
|
|
50
|
+
import { escapeHtml } from '../../core/text.js';
|
|
51
|
+
import { adoptDefaultTokens } from './adopt-tokens.js';
|
|
52
|
+
|
|
53
|
+
/** The keys of a map on its own (engine/keyboard.js): what the canvas is described by */
|
|
54
|
+
export const KEYS_HELP = 'Arrow keys pan, plus and minus zoom, 0 fits the map. N and P select the next and the previous node, Home and End the first and the last. F frames the selected node and what it lights. Escape clears the selection.';
|
|
55
|
+
/** The same keys where the map has the node panel and Freeform beside it (cornfield's page) */
|
|
56
|
+
export const KEYS_HELP_WITH_PANEL = "Arrow keys pan, plus and minus zoom, 0 fits the map. N and P select the next and the previous node, Home and End the first and the last. F frames the selected node and what it lights. Enter goes to the selected node's panel, Escape closes it. In Freeform, Alt and an arrow moves the selected node.";
|
|
57
|
+
|
|
58
|
+
/** The map canvas: one per mount, and one per change of renderer (a canvas that gave a WebGL context can never give a 2d one) */
|
|
59
|
+
const CANVAS = '<canvas id="graph-canvas" data-corn="map-canvas" tabindex="0" role="application" aria-roledescription="map" aria-label="Topology map" aria-describedby="map-keys-help"></canvas>';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The markup of the piece (`stage`): the map canvas, the help for its keys, the announcer the keyboard speaks in (engine/keyboard.js), and the
|
|
63
|
+
* canvas the labels are drawn on (labels.js) over the map; the map canvas still gets the pointer. Hooks get their parts when placed (engine/piece.js).
|
|
64
|
+
* @param {{ keysHelp?: string }} [opts]
|
|
65
|
+
*/
|
|
66
|
+
export const markup = ({ keysHelp = KEYS_HELP } = {}) => ({
|
|
67
|
+
stage: `${CANVAS}
|
|
68
|
+
<p id="map-keys-help" class="hx-sr-only">${escapeHtml(keysHelp)}</p>
|
|
69
|
+
<p id="map-announcer" class="hx-sr-only" role="status" aria-live="polite"></p>
|
|
70
|
+
<canvas id="label-canvas" data-corn="label-canvas" aria-hidden="true"></canvas>`,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** The document or shadow root the map's host sits in: where the default tokens go */
|
|
74
|
+
function hostRootOf(root) {
|
|
75
|
+
const host = root.nodeType === 11 ? /** @type {ShadowRoot} */ (root).host : root;
|
|
76
|
+
return host && typeof host.getRootNode === 'function' ? host.getRootNode() : null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {Element|ShadowRoot} root
|
|
81
|
+
* @param {ReturnType<typeof createInstance>|null} inst
|
|
82
|
+
* @param {object} [options]
|
|
83
|
+
*/
|
|
84
|
+
export function mount(root, inst, options = {}) {
|
|
85
|
+
const ownsInstance = !inst;
|
|
86
|
+
if (ownsInstance) inst = createInstance({ root, base: options.base || '', fetch: options.fetch, strings: options.strings });
|
|
87
|
+
const { state } = inst;
|
|
88
|
+
if (options.tokens) {
|
|
89
|
+
// The defaults go on the document, whose :root every element inherits from (through every shadow root); where the map's host sits in a
|
|
90
|
+
// shadow root of the page's own, that root gets the sheet too, for the rules on the element's `theme` attribute, which match only there
|
|
91
|
+
const where = hostRootOf(root);
|
|
92
|
+
if (where) adoptDefaultTokens(where.nodeType === 9 ? where : where.ownerDocument, options.tokens);
|
|
93
|
+
if (where && where.nodeType === 11) adoptDefaultTokens(where, options.tokens);
|
|
94
|
+
}
|
|
95
|
+
/** What the piece set up, undone by destroy() (the instance's own end runs it too, if the piece is still there) */
|
|
96
|
+
const scope = scopeOf(inst);
|
|
97
|
+
const rendererOf = (v) => (v === 'canvas2d' ? 'canvas2d' : 'auto');
|
|
98
|
+
const themeOf = (v) => (v === 'light' || v === 'dark' ? v : 'auto');
|
|
99
|
+
const current = { renderer: rendererOf(options.renderer), theme: themeOf(options.theme), topology: undefined };
|
|
100
|
+
let ended = false;
|
|
101
|
+
let ready = false;
|
|
102
|
+
|
|
103
|
+
// -- elements ---------------------------------------------------------------------------------------------------------------------
|
|
104
|
+
const html = markup({ keysHelp: options.keysHelp }).stage;
|
|
105
|
+
const slots = options.slots && options.slots.stage ? options.slots : null;
|
|
106
|
+
const made = place(inst, root, slots ? html : `<div data-corn="map-stage">${html}</div>`, slots, 'stage');
|
|
107
|
+
const find = (sel) => made.map((el) => (el.matches(sel) ? el : el.querySelector(sel))).find(Boolean);
|
|
108
|
+
scope.own(() => { for (const el of [...made, state.canvas]) if (el) el.remove(); });
|
|
109
|
+
|
|
110
|
+
// -- what the engine needs on the state, in this order ------------------------------------------------------------------------------
|
|
111
|
+
state.canvas = find('[data-corn="map-canvas"]');
|
|
112
|
+
state.labelCanvas = find('[data-corn="label-canvas"]');
|
|
113
|
+
// A 2d context that only measures text (layout.js labelWidth): the map canvas may be WebGL, and a canvas that gave WebGL can never give 2d
|
|
114
|
+
state.ctx = inst.doc.createElement('canvas').getContext('2d');
|
|
115
|
+
const map = createMap(inst, options.measureCovered ? { measureCovered: options.measureCovered } : {});
|
|
116
|
+
const style = mapStyleOf(inst);
|
|
117
|
+
scope.own(style.watch());
|
|
118
|
+
scope.own(style.onChange(() => map.render()));
|
|
119
|
+
const prefer = () => (current.renderer === 'canvas2d' ? 'canvas2d' : null);
|
|
120
|
+
const installRenderer = () => map.setRenderer(createRenderer(state.canvas, { prefer: prefer(), mapStyle: style.get }));
|
|
121
|
+
|
|
122
|
+
// -- the ground: light or dark ------------------------------------------------------------------------------------------------------
|
|
123
|
+
const lightQuery = inst.win && inst.win.matchMedia ? inst.win.matchMedia('(prefers-color-scheme: light)') : null;
|
|
124
|
+
/** The mode the map is drawn for: the element's own `theme`, else the document's `data-theme`, else the system's */
|
|
125
|
+
function mode() {
|
|
126
|
+
if (current.theme === 'light' || current.theme === 'dark') return current.theme;
|
|
127
|
+
const root$ = inst.doc && inst.doc.documentElement;
|
|
128
|
+
const own = root$ ? root$.getAttribute('data-theme') : null;
|
|
129
|
+
if (own === 'light' || own === 'dark') return own;
|
|
130
|
+
return resolveTheme('system', !!(lightQuery && lightQuery.matches));
|
|
131
|
+
}
|
|
132
|
+
/** Draws for the mode now in force and reads the tokens again (they differ by mode, and the host's `<html>` and head are not watched by the style) */
|
|
133
|
+
function refresh() {
|
|
134
|
+
if (ended) return;
|
|
135
|
+
const next = mode();
|
|
136
|
+
style.refresh();
|
|
137
|
+
if (state.theme !== next) { state.theme = next; inst.touchScene(); }
|
|
138
|
+
map.render();
|
|
139
|
+
}
|
|
140
|
+
state.theme = mode();
|
|
141
|
+
if (lightQuery && lightQuery.addEventListener) { lightQuery.addEventListener('change', refresh); scope.own(() => lightQuery.removeEventListener('change', refresh)); }
|
|
142
|
+
const Mutation = inst.win && inst.win.MutationObserver;
|
|
143
|
+
if (Mutation && inst.doc && inst.doc.documentElement) { // the host page switching its own light and dark
|
|
144
|
+
const watch = new Mutation(refresh);
|
|
145
|
+
watch.observe(inst.doc.documentElement, { attributes: true, attributeFilter: ['data-theme', 'class'] });
|
|
146
|
+
scope.own(() => watch.disconnect());
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// -- the renderer, the layout --------------------------------------------------------------------------------------------------------
|
|
150
|
+
installRenderer();
|
|
151
|
+
|
|
152
|
+
/** Where nodes sit for the current view */
|
|
153
|
+
function applyLayout() {
|
|
154
|
+
computeDegreeCentrality(inst);
|
|
155
|
+
if (state.view === 'dataflow' && state.flow) {
|
|
156
|
+
state.flow.tiers.forEach((tier) => tier.nodes.forEach((n) => { n.r = Math.min(n.r, 13); })); // all small in the lanes
|
|
157
|
+
computeLaneLayout(inst, state.flow);
|
|
158
|
+
} else layoutOrbit(inst);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** A fit asked for while the map has no box (a parent `display: none`, a box of no size) waits for one: fitted to nothing, the camera would be lost */
|
|
162
|
+
let fitWhenSized = false;
|
|
163
|
+
const fit = () => {
|
|
164
|
+
if (!state.nodes.length) return;
|
|
165
|
+
fitWhenSized = !state.width || !state.height;
|
|
166
|
+
if (!fitWhenSized) map.fitGraphToViewport(map.fitPadding());
|
|
167
|
+
};
|
|
168
|
+
const nodeById = (id) => state.nodes.find((n) => n.id === id) || null;
|
|
169
|
+
|
|
170
|
+
// -- selection: one path for the page and a component ---------------------------------------------------------------------------------
|
|
171
|
+
/** A node the host asked for that the map has not got (yet): selected when a topology brings it */
|
|
172
|
+
let pendingNode = null;
|
|
173
|
+
// A tap on a node selects it, a tap on the bare map clears it, a hand on the camera ends a flight (input.js asks; engine/selection.js does)
|
|
174
|
+
scope.on('select', (node) => selectNode(inst, node, { source: 'tap' }));
|
|
175
|
+
scope.on('background', () => clearSelection(inst, { source: 'tap' }));
|
|
176
|
+
scope.on('grab', () => stopFlight(inst));
|
|
177
|
+
// The instance says each change once, whoever made it (a tap, a key, a search, `node`, an assignment to the state: an edge picked on a proposal)
|
|
178
|
+
scope.on('selection', ({ node, edge, source }) => {
|
|
179
|
+
if (ended) return;
|
|
180
|
+
if (node || edge) pendingNode = null;
|
|
181
|
+
if (!options.onSelect) return;
|
|
182
|
+
if (node) options.onSelect({ id: node.id, kind: 'node', source });
|
|
183
|
+
else if (edge) options.onSelect({ id: `${edge.from}->${edge.to}`, kind: 'edge', from: edge.from, to: edge.to, source });
|
|
184
|
+
else options.onSelect({ id: null, kind: null, source });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/** Selects the node with this id and brings it into view; false when the map has no such node */
|
|
188
|
+
function focusNode(id) {
|
|
189
|
+
const node = nodeById(String(id));
|
|
190
|
+
if (!node || ended) return false;
|
|
191
|
+
selectNode(inst, node, { frame: true, source: 'api' });
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// -- pointer and keys on the canvas ---------------------------------------------------------------------------------------------------
|
|
196
|
+
let stopInput = () => {};
|
|
197
|
+
const inDocument = !!inst.root && inst.root.nodeType === 9;
|
|
198
|
+
/** Binds the canvas now in `state.canvas`: map input, a mouse drag that follows the pointer out of the root, the keys. Once more when the canvas is replaced. */
|
|
199
|
+
function bind() {
|
|
200
|
+
const canvas = state.canvas;
|
|
201
|
+
const offs = [bindCanvasInput(inst)];
|
|
202
|
+
if (!inDocument) {
|
|
203
|
+
// input.js hears the release on the root, so a drag let go outside an element root would stick: the canvas holds the pointer until it is let go
|
|
204
|
+
offs.push(inst.listen(canvas, 'pointerdown', (e) => { if (e.pointerType === 'mouse' && canvas.setPointerCapture) { try { canvas.setPointerCapture(e.pointerId); } catch { /* the pointer is gone */ } } }));
|
|
205
|
+
}
|
|
206
|
+
resetKeyboard(inst); // the keys are the canvas's (engine/keyboard.js): a new canvas takes them over
|
|
207
|
+
initKeyboard(inst);
|
|
208
|
+
offs.push(() => resetKeyboard(inst));
|
|
209
|
+
stopInput = () => offs.forEach((off) => off());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// -- the first frame ---------------------------------------------------------------------------------------------------------------------
|
|
213
|
+
scope.own(map.onPaint(() => {
|
|
214
|
+
if (fitWhenSized && state.width && state.height) fit();
|
|
215
|
+
if (ready || !state.nodes.length) return;
|
|
216
|
+
ready = true;
|
|
217
|
+
if (options.onReady) options.onReady();
|
|
218
|
+
}));
|
|
219
|
+
|
|
220
|
+
bind();
|
|
221
|
+
scope.own(() => stopInput());
|
|
222
|
+
scope.own(map.watchSize());
|
|
223
|
+
map.resize();
|
|
224
|
+
|
|
225
|
+
// -- changes ---------------------------------------------------------------------------------------------------------------------------------
|
|
226
|
+
function setTopology(topology) {
|
|
227
|
+
const nodes = topology && Array.isArray(topology.nodes) ? topology.nodes.filter((n) => n && typeof n.id === 'string') : [];
|
|
228
|
+
const edges = topology && Array.isArray(topology.edges) ? topology.edges.filter((e) => e && typeof e.from === 'string' && typeof e.to === 'string') : [];
|
|
229
|
+
const keep = state.selectedNode ? state.selectedNode.id : null; // the selection outlives a new topology that has its node
|
|
230
|
+
// The host's object is never changed: the map works on copies (the layout writes positions on them)
|
|
231
|
+
state.baseNodes = nodes;
|
|
232
|
+
state.baseEdges = edges;
|
|
233
|
+
state.nodes = nodes.map((n) => ({ ...n }));
|
|
234
|
+
state.edges = edges.map((e) => ({ ...e }));
|
|
235
|
+
state.mapNodes = state.nodes;
|
|
236
|
+
state.mapEdges = state.edges;
|
|
237
|
+
state.flow = buildDataFlow(state.nodes, state.edges);
|
|
238
|
+
const again = keep ? nodeById(keep) : null;
|
|
239
|
+
// The same id is the same selection (the instance says nothing); none is a change it says once
|
|
240
|
+
if (again) { state.selectedEdge = null; state.selectedNode = again; } else if (state.selectedNode || state.selectedEdge) { state.selectedNode = null; state.selectedEdge = null; }
|
|
241
|
+
inst.touchScene();
|
|
242
|
+
if (state.nodes.length) { applyLayout(); fit(); }
|
|
243
|
+
const asked = !again && pendingNode ? nodeById(pendingNode) : null; // the node the host asked for before the map had it
|
|
244
|
+
if (asked) selectNode(inst, asked, { source: 'api' });
|
|
245
|
+
map.render();
|
|
246
|
+
map.ensureFlowLoop();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function setView(view) {
|
|
250
|
+
const next = view === 'dataflow' ? 'dataflow' : 'orbit';
|
|
251
|
+
if (state.view === next) return;
|
|
252
|
+
state.view = next;
|
|
253
|
+
inst.touchScene();
|
|
254
|
+
if (state.nodes.length) { applyLayout(); fit(); map.ensureFlowLoop(); }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** A canvas that has given a context cannot give the other kind: the map gets a new one, the old one gives its WebGL context back */
|
|
258
|
+
function replaceRenderer() {
|
|
259
|
+
const previous = map.getRenderer();
|
|
260
|
+
if (previous.release) previous.release(); else previous.destroy(); // before the swap: a renderer already destroyed does not give its context back
|
|
261
|
+
stopInput();
|
|
262
|
+
const holder = inst.doc.createElement('template');
|
|
263
|
+
holder.innerHTML = CANVAS;
|
|
264
|
+
const next = /** @type {HTMLCanvasElement} */ (holder.content.firstElementChild);
|
|
265
|
+
next.setAttribute('part', 'map-canvas');
|
|
266
|
+
const hadFocus = inst.active() === state.canvas;
|
|
267
|
+
state.canvas.replaceWith(next);
|
|
268
|
+
state.canvas = next;
|
|
269
|
+
bind();
|
|
270
|
+
installRenderer();
|
|
271
|
+
map.resize();
|
|
272
|
+
if (hadFocus) next.focus();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Applies what changed in `next` (the options above). Only the keys present are looked at.
|
|
277
|
+
* @param {object} [next]
|
|
278
|
+
*/
|
|
279
|
+
function update(next = {}) {
|
|
280
|
+
if (ended) return;
|
|
281
|
+
if ('renderer' in next) {
|
|
282
|
+
const value = rendererOf(next.renderer);
|
|
283
|
+
if (value !== current.renderer) { current.renderer = value; replaceRenderer(); }
|
|
284
|
+
}
|
|
285
|
+
if ('theme' in next) {
|
|
286
|
+
const value = themeOf(next.theme);
|
|
287
|
+
if (value !== current.theme) { current.theme = value; refresh(); }
|
|
288
|
+
}
|
|
289
|
+
if ('topology' in next && next.topology !== current.topology) { current.topology = next.topology; setTopology(next.topology); }
|
|
290
|
+
if ('view' in next) setView(next.view === 'dataflow' ? 'dataflow' : 'orbit');
|
|
291
|
+
if ('node' in next) {
|
|
292
|
+
const id = next.node == null || next.node === '' ? null : String(next.node);
|
|
293
|
+
if (id === null) {
|
|
294
|
+
pendingNode = null;
|
|
295
|
+
if (state.selectedNode || state.selectedEdge) clearSelection(inst, { source: 'api' });
|
|
296
|
+
} else {
|
|
297
|
+
const node = nodeById(id);
|
|
298
|
+
if (!node) pendingNode = id;
|
|
299
|
+
else if (!state.selectedNode || state.selectedNode.id !== id) selectNode(inst, node, { source: 'api' }); // the same node again changes nothing and says nothing
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Ends the piece: its listeners, observers and timers, its elements, and the map (the renderer gives its WebGL context back) */
|
|
305
|
+
function destroy() {
|
|
306
|
+
if (ended) return;
|
|
307
|
+
ended = true;
|
|
308
|
+
scope.destroy();
|
|
309
|
+
map.destroy();
|
|
310
|
+
if (ownsInstance) inst.destroy();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const first = {};
|
|
314
|
+
for (const key of ['topology', 'view', 'node']) if (options[key] !== undefined) first[key] = options[key];
|
|
315
|
+
update(first);
|
|
316
|
+
refresh();
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
inst,
|
|
320
|
+
map,
|
|
321
|
+
update,
|
|
322
|
+
destroy,
|
|
323
|
+
/** The whole map in view */
|
|
324
|
+
fit,
|
|
325
|
+
/** Selects a node by id and brings it into view; false when there is none */
|
|
326
|
+
focusNode,
|
|
327
|
+
/** Reads the tokens again and draws: for a change the style cannot see (the host's `<html>`, a stylesheet in its head) */
|
|
328
|
+
refresh,
|
|
329
|
+
/** Starts the theme's icons (`config.theme` of /cornfield/config.json), fetched with CORS when the instance has a base */
|
|
330
|
+
applyTheme: (theme) => style.applyTheme(theme),
|
|
331
|
+
};
|
|
332
|
+
}
|