@gramlot/gramlot 0.3.2 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gramlot/gramlot",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -1,7 +1,8 @@
1
1
  export function mountIndexPage(renderer: any, root: any,
2
2
  onSelection?: (entries: Map<string, any>, active: string) => void,
3
3
  prepareEntry?: (entry: any, context: any) => void,
4
- options?: {containerLayout?: boolean; state?: any}): {dispose(): void};
4
+ options?: {containerLayout?: boolean; state?: any; createEntry?: (entry: any) => Promise<void>;
5
+ onLoad?: (key: string, status: string, error?: Error) => void}): {dispose(): void};
5
6
  /** Re-export as Logic, or delegate indexMount from an application's Logic. */
6
7
  export class IndexLogic {
7
8
  indexMount(node: any, args: {shell_id: string}): void;
@@ -3,7 +3,8 @@ import {appearanceStyles} from './index-page-styles.js';
3
3
  import {THEME_READY, THEME_SET, THEMES, APPEARANCES} from './index-theme.js';
4
4
 
5
5
  /** Index-page behavior; writes UI state through the existing Source records. */
6
- export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEntry = () => {}, {containerLayout = false, state: suppliedState = null} = {}) {
6
+ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEntry = () => {}, {containerLayout = false, state: suppliedState = null, createEntry = null, onLoad = () => {}} = {}) {
7
+ if (createEntry && !containerLayout) throw new TypeError('Deferred index entries require containerLayout');
7
8
  const shell = renderer.records.get(root).element;
8
9
  const document = shell.ownerDocument;
9
10
  const window = document.defaultView;
@@ -113,17 +114,49 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
113
114
  set(shell, {data_code_visible: String(visible)});
114
115
  }
115
116
  };
117
+ const prepare = entry => {
118
+ entry.panel = byId(`panel-${entry.key}`);
119
+ entry.tab = containerLayout ? entry.panel : byId(`tab-${entry.key}`);
120
+ entry.code = byId(`codepane-${entry.key}`);
121
+ if (entry.key === 'intro') return;
122
+ prepareEntry(entry, {byId, node, set, listen});
123
+ entry.frame = byId(`frame-${entry.key}`);
124
+ if (entry.frame) listen(entry.frame, 'load', () => send(entry.frame));
125
+ };
126
+ const openEntry = entry => {
127
+ if (opened.getItem(entry.key)) return;
128
+ opened.setItem(entry.key, true);
129
+ set(containerLayout ? entry.panel : entry.tab, {hidden: false});
130
+ if (entry.frame) set(entry.frame, {src: entry.open.getAttribute('href')});
131
+ };
116
132
  let selecting = false;
117
133
  function select(key) {
118
- if (selecting) return;
134
+ if (selecting || disposed) return;
135
+ const requested = entries.get(key);
136
+ if (!requested) return;
137
+ state.setItem('requested', key);
138
+ if (!requested.panel) {
139
+ onLoad(key, 'loading');
140
+ if (!requested.loading) {
141
+ requested.loading = Promise.resolve().then(() => { if (!disposed) return createEntry(requested); }).then(() => {
142
+ if (disposed) return;
143
+ prepare(requested);
144
+ openEntry(requested);
145
+ if (state.getItem('requested') === key) {
146
+ onLoad(key, 'ready');
147
+ select(key);
148
+ }
149
+ }).catch(error => {
150
+ if (!disposed && state.getItem('requested') === key) onLoad(key, 'error', error);
151
+ }).finally(() => { requested.loading = null; });
152
+ }
153
+ return;
154
+ }
155
+ onLoad(key, 'ready');
119
156
  selecting = true;
120
157
  try {
121
158
  const entry = entries.get(key);
122
- if (!opened.getItem(key)) {
123
- opened.setItem(key, true);
124
- set(containerLayout ? entry.panel : entry.tab, {hidden: false});
125
- if (entry.frame) set(entry.frame, {src: entry.open.getAttribute('href')});
126
- }
159
+ openEntry(entry);
127
160
  if (entry.family) set(entry.family, {open: true});
128
161
  for (const [current, item] of entries) {
129
162
  const active = current === key;
@@ -282,14 +315,16 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
282
315
  });
283
316
  listen(window, 'resize', () => setSidebarWidth(state.getItem('preferredSidebarWidth')));
284
317
  }
285
- const choices = containerLayout ? byId('index-pages').children : shell.querySelectorAll('.index-tabs button');
318
+ const choices = createEntry
319
+ ? [byId('panel-intro'), ...shell.querySelectorAll('.index-sidebar a[id^="open-"]')]
320
+ : containerLayout ? byId('index-pages').children : shell.querySelectorAll('.index-tabs button');
286
321
  for (const tab of choices) {
287
- const key = tab.id.slice(containerLayout ? 'panel-'.length : 'tab-'.length);
322
+ const key = tab.id.slice(createEntry && tab.id.startsWith('open-') ? 'open-'.length : containerLayout ? 'panel-'.length : 'tab-'.length);
288
323
  const entry = {key, tab, panel: byId(`panel-${key}`),
289
324
  code: byId(`codepane-${key}`),
290
325
  open: key === 'intro' ? null : byId(`open-${key}`)};
291
326
  entries.set(key, entry);
292
- if (containerLayout ? node(tab).getAttr('hidden') !== true : !tab.hidden) opened.setItem(key, true);
327
+ if (entry.panel && (containerLayout ? node(entry.panel).getAttr('hidden') !== true : !tab.hidden)) opened.setItem(key, true);
293
328
  if (!containerLayout && tab.getAttribute('aria-selected') === 'true') state.setItem('active', key);
294
329
  if (!containerLayout) listen(tab, 'click', () => select(key));
295
330
  if (entry.open) listen(entry.open, 'click', event => { event.preventDefault(); select(key); });
@@ -305,12 +340,17 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
305
340
  });
306
341
  if (key === 'intro') continue;
307
342
  entry.family = entry.open.closest('details');
308
- prepareEntry(entry, {byId, node, set, listen});
309
- if (entry.panel.classList.contains('index-category')) continue;
310
- entry.frame = byId(`frame-${key}`);
311
- listen(entry.frame, 'load', () => send(entry.frame));
343
+ if (entry.panel) prepare(entry);
312
344
  }
313
345
  if (containerLayout) {
346
+ const pages = byId('index-pages');
347
+ listen(pages, 'click', event => {
348
+ // Re-clicking the active tab need not change Data, but supersedes a pending navigation.
349
+ if (event.target === pages && event.composedPath().some(element =>
350
+ element instanceof window.Element && element.getAttribute('role') === 'tab')) {
351
+ select(state.getItem('active'));
352
+ }
353
+ });
314
354
  const subscription = `${shell.id}-selection`;
315
355
  state.subscribe(subscription, {any: event => {
316
356
  if (event.node === state.getNode('active')) select(state.getItem('active'));