From 5368cf3a4b1dbcb69b1e859e15cd53cb705a4606 Mon Sep 17 00:00:00 2001 From: Jakob Vogelsang Date: Tue, 24 May 2022 10:30:39 +0200 Subject: [PATCH] refactor(foundation): change menu action API (#769) * refactor(foundation): change menu action API * fix(wizards/ied): add missing import statement --- src/editors/templates/datype-wizards.ts | 14 +++++++----- src/editors/templates/dotype-wizards.ts | 18 +++++++++------ src/editors/templates/enumtype-wizard.ts | 14 +++++++----- src/editors/templates/lnodetype-wizard.ts | 13 ++++++----- src/foundation.ts | 2 +- src/wizard-dialog.ts | 18 ++++----------- src/wizards/bda.ts | 9 ++++++-- src/wizards/da.ts | 9 ++++++-- src/wizards/dataset.ts | 5 +++-- src/wizards/gsecontrol.ts | 17 +++++++------- src/wizards/ied.ts | 22 ++++++++++-------- src/wizards/reportcontrol.ts | 27 +++++++++++++---------- src/wizards/sampledvaluecontrol.ts | 21 +++++++++--------- 13 files changed, 107 insertions(+), 82 deletions(-) diff --git a/src/editors/templates/datype-wizards.ts b/src/editors/templates/datype-wizards.ts index f174ea778..c19696f31 100644 --- a/src/editors/templates/datype-wizards.ts +++ b/src/editors/templates/datype-wizards.ts @@ -18,12 +18,13 @@ import { EditorAction, getValue, identity, + newActionEvent, newSubWizardEvent, + newWizardEvent, patterns, Replace, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -36,14 +37,17 @@ import { } from './foundation.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } function openAddBda(parent: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => createBDAWizard(parent)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => createBDAWizard(parent))); }; } diff --git a/src/editors/templates/dotype-wizards.ts b/src/editors/templates/dotype-wizards.ts index 33fdec4fa..5562f2c27 100644 --- a/src/editors/templates/dotype-wizards.ts +++ b/src/editors/templates/dotype-wizards.ts @@ -19,11 +19,12 @@ import { getValue, identity, isPublic, + newActionEvent, newSubWizardEvent, + newWizardEvent, Replace, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -40,8 +41,11 @@ import { } from './foundation.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } @@ -296,14 +300,14 @@ export function createDOTypeWizard( } function openAddSdo(parent: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => sDOWizard({ parent })!]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => sDOWizard({ parent })!)); }; } function openAddDa(parent: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => createDaWizard(parent)!]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => createDaWizard(parent)!)); }; } diff --git a/src/editors/templates/enumtype-wizard.ts b/src/editors/templates/enumtype-wizard.ts index 6e81d41c2..282debb33 100644 --- a/src/editors/templates/enumtype-wizard.ts +++ b/src/editors/templates/enumtype-wizard.ts @@ -18,12 +18,13 @@ import { getValue, identity, isPublic, + newActionEvent, newSubWizardEvent, + newWizardEvent, patterns, Replace, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -31,8 +32,11 @@ import { import { CreateOptions, UpdateOptions, WizardOptions } from './foundation.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } @@ -250,8 +254,8 @@ export function createEnumTypeWizard( } function openAddEnumVal(parent: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => eNumValWizard({ parent })]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => eNumValWizard({ parent }))); }; } diff --git a/src/editors/templates/lnodetype-wizard.ts b/src/editors/templates/lnodetype-wizard.ts index 74791f22a..51fa41ee9 100644 --- a/src/editors/templates/lnodetype-wizard.ts +++ b/src/editors/templates/lnodetype-wizard.ts @@ -22,13 +22,13 @@ import { getValue, identity, isPublic, + newActionEvent, newSubWizardEvent, newWizardEvent, patterns, Replace, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -44,14 +44,17 @@ import { } from './foundation.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } function openAddDo(parent: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => dOWizard({ parent })!]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => dOWizard({ parent })!)); }; } diff --git a/src/foundation.ts b/src/foundation.ts index 02643c4d9..8d2a01d6b 100644 --- a/src/foundation.ts +++ b/src/foundation.ts @@ -293,7 +293,7 @@ interface WizardInputCheckbox extends WizardInputBase { } /** @returns [[`WizardAction`]]s to dispatch on [[`WizardDialog`]] menu action. */ -export type WizardMenuActor = () => WizardAction[]; +export type WizardMenuActor = (wizard: Element) => void; /** User interactions rendered in the wizard-dialog menu */ export interface MenuAction { diff --git a/src/wizard-dialog.ts b/src/wizard-dialog.ts index 5809a1bff..c279714e6 100644 --- a/src/wizard-dialog.ts +++ b/src/wizard-dialog.ts @@ -42,7 +42,6 @@ import { identity, WizardInput, WizardMenuActor, - newSubWizardEvent, } from './foundation.js'; function renderWizardInput( @@ -214,20 +213,11 @@ export class WizardDialog extends LitElement { return true; } - /** Triggers sub-wizard or editor-action with valid manu action */ - async menuAct(action?: WizardMenuActor): Promise { - if (!action) return false; + /** Triggers menu action callback */ + async menuAct(action?: WizardMenuActor): Promise { + if (!action) return; - const wizardActions = action(); - - wizardActions.forEach(wa => { - if (isWizardFactory(wa)) this.dispatchEvent(newSubWizardEvent(wa)); - else { - this.dispatchEvent(newWizardEvent()); - this.dispatchEvent(newActionEvent(wa)); - } - }); - return true; + action(this); } private onClosed(ae: CustomEvent<{ action: string } | null>): void { diff --git a/src/wizards/bda.ts b/src/wizards/bda.ts index add31b477..2d0e9b2b3 100644 --- a/src/wizards/bda.ts +++ b/src/wizards/bda.ts @@ -8,6 +8,8 @@ import { EditorAction, getValue, isPublic, + newActionEvent, + newWizardEvent, Wizard, WizardActor, WizardInputElement, @@ -16,8 +18,11 @@ import { import { getValAction, wizardContent } from './abstractda.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } diff --git a/src/wizards/da.ts b/src/wizards/da.ts index 7b7efcf57..6d2dbf573 100644 --- a/src/wizards/da.ts +++ b/src/wizards/da.ts @@ -12,6 +12,8 @@ import { EditorAction, getValue, isPublic, + newActionEvent, + newWizardEvent, Wizard, WizardActor, WizardInputElement, @@ -21,8 +23,11 @@ import { getValAction, wizardContent } from './abstractda.js'; import { functionalConstraintEnum } from './foundation/enums.js'; function remove(element: Element): WizardMenuActor { - return (): EditorAction[] => { - return [{ old: { parent: element.parentElement!, element } }]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } diff --git a/src/wizards/dataset.ts b/src/wizards/dataset.ts index 42c20f27a..dd8dcdb21 100644 --- a/src/wizards/dataset.ts +++ b/src/wizards/dataset.ts @@ -18,12 +18,13 @@ import { WizardActor, WizardInputElement, WizardMenuActor, + newSubWizardEvent, } from '../foundation.js'; import { createFCDAsWizard } from './fcda.js'; function openFcdaWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => createFCDAsWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => createFCDAsWizard(element))); }; } diff --git a/src/wizards/gsecontrol.ts b/src/wizards/gsecontrol.ts index 0da3df04f..f4ce26391 100644 --- a/src/wizards/gsecontrol.ts +++ b/src/wizards/gsecontrol.ts @@ -23,10 +23,11 @@ import { identity, isPublic, MenuAction, + newActionEvent, newSubWizardEvent, + newWizardEvent, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -432,22 +433,22 @@ export function removeGseControlAction(element: Element): ComplexAction | null { } export function removeGseControl(element: Element): WizardMenuActor { - return (): WizardAction[] => { + return (wizard: Element): void => { const complexAction = removeGseControlAction(element); - if (complexAction) return [complexAction]; - return []; + if (complexAction) wizard.dispatchEvent(newActionEvent(complexAction)); + wizard.dispatchEvent(newWizardEvent()); }; } function openDataSetWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editDataSetWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editDataSetWizard(element))); }; } function openGseWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editGseWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editGseWizard(element))); }; } diff --git a/src/wizards/ied.ts b/src/wizards/ied.ts index 3cb02ed55..8fafe5870 100644 --- a/src/wizards/ied.ts +++ b/src/wizards/ied.ts @@ -11,14 +11,15 @@ import { EditorAction, identity, isPublic, + newActionEvent, + newSubWizardEvent, newWizardEvent, Wizard, - WizardAction, WizardActor, WizardInputElement, - WizardMenuActor + WizardMenuActor, } from '../foundation.js'; -import { patterns } from "./foundation/limits.js"; +import { patterns } from './foundation/limits.js'; import { updateNamingAttributeWithReferencesAction } from "./foundation/actions.js"; import { deleteReferences } from "./foundation/references.js"; @@ -129,13 +130,16 @@ export function removeIEDWizard(element: Element): Wizard | null { export function editIEDWizard(element: Element): Wizard { function removeIED(element: Element): WizardMenuActor { - return (): WizardAction[] => { - const wizard = removeIEDWizard(element); - if (wizard) { - return [() => wizard]; - } + return (wizard: Element): void => { + const removeWizard = removeIEDWizard(element); + if (removeWizard) + wizard.dispatchEvent(newSubWizardEvent(() => removeWizard)); + // If no Wizard is needed, just remove the element. - return [{ old: { parent: element.parentElement!, element } }]; + wizard.dispatchEvent( + newActionEvent({ old: { parent: element.parentElement!, element } }) + ); + wizard.dispatchEvent(newWizardEvent()); }; } diff --git a/src/wizards/reportcontrol.ts b/src/wizards/reportcontrol.ts index 6249ac163..0e8d7d2a3 100644 --- a/src/wizards/reportcontrol.ts +++ b/src/wizards/reportcontrol.ts @@ -30,8 +30,9 @@ import { getUniqueElementName, ComplexAction, WizardMenuActor, - WizardAction, MenuAction, + newActionEvent, + newWizardEvent, } from '../foundation.js'; import { FilteredList } from '../filtered-list.js'; import { FinderList } from '../finder-list.js'; @@ -546,34 +547,36 @@ export function reportControlCopyToIedSelector(element: Element): Wizard { } function openIedsSelector(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => reportControlCopyToIedSelector(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent( + newSubWizardEvent(() => reportControlCopyToIedSelector(element)) + ); }; } export function removeReportControl(element: Element): WizardMenuActor { - return (): WizardAction[] => { + return (wizard: Element): void => { const complexAction = removeReportControlAction(element); - if (complexAction) return [complexAction]; - return []; + if (complexAction) wizard.dispatchEvent(newActionEvent(complexAction)); + wizard.dispatchEvent(newWizardEvent()); }; } function openDataSetWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editDataSetWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editDataSetWizard(element))); }; } function openTrgOpsWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editTrgOpsWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editTrgOpsWizard(element))); }; } function openOptFieldsWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editOptFieldsWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editOptFieldsWizard(element))); }; } diff --git a/src/wizards/sampledvaluecontrol.ts b/src/wizards/sampledvaluecontrol.ts index 6c9d5c14f..995db6ac2 100644 --- a/src/wizards/sampledvaluecontrol.ts +++ b/src/wizards/sampledvaluecontrol.ts @@ -19,10 +19,11 @@ import { identity, isPublic, MenuAction, + newActionEvent, newSubWizardEvent, + newWizardEvent, selector, Wizard, - WizardAction, WizardActor, WizardInputElement, WizardMenuActor, @@ -195,28 +196,28 @@ function contentSampledValueControlWizard( } function removeSampledValueControl(element: Element): WizardMenuActor { - return (): WizardAction[] => { + return (wizard: Element): void => { const complexAction = removeSampledValueControlAction(element); - if (complexAction) return [complexAction]; - return []; + if (complexAction) wizard.dispatchEvent(newActionEvent(complexAction)); + wizard.dispatchEvent(newWizardEvent()); }; } function openDataSetWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editDataSetWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editDataSetWizard(element))); }; } function openSmvOptsWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editSmvOptsWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editSmvOptsWizard(element))); }; } function openSMvWizard(element: Element): WizardMenuActor { - return (): WizardAction[] => { - return [() => editSMvWizard(element)]; + return (wizard: Element): void => { + wizard.dispatchEvent(newSubWizardEvent(() => editSMvWizard(element))); }; }