Skip to content

Commit 5368cf3

Browse files
refactor(foundation): change menu action API (openscd#769)
* refactor(foundation): change menu action API * fix(wizards/ied): add missing import statement
1 parent 6e9c928 commit 5368cf3

File tree

13 files changed

+107
-82
lines changed

13 files changed

+107
-82
lines changed

src/editors/templates/datype-wizards.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import {
1818
EditorAction,
1919
getValue,
2020
identity,
21+
newActionEvent,
2122
newSubWizardEvent,
23+
newWizardEvent,
2224
patterns,
2325
Replace,
2426
selector,
2527
Wizard,
26-
WizardAction,
2728
WizardActor,
2829
WizardInputElement,
2930
WizardMenuActor,
@@ -36,14 +37,17 @@ import {
3637
} from './foundation.js';
3738

3839
function remove(element: Element): WizardMenuActor {
39-
return (): EditorAction[] => {
40-
return [{ old: { parent: element.parentElement!, element } }];
40+
return (wizard: Element): void => {
41+
wizard.dispatchEvent(
42+
newActionEvent({ old: { parent: element.parentElement!, element } })
43+
);
44+
wizard.dispatchEvent(newWizardEvent());
4145
};
4246
}
4347

4448
function openAddBda(parent: Element): WizardMenuActor {
45-
return (): WizardAction[] => {
46-
return [() => createBDAWizard(parent)];
49+
return (wizard: Element): void => {
50+
wizard.dispatchEvent(newSubWizardEvent(() => createBDAWizard(parent)));
4751
};
4852
}
4953

src/editors/templates/dotype-wizards.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import {
1919
getValue,
2020
identity,
2121
isPublic,
22+
newActionEvent,
2223
newSubWizardEvent,
24+
newWizardEvent,
2325
Replace,
2426
selector,
2527
Wizard,
26-
WizardAction,
2728
WizardActor,
2829
WizardInputElement,
2930
WizardMenuActor,
@@ -40,8 +41,11 @@ import {
4041
} from './foundation.js';
4142

4243
function remove(element: Element): WizardMenuActor {
43-
return (): EditorAction[] => {
44-
return [{ old: { parent: element.parentElement!, element } }];
44+
return (wizard: Element): void => {
45+
wizard.dispatchEvent(
46+
newActionEvent({ old: { parent: element.parentElement!, element } })
47+
);
48+
wizard.dispatchEvent(newWizardEvent());
4549
};
4650
}
4751

@@ -296,14 +300,14 @@ export function createDOTypeWizard(
296300
}
297301

298302
function openAddSdo(parent: Element): WizardMenuActor {
299-
return (): WizardAction[] => {
300-
return [() => sDOWizard({ parent })!];
303+
return (wizard: Element): void => {
304+
wizard.dispatchEvent(newSubWizardEvent(() => sDOWizard({ parent })!));
301305
};
302306
}
303307

304308
function openAddDa(parent: Element): WizardMenuActor {
305-
return (): WizardAction[] => {
306-
return [() => createDaWizard(parent)!];
309+
return (wizard: Element): void => {
310+
wizard.dispatchEvent(newSubWizardEvent(() => createDaWizard(parent)!));
307311
};
308312
}
309313

src/editors/templates/enumtype-wizard.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@ import {
1818
getValue,
1919
identity,
2020
isPublic,
21+
newActionEvent,
2122
newSubWizardEvent,
23+
newWizardEvent,
2224
patterns,
2325
Replace,
2426
selector,
2527
Wizard,
26-
WizardAction,
2728
WizardActor,
2829
WizardInputElement,
2930
WizardMenuActor,
3031
} from '../../foundation.js';
3132
import { CreateOptions, UpdateOptions, WizardOptions } from './foundation.js';
3233

3334
function remove(element: Element): WizardMenuActor {
34-
return (): EditorAction[] => {
35-
return [{ old: { parent: element.parentElement!, element } }];
35+
return (wizard: Element): void => {
36+
wizard.dispatchEvent(
37+
newActionEvent({ old: { parent: element.parentElement!, element } })
38+
);
39+
wizard.dispatchEvent(newWizardEvent());
3640
};
3741
}
3842

@@ -250,8 +254,8 @@ export function createEnumTypeWizard(
250254
}
251255

252256
function openAddEnumVal(parent: Element): WizardMenuActor {
253-
return (): WizardAction[] => {
254-
return [() => eNumValWizard({ parent })];
257+
return (wizard: Element): void => {
258+
wizard.dispatchEvent(newSubWizardEvent(() => eNumValWizard({ parent })));
255259
};
256260
}
257261

src/editors/templates/lnodetype-wizard.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import {
2222
getValue,
2323
identity,
2424
isPublic,
25+
newActionEvent,
2526
newSubWizardEvent,
2627
newWizardEvent,
2728
patterns,
2829
Replace,
2930
selector,
3031
Wizard,
31-
WizardAction,
3232
WizardActor,
3333
WizardInputElement,
3434
WizardMenuActor,
@@ -44,14 +44,17 @@ import {
4444
} from './foundation.js';
4545

4646
function remove(element: Element): WizardMenuActor {
47-
return (): EditorAction[] => {
48-
return [{ old: { parent: element.parentElement!, element } }];
47+
return (wizard: Element): void => {
48+
wizard.dispatchEvent(
49+
newActionEvent({ old: { parent: element.parentElement!, element } })
50+
);
51+
wizard.dispatchEvent(newWizardEvent());
4952
};
5053
}
5154

5255
function openAddDo(parent: Element): WizardMenuActor {
53-
return (): WizardAction[] => {
54-
return [() => dOWizard({ parent })!];
56+
return (wizard: Element): void => {
57+
wizard.dispatchEvent(newSubWizardEvent(() => dOWizard({ parent })!));
5558
};
5659
}
5760

src/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ interface WizardInputCheckbox extends WizardInputBase {
293293
}
294294

295295
/** @returns [[`WizardAction`]]s to dispatch on [[`WizardDialog`]] menu action. */
296-
export type WizardMenuActor = () => WizardAction[];
296+
export type WizardMenuActor = (wizard: Element) => void;
297297

298298
/** User interactions rendered in the wizard-dialog menu */
299299
export interface MenuAction {

src/wizard-dialog.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
identity,
4343
WizardInput,
4444
WizardMenuActor,
45-
newSubWizardEvent,
4645
} from './foundation.js';
4746

4847
function renderWizardInput(
@@ -214,20 +213,11 @@ export class WizardDialog extends LitElement {
214213
return true;
215214
}
216215

217-
/** Triggers sub-wizard or editor-action with valid manu action */
218-
async menuAct(action?: WizardMenuActor): Promise<boolean> {
219-
if (!action) return false;
216+
/** Triggers menu action callback */
217+
async menuAct(action?: WizardMenuActor): Promise<void> {
218+
if (!action) return;
220219

221-
const wizardActions = action();
222-
223-
wizardActions.forEach(wa => {
224-
if (isWizardFactory(wa)) this.dispatchEvent(newSubWizardEvent(wa));
225-
else {
226-
this.dispatchEvent(newWizardEvent());
227-
this.dispatchEvent(newActionEvent(wa));
228-
}
229-
});
230-
return true;
220+
action(this);
231221
}
232222

233223
private onClosed(ae: CustomEvent<{ action: string } | null>): void {

src/wizards/bda.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
EditorAction,
99
getValue,
1010
isPublic,
11+
newActionEvent,
12+
newWizardEvent,
1113
Wizard,
1214
WizardActor,
1315
WizardInputElement,
@@ -16,8 +18,11 @@ import {
1618
import { getValAction, wizardContent } from './abstractda.js';
1719

1820
function remove(element: Element): WizardMenuActor {
19-
return (): EditorAction[] => {
20-
return [{ old: { parent: element.parentElement!, element } }];
21+
return (wizard: Element): void => {
22+
wizard.dispatchEvent(
23+
newActionEvent({ old: { parent: element.parentElement!, element } })
24+
);
25+
wizard.dispatchEvent(newWizardEvent());
2126
};
2227
}
2328

src/wizards/da.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
EditorAction,
1313
getValue,
1414
isPublic,
15+
newActionEvent,
16+
newWizardEvent,
1517
Wizard,
1618
WizardActor,
1719
WizardInputElement,
@@ -21,8 +23,11 @@ import { getValAction, wizardContent } from './abstractda.js';
2123
import { functionalConstraintEnum } from './foundation/enums.js';
2224

2325
function remove(element: Element): WizardMenuActor {
24-
return (): EditorAction[] => {
25-
return [{ old: { parent: element.parentElement!, element } }];
26+
return (wizard: Element): void => {
27+
wizard.dispatchEvent(
28+
newActionEvent({ old: { parent: element.parentElement!, element } })
29+
);
30+
wizard.dispatchEvent(newWizardEvent());
2631
};
2732
}
2833

src/wizards/dataset.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import {
1818
WizardActor,
1919
WizardInputElement,
2020
WizardMenuActor,
21+
newSubWizardEvent,
2122
} from '../foundation.js';
2223
import { createFCDAsWizard } from './fcda.js';
2324

2425
function openFcdaWizard(element: Element): WizardMenuActor {
25-
return (): WizardAction[] => {
26-
return [() => createFCDAsWizard(element)];
26+
return (wizard: Element): void => {
27+
wizard.dispatchEvent(newSubWizardEvent(() => createFCDAsWizard(element)));
2728
};
2829
}
2930

src/wizards/gsecontrol.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import {
2323
identity,
2424
isPublic,
2525
MenuAction,
26+
newActionEvent,
2627
newSubWizardEvent,
28+
newWizardEvent,
2729
selector,
2830
Wizard,
29-
WizardAction,
3031
WizardActor,
3132
WizardInputElement,
3233
WizardMenuActor,
@@ -432,22 +433,22 @@ export function removeGseControlAction(element: Element): ComplexAction | null {
432433
}
433434

434435
export function removeGseControl(element: Element): WizardMenuActor {
435-
return (): WizardAction[] => {
436+
return (wizard: Element): void => {
436437
const complexAction = removeGseControlAction(element);
437-
if (complexAction) return [complexAction];
438-
return [];
438+
if (complexAction) wizard.dispatchEvent(newActionEvent(complexAction));
439+
wizard.dispatchEvent(newWizardEvent());
439440
};
440441
}
441442

442443
function openDataSetWizard(element: Element): WizardMenuActor {
443-
return (): WizardAction[] => {
444-
return [() => editDataSetWizard(element)];
444+
return (wizard: Element): void => {
445+
wizard.dispatchEvent(newSubWizardEvent(() => editDataSetWizard(element)));
445446
};
446447
}
447448

448449
function openGseWizard(element: Element): WizardMenuActor {
449-
return (): WizardAction[] => {
450-
return [() => editGseWizard(element)];
450+
return (wizard: Element): void => {
451+
wizard.dispatchEvent(newSubWizardEvent(() => editGseWizard(element)));
451452
};
452453
}
453454

0 commit comments

Comments
 (0)