Skip to content

Commit 3801349

Browse files
refactor(wizards): move wizards in substation editor to wizard library (#242)
* feat(wizards/wizard-library): create empty library * refactor(editors/substation): move wizards to functions * refactor(editors/substation): move yet more wizards to library * refactor(wizards/lnode): move lnode wizard to wizard library * fix(wizards): fix broken imports * refactor(wizardslibrary): better naming
1 parent 2a09fe0 commit 3801349

18 files changed

+1274
-741
lines changed

src/editors/Substation.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { LitElement, html, TemplateResult, property, css } from 'lit-element';
22
import { translate, get } from 'lit-translate';
33

44
import { newWizardEvent } from '../foundation.js';
5+
import { wizards } from '../wizards/wizard-library.js';
56

67
import { selectors, styles } from './substation/foundation.js';
78

@@ -16,11 +17,8 @@ export default class SubstationPlugin extends LitElement {
1617

1718
/** Opens a [[`WizardDialog`]] for creating a new `Substation` element. */
1819
openCreateSubstationWizard(): void {
19-
this.dispatchEvent(
20-
newWizardEvent(
21-
SubstationEditor.wizard({ parent: this.doc.documentElement })
22-
)
23-
);
20+
const wizard = wizards['Substation'].create(this.doc.documentElement);
21+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
2422
}
2523

2624
render(): TemplateResult {

src/editors/substation/bay-editor.ts

Lines changed: 11 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,14 @@ import {
66
property,
77
TemplateResult,
88
} from 'lit-element';
9-
import { translate, get } from 'lit-translate';
9+
import { translate } from 'lit-translate';
1010

11-
import {
12-
createElement,
13-
EditorAction,
14-
getReference,
15-
getValue,
16-
newActionEvent,
17-
newWizardEvent,
18-
Wizard,
19-
WizardActor,
20-
WizardInput,
21-
} from '../../foundation.js';
11+
import { newActionEvent, newWizardEvent } from '../../foundation.js';
2212

23-
import {
24-
isCreateOptions,
25-
startMove,
26-
styles,
27-
updateNamingAction,
28-
WizardOptions,
29-
cloneElement,
30-
} from './foundation.js';
13+
import { startMove, styles, cloneElement } from './foundation.js';
3114
import './conducting-equipment-editor.js';
32-
import { ConductingEquipmentEditor } from './conducting-equipment-editor.js';
33-
import { editlNode } from './lnodewizard.js';
3415
import { VoltageLevelEditor } from './voltage-level-editor.js';
16+
import { wizards } from '../../wizards/wizard-library.js';
3517

3618
/** [[`SubstationEditor`]] subeditor for a `Bay` element. */
3719
@customElement('bay-editor')
@@ -49,21 +31,19 @@ export class BayEditor extends LitElement {
4931
}
5032

5133
openEditWizard(): void {
52-
this.dispatchEvent(
53-
newWizardEvent(BayEditor.wizard({ element: this.element }))
54-
);
34+
const wizard = wizards['Bay'].edit(this.element);
35+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
5536
}
5637

5738
openConductingEquipmentWizard(): void {
58-
if (!this.element) return;
59-
const event = newWizardEvent(
60-
ConductingEquipmentEditor.wizard({ parent: this.element })
61-
);
62-
this.dispatchEvent(event);
39+
const wizard = wizards['ConductingEquipment'].create(this.element);
40+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
6341
}
6442

43+
/** Opens a [[`WizardDialog`]] for editing `LNode` connections. */
6544
openLNodeWizard(): void {
66-
this.dispatchEvent(newWizardEvent(editlNode(this.element)));
45+
const wizard = wizards['LNode'].edit(this.element);
46+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
6747
}
6848

6949
remove(): void {
@@ -141,78 +121,6 @@ export class BayEditor extends LitElement {
141121
</section> `;
142122
}
143123

144-
static createAction(parent: Element): WizardActor {
145-
return (inputs: WizardInput[]): EditorAction[] => {
146-
const name = getValue(inputs.find(i => i.label === 'name')!);
147-
const desc = getValue(inputs.find(i => i.label === 'desc')!);
148-
const element = createElement(parent.ownerDocument, 'Bay', {
149-
name,
150-
desc,
151-
});
152-
153-
const action = {
154-
new: {
155-
parent,
156-
element,
157-
reference: getReference(parent, 'Bay'),
158-
},
159-
};
160-
161-
return [action];
162-
};
163-
}
164-
165-
static wizard(options: WizardOptions): Wizard {
166-
const [heading, actionName, actionIcon, action, name, desc, element] =
167-
isCreateOptions(options)
168-
? [
169-
get('bay.wizard.title.add'),
170-
get('add'),
171-
'add',
172-
BayEditor.createAction(options.parent),
173-
'',
174-
'',
175-
undefined,
176-
]
177-
: [
178-
get('bay.wizard.title.edit'),
179-
get('save'),
180-
'edit',
181-
updateNamingAction(options.element),
182-
options.element.getAttribute('name'),
183-
options.element.getAttribute('desc'),
184-
options.element,
185-
];
186-
187-
return [
188-
{
189-
title: heading,
190-
element,
191-
primary: {
192-
icon: actionIcon,
193-
label: actionName,
194-
action: action,
195-
},
196-
content: [
197-
html`<wizard-textfield
198-
label="name"
199-
.maybeValue=${name}
200-
helper="${translate('bay.wizard.nameHelper')}"
201-
required
202-
validationMessage="${translate('textfield.required')}"
203-
dialogInitialFocus
204-
></wizard-textfield>`,
205-
html`<wizard-textfield
206-
label="desc"
207-
.maybeValue=${desc}
208-
nullable
209-
helper="${translate('bay.wizard.descHelper')}"
210-
></wizard-textfield>`,
211-
],
212-
},
213-
];
214-
}
215-
216124
static styles = css`
217125
${styles}
218126

src/editors/substation/conducting-equipment-editor.ts

Lines changed: 11 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,16 @@ import {
66
property,
77
TemplateResult,
88
} from 'lit-element';
9-
import { translate, get } from 'lit-translate';
109

11-
import {
12-
createElement,
13-
EditorAction,
14-
getReference,
15-
getValue,
16-
newActionEvent,
17-
newWizardEvent,
18-
Wizard,
19-
WizardActor,
20-
WizardInput,
21-
} from '../../foundation.js';
10+
import { newActionEvent, newWizardEvent } from '../../foundation.js';
2211

23-
import {
24-
isCreateOptions,
25-
selectors,
26-
startMove,
27-
updateNamingAction,
28-
WizardOptions,
29-
} from './foundation.js';
30-
import { typeIcon, typeName, types } from './conducting-equipment-types.js';
31-
import { editlNode } from './lnodewizard.js';
12+
import { startMove } from './foundation.js';
3213
import { BayEditor } from './bay-editor.js';
3314

15+
import { typeIcon } from './conducting-equipment-types.js';
16+
17+
import { wizards } from '../../wizards/wizard-library.js';
18+
3419
/** [[`SubstationEditor`]] subeditor for a `ConductingEquipment` element. */
3520
@customElement('conducting-equipment-editor')
3621
export class ConductingEquipmentEditor extends LitElement {
@@ -47,15 +32,14 @@ export class ConductingEquipmentEditor extends LitElement {
4732
}
4833

4934
openEditWizard(): void {
50-
this.dispatchEvent(
51-
newWizardEvent(
52-
ConductingEquipmentEditor.wizard({ element: this.element })
53-
)
54-
);
35+
const wizard = wizards['ConductingEquipment'].edit(this.element);
36+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
5537
}
5638

39+
/** Opens a [[`WizardDialog`]] for editing `LNode` connections. */
5740
openLNodeWizard(): void {
58-
this.dispatchEvent(newWizardEvent(editlNode(this.element)));
41+
const wizard = wizards['LNode'].edit(this.element);
42+
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
5943
}
6044

6145
remove(): void {
@@ -105,135 +89,6 @@ export class ConductingEquipmentEditor extends LitElement {
10589
`;
10690
}
10791

108-
static createAction(parent: Element): WizardActor {
109-
return (inputs: WizardInput[]): EditorAction[] => {
110-
const name = getValue(inputs.find(i => i.label === 'name')!);
111-
const desc = getValue(inputs.find(i => i.label === 'desc')!);
112-
const proxyType = getValue(inputs.find(i => i.label === 'type')!);
113-
const type = proxyType === 'ERS' ? 'DIS' : proxyType;
114-
115-
const element = createElement(
116-
parent.ownerDocument,
117-
'ConductingEquipment',
118-
{ name, type, desc }
119-
);
120-
121-
if (proxyType === 'ERS')
122-
element.appendChild(
123-
createElement(parent.ownerDocument, 'Terminal', {
124-
name: 'T1',
125-
cNodeName: 'grounded',
126-
})
127-
);
128-
129-
const action = {
130-
new: {
131-
parent,
132-
element,
133-
reference: getReference(parent, 'ConductingEquipment'),
134-
},
135-
};
136-
137-
return [action];
138-
};
139-
}
140-
141-
static wizard(options: WizardOptions): Wizard {
142-
const [
143-
heading,
144-
actionName,
145-
actionIcon,
146-
action,
147-
name,
148-
desc,
149-
reservedNames,
150-
element,
151-
] = isCreateOptions(options)
152-
? [
153-
get('conductingequipment.wizard.title.add'),
154-
get('add'),
155-
'add',
156-
ConductingEquipmentEditor.createAction(options.parent),
157-
'',
158-
'',
159-
Array.from(
160-
options.parent.querySelectorAll(selectors.ConductingEquipment)
161-
).map(condEq => condEq.getAttribute('name') ?? ''),
162-
undefined,
163-
]
164-
: [
165-
get('conductingequipment.wizard.title.edit'),
166-
get('save'),
167-
'edit',
168-
updateNamingAction(options.element),
169-
options.element.getAttribute('name'),
170-
options.element.getAttribute('desc'),
171-
Array.from(
172-
options.element.parentNode!.querySelectorAll(
173-
selectors.ConductingEquipment
174-
)
175-
)
176-
.map(condEq => condEq.getAttribute('name') ?? '')
177-
.filter(name => name !== options.element.getAttribute('name')),
178-
options.element,
179-
];
180-
181-
return [
182-
{
183-
title: heading,
184-
element,
185-
primary: {
186-
icon: actionIcon,
187-
label: actionName,
188-
action: action,
189-
},
190-
content: [
191-
ConductingEquipmentEditor.renderTypeSelector(options),
192-
html`<wizard-textfield
193-
label="name"
194-
.maybeValue=${name}
195-
helper="${translate('conductingequipment.wizard.nameHelper')}"
196-
required
197-
validationMessage="${translate('textfield.required')}"
198-
dialogInitialFocus
199-
.reservedValues=${reservedNames}
200-
></wizard-textfield>`,
201-
html`<wizard-textfield
202-
label="desc"
203-
.maybeValue=${desc}
204-
nullable
205-
helper="${translate('conductingequipment.wizard.descHelper')}"
206-
></wizard-textfield>`,
207-
],
208-
},
209-
];
210-
}
211-
212-
static renderTypeSelector(options: WizardOptions): TemplateResult {
213-
return isCreateOptions(options)
214-
? html`<mwc-select
215-
style="--mdc-menu-max-height: 196px;"
216-
required
217-
label="type"
218-
helper="${translate('conductingequipment.wizard.typeHelper')}"
219-
validationMessage="${translate('textfield.required')}"
220-
>
221-
${Object.keys(types).map(
222-
v => html`<mwc-list-item value="${v}">${types[v]}</mwc-list-item>`
223-
)}
224-
</mwc-select>`
225-
: html`<mwc-select
226-
label="type"
227-
helper="${translate('conductingequipment.wizard.typeHelper')}"
228-
validationMessage="${translate('textfield.required')}"
229-
disabled
230-
>
231-
<mwc-list-item selected value="0"
232-
>${typeName(options.element)}</mwc-list-item
233-
>
234-
</mwc-select>`;
235-
}
236-
23792
static styles = css`
23893
#container {
23994
color: var(--mdc-theme-on-surface);

0 commit comments

Comments
 (0)