@gramlot/gramlot 0.3.1 → 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/dist/gramlot.js +203 -74
- package/dist/gramlot.min.js +42 -24
- package/package.json +1 -1
- package/src/builder/source.js +1 -0
- package/src/components/border-container.js +399 -0
- package/src/components/choice-editor.js +3 -1
- package/src/components/container.js +25 -0
- package/src/components/layout.js +6 -23
- package/src/components/popup.js +3 -3
- package/src/components/registry.js +5 -3
- package/src/components/selected-container.js +289 -0
- package/src/index-page-runtime.d.ts +3 -1
- package/src/index-page-runtime.js +137 -53
- package/src/index-page-styles.js +1 -1
- package/src/renderer/attributes.js +4 -1
- package/src/renderer/gramlot-renderer.js +6 -3
- package/src/view/html.js +30 -6
- package/themes/gramlot-base/theme.css +3 -2
- package/themes/joanna/theme.css +9 -0
- package/themes/mimi/theme.css +9 -0
- package/tools/gramlot-tools-inspector.js +549 -35
- package/tools/inspector-agent.js +259 -5
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/** Shared selection keeps panel Source and light DOM mounted until explicit closing. */
|
|
2
|
+
import {defineComponent} from './registry.js';
|
|
3
|
+
import {Container} from './container.js';
|
|
4
|
+
|
|
5
|
+
const selectionPanels = new WeakMap();
|
|
6
|
+
let panelSequence = 0;
|
|
7
|
+
|
|
8
|
+
function projectSelectionChild(element, attrs, actions) {
|
|
9
|
+
const {pageName = null, title = '', icon = '', badge = null, badge_color = '', closable = false, onClose, hidden = false, disabled = false, ...rest} = attrs;
|
|
10
|
+
if (pageName !== null && typeof pageName !== 'string') throw new TypeError('pageName must be a string');
|
|
11
|
+
if (typeof closable !== 'boolean') throw new TypeError('closable must be a boolean');
|
|
12
|
+
const slot = selectionPanels.get(element)?.slot ?? `panel-${++panelSequence}`;
|
|
13
|
+
selectionPanels.set(element, {pageName, title, icon, badge, badge_color, closable, hidden, disabled, slot,
|
|
14
|
+
remove: actions.remove, allowClose: () => onClose == null ? true : actions.invoke('onClose', onClose, {})});
|
|
15
|
+
rest.slot = slot;
|
|
16
|
+
if (element.parentElement) element.parentElement.scheduleSelection();
|
|
17
|
+
return rest;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function SelectedContainer(HTMLElement) {
|
|
21
|
+
return class extends Container(HTMLElement) {
|
|
22
|
+
static projectChild(element, attrs, actions) { return projectSelectionChild(element, attrs, actions); }
|
|
23
|
+
#panels = new Map();
|
|
24
|
+
#selected = null;
|
|
25
|
+
#initialized = false;
|
|
26
|
+
#scheduled = false;
|
|
27
|
+
#requested = {selectedPage: null, selected: null};
|
|
28
|
+
#observer;
|
|
29
|
+
#context;
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
this.control.replaceChildren();
|
|
33
|
+
this.control.className = 'control-box selection';
|
|
34
|
+
const style = this.ownerDocument.createElement('style');
|
|
35
|
+
style.textContent = `:host{min-height:0;overflow:hidden}.selection{height:100%;min-height:0;display:flex;flex-direction:column}
|
|
36
|
+
.panels{flex:1;min-height:0;min-width:0;position:relative}.panel{height:100%;min-height:0;overflow:auto}
|
|
37
|
+
.panel[hidden]{display:none}::slotted(*){height:100%;min-height:0}`;
|
|
38
|
+
this.panelArea = this.ownerDocument.createElement('div'); this.panelArea.className = 'panels';
|
|
39
|
+
this.control.append(this.panelArea); this.shadowRoot.append(style);
|
|
40
|
+
this.#observer = new this.ownerDocument.defaultView.MutationObserver(() => this.scheduleSelection());
|
|
41
|
+
}
|
|
42
|
+
setSourceContext(context) { this.#context = context; }
|
|
43
|
+
connectedCallback() { this.#observer.observe(this, {childList:true}); this.scheduleSelection(); }
|
|
44
|
+
disconnectedCallback() { this.#observer.disconnect(); }
|
|
45
|
+
scheduleSelection() {
|
|
46
|
+
if (this.#scheduled) return;
|
|
47
|
+
this.#scheduled = true;
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
this.#scheduled = false;
|
|
50
|
+
if (this.isConnected) this.#sync();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
projectAttributes(attrs, adapt) {
|
|
54
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
55
|
+
const {selected, selectedPage} = this.parameters;
|
|
56
|
+
if (selected != null && (!Number.isInteger(selected) || selected < 0)) throw new TypeError('selected must be a nonnegative index or null');
|
|
57
|
+
if (selectedPage != null && typeof selectedPage !== 'string') throw new TypeError('selectedPage must be a string or null');
|
|
58
|
+
this.scheduleSelection();
|
|
59
|
+
return rest;
|
|
60
|
+
}
|
|
61
|
+
#sync() {
|
|
62
|
+
const records = [], names = new Set();
|
|
63
|
+
for (const child of this.children) {
|
|
64
|
+
const config = selectionPanels.get(child);
|
|
65
|
+
if (!config) continue;
|
|
66
|
+
if (config.pageName != null && names.has(config.pageName)) throw new Error(`Duplicate pageName: ${config.pageName}`);
|
|
67
|
+
names.add(config.pageName);
|
|
68
|
+
let record = this.#panels.get(child);
|
|
69
|
+
if (!record) {
|
|
70
|
+
const viewport = this.ownerDocument.createElement('div'); viewport.className = 'panel'; viewport.hidden = true;
|
|
71
|
+
viewport.id = config.slot; viewport.setAttribute('role', 'group');
|
|
72
|
+
const slot = this.ownerDocument.createElement('slot'); slot.name = config.slot; viewport.append(slot);
|
|
73
|
+
this.panelArea.append(viewport);
|
|
74
|
+
record = {child, viewport, config, index:records.length}; this.#panels.set(child, record);
|
|
75
|
+
}
|
|
76
|
+
record.config = config;
|
|
77
|
+
records.push(record);
|
|
78
|
+
}
|
|
79
|
+
const previousIndex = this.#selected?.index ?? 0;
|
|
80
|
+
for (const [child, record] of this.#panels) {
|
|
81
|
+
if (!records.includes(record)) { record.viewport.remove(); this.#panels.delete(child); }
|
|
82
|
+
}
|
|
83
|
+
records.forEach((record, index) => { record.index = index; });
|
|
84
|
+
this.#panels = new Map(records.map(record => [record.child, record]));
|
|
85
|
+
const {selectedPage, selected} = this.parameters;
|
|
86
|
+
const nameChanged = !Object.is(selectedPage, this.#requested.selectedPage);
|
|
87
|
+
const indexChanged = !Object.is(selected, this.#requested.selected);
|
|
88
|
+
this.#requested = {selectedPage, selected};
|
|
89
|
+
const available = record => record && !record.config.hidden && !record.config.disabled;
|
|
90
|
+
let target = this.#selected;
|
|
91
|
+
if (!this.#initialized || nameChanged || indexChanged) {
|
|
92
|
+
const requested = (nameChanged || !this.#initialized && selectedPage != null)
|
|
93
|
+
? records.find(record => record.config.pageName === selectedPage) : records[selected];
|
|
94
|
+
if (available(requested)) target = requested;
|
|
95
|
+
}
|
|
96
|
+
if (!records.includes(target) || !available(target)) {
|
|
97
|
+
target = records.slice(previousIndex).find(available) ?? records.slice(0, previousIndex).reverse().find(available) ?? null;
|
|
98
|
+
}
|
|
99
|
+
this.renderTabs(records, target);
|
|
100
|
+
if (records.length || this.#initialized) this.#select(target);
|
|
101
|
+
}
|
|
102
|
+
renderTabs() {}
|
|
103
|
+
selectPanel(record) {
|
|
104
|
+
if (record.config.hidden || record.config.disabled) return;
|
|
105
|
+
this.#select(record);
|
|
106
|
+
}
|
|
107
|
+
#payload(record, selected) {
|
|
108
|
+
const page = record.config.pageName ?? record.index;
|
|
109
|
+
return {page, index:record.index, selected, change:`${page}_${selected ? 'show' : 'hide'}`};
|
|
110
|
+
}
|
|
111
|
+
#writeSelection(target) {
|
|
112
|
+
this.#context.writeParameter('selectedPage', target?.config.pageName ?? null);
|
|
113
|
+
this.#context.writeParameter('selected', target?.index ?? null);
|
|
114
|
+
}
|
|
115
|
+
#select(target) {
|
|
116
|
+
const previous = this.#selected, initial = !this.#initialized;
|
|
117
|
+
if (!initial && target === previous) {
|
|
118
|
+
this.#writeSelection(target);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!initial) {
|
|
122
|
+
if (previous) this.#context.publish('hiding', {pageName:previous.config.pageName, index:previous.index});
|
|
123
|
+
if (target) this.#context.publish('showing', {pageName:target.config.pageName, index:target.index});
|
|
124
|
+
}
|
|
125
|
+
this.#selected = target; this.#initialized = true;
|
|
126
|
+
for (const record of this.#panels.values()) record.viewport.hidden = record !== target;
|
|
127
|
+
this.renderTabs([...this.#panels.values()], target);
|
|
128
|
+
this.#writeSelection(target);
|
|
129
|
+
if (!initial && previous) this.#context.publish('selected', this.#payload(previous, false));
|
|
130
|
+
if (target) this.#context.publish('selected', this.#payload(target, true));
|
|
131
|
+
}
|
|
132
|
+
closePanel(record) {
|
|
133
|
+
if (!record.config.closable) return;
|
|
134
|
+
const allowed = record.config.allowClose();
|
|
135
|
+
if (typeof allowed !== 'boolean') throw new TypeError('onClose must return a synchronous boolean');
|
|
136
|
+
if (!allowed) return;
|
|
137
|
+
const payload = {pageName:record.config.pageName, index:record.index, wasSelected:record === this.#selected};
|
|
138
|
+
this.#context.publish('closing', payload);
|
|
139
|
+
record.config.remove();
|
|
140
|
+
this.#sync();
|
|
141
|
+
this.#context.publish('closed', payload);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
SelectedContainer.parameters = {
|
|
146
|
+
selectedPage: {dtype:'T', default:null, nullable:true, binding:'twoWay'},
|
|
147
|
+
selected: {dtype:'L', default:null, nullable:true, binding:'twoWay'},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const selectionChildParameters = {onClose: {inline: []}};
|
|
151
|
+
|
|
152
|
+
export const StackContainer = defineComponent({
|
|
153
|
+
name:'stackContainer', tag:'gramlot-stack-container', base:SelectedContainer, subTags:'*',
|
|
154
|
+
sourceContext: true, childParameters: selectionChildParameters,
|
|
155
|
+
implementation: Base => class extends Base {},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
export const TabContainer = defineComponent({
|
|
159
|
+
name:'tabContainer', tag:'gramlot-tab-container', base:SelectedContainer, subTags:'*',
|
|
160
|
+
sourceContext: true, childParameters: selectionChildParameters,
|
|
161
|
+
parameters: {
|
|
162
|
+
tabMode: {dtype:'T', default:'tabs', binding:'read', enum:['tabs','underline']},
|
|
163
|
+
tabPosition: {dtype:'T', default:'top', binding:'read', enum:['top','bottom','left','right']},
|
|
164
|
+
tabAlign: {dtype:'T', default:'start', binding:'read', enum:['start','center','end']},
|
|
165
|
+
},
|
|
166
|
+
implementation: Base => class extends Base {
|
|
167
|
+
#tabs = new Map();
|
|
168
|
+
constructor() {
|
|
169
|
+
super();
|
|
170
|
+
const doc = this.ownerDocument;
|
|
171
|
+
this.tabStrip = doc.createElement('div'); this.tabStrip.className = 'tabs'; this.tabStrip.setAttribute('role', 'tablist');
|
|
172
|
+
this.control.prepend(this.tabStrip);
|
|
173
|
+
const style = doc.createElement('style');
|
|
174
|
+
style.textContent = `
|
|
175
|
+
.selection{min-width:0;--tab-border:var(--gramlot-tab-border,var(--gramlot-border,#aaa));
|
|
176
|
+
--tab-radius:var(--gramlot-tab-radius,4px);--tab-indicator:var(--gramlot-tab-indicator-color,var(--gramlot-action,#3766b5))}
|
|
177
|
+
.panels{background:var(--gramlot-tab-selected-background,var(--gramlot-surface,white))}
|
|
178
|
+
.tabs{display:var(--gramlot-tab-strip-display,flex);flex:none;align-items:stretch;gap:var(--gramlot-tab-gap,3px);min-width:0;min-height:0;
|
|
179
|
+
padding:var(--gramlot-tab-strip-padding,4px);overflow:auto;overscroll-behavior:contain;
|
|
180
|
+
border-bottom:1px solid var(--tab-border);background:var(--gramlot-tab-background,var(--gramlot-surface-subtle,#eef1f5))}
|
|
181
|
+
[data-align=center] .tabs{justify-content:safe center}
|
|
182
|
+
[data-align=end] .tabs{justify-content:safe flex-end}
|
|
183
|
+
.tab-entry{position:relative;display:flex;flex:none;align-items:stretch;border:1px solid transparent;border-bottom:0;
|
|
184
|
+
border-radius:var(--tab-radius) var(--tab-radius) 0 0;color:var(--gramlot-tab-color,var(--gramlot-text-secondary,#555))}
|
|
185
|
+
.tab-entry:has([aria-selected=true]){border-color:var(--tab-border);color:var(--gramlot-tab-selected-color,var(--gramlot-ink,#222));
|
|
186
|
+
background:var(--gramlot-tab-selected-background,var(--gramlot-surface,white))}
|
|
187
|
+
button{box-sizing:border-box;display:flex;align-items:center;justify-content:center;gap:var(--gramlot-tab-content-gap,.4em);
|
|
188
|
+
font:inherit;line-height:1.2;color:inherit;border:0;background:transparent;cursor:pointer;white-space:nowrap;min-width:0;
|
|
189
|
+
padding:var(--gramlot-tab-padding,4px 10px);min-height:max(var(--gramlot-touch-target,0px),var(--gramlot-tab-height,28px));border-radius:inherit}
|
|
190
|
+
button[aria-selected=true]{font-weight:600}
|
|
191
|
+
.icon,.badge{flex:none}.icon:empty{display:none}.caption{overflow:hidden;text-overflow:ellipsis}
|
|
192
|
+
.badge{font-size:var(--gramlot-tab-badge-size,.8em);font-weight:600;line-height:1.3;border-radius:999px;
|
|
193
|
+
min-width:1.3em;padding:var(--gramlot-tab-badge-padding,0 .35em);text-align:center;
|
|
194
|
+
background:var(--gramlot-tab-badge-background,var(--gramlot-brand-blue,#3766b5));color:var(--gramlot-tab-badge-color,white)}
|
|
195
|
+
@media(hover:hover){button:not(:disabled):hover{background:var(--gramlot-tab-hover-background,var(--gramlot-selected,#e8f1fc))}}
|
|
196
|
+
button:disabled{opacity:.5;cursor:default}
|
|
197
|
+
button:focus-visible{outline:2px solid var(--gramlot-focus-ring,Highlight);outline-offset:-3px}
|
|
198
|
+
.close{align-self:center;flex:none;min-height:var(--gramlot-touch-target,20px);min-width:var(--gramlot-touch-target,20px);
|
|
199
|
+
padding:0;margin-inline-end:3px;border-radius:var(--tab-radius);font-size:1.15em}
|
|
200
|
+
[hidden]{display:none!important}
|
|
201
|
+
[data-position=top] .tabs{padding-bottom:0}
|
|
202
|
+
[data-position=bottom]{flex-direction:column-reverse}
|
|
203
|
+
[data-position=bottom] .tabs{border-bottom:0;border-top:1px solid var(--tab-border);padding-top:0}
|
|
204
|
+
[data-position=bottom] .tab-entry{border-bottom:1px solid transparent;border-top:0;border-radius:0 0 var(--tab-radius) var(--tab-radius)}
|
|
205
|
+
[data-position=left]{flex-direction:row}[data-position=right]{flex-direction:row-reverse}
|
|
206
|
+
:is([data-position=left],[data-position=right]) .tabs{flex-direction:column;max-width:min(40%,var(--gramlot-tab-side-width,12rem));border-bottom:0}
|
|
207
|
+
:is([data-position=left],[data-position=right]) button[role=tab]{flex:1;justify-content:flex-start}
|
|
208
|
+
[data-position=left] .tabs{border-right:1px solid var(--tab-border);padding-right:0}
|
|
209
|
+
[data-position=right] .tabs{border-left:1px solid var(--tab-border);padding-left:0}
|
|
210
|
+
[data-position=left] .tab-entry{border-bottom:1px solid transparent;border-right:0;border-radius:var(--tab-radius) 0 0 var(--tab-radius)}
|
|
211
|
+
[data-position=right] .tab-entry{border-bottom:1px solid transparent;border-left:0;border-radius:0 var(--tab-radius) var(--tab-radius) 0}
|
|
212
|
+
.tab-entry:has([aria-selected=true]){border-color:var(--tab-border)}
|
|
213
|
+
[data-mode=underline] .tabs{background:transparent}
|
|
214
|
+
[data-mode=underline] .tab-entry{border:0;border-radius:0;background:transparent}
|
|
215
|
+
[data-mode=underline] .tab-entry:has([aria-selected=true])::after{content:'';position:absolute;pointer-events:none;
|
|
216
|
+
background:var(--tab-indicator);height:var(--gramlot-tab-indicator-size,2px);bottom:0;left:0;right:0}
|
|
217
|
+
[data-mode=underline][data-position=bottom] .tab-entry::after{top:0;bottom:auto}
|
|
218
|
+
[data-mode=underline]:is([data-position=left],[data-position=right]) .tab-entry::after{height:auto;width:var(--gramlot-tab-indicator-size,2px);top:0;bottom:0}
|
|
219
|
+
[data-mode=underline][data-position=left] .tab-entry::after{left:auto;right:0}
|
|
220
|
+
[data-mode=underline][data-position=right] .tab-entry::after{left:0;right:auto}`;
|
|
221
|
+
this.shadowRoot.append(style);
|
|
222
|
+
}
|
|
223
|
+
projectAttributes(attrs, adapt) {
|
|
224
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
225
|
+
const {tabMode, tabPosition, tabAlign} = this.parameters;
|
|
226
|
+
if (!['tabs','underline'].includes(tabMode)) throw new TypeError('Invalid tabMode');
|
|
227
|
+
if (!['top','bottom','left','right'].includes(tabPosition)) throw new TypeError('Invalid tabPosition');
|
|
228
|
+
if (!['start','center','end'].includes(tabAlign)) throw new TypeError('Invalid tabAlign');
|
|
229
|
+
this.control.dataset.mode = tabMode; this.control.dataset.position = tabPosition;
|
|
230
|
+
this.control.dataset.align = tabAlign;
|
|
231
|
+
this.tabStrip.setAttribute('aria-orientation', ['left','right'].includes(tabPosition) ? 'vertical' : 'horizontal');
|
|
232
|
+
return rest;
|
|
233
|
+
}
|
|
234
|
+
renderTabs(records, selected) {
|
|
235
|
+
for (const [record, entry] of this.#tabs) {
|
|
236
|
+
if (!records.includes(record)) { entry.root.remove(); this.#tabs.delete(record); }
|
|
237
|
+
}
|
|
238
|
+
for (const [index, record] of records.entries()) {
|
|
239
|
+
let entry = this.#tabs.get(record);
|
|
240
|
+
if (!entry) {
|
|
241
|
+
const doc = this.ownerDocument, root = doc.createElement('div'), button = doc.createElement('button'), close = doc.createElement('button');
|
|
242
|
+
root.className = 'tab-entry'; button.type = close.type = 'button'; button.setAttribute('role', 'tab');
|
|
243
|
+
button.id = `tab-${record.config.slot}`; button.setAttribute('aria-controls', record.viewport.id);
|
|
244
|
+
record.viewport.setAttribute('role', 'tabpanel');
|
|
245
|
+
record.viewport.setAttribute('aria-labelledby', button.id);
|
|
246
|
+
const icon = doc.createElement('span'), caption = doc.createElement('span'), badge = doc.createElement('span');
|
|
247
|
+
icon.className = 'icon'; icon.setAttribute('aria-hidden', 'true'); caption.className = 'caption'; badge.className = 'badge';
|
|
248
|
+
button.append(icon, caption, badge);
|
|
249
|
+
close.className = 'close'; close.textContent = '×';
|
|
250
|
+
button.addEventListener('click', () => this.selectPanel(record));
|
|
251
|
+
close.addEventListener('click', () => {
|
|
252
|
+
this.closePanel(record);
|
|
253
|
+
if (!close.isConnected) this.tabStrip.querySelector('[aria-selected=true]')?.focus();
|
|
254
|
+
});
|
|
255
|
+
button.addEventListener('keydown', event => {
|
|
256
|
+
if (event.key === 'Delete') { event.preventDefault(); this.closePanel(record); this.tabStrip.querySelector('[aria-selected=true]')?.focus(); return; }
|
|
257
|
+
const vertical = ['left','right'].includes(this.parameters.tabPosition);
|
|
258
|
+
const previous = vertical ? 'ArrowUp' : 'ArrowLeft', nextKey = vertical ? 'ArrowDown' : 'ArrowRight';
|
|
259
|
+
if (![previous,nextKey,'Home','End'].includes(event.key)) return;
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
const available = [...this.#tabs].filter(([r]) => !r.config.hidden && !r.config.disabled)
|
|
262
|
+
.sort(([a], [b]) => a.index - b.index);
|
|
263
|
+
let index = available.findIndex(([r]) => r === record);
|
|
264
|
+
index = event.key === 'Home' ? 0 : event.key === 'End' ? available.length - 1
|
|
265
|
+
: (index + (event.key === nextKey ? 1 : -1) + available.length) % available.length;
|
|
266
|
+
const [next, tab] = available[index]; this.selectPanel(next); tab.button.focus();
|
|
267
|
+
tab.button.scrollIntoView({block:'nearest',inline:'nearest'});
|
|
268
|
+
});
|
|
269
|
+
root.append(button, close); this.tabStrip.append(root); entry = {root,button,close,icon,caption,badge}; this.#tabs.set(record, entry);
|
|
270
|
+
}
|
|
271
|
+
if (this.tabStrip.children[index] !== entry.root) this.tabStrip.insertBefore(entry.root, this.tabStrip.children[index] ?? null);
|
|
272
|
+
entry.root.hidden = !!record.config.hidden;
|
|
273
|
+
const caption = record.config.title || record.config.pageName || String(record.index + 1);
|
|
274
|
+
entry.caption.textContent = caption; entry.button.title = caption;
|
|
275
|
+
entry.icon.textContent = record.config.icon;
|
|
276
|
+
entry.badge.textContent = record.config.badge ?? '';
|
|
277
|
+
entry.badge.hidden = record.config.badge == null || record.config.badge === '';
|
|
278
|
+
entry.badge.style.backgroundColor = record.config.badge_color;
|
|
279
|
+
entry.button.setAttribute('aria-label', entry.badge.hidden ? caption : `${caption} ${record.config.badge}`);
|
|
280
|
+
entry.button.disabled = !!record.config.disabled;
|
|
281
|
+
entry.button.setAttribute('aria-selected', String(record === selected));
|
|
282
|
+
entry.button.tabIndex = record === selected ? 0 : -1;
|
|
283
|
+
entry.close.hidden = !record.config.closable;
|
|
284
|
+
entry.close.disabled = !!record.config.disabled;
|
|
285
|
+
entry.close.setAttribute('aria-label', `Close ${caption}`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export function mountIndexPage(renderer: any, root: any,
|
|
2
2
|
onSelection?: (entries: Map<string, any>, active: string) => void,
|
|
3
|
-
prepareEntry?: (entry: any, context: any) => void
|
|
3
|
+
prepareEntry?: (entry: any, context: any) => void,
|
|
4
|
+
options?: {containerLayout?: boolean; state?: any; createEntry?: (entry: any) => Promise<void>;
|
|
5
|
+
onLoad?: (key: string, status: string, error?: Error) => void}): {dispose(): void};
|
|
4
6
|
/** Re-export as Logic, or delegate indexMount from an application's Logic. */
|
|
5
7
|
export class IndexLogic {
|
|
6
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 = () => {}) {
|
|
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;
|
|
@@ -37,7 +38,7 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
37
38
|
restoreChoice(appearance, preferences.appearance, APPEARANCES);
|
|
38
39
|
const families = [...shell.querySelectorAll('.index-sidebar details')];
|
|
39
40
|
const Bag = root.builder.data.constructor;
|
|
40
|
-
const state = new Bag();
|
|
41
|
+
const state = suppliedState ?? new Bag();
|
|
41
42
|
const opened = new Bag();
|
|
42
43
|
state.setItem('opened', opened);
|
|
43
44
|
state.setItem('keyboard', (keyboard?.checked ?? false));
|
|
@@ -99,38 +100,77 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
99
100
|
const updatePanels = () => {
|
|
100
101
|
if (navigationToggle) {
|
|
101
102
|
const visible = state.getItem('navigationVisible');
|
|
102
|
-
set(sidebar, {hidden: !visible});
|
|
103
|
-
set(sidebarHandle, {hidden: !visible});
|
|
103
|
+
set(sidebar, containerLayout ? {drawer: visible ? true : 'close'} : {hidden: !visible});
|
|
104
|
+
if (!containerLayout) set(sidebarHandle, {hidden: !visible});
|
|
104
105
|
set(navigationToggle, {aria_expanded: String(visible)});
|
|
105
106
|
set(shell, {data_navigation_hidden: String(!visible)});
|
|
106
107
|
}
|
|
107
108
|
if (codeSidebar) {
|
|
108
109
|
const available = Boolean(entries.get(state.getItem('active'))?.code);
|
|
109
110
|
const visible = available && state.getItem('codeVisible');
|
|
110
|
-
set(codeSidebar, {hidden: !visible});
|
|
111
|
-
set(codeHandle, {hidden: !visible});
|
|
111
|
+
set(codeSidebar, containerLayout ? {hidden: !available, drawer: visible ? true : 'close'} : {hidden: !visible});
|
|
112
|
+
if (!containerLayout) set(codeHandle, {hidden: !visible});
|
|
112
113
|
set(codeToggle, {disabled: !available, aria_expanded: String(visible)});
|
|
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
|
+
};
|
|
132
|
+
let selecting = false;
|
|
116
133
|
function select(key) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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;
|
|
130
154
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
onLoad(key, 'ready');
|
|
156
|
+
selecting = true;
|
|
157
|
+
try {
|
|
158
|
+
const entry = entries.get(key);
|
|
159
|
+
openEntry(entry);
|
|
160
|
+
if (entry.family) set(entry.family, {open: true});
|
|
161
|
+
for (const [current, item] of entries) {
|
|
162
|
+
const active = current === key;
|
|
163
|
+
if (!containerLayout) {
|
|
164
|
+
set(item.tab, {aria_selected: String(active), tabindex: active && state.getItem('keyboard') ? 0 : -1});
|
|
165
|
+
set(item.panel, {hidden: !active});
|
|
166
|
+
}
|
|
167
|
+
if (item.code) set(item.code, {hidden: !active});
|
|
168
|
+
if (item.open) set(item.open, {aria_current: active ? 'page' : null});
|
|
169
|
+
}
|
|
170
|
+
if (state.getItem('active') !== key) state.setItem('active', key);
|
|
171
|
+
updatePanels();
|
|
172
|
+
updateInspector();
|
|
173
|
+
} finally { selecting = false; }
|
|
134
174
|
}
|
|
135
175
|
const sidebarMaximum = () => Math.max(120, Math.min(460, Math.floor(window.innerWidth * .6)));
|
|
136
176
|
const applyWidths = () => set(shell, {style: `--sidebar-width: ${state.getItem('sidebarWidth')}px; --code-width: ${state.getItem('codeWidth') ?? 420}px`});
|
|
@@ -197,11 +237,38 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
197
237
|
}
|
|
198
238
|
set(shell, {data_resizing: null});
|
|
199
239
|
// Retain the preferred width when a narrow viewport temporarily limits the visible sidebar.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
240
|
+
if (!containerLayout) {
|
|
241
|
+
const savedWidth = preferences.sidebarWidth;
|
|
242
|
+
const width = typeof savedWidth === 'number' && Number.isFinite(savedWidth) && savedWidth >= 120 && savedWidth <= 460
|
|
243
|
+
? savedWidth : sidebar.getBoundingClientRect().width || Number(sidebarHandle.getAttribute('aria-valuenow'));
|
|
244
|
+
state.setItem('preferredSidebarWidth', width);
|
|
245
|
+
setSidebarWidth(width);
|
|
246
|
+
} else {
|
|
247
|
+
const gramlot = renderer.binding.gramlot;
|
|
248
|
+
for (const [pane, property, widthProperty] of [[sidebar, 'navigationVisible', 'sidebarWidth'], [codeSidebar, 'codeVisible', 'codeWidth']]) {
|
|
249
|
+
const source = node(pane);
|
|
250
|
+
const savedWidth = preferences[widthProperty];
|
|
251
|
+
if (typeof savedWidth === 'number' && Number.isFinite(savedWidth) && savedWidth > 0) set(pane, {width: `${savedWidth}px`});
|
|
252
|
+
const handle = gramlot.subscribe(`${source.getAttr('node_id')}_closable_change`, ({open}) => {
|
|
253
|
+
if (source.getAttr('hidden') === true || source.getAttr('drawer') === false) return;
|
|
254
|
+
state.setItem(property, open);
|
|
255
|
+
set(pane, {drawer: open ? true : 'close'});
|
|
256
|
+
set(property === 'navigationVisible' ? navigationToggle : codeToggle, {aria_expanded: String(open)});
|
|
257
|
+
set(shell, {[property === 'navigationVisible' ? 'data_navigation_hidden' : 'data_code_visible']: String(property === 'navigationVisible' ? !open : open)});
|
|
258
|
+
savePreferences();
|
|
259
|
+
});
|
|
260
|
+
releases.push(() => gramlot.unsubscribe(handle));
|
|
261
|
+
}
|
|
262
|
+
const border = shell.querySelector('[data-index-regions]');
|
|
263
|
+
const handle = gramlot.subscribe(`${node(border).getAttr('node_id')}_splitter_resize`, ({region, size}) => {
|
|
264
|
+
set(region === 'left' ? sidebar : codeSidebar, {width: size});
|
|
265
|
+
state.setItem(region === 'left' ? 'preferredSidebarWidth' : 'preferredCodeWidth', Number.parseFloat(size));
|
|
266
|
+
savePreferences();
|
|
267
|
+
});
|
|
268
|
+
releases.push(() => gramlot.unsubscribe(handle));
|
|
269
|
+
state.setItem('preferredSidebarWidth', preferences.sidebarWidth);
|
|
270
|
+
state.setItem('preferredCodeWidth', preferences.codeWidth);
|
|
271
|
+
}
|
|
205
272
|
listen(navigationToggle, 'click', () => {
|
|
206
273
|
state.setItem('navigationVisible', !state.getItem('navigationVisible'));
|
|
207
274
|
updatePanels();
|
|
@@ -212,7 +279,7 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
212
279
|
updatePanels();
|
|
213
280
|
savePreferences();
|
|
214
281
|
});
|
|
215
|
-
if (codeHandle) {
|
|
282
|
+
if (!containerLayout && codeHandle) {
|
|
216
283
|
const saved = preferences.codeWidth;
|
|
217
284
|
const width = typeof saved === 'number' && Number.isFinite(saved) && saved >= 200 ? saved : 420;
|
|
218
285
|
state.setItem('preferredCodeWidth', width);
|
|
@@ -232,31 +299,36 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
232
299
|
});
|
|
233
300
|
listen(window, 'resize', () => setCodeWidth(state.getItem('preferredCodeWidth')));
|
|
234
301
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
302
|
+
if (!containerLayout) {
|
|
303
|
+
listen(sidebarHandle, 'keydown', event => {
|
|
304
|
+
const current = state.getItem('sidebarWidth');
|
|
305
|
+
const value = event.key === 'ArrowLeft' ? current - 10 : event.key === 'ArrowRight' ? current + 10
|
|
306
|
+
: event.key === 'Home' ? 120 : event.key === 'End' ? sidebarMaximum() : null;
|
|
307
|
+
if (value == null) return;
|
|
308
|
+
event.preventDefault();
|
|
309
|
+
setSidebarWidth(value, true);
|
|
310
|
+
});
|
|
311
|
+
listen(sidebarHandle, 'pointerdown', event => beginDrag(sidebarHandle, event,
|
|
312
|
+
x => setSidebarWidth(Math.round(x - shell.getBoundingClientRect().left), true)));
|
|
313
|
+
listen(sidebarHandle, 'lostpointercapture', () => {
|
|
314
|
+
if (drag?.handle === sidebarHandle) stopDrag();
|
|
315
|
+
});
|
|
316
|
+
listen(window, 'resize', () => setSidebarWidth(state.getItem('preferredSidebarWidth')));
|
|
317
|
+
}
|
|
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');
|
|
321
|
+
for (const tab of choices) {
|
|
322
|
+
const key = tab.id.slice(createEntry && tab.id.startsWith('open-') ? 'open-'.length : containerLayout ? 'panel-'.length : 'tab-'.length);
|
|
251
323
|
const entry = {key, tab, panel: byId(`panel-${key}`),
|
|
252
324
|
code: byId(`codepane-${key}`),
|
|
253
325
|
open: key === 'intro' ? null : byId(`open-${key}`)};
|
|
254
326
|
entries.set(key, entry);
|
|
255
|
-
if (!tab.hidden) opened.setItem(key, true);
|
|
256
|
-
if (tab.getAttribute('aria-selected') === 'true') state.setItem('active', key);
|
|
257
|
-
listen(tab, 'click', () => select(key));
|
|
327
|
+
if (entry.panel && (containerLayout ? node(entry.panel).getAttr('hidden') !== true : !tab.hidden)) opened.setItem(key, true);
|
|
328
|
+
if (!containerLayout && tab.getAttribute('aria-selected') === 'true') state.setItem('active', key);
|
|
329
|
+
if (!containerLayout) listen(tab, 'click', () => select(key));
|
|
258
330
|
if (entry.open) listen(entry.open, 'click', event => { event.preventDefault(); select(key); });
|
|
259
|
-
listen(tab, 'keydown', event => {
|
|
331
|
+
if (!containerLayout) listen(tab, 'keydown', event => {
|
|
260
332
|
if (!state.getItem('keyboard') || !['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
|
|
261
333
|
const keys = [...entries.keys()].filter(key => opened.getItem(key));
|
|
262
334
|
const index = keys.indexOf(key);
|
|
@@ -268,10 +340,22 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
268
340
|
});
|
|
269
341
|
if (key === 'intro') continue;
|
|
270
342
|
entry.family = entry.open.closest('details');
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
343
|
+
if (entry.panel) prepare(entry);
|
|
344
|
+
}
|
|
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
|
+
});
|
|
354
|
+
const subscription = `${shell.id}-selection`;
|
|
355
|
+
state.subscribe(subscription, {any: event => {
|
|
356
|
+
if (event.node === state.getNode('active')) select(state.getItem('active'));
|
|
357
|
+
}});
|
|
358
|
+
releases.push(() => state.unsubscribe(subscription, {any: true}));
|
|
275
359
|
}
|
|
276
360
|
listen(document, 'pointermove', event => {
|
|
277
361
|
if (!drag || event.pointerId !== drag.pointerId) return;
|
|
@@ -286,7 +370,7 @@ export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEn
|
|
|
286
370
|
set(keyboard, {checked: (keyboard?.checked ?? false)});
|
|
287
371
|
for (const [key, entry] of entries) {
|
|
288
372
|
if (entry.open) set(entry.open, {tabindex: (keyboard?.checked ?? false) ? 0 : -1});
|
|
289
|
-
set(entry.tab, {tabindex: (keyboard?.checked ?? false) && key === state.getItem('active') ? 0 : -1});
|
|
373
|
+
if (!containerLayout) set(entry.tab, {tabindex: (keyboard?.checked ?? false) && key === state.getItem('active') ? 0 : -1});
|
|
290
374
|
}
|
|
291
375
|
});
|
|
292
376
|
listen(appearance, 'change', () => {
|