Skip to content

Commit 96d79e5

Browse files
refactor(editor/template/datype): use menu actions (openscd#621)
1 parent 3dcd6b5 commit 96d79e5

File tree

4 files changed

+88
-73
lines changed

4 files changed

+88
-73
lines changed

src/editors/templates/datype-wizards.ts

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import {
1818
EditorAction,
1919
getValue,
2020
identity,
21-
newActionEvent,
2221
newSubWizardEvent,
23-
newWizardEvent,
2422
patterns,
2523
Replace,
2624
selector,
2725
Wizard,
26+
WizardAction,
2827
WizardActor,
2928
WizardInput,
29+
WizardMenuActor,
3030
} from '../../foundation.js';
3131
import { createBDAWizard, editBDAWizard } from '../../wizards/bda.js';
3232
import {
@@ -35,6 +35,18 @@ import {
3535
unifyCreateActionArray,
3636
} from './foundation.js';
3737

38+
function remove(element: Element): WizardMenuActor {
39+
return (): EditorAction[] => {
40+
return [{ old: { parent: element.parentElement!, element } }];
41+
};
42+
}
43+
44+
function openAddBda(parent: Element): WizardMenuActor {
45+
return (): WizardAction[] => {
46+
return [() => createBDAWizard(parent)];
47+
};
48+
}
49+
3850
function updateDATpyeAction(element: Element): WizardActor {
3951
return (inputs: WizardInput[]): EditorAction[] => {
4052
const id = getValue(inputs.find(i => i.label === 'id')!)!;
@@ -88,25 +100,19 @@ export function editDaTypeWizard(
88100
label: get('save'),
89101
action: updateDATpyeAction(datype),
90102
},
103+
menuActions: [
104+
{
105+
label: get('remove'),
106+
icon: 'delete',
107+
action: remove(datype),
108+
},
109+
{
110+
label: get('scl.DA'),
111+
icon: 'playlist_add',
112+
action: openAddBda(datype),
113+
},
114+
],
91115
content: [
92-
html`<mwc-button
93-
icon="delete"
94-
trailingIcon
95-
label="${translate('remove')}"
96-
@click=${(e: MouseEvent) => {
97-
e.target!.dispatchEvent(newWizardEvent());
98-
e.target!.dispatchEvent(
99-
newActionEvent({
100-
old: {
101-
parent: datype.parentElement!,
102-
element: datype,
103-
reference: datype.nextSibling,
104-
},
105-
})
106-
);
107-
}}
108-
fullwidth
109-
></mwc-button> `,
110116
html`<wizard-textfield
111117
label="id"
112118
helper="${translate('scl.id')}"
@@ -124,44 +130,29 @@ export function editDaTypeWizard(
124130
nullable
125131
pattern="${patterns.normalizedString}"
126132
></wizard-textfield>`,
127-
html`<mwc-button
128-
slot="graphic"
129-
icon="playlist_add"
130-
trailingIcon
131-
label="${translate('scl.DA')}"
132-
@click=${(e: Event) => {
133-
if (datype)
134-
e.target!.dispatchEvent(
135-
newSubWizardEvent(createBDAWizard(datype))
136-
);
137-
}}
138-
></mwc-button>
139-
<mwc-list
140-
style="margin-top: 0px;"
141-
@selected=${(e: SingleSelectedEvent) => {
142-
const bdaIdentity = (<ListItem>(<List>e.target).selected).value;
143-
const bda = doc.querySelector(selector('BDA', bdaIdentity));
133+
html`<mwc-list
134+
style="margin-top: 0px;"
135+
@selected=${(e: SingleSelectedEvent) => {
136+
const bdaIdentity = (<ListItem>(<List>e.target).selected).value;
137+
const bda = doc.querySelector(selector('BDA', bdaIdentity));
144138
145-
if (bda)
146-
e.target!.dispatchEvent(newSubWizardEvent(editBDAWizard(bda)));
147-
}}
148-
>
149-
${Array.from(datype.querySelectorAll('BDA')).map(
150-
bda =>
151-
html`<mwc-list-item
152-
twoline
153-
tabindex="0"
154-
value="${identity(bda)}"
155-
><span>${bda.getAttribute('name')}</span
156-
><span slot="secondary"
157-
>${bda.getAttribute('bType') === 'Enum' ||
158-
bda.getAttribute('bType') === 'Struct'
159-
? '#' + bda.getAttribute('type')
160-
: bda.getAttribute('bType')}</span
161-
></mwc-list-item
162-
>`
163-
)}
164-
</mwc-list> `,
139+
if (bda)
140+
e.target!.dispatchEvent(newSubWizardEvent(editBDAWizard(bda)));
141+
}}
142+
>
143+
${Array.from(datype.querySelectorAll('BDA')).map(
144+
bda =>
145+
html`<mwc-list-item twoline tabindex="0" value="${identity(bda)}"
146+
><span>${bda.getAttribute('name')}</span
147+
><span slot="secondary"
148+
>${bda.getAttribute('bType') === 'Enum' ||
149+
bda.getAttribute('bType') === 'Struct'
150+
? '#' + bda.getAttribute('type')
151+
: bda.getAttribute('bType')}</span
152+
></mwc-list-item
153+
>`
154+
)}
155+
</mwc-list> `,
165156
],
166157
},
167158
];

test/integration/editors/templates/__snapshots__/datype-wizarding.test.snap.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,45 @@ snapshots["DAType wizards defines a dATypeWizard looks like the latest snapshot"
620620
heading="[datype.wizard.title.edit]"
621621
open=""
622622
>
623-
<div id="wizard-content">
624-
<mwc-button
625-
fullwidth=""
626-
icon="delete"
627-
label="[remove]"
628-
trailingicon=""
623+
<nav>
624+
<mwc-icon-button icon="more_vert">
625+
</mwc-icon-button>
626+
<mwc-menu
627+
class="actions-menu"
628+
corner="BOTTOM_RIGHT"
629+
menucorner="END"
629630
>
630-
</mwc-button>
631+
<mwc-list-item
632+
aria-disabled="false"
633+
graphic="icon"
634+
mwc-list-item=""
635+
role="menuitem"
636+
tabindex="0"
637+
>
638+
<span>
639+
[remove]
640+
</span>
641+
<mwc-icon slot="graphic">
642+
delete
643+
</mwc-icon>
644+
</mwc-list-item>
645+
<mwc-list-item
646+
aria-disabled="false"
647+
graphic="icon"
648+
mwc-list-item=""
649+
role="menuitem"
650+
tabindex="-1"
651+
>
652+
<span>
653+
[scl.DA]
654+
</span>
655+
<mwc-icon slot="graphic">
656+
playlist_add
657+
</mwc-icon>
658+
</mwc-list-item>
659+
</mwc-menu>
660+
</nav>
661+
<div id="wizard-content">
631662
<wizard-textfield
632663
dialoginitialfocus=""
633664
helper="[scl.id]"
@@ -646,13 +677,6 @@ snapshots["DAType wizards defines a dATypeWizard looks like the latest snapshot"
646677
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
647678
>
648679
</wizard-textfield>
649-
<mwc-button
650-
icon="playlist_add"
651-
label="[scl.DA]"
652-
slot="graphic"
653-
trailingicon=""
654-
>
655-
</mwc-button>
656680
<mwc-list style="margin-top: 0px;">
657681
<mwc-list-item
658682
aria-disabled="false"

test/integration/editors/templates/datype-wizarding.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('DAType wizards', () => {
135135
)
136136
);
137137
deleteButton = <HTMLElement>(
138-
parent.wizardUI.dialog?.querySelector('mwc-button[icon="delete"]')
138+
parent.wizardUI.dialog?.querySelector('mwc-menu > mwc-list-item')
139139
);
140140
});
141141

test/integration/wizards/bda-wizarding-editing.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('BDA wizarding editing integration', () => {
132132
await parent.requestUpdate();
133133
await new Promise(resolve => setTimeout(resolve, 100)); // await animation
134134
(<HTMLElement>(
135-
parent.wizardUI.dialog?.querySelector('mwc-button[icon="playlist_add"]')
135+
parent.wizardUI.dialog?.querySelectorAll('mwc-menu > mwc-list-item')[1]
136136
)).click();
137137
await parent.requestUpdate();
138138
await new Promise(resolve => setTimeout(resolve, 100)); // await animation

0 commit comments

Comments
 (0)