-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathestreUi-pageManager.js
More file actions
667 lines (588 loc) · 31.7 KB
/
Copy pathestreUi-pageManager.js
File metadata and controls
667 lines (588 loc) · 31.7 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
EstreUI rimwork — Page Manager
Part of the split from estreUi.js (roadmap #002 phase 2).
This file is loaded as a plain <script> tag and shares the global scope
with the other estreUi-*.js files. Load order matters: see index.html.
*/
// MODULE: Page Manager -- EstreUiPageManager, EstreUiCustomPageManager
// ======================================================================
/**
* Pages operation manager
*/
/**
* Internal page manager handling page navigation, showing, hiding, and closing.
* Pages are identified by PID (Page IDentifier) strings, with `!` prefix (managed) and `*` prefix (external) alias mapping.
* @class
*/
class EstreUiPageManager {
// class property
// static methods
// constants
// instnace property
/** @type {Object<string, EstreUiPage>} PID → EstreUiPage instance map. */
#pages = {};
/** @type {Object<string, EstreUiPage>} */
get pages() { return this.#pages; }
/** @type {Object<string, string>} Built-in managed page alias → actual PID mapping. Access via `!alert`, `!confirm`, etc. */
#managedPidMap = {
get appbar() { return "$s&h=appbar"; },
get popupBrowser() { return "$i&o=functional#popupBrowser^"; },
get toastAlert() { return "$i&o=toastUpSlide#alert^"; },
get toastConfirm() { return "$i&o=toastUpSlide#confirm^"; },
get toastPrompt() { return "$i&o=toastUpSlide#prompt^"; },
get toastOption() { return "$i&o=toastUpSlide#option^"; },
get toastSelection() { return "$i&o=toastUpSlide#selection^"; },
get toastDials() { return "$i&o=toastUpSlide#dials^"; },
get onRunning() { return "$i&o=interaction#onRunning^"; },
get onProgress() { return "$i&o=interaction#onProgress^"; },
get alert() { return "$i&o=interaction#alert^"; },
get confirm() { return "$i&o=interaction#confirm^"; },
get prompt() { return "$i&o=interaction#prompt^"; },
get option() { return "$i&o=interaction#option^"; },
get selection() { return "$i&o=interaction#selection^"; },
get popNoti() { return "$i&o=notification#noti@noti^"; },
get popNote() { return "$i&o=notification#note@note^"; },
get timeline() { return "$s&o=operation#root@timeline"; },
get quickPanel() { return "$s&o=operation#root@quickPanel"; },
}
/** @type {Object<string, string>|null} External (custom) page alias → actual PID mapping. Access via `*home`, etc. Set by EstreUiCustomPageManager.init(). */
#extPidMap = null;
/** @type {Object<string, string>|null} */
get extPidMap() { return this.#extPidMap; }
/** @param {Object<string, string>} value — Can only be set once. */
set extPidMap(value) {
if (this.#extPidMap == null) this.#extPidMap = value;
}
constructor() {}
/** Initialization hook. Override in subclasses. */
init() {
}
/**
* Registers an EstreUiPage instance with the manager.
* @param {EstreUiPage} euiPage - The page object to register.
*/
register(euiPage) {
const pid = euiPage.pid;
if (this.#pages[pid] == null) {
this.#pages[pid] = euiPage;
}
}
/**
* Finds a full PID from a PID with the statement prefix (`$i`, `$s`) omitted.
* @param {string|null} pid - Full or partial PID.
* @returns {string|null} The matching full PID, or null.
*/
findPid(pid) {
if (pid == null) return null;
else if (this.get(pid) != null) return pid;
else if (this.get("$i" + pid) != null) return "$i" + pid;
else if (this.get("$s" + pid) != null) return "$s" + pid;
else if (this.get("$i" + pid + "^") != null) return "$i" + pid + "^";
else if (this.get("$s" + pid + "^") != null) return "$s" + pid + "^";
else return null;
}
/**
* Looks up a page by PID.
* @param {string} pid - Full PID.
* @returns {EstreUiPage|undefined}
*/
get(pid) {
return this.pages[pid];
}
/**
* Looks up a page by component ID.
* @param {string} id - Component ID.
* @param {string} [sectionBound="blind"] - Section bound ("main"|"blind"|"menu"|"overlay"|"header").
* @param {string} [statement] - "static" or "instant".
* @returns {EstreUiPage|null}
*/
getComponent(id, sectionBound = "blind", statement) {
var pid = this.findPid(EstreUiPage.getPidComponent(id, sectionBound, statement));
if (pid != null) return this.get(pid);
else return null;
}
/**
* Looks up a page by container ID.
* @param {string} id - Container ID.
* @param {string} componentId - Parent component ID.
* @param {string} [sectionBound] - Section bound.
* @param {string} [statement] - "static" or "instant".
* @returns {EstreUiPage|null}
*/
getContainer(id, componentId, sectionBound, statement) {
var pid = this.findPid(EstreUiPage.getPidContainer(id, componentId, sectionBound, statement));
if (pid != null) return this.get(pid);
else return null;
}
/**
* Looks up a page by article ID.
* @param {string} id - Article ID.
* @param {string} containerId - Parent container ID.
* @param {string} componentId - Parent component ID.
* @param {string} [sectionBound] - Section bound.
* @param {string} [statement] - "static" or "instant".
* @returns {EstreUiPage|null}
*/
getArticle(id, containerId, componentId, sectionBound, statement) {
var pid = this.findPid(EstreUiPage.getPidArticle(id, containerId, componentId, sectionBound, statement));
if (pid != null) return this.get(pid);
else return null;
}
/**
* Returns the total number of step article pages.
* @param {string} articleStepsId - Base ID for the step articles (e.g. "step").
* @param {string} containerId - Parent container ID.
* @param {string} componentId - Parent component ID.
* @param {string} [sectionBound] - Section bound.
* @returns {number} Number of step pages.
*/
getStepPagesLength(articleStepsId, containerId, componentId, sectionBound) {
var pid0 = this.findPid(EstreUiPage.getPidArticle(articleStepsId + "%0", containerId, componentId, sectionBound));
var pidPrefix = pid0.split("%")[0];
var length = 0;
for (var pid in this.pages) if (pid.indexOf(pidPrefix) === 0) length++;
return length;
}
/**
* Navigates (brings) a page by PID. Creates (opens) the component/container/article if absent, or shows it if already present.
* `!` prefix resolves via managedPidMap, `*` prefix via extPidMap.
* @param {string} pid - Full PID, or `!`/`*` prefixed alias.
* @param {EstreIntent} [intent] - Intent data passed to the page handler.
* @param {string|string[]} [instanceOrigin] - Instance origin for multi-instance pages.
* @returns {*|null|false} Result for the target hostType. null if page not found, false if cannot open.
*/
bringPage(pid, intent, instanceOrigin) {
if (pid.indexOf("!") > -1) pid = this.#managedPidMap[pid.replace(/^\!/, "")];
if (pid == null) return null;
if (pid.indexOf("*") > -1) pid = this.extPidMap[pid.replace(/^\*/, "")];
if (pid == null) return null;
if (pid.indexOf("$") < 0) pid = this.findPid(pid);
const page = this.get(pid);
if (page == null) return null;
page.setInstanceOrigin(instanceOrigin);
const sections = page.sections;
if (sections == null) return null;
if (intent?.bringOnBack != n && intent.bringOnBack.hostType == n) intent.bringOnBack.hostType = page.hostType;
//check open component
const isIntentNone = typeof intent == UNDEFINED;
const componentInstanceOrigin = page.componentInstanceOrigin;
var componentIntentPushed = false;
var component = sections[page.componentInstanceId];
var existComponent = false;
if (component == null) {
if (page.componentIsInatant) {
if (page.isMenu) {
if (page.isComponent) {
component = estreUi.openMenuArea(page.component, intent, componentInstanceOrigin);
componentIntentPushed = true;
} else component = estreUi.openMenuArea(page.component, u, componentInstanceOrigin);
} else if (page.isBlinded) {
if (page.isComponent) {
component = estreUi.openInstantBlinded(page.component, intent, componentInstanceOrigin);
componentIntentPushed = true;
} else component = estreUi.openInstantBlinded(page.component, u, componentInstanceOrigin);
} else if (page.isOverlay) {
if (page.isComponent) {
component = estreUi.openManagedOverlay(page.component, intent, componentInstanceOrigin);
componentIntentPushed = true;
} else component = estreUi.openManagedOverlay(page.component, u, componentInstanceOrigin);
} else if (page.isHeader) {
if (page.isComponent) {
component = estreUi.openHeaderBar(page.component, intent, componentInstanceOrigin);
componentIntentPushed = true;
} else component = estreUi.openHeaderBar(page.component, u, componentInstanceOrigin);
} else return false;
} else return false;
} else existComponent = true;
if (component == null) return null;
const containerInstanceOrigin = page.containerInstanceOrigin;
const articleInstanceOrigin = page.articleInstanceOrigin;
var containerIntentPushed = false;
var articleIntentPushed = false;
var container = null;
var article = null;
var existContainer = false;
var existArticle = false;
if (page.container != null) {
//check open container
container = component.containers[page.containerInstanceId];
if (container == null) {
if (page.containerIsInatant) {
if (page.isArticle || page.isContainer || page.container == "root") {
container = component.openContainer(page.container, intent, containerInstanceOrigin);
containerIntentPushed = true;
} else container = component.openContainer(page.container, u, containerInstanceOrigin);
} else if (page.isContainer) return false;//static container is cannot open
else {
}
} else existContainer = true;
if (container == null) return null;
if (page.article != null) {
//check open article
article = container.articles[page.articleInstanceId];
if (article == null) {
if (page.articleIsInatant) {
if (page.isArticle || page.article == "main") {
article = container.openArticle(page.article, intent, articleInstanceOrigin);
articleIntentPushed = true;
} else article = container.openArticle(page.article, u, articleInstanceOrigin);
} else return false;//static article is cannot open
} else existArticle = true;
if (article == null) return null;
}
}
var success = true;
var targetProcessed = { component: null, container: null, article: null };
const isRootMain = page.container == "root" && page.article == "main";
switch (page.hostType) {
case "article":
if (!isIntentNone && existArticle && (page.isArticle || page.article == "main")) targetProcessed.article = container.showArticle(page.article, intent, articleInstanceOrigin);
else targetProcessed.article = article.show();
success = targetProcessed.article;
// falls through
case "container":
if (success) {
if (!isIntentNone && existContainer && (page.isContainer || isRootMain)) targetProcessed.container = component.showContainer(page.container, intent, containerInstanceOrigin);
else targetProcessed.container = container.show();
success = targetProcessed.container;
}
// falls through
case "component":
if (success) {
if (page.isHeader) {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showHeaderBar(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showHeaderBar(page.component, u, componentInstanceOrigin);
} else if (page.isMenu) {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showMenuArea(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showMenuArea(page.component, u, componentInstanceOrigin);
} else if (page.isOverlay) {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showManagedOverlay(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showManagedOverlay(page.component, u, componentInstanceOrigin);
} else if (page.isBlinded) {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showInstantBlinded(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showInstantBlinded(page.component, u, componentInstanceOrigin);
} else if (component.isModal) {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.openModalTab(page.component, component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.openModalTab(page.component, u, componentInstanceOrigin);
} else {
if (!isIntentNone && existComponent && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.switchRootTab(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.mainCurrentOnTop == component || estreUi.switchRootTab(page.component, u, componentInstanceOrigin);
}
success = targetProcessed.component;
}
}
if (window.isVerbosely) console.log("[bringPage] targetProcessed: ", targetProcessed);
return targetProcessed[page.hostType];
}
/**
* Shows an already-existing page. Returns null if the component/container/article does not exist.
* @param {string} pid - Full PID, or `!`/`*` prefixed alias.
* @param {EstreIntent} [intent] - Intent data passed to the page handler.
* @param {string|string[]} [instanceOrigin] - Instance origin for multi-instance pages.
* @returns {*|null} Result for the target hostType. null if page not found.
*/
showPage(pid, intent, instanceOrigin) {
if (pid.indexOf("!") > -1) pid = this.#managedPidMap[pid.replace(/^\!/, "")];
if (pid == null) return null;
if (pid.indexOf("*") > -1) pid = this.extPidMap[pid.replace(/^\*/, "")];
if (pid == null) return null;
if (pid.indexOf("$") < 0) pid = this.findPid(pid);
const page = this.get(pid);
if (page == null) return null;
page.setInstanceOrigin(instanceOrigin);
const sections = page.sections;
if (sections == null) return null;
if (intent?.bringOnBack != n && intent.bringOnBack.hostType == n) intent.bringOnBack.hostType = page.hostType;
const isIntentNone = typeof intent == UNDEFINED;
var component = sections[page.componentInstanceId];
if (component == null) return null;
const componentInstanceOrigin = page.componentInstanceOrigin;
const containerInstanceOrigin = page.containerInstanceOrigin;
const articleInstanceOrigin = page.articleInstanceOrigin;
var container = null;
var article = null;
if (page.container != null) {
container = component.containers[page.containerInstanceId];
if (container == null) return null;
if (page.article != null) {
article = container.articles[page.articleInstanceId];
if (article == null) return null;
}
}
var success = true;
var targetProcessed = { component: null, container: null, article: null };
switch (page.hostType) {
case "article":
if (!isIntentNone && (page.isArticle || page.article == "main")) targetProcessed.article = container.showArticle(page.article, intent, articleInstanceOrigin);
else targetProcessed.article = article.show();
success = targetProcessed.article;
// falls through
case "container":
if (success) {
if (!isIntentNone && (page.isContainer || (page.article == "main" && page.container == "root"))) targetProcessed.container = component.showContainer(page.container, intent, containerInstanceOrigin);
else targetProcessed.container = container.show();
success = targetProcessed.container;
}
// falls through
case "component":
if (success) {
const isRootMain = page.container == "root" && page.article == "main";
if (page.isOverlay) {
if (!isIntentNone && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showManagedOverlay(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showManagedOverlay(page.component, u, componentInstanceOrigin);
} else if (page.isMenu) {
if (!isIntentNone && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showMenuArea(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showMenuArea(page.component, u, componentInstanceOrigin);
} else if (page.isBlinded) {
if (!isIntentNone && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.showInstantBlinded(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.showInstantBlinded(page.component, u, componentInstanceOrigin);
} else if (component.isModal) {
if (!isIntentNone && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.openModalTab(page.component, component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.openModalTab(page.component, component, u, componentInstanceOrigin);
} else {
if (!isIntentNone && (page.isComponent || isRootMain)) targetProcessed.component = estreUi.switchRootTab(page.component, intent, componentInstanceOrigin);
else targetProcessed.component = estreUi.mainCurrentOnTop == component || estreUi.switchRootTab(page.component, intent, u, componentInstanceOrigin);
}
success = targetProcessed.component;
}
}
if (window.isVerbosely) console.log("[showPage] targetProcessed: ", targetProcessed);
return targetProcessed[page.hostType];
}
/**
* Attempts showPage first; falls back to bringPage if the result is falsy.
* @param {string} pid - Full PID, or `!`/`*` prefixed alias.
* @param {EstreIntent} [intent] - Intent data passed to the page handler.
* @param {string|string[]} [instanceOrigin] - Instance origin for multi-instance pages.
* @returns {*|null|false}
*/
showOrBringPage(pid, intent, instanceOrigin) {
return this.showPage(pid, intent, instanceOrigin) || this.bringPage(pid, intent, instanceOrigin);
}
/**
* Hides a page. Sequentially hides the article/container/component based on hostType.
* @param {string} pid - Full PID, or `!`/`*` prefixed alias.
* @param {boolean} [hideHost=false] - If true, also hides the parent host.
* @param {string|string[]|null} [instanceOrigin=null] - Instance origin for multi-instance pages.
* @returns {*|null} Result for the target hostType.
*/
hidePage(pid, hideHost = false, instanceOrigin = null) {
if (pid.indexOf("!") > -1) pid = this.#managedPidMap[pid.replace(/^\!/, "")];
if (pid == null) return null;
if (pid.indexOf("*") > -1) pid = this.extPidMap[pid.replace(/^\*/, "")];
if (pid == null) return null;
if (pid.indexOf("$") < 0) pid = this.findPid(pid);
const page = this.get(pid);
if (page == null) return null;
page.setInstanceOrigin(instanceOrigin);
const sections = page.sections;
if (sections == null) return null;
var component = sections[page.componentInstanceId];
if (component == null) return null;
var container = null;
var article = null;
const componentInstanceOrigin = page.componentInstanceOrigin;
const containerInstanceOrigin = page.containerInstanceOrigin;
const articleInstanceOrigin = page.articleInstanceOrigin;
var targetProcessed = { component: null, container: null, article: null };
if (page.container != null) {
container = component.containers[page.containerInstanceId];
if (container != null) {
if (page.article != null) {
article = container.articles[page.articleInstanceId];
if (article != null) {
targetProcessed.article = article.hide();
}
}
if (page.isContainer || hideHost || (page.isArticle && page.articleIsStatic && container.isArticlesAllyStatic)) {
targetProcessed.container = container.hide();
}
}
}
if (page.isComponent || hideHost || (!page.isComponent && page.containerIsStatic && component.isContainersAllyStatic)) {
if (page.isOverlay || page.isMenu || page.isBlinded) {
targetProcessed.component = component.hide();
} else if (component.isModal) {
targetProcessed.component = estreUi.closeModalTab(page.component, $(component));
} else {
targetProcessed.component = estreUi.switchRootTab("home");
}
}
if (window.isVerbosely) console.log("[hidePage] targetProcessed: ", targetProcessed);
return targetProcessed[page.hostType];
}
/**
* Closes a page (async). Sequentially closes the article/container/component based on hostType.
* @param {string} pid - Full PID, or `!`/`*` prefixed alias.
* @param {boolean} [closeHost=false] - If true, also closes the parent host.
* @param {string|string[]} [instanceOrigin] - Instance origin for multi-instance pages.
* @returns {Promise<*|null>} Result for the target hostType.
*/
closePage(pid, closeHost = false, instanceOrigin) {
return postPromise(resolve => {
postQueue(async _ => {
if (pid.indexOf("!") > -1) pid = this.#managedPidMap[pid.replace(/^\!/, "")];
if (pid == null) return resolve(null);
if (pid.indexOf("*") > -1) pid = this.extPidMap[pid.replace(/^\*/, "")];
if (pid == null) return resolve(null);
if (pid.indexOf("$") < 0) pid = this.findPid(pid);
const page = this.get(pid);
if (page == null) return resolve(null);
page.setInstanceOrigin(instanceOrigin);
const sections = page.sections;
if (sections == null) return resolve(null);
var component = sections[page.componentInstanceId];
if (component == null) return resolve(null);
var container = null;
var article = null;
const componentInstanceOrigin = page.componentInstanceOrigin;
const containerInstanceOrigin = page.containerInstanceOrigin;
const articleInstanceOrigin = page.articleInstanceOrigin;
var targetProcessed = { component: null, container: null, article: null };
if (page.container != null) {
container = component.containers[page.containerInstanceId];
if (container != null) {
if (page.article != null) {
article = container.articles[page.articleInstanceId];
if (article != null) {
targetProcessed.article = await container.closeArticle(page.article, articleInstanceOrigin);
}
}
if (page.isContainer || closeHost || (page.isArticle && page.articleIsStatic && container.isArticlesAllyStatic)) {
targetProcessed.container = await component.closeContainer(page.container, containerInstanceOrigin);
}
}
}
if (page.isComponent || closeHost || (!page.isComponent && page.containerIsStatic && component.isContainersAllyStatic)) {
if (page.isOverlay) {
targetProcessed.component = await estreUi.closeManagedOverlay(page.component, componentInstanceOrigin);
} else if (page.isMenu) {
targetProcessed.component = await estreUi.closeMenuArea(page.component, componentInstanceOrigin);
} else if (page.isBlinded) {
targetProcessed.component = await estreUi.closeInstantBlinded(page.component, componentInstanceOrigin);
} else if (component.isModal) {
targetProcessed.component = await estreUi.closeModalTab(page.component, $(component), componentInstanceOrigin);
} else {
targetProcessed.component = await estreUi.switchRootTab("home");
}
}
if (window.isVerbosely) console.log("[closePage] targetProcessed: ", targetProcessed);
resolve(targetProcessed[page.hostType]);
});
});
}
/**
* Default auto-focus policy for a newly-focused page handle.
* Priority:
* 1. On repeat focus, restore `handle.lastFocusedElement` if still in the DOM.
* 2. First `[data-autofocus]` element inside the host.
* 3. First tab-reachable focusable element inside the host.
* 4. Otherwise no-op.
* Invoked from `pageHandle.onFocus()` when `handler.onFocus` does not return true.
* Projects can override on an `EstreUiCustomPageManager` subclass if a different policy is needed.
* @param {EstrePageHandle} handle - The page handle receiving focus.
* @param {boolean} isFirstFocus - True on the first focus after onOpen; false on subsequent focuses.
* @returns {boolean} Whether a focus() call succeeded.
*/
autoFocus(handle, isFirstFocus) {
const host = handle?.host;
if (host == null) return false;
if (!isFirstFocus) {
const last = handle.lastFocusedElement;
if (last != null && host.contains(last) && document.body.contains(last)) {
last.focus();
if (document.activeElement === last) return true;
}
}
const markedTarget = host.querySelector("[data-autofocus]");
if (markedTarget != null && !markedTarget.hasAttribute("disabled") && !markedTarget.hidden) {
markedTarget.focus();
if (document.activeElement === markedTarget) return true;
}
const candidates = host.querySelectorAll(
'input:not([disabled]),textarea:not([disabled]),select:not([disabled]),button:not([disabled]),[tabindex]:not([tabindex="-1"])'
);
for (const el of candidates) {
if (el.hidden) continue;
el.focus();
if (document.activeElement === el) return true;
}
return false;
}
}
const pageManager = new EstreUiPageManager();
/**
* Base format for project-specific custom page managers.
* Registers extPidMap and pageHandlers, delegating page navigation via `*` prefixed aliases.
* @class
*/
class EstreUiCustomPageManager {
// class property
// static methods
// constants
// instnace property
constructor() {}
/**
* Initializes the custom page manager. Must be called after estreUi initialization.
* @param {Object<string, string>} extPidMap - Alias → actual PID mapping (e.g. `{ home: "$s&m=home#root@main" }`).
* @param {Object<string, EstrePageHandler>} pageHandlers - Alias → page handler instance mapping.
* @returns {this} this for chaining.
*/
init(extPidMap, pageHandlers) {
pageManager.extPidMap = extPidMap;
EstreUiPage.registerProvider(pageHandlers);
for (var id in pageHandlers) EstreUiPage.registerHandler(extPidMap[id], pageHandlers[id]);
return this;
}
/**
* Brings a page by external alias ID.
* @param {string} id - Alias registered in extPidMap.
* @param {EstreIntent} [intent] - Intent data passed to the page handler.
* @param {string|string[]} [instanceOrigin] - Multi-instance origin.
* @returns {*|null|false}
*/
bringPage(id, intent, instanceOrigin) {
return pageManager.bringPage("*" + id, intent, instanceOrigin);
}
/**
* Shows a page by external alias ID.
* @param {string} id - Alias registered in extPidMap.
* @param {EstreIntent} [intent] - Intent data.
* @param {string|string[]} [instanceOrigin] - Multi-instance origin.
* @returns {*|null}
*/
showPage(id, intent, instanceOrigin) {
return pageManager.showPage("*" + id, intent, instanceOrigin);
}
/**
* Attempts showPage; falls back to bringPage on failure.
* @param {string} id - Alias registered in extPidMap.
* @param {EstreIntent} [intent] - Intent data.
* @param {string|string[]} [instanceOrigin] - Multi-instance origin.
* @returns {*|null|false}
*/
showOrBringPage(id, intent, instanceOrigin) {
return pageManager.showOrBringPage("*" + id, intent, instanceOrigin);
}
/**
* Hides a page by external alias ID.
* @param {string} id - Alias registered in extPidMap.
* @param {boolean} [hideHost=false] - Whether to also hide the parent host.
* @param {string|string[]|null} [instanceOrigin=null] - Multi-instance origin.
* @returns {*|null}
*/
hidePage(id, hideHost = false, instanceOrigin = null) {
return pageManager.hidePage("*" + id, hideHost, instanceOrigin);
}
/**
* Closes a page by external alias ID (async).
* @param {string} id - Alias registered in extPidMap.
* @param {boolean} [closeHost=false] - Whether to also close the parent host.
* @param {string|string[]|null} [instanceOrigin=null] - Multi-instance origin.
* @returns {Promise<*|null>}
*/
closePage(id, closeHost = false, instanceOrigin = null) {
return pageManager.closePage("*" + id, closeHost, instanceOrigin);
}
}
// ======================================================================