@api-client/ui 0.0.9 → 0.0.11

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.
Files changed (56) hide show
  1. package/.eslintrc +8 -1
  2. package/demo/index.html +3 -0
  3. package/demo/layout/index.html +91 -0
  4. package/demo/layout/index.ts +182 -0
  5. package/dist/elements/layout/SplitItem.d.ts +1 -9
  6. package/dist/elements/layout/SplitItem.d.ts.map +1 -1
  7. package/dist/elements/layout/SplitItem.js +27 -20
  8. package/dist/elements/layout/SplitItem.js.map +1 -1
  9. package/dist/elements/layout/SplitLayout.d.ts +16 -14
  10. package/dist/elements/layout/SplitLayout.d.ts.map +1 -1
  11. package/dist/elements/layout/SplitLayout.js +47 -42
  12. package/dist/elements/layout/SplitLayout.js.map +1 -1
  13. package/dist/elements/layout/SplitPanel.d.ts +7 -2
  14. package/dist/elements/layout/SplitPanel.d.ts.map +1 -1
  15. package/dist/elements/layout/SplitPanel.js +130 -52
  16. package/dist/elements/layout/SplitPanel.js.map +1 -1
  17. package/dist/elements/layout/SplitView.d.ts.map +1 -1
  18. package/dist/elements/layout/SplitView.js +18 -14
  19. package/dist/elements/layout/SplitView.js.map +1 -1
  20. package/dist/elements/layout/type.d.ts +3 -3
  21. package/dist/elements/layout/type.d.ts.map +1 -1
  22. package/dist/elements/layout/type.js.map +1 -1
  23. package/dist/elements/schema-design/DataModelVisualizationElement.d.ts.map +1 -1
  24. package/dist/elements/schema-design/DataModelVisualizationElement.js +18 -1
  25. package/dist/elements/schema-design/DataModelVisualizationElement.js.map +1 -1
  26. package/dist/pages/http-project/HttpClientCommands.d.ts.map +1 -1
  27. package/dist/pages/http-project/HttpClientCommands.js +28 -12
  28. package/dist/pages/http-project/HttpClientCommands.js.map +1 -1
  29. package/package.json +2 -1
  30. package/src/elements/layout/SplitItem.ts +29 -21
  31. package/src/elements/layout/SplitLayout.ts +53 -43
  32. package/src/elements/layout/SplitPanel.ts +140 -57
  33. package/src/elements/layout/SplitView.ts +18 -15
  34. package/src/elements/layout/type.ts +3 -4
  35. package/src/elements/schema-design/DataModelVisualizationElement.ts +18 -1
  36. package/src/pages/http-project/HttpClientCommands.ts +28 -12
  37. package/test/elements/layout/SplitItem.test.ts +76 -75
  38. package/test/elements/layout/SplitLayoutManager.test.ts +70 -69
  39. package/test/elements/layout/SplitPanel.test.ts +10 -7
  40. package/tsconfig.eslint.json +8 -0
  41. package/web-test-runner.config.mjs +4 -1
  42. package/dist/define/layout/layout-panel.d.ts +0 -7
  43. package/dist/define/layout/layout-panel.d.ts.map +0 -1
  44. package/dist/define/layout/layout-panel.js +0 -3
  45. package/dist/define/layout/layout-panel.js.map +0 -1
  46. package/dist/elements/layout/LayoutManager.d.ts +0 -327
  47. package/dist/elements/layout/LayoutManager.d.ts.map +0 -1
  48. package/dist/elements/layout/LayoutManager.js +0 -747
  49. package/dist/elements/layout/LayoutManager.js.map +0 -1
  50. package/dist/elements/layout/LayoutPanelElement.d.ts +0 -62
  51. package/dist/elements/layout/LayoutPanelElement.d.ts.map +0 -1
  52. package/dist/elements/layout/LayoutPanelElement.js +0 -628
  53. package/dist/elements/layout/LayoutPanelElement.js.map +0 -1
  54. package/src/define/layout/layout-panel.ts +0 -9
  55. package/src/elements/layout/LayoutManager.ts +0 -930
  56. package/src/elements/layout/LayoutPanelElement.ts +0 -651
