forked from openscd/open-scd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sub-equipment-editor): edit wizard (openscd#1063)
* Added edit wizard for sub-equipment * Removed dist folders Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> * Removed dist folder Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> * Added integration tests * Fixed review comments Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com> Signed-off-by: Pascal Wilbrink <pascal.wilbrink@alliander.com>
- Loading branch information
1 parent
2963c27
commit 4778e7d
Showing
10 changed files
with
544 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/*.js | ||
**/*.ts.hbs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { html, TemplateResult } from 'lit-html'; | ||
import { get, translate } from 'lit-translate'; | ||
|
||
import { | ||
cloneElement, | ||
getChildElementsByTagName, | ||
getValue, | ||
SimpleAction, | ||
Wizard, | ||
WizardActor, | ||
WizardInputElement, | ||
} from '../foundation.js'; | ||
|
||
import '../wizard-textfield.js'; | ||
import '../wizard-select.js'; | ||
|
||
interface ContentOptions { | ||
name: string | null; | ||
desc: string | null; | ||
phase: string | null; | ||
virtual: string | null; | ||
reservedNames: string[]; | ||
} | ||
|
||
export function contentFunctionWizard( | ||
content: ContentOptions | ||
): TemplateResult[] { | ||
return [ | ||
html`<wizard-textfield | ||
label="name" | ||
.maybeValue=${content.name} | ||
.reservedValues=${content.reservedNames} | ||
helper="${translate('scl.name')}" | ||
required | ||
validationMessage="${translate('textfield.required')}" | ||
dialogInitialFocus | ||
></wizard-textfield>`, | ||
html`<wizard-textfield | ||
label="desc" | ||
.maybeValue=${content.desc} | ||
nullable | ||
helper="${translate('scl.desc')}" | ||
></wizard-textfield>`, | ||
html`<wizard-select | ||
label="phase" | ||
fixedMenuPosition | ||
.maybeValue=${content.phase} | ||
nullable | ||
helper="${translate('scl.phase')}" | ||
> | ||
${['A', 'B', 'C', 'N', 'all', 'none', 'AB', 'BC', 'CA'].map( | ||
value => | ||
html`<mwc-list-item value="${value}"> | ||
${value.charAt(0).toUpperCase() + value.slice(1)} | ||
</mwc-list-item>` | ||
)} | ||
</wizard-select> `, | ||
html`<wizard-checkbox | ||
label="virtual" | ||
.maybeValue=${content.virtual} | ||
nullable | ||
helper="${translate('scl.virtual')}" | ||
></wizard-checkbox>`, | ||
]; | ||
} | ||
|
||
function updateSubEquipmentAction(element: Element): WizardActor { | ||
return (inputs: WizardInputElement[]): SimpleAction[] => { | ||
const subfunctionAttrs: Record<string, string | null> = {}; | ||
const subFunctionKeys = ['name', 'desc', 'phase', 'virtual']; | ||
subFunctionKeys.forEach(key => { | ||
subfunctionAttrs[key] = getValue(inputs.find(i => i.label === key)!); | ||
}); | ||
|
||
if ( | ||
subFunctionKeys.some( | ||
key => subfunctionAttrs[key] !== element.getAttribute(key) | ||
) | ||
) { | ||
const newElement = cloneElement(element, subfunctionAttrs); | ||
return [ | ||
{ | ||
old: { element }, | ||
new: { element: newElement }, | ||
}, | ||
]; | ||
} | ||
|
||
return []; | ||
}; | ||
} | ||
|
||
export function editSubEquipmentWizard(element: Element): Wizard { | ||
const name = element.getAttribute('name'); | ||
const desc = element.getAttribute('desc'); | ||
const phase = element.getAttribute('phase'); | ||
const virtual = element.getAttribute('virtual'); | ||
|
||
const reservedNames: string[] = getChildElementsByTagName( | ||
element.parentElement!, | ||
'SubEquipment' | ||
) | ||
.filter(sibling => sibling !== element) | ||
.map(sibling => sibling.getAttribute('name')!); | ||
|
||
return [ | ||
{ | ||
title: get('wizard.title.edit', { tagName: 'SubEquipment' }), | ||
primary: { | ||
icon: 'save', | ||
label: get('save'), | ||
action: updateSubEquipmentAction(element), | ||
}, | ||
content: [ | ||
...contentFunctionWizard({ | ||
name, | ||
desc, | ||
phase, | ||
virtual, | ||
reservedNames, | ||
}), | ||
], | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
test/integration/editors/substation/sub-equipment-wizarding-editing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { fixture, html, expect } from '@open-wc/testing'; | ||
|
||
import '../../../mock-wizard-editor.js'; | ||
import { MockWizardEditor } from '../../../mock-wizard-editor.js'; | ||
|
||
import '../../../../src/editors/substation/sub-equipment-editor.js'; | ||
import { WizardTextField } from '../../../../src/wizard-textfield.js'; | ||
import { SubEquipmentEditor } from '../../../../src/editors/substation/sub-equipment-editor.js'; | ||
import { WizardCheckbox } from '../../../../src/wizard-checkbox.js'; | ||
|
||
describe('sub-equipment-editor wizarding editing integration', () => { | ||
let doc: XMLDocument; | ||
let parent: MockWizardEditor; | ||
let element: SubEquipmentEditor | null; | ||
|
||
beforeEach(async () => { | ||
doc = await fetch('/test/testfiles/SubEquipment.scd') | ||
.then(response => response.text()) | ||
.then(str => new DOMParser().parseFromString(str, 'application/xml')); | ||
parent = <MockWizardEditor>( | ||
await fixture( | ||
html`<mock-wizard-editor | ||
><sub-equipment-editor | ||
.element=${doc.querySelector('SubEquipment[name="subque"]')} | ||
></sub-equipment-editor | ||
></mock-wizard-editor>` | ||
) | ||
); | ||
|
||
element = parent.querySelector('sub-equipment-editor'); | ||
}); | ||
|
||
describe('open edit wizard', () => { | ||
let nameField: WizardTextField; | ||
let virtualField: WizardCheckbox; | ||
let primaryAction: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
(<HTMLElement>( | ||
element?.shadowRoot?.querySelector('mwc-icon-button[icon="edit"]') | ||
)).click(); | ||
await parent.updateComplete; | ||
|
||
nameField = <WizardTextField>( | ||
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="name"]') | ||
); | ||
|
||
virtualField = <WizardCheckbox>( | ||
parent.wizardUI.dialog?.querySelector( | ||
'wizard-checkbox[label="virtual"]' | ||
) | ||
); | ||
|
||
primaryAction = <HTMLElement>( | ||
parent.wizardUI.dialog?.querySelector( | ||
'mwc-button[slot="primaryAction"]' | ||
) | ||
); | ||
}); | ||
|
||
it('does not update SubEquipment if name attribute is not unique', async () => { | ||
nameField.value = 'addEqi'; | ||
primaryAction.click(); | ||
await parent.updateComplete; | ||
|
||
expect(doc.querySelectorAll('SubEquipment[name="addEqi"]')).to.lengthOf( | ||
1 | ||
); | ||
}); | ||
|
||
it('does update SubEquipment if name attribute is unique', async () => { | ||
nameField.value = 'addEqi2'; | ||
await parent.updateComplete; | ||
primaryAction.click(); | ||
|
||
expect(doc.querySelector('SubEquipment[name="addEqi2"]')).to.exist; | ||
expect(doc.querySelector('SubEquipment[name="subque"]')).to.not.exist; | ||
}); | ||
|
||
it('does update SubEquipment when virtual is checked', async () => { | ||
expect(virtualField.nullSwitch).to.exist; | ||
|
||
virtualField.nullSwitch?.click(); | ||
|
||
virtualField.maybeValue = 'true'; | ||
|
||
await parent.updateComplete; | ||
primaryAction.click(); | ||
|
||
expect( | ||
doc | ||
.querySelector('SubEquipment[name="subque"]') | ||
?.hasAttribute('virtual') | ||
); | ||
expect( | ||
doc | ||
.querySelector('SubEquipment[name="subque"]') | ||
?.getAttribute('virtual') | ||
).to.equal('true'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.