Skip to content

Commit a00a8f5

Browse files
authored
ref: extract tab menu from layout (openscd#1631)
1 parent 54acfbc commit a00a8f5

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

packages/openscd/src/addons/Layout.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import {OscdPluginManager} from "./plugin-manager/plugin-manager.js";
5858
import "./plugin-manager/plugin-manager.js";
5959
import {OscdCustomPluginDialog} from "./plugin-manager/custom-plugin-dialog.js";
6060
import "./plugin-manager/custom-plugin-dialog.js";
61+
import "./menu-tabs/menu-tabs.js";
62+
import { TabActivatedEvent } from "./menu-tabs/menu-tabs.js";
6163

6264

6365
@customElement('oscd-layout')
@@ -82,6 +84,7 @@ export class OscdLayout extends LitElement {
8284

8385
@state() validated: Promise<void> = Promise.resolve();
8486
@state() shouldValidate = false;
87+
@state() activeEditor: Plugin | undefined = this.calcActiveEditors()[0];
8588

8689
@query('#menu') menuUI!: Drawer;
8790
@query('#pluginManager') pluginUI!: OscdPluginManager;
@@ -462,17 +465,17 @@ export class OscdLayout extends LitElement {
462465
if(!hasActiveEditors){ return html``; }
463466

464467
return html`
465-
<mwc-tab-bar
466-
@MDCTabBar:activated=${this.handleActivatedEditorTabByUser}
467-
activeIndex=${this.activeTab}
468+
<oscd-menu-tabs
469+
.editors=${this.calcActiveEditors()}
470+
.activeEditor=${this.activeEditor}
471+
@oscd-editor-tab-activated=${this.handleEditorTabActivated}
468472
>
469-
${activeEditors}
470-
</mwc-tab-bar>
471-
${renderEditorContent(this.editors, this.activeTab, this.doc)}
473+
</oscd-menu-tabs>
474+
${renderEditorContent(this.doc, this.activeEditor, )}
472475
`;
473476

474-
function renderEditorContent(editors: Plugin[], activeTab: number, doc: XMLDocument | null){
475-
const editor = editors[activeTab];
477+
function renderEditorContent(doc: XMLDocument | null, activeEditor?: Plugin){
478+
const editor = activeEditor;
476479
const requireDoc = editor?.requireDoc
477480
if(requireDoc && !doc) { return html`` }
478481

@@ -483,6 +486,10 @@ export class OscdLayout extends LitElement {
483486
}
484487
}
485488

489+
private handleEditorTabActivated(e: TabActivatedEvent){
490+
this.activeEditor = e.detail.editor
491+
}
492+
486493
private handleActivatedEditorTabByUser(e: CustomEvent): void {
487494
const tabIndex = e.detail.index;
488495
this.activateTab(tabIndex);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
customElement,
3+
html,
4+
LitElement,
5+
property,
6+
query,
7+
state,
8+
TemplateResult,
9+
css
10+
} from 'lit-element';
11+
12+
import '@material/mwc-list';
13+
import '@material/mwc-tab';
14+
import '@material/mwc-tab-bar';
15+
import '@material/mwc-button';
16+
17+
import {
18+
Plugin,
19+
} from "../../plugin.js"
20+
21+
22+
@customElement('oscd-menu-tabs')
23+
export class OscdMenuTabs extends LitElement {
24+
25+
@property({ type: Array }) editors: Plugin[] = [];
26+
_activeEditor: Plugin | undefined;
27+
@property({ type: Object }) get activeEditor() { return this._activeEditor; }
28+
set activeEditor(editor: Plugin | undefined) {
29+
this._activeEditor = editor;
30+
this.activeTabIndex = this.editors.indexOf(this.activeEditor || this.editors[0]);
31+
this.requestUpdate();
32+
};
33+
34+
@state() private activeTabIndex = 0;
35+
36+
render(){
37+
if(this.editors.length === 0){ return html``; }
38+
39+
return html`
40+
<mwc-tab-bar
41+
@MDCTabBar:activated=${this.handleActivatedEditorTab}
42+
activeIndex=${this.activeTabIndex}
43+
>
44+
${ this.editors.map( EditorTab ) }
45+
</mwc-tab-bar>
46+
`
47+
}
48+
49+
static styles = css`
50+
mwc-tab {
51+
background-color: var(--primary);
52+
--mdc-theme-primary: var(--mdc-theme-on-primary);
53+
}
54+
`
55+
56+
private handleActivatedEditorTab(e: CustomEvent): void {
57+
const tabIndex = e.detail.index;
58+
const editor = this.editors[tabIndex];
59+
this.activeTabIndex = tabIndex;
60+
this.dispatchActivateEditor(editor);
61+
}
62+
63+
private dispatchActivateEditor( editor: Plugin ){
64+
const newEvent = new CustomEvent(TabActivatedEventKey, {
65+
detail: { editor },
66+
composed: true,
67+
bubbles: true
68+
})
69+
this.dispatchEvent(newEvent)
70+
}
71+
}
72+
73+
function EditorTab({ name, icon }: Plugin): TemplateResult {
74+
return html`
75+
<mwc-tab label=${name} icon=${icon || 'edit'}> </mwc-tab>
76+
`;
77+
}
78+
79+
export const TabActivatedEventKey = 'oscd-editor-tab-activated'
80+
export type TabActivatedEvent = CustomEvent<TabActivatedEventDetail>;
81+
export type TabActivatedEventDetail = { editor: Plugin }

0 commit comments

Comments
 (0)