@@ -1,930 +0,0 @@
1
- /* eslint-disable prefer-destructuring */
2
- /* eslint-disable max-classes-per-file */
3
-
4
- import { TemplateResult, html } from 'lit';
5
- import { Events } from '../../events/Events.js';
6
- // eslint-disable-next-line import/no-cycle
7
- import LayoutPanelElement from './LayoutPanelElement.js';
8
- import '../../define/layout/layout-panel.js';
9
- import { IconType } from '../../ui/icons/Icons.js';
10
-
11
- export type Direction = 'horizontal' | 'vertical';
12
- export type DropRegion = 'center' | 'east' | 'west' | 'north' | 'south';
13
-
14
- enum PanelState {
15
- Idle,
16
- Busy
17
- }
18
-
19
- export interface ILayoutItem {
20
- /**
21
- * The kind of opened item
22
- */
23
- kind: string;
24
- /**
25
- * The key of the opened item
26
- */
27
- key: string;
28
- /**
29
- * Optional parent information that helps locating this object.
30
- */
31
- parent?: string;
32
- /**
33
- * The label to render in the tab.
34
- */
35
- label: string;
36
- /**
37
- * Whether the tab is pinned (cannot be closed or moved).
38
- */
39
- pinned?: boolean;
40
- /**
41
- * The tab index.
42
- */
43
- index?: number;
44
- /**
45
- * The icon defined in the internal library to render with the tab.
46
- */
47
- icon?: IconType;
48
- /**
49
- * A tab that is always present in the layout. The user can't close this tab.
50
- */
51
- persistent?: boolean;
52
- /**
53
- * A property to be used by the screen to indicate the property is being loaded
54
- * (from a data store, file, etc).
55
- */
56
- loading?: boolean;
57
- /**
58
- * Indicates the item has been changed and is out of sync with the data store.
59
- */
60
- isDirty?: boolean;
61
- }
62
-
63
- export interface ILayoutOptions {
64
- /**
65
- * The list of DataTransfer types to test against when handling drag and drop.
66
- * When set it checks whether all types are set on the dragged item.
67
- * If not set all items are allowed.
68
- */
69
- dragTypes?: string[];
70
-
71
- /**
72
- * The local store key to use with `autoStore`,
73
- */
74
- storeKey?: string;
75
-
76
- /**
77
- * Uses application's internal events system to store the layout data
78
- * in the local store, when anything change.
79
- * This must be set with the `storeKey` property.
80
- *
81
- * When this is set it also restores the state during initialization.
82
- */
83
- autoStore?: boolean;
84
-
85
- /**
86
- * When set it adds the `overflow` hidden on the container that holds the tab contents.
87
- */
88
- constrain?: boolean;
89
- }
90
-
91
- export interface IPanelSplitOptions {
92
- layout?: Direction;
93
- itemsTarget?: 0 | 1;
94
- }
95
-
96
- export type ItemRenderCallback = (item: ILayoutItem, visible: boolean) => TemplateResult;
97
-
98
- export interface ILayoutPanelState {
99
- id: number;
100
- layout: Direction;
101
- panels?: ILayoutPanelState[];
102
- items?: ILayoutItem[];
103
- selected?: string;
104
- }
105
-
106
- export interface ILayoutState {
107
- panels: ILayoutPanelState[];
108
- }
109
-
110
- export interface ILayoutItemAddOptions {
111
- /**
112
- * The region to add the item.
113
- * When other than `center` it splits the panel.
114
- */
115
- region?: DropRegion;
116
- /**
117
- * The index at which to put the item.
118
- * By default it is added as a last item.
119
- */
120
- index?: number;
121
- }
122
-
123
- let panelId = 0;
124
-
125
- export class LayoutPanel {
126
- id = panelId++;
127
-
128
- parent?: LayoutPanel;
129
-
130
- manager: LayoutManager
131
-
132
- layout: Direction = 'horizontal';
133
-
134
- panels: LayoutPanel[] = [];
135
-
136
- items: ILayoutItem[] = [];
137
-
138
- /**
139
- * The current state of the panel.
140
- */
141
- state: PanelState = PanelState.Idle;
142
-
143
- /**
144
- * The item being rendered in the panel.
145
- */
146
- selected?: string;
147
-
148
- /**
149
- * The computed size value for each item.
150
- * The value of `undefined` means "auto".
151
- */
152
- sizes: string[] = [];
153
-
154
- get hasPanels(): boolean {
155
- return this.panels.length > 0;
156
- }
157
-
158
- get hasItems(): boolean {
159
- return this.items.length > 0;
160
- }
161
-
162
- static fromSchema(state: ILayoutPanelState, manager: LayoutManager, parent?: LayoutPanel): LayoutPanel {
163
- const panel = new LayoutPanel(manager, parent);
164
- panel.id = state.id;
165
- panel.selected = state.selected;
166
- panel.layout = state.layout;
167
- if (Array.isArray(state.items)) {
168
- panel.items = state.items.map(i => ({ ...i }));
169
- }
170
- if (Array.isArray(state.panels)) {
171
- panel.panels = state.panels.map(i => LayoutPanel.fromSchema(i, manager, panel));
172
- }
173
- return panel;
174
- }
175
-
176
- constructor(manager: LayoutManager, parent?: LayoutPanel) {
177
- this.manager = manager;
178
- this.state = PanelState.Idle;
179
- this.parent = parent;
180
- }
181
-
182
- /**
183
- * @returns Returns a **copy** of the items array sorted by index.
184
- */
185
- sortedItems(): ILayoutItem[] {
186
- const { items } = this;
187
- return [...items].sort((a, b) => (a.index || 0) - (b.index || 0));
188
- }
189
-
190
- /**
191
- * @returns True when the panel accepts drop events.
192
- */
193
- canDrop(): boolean {
194
- if (this.hasItems) {
195
- return true;
196
- }
197
- return !this.hasPanels;
198
- }
199
-
200
- nextIndex(): number {
201
- let result = 0;
202
- if (!this.items.length) {
203
- return result;
204
- }
205
- this.items.forEach((item) => {
206
- const { index = 0 } = item;
207
- if (result < index) {
208
- result = index;
209
- }
210
- });
211
- return result + 1;
212
- }
213
-
214
- /**
215
- * Adds an item to the layout.
216
- *
217
- * @param item The item to add
218
- * @param opts Layout adding item options
219
- * @returns Whether a new item was added to the layout. false when the item is already in the layout panel.
220
- */
221
- addItem(item: ILayoutItem, opts: ILayoutItemAddOptions = {}): boolean {
222
- const { region = 'center' } = opts;
223
- const hasItem = region === 'center' && this.items.some(i => i.key === item.key);
224
- if (hasItem) {
225
- this.selected = item.key;
226
- this.manager.changed();
227
- return false;
228
- }
229
-
230
- const hasIndex = typeof opts.index === 'number';
231
- const index = !hasIndex ? this.nextIndex() : opts.index as number;
232
-
233
- if (region === 'center' || (!this.hasItems && !this.hasPanels)) {
234
- if (hasIndex) {
235
- this.increaseIndex(index + 1);
236
- }
237
- this.manager.nameItem(item);
238
- this.items.push({ ...item, index });
239
- this.selected = item.key;
240
- this.manager.changed();
241
- return true;
242
- }
243
- let panel: LayoutPanel;
244
- if (region === 'east') {
245
- panel = this.split({
246
- layout: 'horizontal',
247
- itemsTarget: 0,
248
- })[1];
249
- } else if (region === 'west') {
250
- panel = this.split({
251
- layout: 'horizontal',
252
- itemsTarget: 1,
253
- })[0];
254
- } else if (region === 'south') {
255
- panel = this.split({
256
- layout: 'vertical',
257
- itemsTarget: 0,
258
- })[1];
259
- } else {
260
- panel = this.split({
261
- layout: 'horizontal',
262
- itemsTarget: 1,
263
- })[0];
264
- }
265
- panel.addItem(item, { index });
266
- this.manager.changed();
267
- return true;
268
- }
269
-
270
- /**
271
- * Splits this panel into 2 panels.
272
- * This to be used when the panel has no other panels. Only items are allowed.
273
- * It produces 2 new panels and moves the items to the first one leaving the other one available.
274
- */
275
- split(opts: IPanelSplitOptions = {}): LayoutPanel[] {
276
- const { layout = 'horizontal', itemsTarget = 0 } = opts;
277
- if (this.hasPanels) {
278
- throw new Error(`Invalid state. Panels can be split only when containing items only.`);
279
- }
280
- this.layout = layout;
281
- const { items, selected } = this;
282
- this.items = [];
283
- this.selected = undefined;
284
- const p1 = new LayoutPanel(this.manager, this);
285
- const p2 = new LayoutPanel(this.manager, this);
286
- this.panels = [p1, p2];
287
- this.panels[itemsTarget].items = items;
288
- this.panels[itemsTarget].selected = selected;
289
- this.manager.changed();
290
- return this.panels;
291
- }
292
-
293
- unsplit(): void {
294
- const { items, selected } = this.panels[0];
295
- this.panels = [];
296
- this.items = items;
297
- this.selected = selected;
298
- this.manager.changed();
299
- }
300
-
301
- /**
302
- * Decreases items index by 1 to all items with index at least equal to `fromIndex`.
303
- * @param fromIndex The minimal index to affect.
304
- */
305
- decreaseIndex(fromIndex: number): void {
306
- for (const item of this.items) {
307
- const { index = 0 } = item;
308
- if (index >= fromIndex && index > 0) {
309
- (item.index as number) -= 1;
310
- }
311
- }
312
- }
313
-
314
- /**
315
- * Increases items index by 1 to all items with index at least equal to `fromIndex`.
316
- * @param fromIndex The minimal index to affect.
317
- */
318
- increaseIndex(fromIndex: number): void {
319
- for (const item of this.items) {
320
- const { index = 0 } = item;
321
- if (index >= fromIndex) {
322
- (item.index as number) += 1;
323
- }
324
- }
325
- }
326
-
327
- /**
328
- * Removes an item from the layout
329
- * @param key The key of the item.
330
- * @returns The removed item, if any.
331
- */
332
- removeItem(key: string): ILayoutItem | undefined {
333
- const index = this.items.findIndex(i => i.key === key);
334
- if (index < 0) {
335
- return undefined;
336
- }
337
- const removed = this.items[index];
338
- this.items.splice(index, 1);
339
- this.decreaseIndex(removed.index || 0);
340
-
341
- if (this.items.length === 0) {
342
- // remove panel
343
- if (this.parent) {
344
- this.parent.removePanel(this.id);
345
- }
346
- }
347
- if (this.selected === key) {
348
- let nextKey: string | undefined;
349
- if (this.items[index]) {
350
- nextKey = this.items[index].key;
351
- } else if (this.items[index - 1]) {
352
- nextKey = this.items[index - 1].key;
353
- }
354
- this.selected = nextKey;
355
- }
356
- this.manager.dispatchEvent(new CustomEvent('closetab', {
357
- bubbles: true,
358
- cancelable: true,
359
- composed: true,
360
- detail: key,
361
- }));
362
- this.manager.changed();
363
- this.manager.forceUpdateLayout(this.id);
364
- return removed;
365
- }
366
-
367
- /**
368
- * @param key The key of the item to perform a relative operation from.
369
- * @param dir The direction to which close other items. Default to both directions leaving only the `key` item
370
- */
371
- relativeClose(key: string, dir: 'left' | 'right' | 'both' = 'both'): void {
372
- const index = this.items.findIndex(i => i.key === key);
373
- if (index < 0) {
374
- return;
375
- }
376
- const item = this.items[index];
377
- if (dir === 'both') {
378
- this.items = [item];
379
- this.selected = item.key;
380
- } else if (dir === 'left') {
381
- this.items = this.items.splice(index);
382
- this.selected = item.key;
383
- } else {
384
- this.items = this.items.splice(0, index + 1);
385
- this.selected = item.key;
386
- }
387
- this.manager.changed();
388
- this.manager.forceUpdateLayout(this.id);
389
- }
390
-
391
- removePanel(id: number): void {
392
- const index = this.panels.findIndex(p => p.id === id);
393
- if (index < 0) {
394
- return;
395
- }
396
- this.panels.splice(index, 1);
397
- if (this.panels.length === 1) {
398
- this.unsplit();
399
- }
400
- this.manager.changed();
401
- this.manager.forceUpdateLayout(this.id);
402
- }
403
-
404
- /**
405
- * Moves an item to a new index.
406
- *
407
- * @param key The item key
408
- * @param toIndex The new index. When not set it moves the item to the end.
409
- */
410
- moveItem(key: string, toIndex?: number): void {
411
- const item = this.items.find(i => i.key === key);
412
- if (!item) {
413
- return;
414
- }
415
- const hasIndex = typeof toIndex === 'number';
416
- if (hasIndex && item.index === toIndex) {
417
- return;
418
- }
419
- let hasTargetAtTarget = false;
420
- if (hasIndex) {
421
- hasTargetAtTarget = !!this.items[toIndex];
422
- }
423
- if (item.index !== undefined) {
424
- this.decreaseIndex(item.index);
425
- }
426
- const finalIndex = hasIndex ? toIndex as number : this.nextIndex();
427
- if (hasTargetAtTarget) {
428
- this.increaseIndex(finalIndex);
429
- }
430
- item.index = finalIndex;
431
- this.manager.changed();
432
- this.manager.forceUpdateLayout(this.id);
433
- }
434
-
435
- rename(key: string, label: string): void {
436
- const item = this.items.find(i => i.key === key);
437
- if (!item) {
438
- return;
439
- }
440
- item.label = label;
441
- this.manager.changed();
442
- this.manager.forceUpdateLayout(this.id);
443
- }
444
-
445
- toJSON(): ILayoutPanelState {
446
- const result:ILayoutPanelState = {
447
- id: this.id,
448
- layout: this.layout,
449
- };
450
- if (this.items.length) {
451
- result.items = this.items.map(i => ({ ...i }));
452
- }
453
- if (this.panels) {
454
- result.panels = this.panels.map(i => i.toJSON());
455
- }
456
- if (this.selected) {
457
- result.selected = this.selected;
458
- }
459
- return result;
460
- }
461
-
462
- panelTemplate(panel: LayoutPanel, itemCallback: ItemRenderCallback): TemplateResult | undefined {
463
- const { layout, panels, items, selected } = panel;
464
- const { manager } = this;
465
- const { dragTypes, constrain } = manager.opts;
466
- let content: (TemplateResult | undefined)[];
467
- if (panels.length) {
468
- content = panels.map(p => this.panelTemplate(p, itemCallback));
469
- } else {
470
- content = items.map(p => this.itemTemplate(p, p.key === selected, itemCallback));
471
- }
472
- const valid = content.filter(i => !!i);
473
- if (!valid.length) {
474
- return undefined;
475
- }
476
- return html`
477
- <layout-panel
478
- layout="${layout}"
479
- .dragTypes="${dragTypes}"
480
- .panel="${panel}"
481
- .layoutId="${panel.id}"
482
- ?constrain="${constrain}"
483
- >
484
- ${content}
485
- </layout-panel>
486
- `;
487
- }
488
-
489
- itemTemplate(item: ILayoutItem, visible: boolean, itemCallback: ItemRenderCallback): TemplateResult {
490
- return itemCallback(item, visible);
491
- }
492
-
493
- render(itemCallback: ItemRenderCallback): TemplateResult | undefined {
494
- return this.panelTemplate(this as LayoutPanel, itemCallback);
495
- }
496
- }
497
-
498
- /**
499
- * Layout manager for API Client apps.
500
- *
501
- * Supports:
502
- * - layout splitting depending on the selected region (east, west, north, south)
503
- * - drag and drop of items into the layout
504
- * - auto storing and restoring layout state from the application local storage.
505
- * - adding items to the last focused panel
506
- *
507
- * Limitations
508
- * - the rendered content has to be focusable (tabindex must be set) in order to detect active panel
509
- */
510
- export class LayoutManager extends EventTarget {
511
- opts: ILayoutOptions;
512
-
513
- panels: LayoutPanel[] = [];
514
-
515
- protected isDirty = false;
516
-
517
- protected storing = false;
518
-
519
- private _activePanel?: LayoutPanel;
520
-
521
- /**
522
- * An active panel
523
- */
524
- get activePanel(): LayoutPanel | undefined {
525
- if (!this._activePanel) {
526
- this._activePanel = this.findFirstItemsPanel();
527
- }
528
- return this._activePanel;
529
- }
530
-
531
- constructor(opts: ILayoutOptions = {}) {
532
- super();
533
- this.opts = opts;
534
- }
535
-
536
- /**
537
- * @param itemCallback The callback called when rendering an item in layout view.
538
- * @returns The template for the page layout.
539
- */
540
- render(itemCallback: ItemRenderCallback): TemplateResult[] {
541
- const result: TemplateResult[] = [];
542
- this.panels.forEach(p => {
543
- const content = p.render(itemCallback);
544
- if (content) {
545
- result.push(content);
546
- }
547
- });
548
-
549
- return result;
550
- }
551
-
552
- /**
553
- * Initializes the manager.
554
- *
555
- * @param restore Previously stored layout state, if any.
556
- */
557
- async initialize(restore?: ILayoutState): Promise<void> {
558
- if (restore) {
559
- this.restore(restore);
560
- } else if (this.opts.storeKey && this.opts.autoStore) {
561
- await this.restoreLayout(this.opts.storeKey);
562
- }
563
-
564
- if (!this.panels.length) {
565
- this.panels.push(new LayoutPanel(this));
566
- }
567
-
568
- document.body.addEventListener('focusin', this._focusInHandler.bind(this));
569
- }
570
-
571
- protected restore(restore: ILayoutState): void {
572
- if (Array.isArray(restore.panels)) {
573
- this.panels = restore.panels.map(i => LayoutPanel.fromSchema(i, this));
574
- }
575
- }
576
-
577
- /**
578
- * Informs the screen that something has changed
579
- */
580
- changed(): void {
581
- this.dispatchEvent(new Event('change'));
582
- this.autoStore();
583
- }
584
-
585
- /**
586
- * Dispatches the `nameitem` event.
587
- * The detail object has the item to be added to the items.
588
- * The event handler can manipulate properties of the item, except for the index which will be set by the manager.
589
- *
590
- * @param item The item to notify.
591
- */
592
- nameItem(item: ILayoutItem): void {
593
- this.dispatchEvent(new CustomEvent('nameitem', {
594
- detail: item,
595
- }));
596
- }
597
-
598
- /**
599
- * Serializes the layout state to a JSON safe object.
600
- * This is automatically called when passing this object to `JSON.stringify()`.
601
- */
602
- toJSON(): ILayoutState {
603
- const result: ILayoutState = {
604
- panels: this.panels.map(i => i.toJSON()),
605
- };
606
- return result;
607
- }
608
-
609
- protected autoStore(): void {
610
- const { autoStore, storeKey } = this.opts;
611
- if (!autoStore || !storeKey) {
612
- return;
613
- }
614
- this.storeLayout(storeKey);
615
- }
616
-
617
- protected async storeLayout(key: string): Promise<void> {
618
- if (this.storing) {
619
- this.isDirty = true;
620
- return;
621
- }
622
- this.storing = true;
623
- try {
624
- await Events.Config.Local.set(key, this.toJSON());
625
- } finally {
626
- this.storing = false;
627
- }
628
- if (this.isDirty) {
629
- this.isDirty = false;
630
- await this.storeLayout(key);
631
- }
632
- }
633
-
634
- protected async restoreLayout(key: string): Promise<void> {
635
- try {
636
- const data = await Events.Config.Local.get(key) as ILayoutState;
637
- if (data) {
638
- this.restore(data);
639
- }
640
- } catch (e) {
641
- //
642
- }
643
- }
644
-
645
- protected _focusInHandler(e: Event): void {
646
- const layout = this.findLayout(e);
647
- if (layout) {
648
- const id = Number(layout.dataset.panel);
649
- const { activePanel } = this;
650
- if (activePanel && activePanel.id === id) {
651
- return;
652
- }
653
- this._activePanel = this.findPanel(id);
654
- }
655
- }
656
-
657
- protected findLayout(e: Event, last=true): LayoutPanelElement | undefined {
658
- const path = e.composedPath();
659
- if (!last) {
660
- path.reverse();
661
- }
662
- while (path.length) {
663
- const node = path.shift() as Element;
664
- if (node.nodeType !== Node.ELEMENT_NODE) {
665
- continue;
666
- }
667
- if (node.localName === 'layout-panel') {
668
- return node as LayoutPanelElement;
669
- }
670
- }
671
- return undefined;
672
- }
673
-
674
- /**
675
- * Finds a panel by id.
676
- *
677
- * @param id The id of the panel.
678
- * @param parent THe parent panel to start searching from
679
- * @returns The panel if found
680
- */
681
- findPanel(id: number, parent?: LayoutPanel): LayoutPanel | undefined {
682
- const panels = parent && parent.panels || this.panels;
683
-
684
- for (const p of panels) {
685
- if (p.id === id) {
686
- return p;
687
- }
688
- const child = this.findPanel(id, p);
689
- if (child) {
690
- return child;
691
- }
692
- }
693
-
694
- return undefined;
695
- }
696
-
697
- /**
698
- * Finds a first panel that can accept items.
699
- * This will be a panel that has no other panels.
700
- */
701
- findFirstItemsPanel(parent?: LayoutPanel): LayoutPanel | undefined {
702
- const panels = parent && parent.panels || this.panels;
703
-
704
- for (const p of panels) {
705
- if (!p.hasPanels) {
706
- return p;
707
- }
708
- const child = this.findFirstItemsPanel(p);
709
- if (child) {
710
- return child;
711
- }
712
- }
713
-
714
- return undefined;
715
- }
716
-
717
- /**
718
- * Finds a panel for the item.
719
- * @param key the key of the item to find.
720
- */
721
- findItemPanel(key: string, parent?: LayoutPanel): LayoutPanel | undefined {
722
- const panels = parent && parent.panels || this.panels;
723
-
724
- for (const p of panels) {
725
- const { items } = p;
726
- for (const i of items) {
727
- if (i.key === key) {
728
- return p;
729
- }
730
- }
731
- const deep = this.findItemPanel(key, p);
732
- if (deep) {
733
- return deep;
734
- }
735
- }
736
-
737
- return undefined;
738
- }
739
-
740
- /**
741
- * Finds a layout item.
742
- * @param key the key of the item to find.
743
- */
744
- findItem(key: string, parent?: LayoutPanel): ILayoutItem | undefined {
745
- const panels = parent && parent.panels || this.panels;
746
-
747
- for (const p of panels) {
748
- const { items } = p;
749
- for (const i of items) {
750
- if (i.key === key) {
751
- return i;
752
- }
753
- }
754
- const deep = this.findItem(key, p);
755
- if (deep) {
756
- return deep;
757
- }
758
- }
759
-
760
- return undefined;
761
- }
762
-
763
- * parentItemIterator(key: string, parent?: LayoutPanel): Generator<ILayoutItem> {
764
- const panels = parent ? parent.panels : this.panels;
765
- for (const p of panels) {
766
- const { items } = p;
767
- for (const i of items) {
768
- if (i.parent === key) {
769
- yield i;
770
- }
771
- }
772
- for (const result of this.parentItemIterator(key, p)) {
773
- yield result;
774
- }
775
- }
776
- }
777
-
778
- /**
779
- * Adds an item to the active panel.
780
- *
781
- * @param item The item to add.
782
- */
783
- addItem(item: ILayoutItem): void {
784
- const { activePanel } = this;
785
- if (!activePanel) {
786
- throw new Error(`Unable to determine an active panel.`);
787
- }
788
- activePanel.addItem(item);
789
- this.forceUpdateLayout();
790
- }
791
-
792
- /**
793
- * Removes an item from layout.
794
- * @param key The key of the item to remove.
795
- */
796
- removeItem(key: string): void {
797
- const panel = this.findItemPanel(key);
798
- if (panel) {
799
- panel.removeItem(key);
800
- }
801
- }
802
-
803
- /**
804
- * @param key The key of the item to perform a relative operation from.
805
- * @param dir The direction to which close other items. Default to both directions leaving only the `key` item
806
- */
807
- relativeClose(key: string, dir: 'left' | 'right' | 'both' = 'both'): void {
808
- const panel = this.findItemPanel(key);
809
- if (panel) {
810
- panel.relativeClose(key, dir);
811
- }
812
- }
813
-
814
- /**
815
- * Requests an update on a layout.
816
- *
817
- * @param id The id of the panel. When not set it uses the active panel
818
- */
819
- forceUpdateLayout(id?: number): void {
820
- let key;
821
- if (id === undefined) {
822
- const panel = this.activePanel;
823
- if (!panel) {
824
- return;
825
- }
826
- key = panel.id;
827
- } else {
828
- key = id;
829
- }
830
- const layout = document.querySelector(`layout-panel[layoutId="${key}"]`) as LayoutPanelElement | undefined;
831
- if (!layout) {
832
- return;
833
- }
834
- layout.requestUpdate();
835
- }
836
-
837
- /**
838
- * Moves a tab between panels or inside a panel
839
- *
840
- * @param fromPanel The id of the source panel of the item
841
- * @param toPanel The id of the target panel of the item
842
- * @param key The key of the item
843
- * @param toIndex The index to which add the item. Default as the last.
844
- */
845
- moveTab(fromPanel: number, toPanel: number, key: string, toIndex?: number): void {
846
- const singlePanel = fromPanel === toPanel;
847
- const from = this.findPanel(fromPanel);
848
- if (!from) {
849
- throw new Error(`The source layout panel not found.`);
850
- }
851
- if (singlePanel) {
852
- from.moveItem(key, toIndex);
853
- } else {
854
- const to = this.findPanel(toPanel);
855
- if (!to) {
856
- throw new Error(`The target layout panel not found.`);
857
- }
858
- const removed = from.removeItem(key);
859
- if (!removed) {
860
- return;
861
- }
862
- to.addItem(removed, { index: toIndex })
863
- }
864
- this.changed();
865
- }
866
-
867
- /**
868
- * Finds the item's panel and renames the item.
869
- *
870
- * @param key The item's key
871
- * @param label The new label
872
- */
873
- rename(key: string, label: string): void {
874
- const panel = this.findItemPanel(key);
875
- if (panel) {
876
- panel.rename(key, label);
877
- this.changed();
878
- this.forceUpdateLayout(panel.id);
879
- }
880
- }
881
-
882
- /**
883
- * Requests to dispatch the `nameitem` event so the application can update the name of the tab.
884
- *
885
- * @param key The key of the item.
886
- */
887
- requestNameUpdate(key: string): void {
888
- const item = this.findItem(key);
889
- if (!item) {
890
- return;
891
- }
892
- const before = item.label;
893
- this.nameItem(item);
894
- if (before !== item.label) {
895
- const panel = this.findItemPanel(key);
896
- if (panel) {
897
- this.forceUpdateLayout(panel.id);
898
- }
899
- }
900
- }
901
-
902
- /**
903
- * Requests to dispatch the `nameitem` event so the application can update the name of a tab
904
- * that has a parent.
905
- *
906
- * @param parent The key of the parent.
907
- */
908
- parentNameUpdate(parent: string): void {
909
- for (const item of this.parentItemIterator(parent)) {
910
- const before = item.label;
911
- this.nameItem(item);
912
- if (before !== item.label) {
913
- const panel = this.findItemPanel(item.key);
914
- if (panel) {
915
- this.forceUpdateLayout(panel.id);
916
- }
917
- }
918
- }
919
- }
920
-
921
- /**
922
- * Finds all items in all panels that have specified parent.
923
- * @param key The key of the parent to search for.
924
- */
925
- removeByParent(key: string): void {
926
- for (const item of this.parentItemIterator(key)) {
927
- this.removeItem(item.key);
928
- }
929
- }
930
- }