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(substation/general-equipment-editor): edit wizard (openscd#1089)
* fix(rebasing 976 in main) * fix(generalEquipment.ts): added pattern and maxLength * fix(generalEquipment.ts): changed maxLenght into minLength
- Loading branch information
1 parent
cfd16d6
commit 95ba5ab
Showing
9 changed files
with
474 additions
and
9 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
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,114 @@ | ||
import { html, TemplateResult } from 'lit-element'; | ||
import { get, translate } from 'lit-translate'; | ||
import { | ||
cloneElement, | ||
getChildElementsByTagName, | ||
getValue, | ||
SimpleAction, | ||
Wizard, | ||
WizardActor, | ||
WizardInputElement, | ||
} from '../foundation'; | ||
|
||
export function editGeneralEquipmentWizard(element: Element): Wizard { | ||
const name = element.getAttribute('name'); | ||
const desc = element.getAttribute('desc'); | ||
const type = element.getAttribute('type'); | ||
const virtual = element.getAttribute('virtual'); | ||
const reservedNames: string[] = getChildElementsByTagName( | ||
element.parentElement!, | ||
'GeneralEquipment' | ||
) | ||
.filter(sibling => sibling !== element) | ||
.map(sibling => sibling.getAttribute('name')!); | ||
|
||
return [ | ||
{ | ||
title: get('wizard.title.edit', { tagName: 'GeneralEquipment' }), | ||
primary: { | ||
icon: 'save', | ||
label: get('save'), | ||
action: updateGeneralEquipmentAction(element), | ||
}, | ||
content: [ | ||
...contentGeneralEquipmentWizard({ | ||
name, | ||
desc, | ||
type, | ||
virtual, | ||
reservedNames, | ||
}), | ||
], | ||
}, | ||
]; | ||
} | ||
|
||
function updateGeneralEquipmentAction(element: Element): WizardActor { | ||
return (inputs: WizardInputElement[]): SimpleAction[] => { | ||
const generalEquipmentAttrs: Record<string, string | null> = {}; | ||
const generalEquipmentKeys = ['name', 'desc', 'type', 'virtual']; | ||
generalEquipmentKeys.forEach(key => { | ||
generalEquipmentAttrs[key] = getValue(inputs.find(i => i.label === key)!); | ||
}); | ||
|
||
if ( | ||
generalEquipmentKeys.some( | ||
key => generalEquipmentAttrs[key] !== element.getAttribute(key) | ||
) | ||
) { | ||
const newElement = cloneElement(element, generalEquipmentAttrs); | ||
return [ | ||
{ | ||
old: { element }, | ||
new: { element: newElement }, | ||
}, | ||
]; | ||
} | ||
|
||
return []; | ||
}; | ||
} | ||
|
||
interface ContentOptions { | ||
name: string | null; | ||
desc: string | null; | ||
type: string | null; | ||
virtual: string | null; | ||
reservedNames: string[]; | ||
} | ||
|
||
export function contentGeneralEquipmentWizard( | ||
content: ContentOptions | ||
): TemplateResult[] { | ||
return [ | ||
html`<wizard-textfield | ||
label="name" | ||
.maybeValue=${content.name} | ||
helper="${translate('scl.name')}" | ||
required | ||
validationMessage="${translate('textfield.required')}" | ||
.reservedValues=${content.reservedNames} | ||
dialogInitialFocus | ||
></wizard-textfield>`, | ||
html`<wizard-textfield | ||
label="desc" | ||
.maybeValue=${content.desc} | ||
nullable | ||
helper="${translate('scl.desc')}" | ||
></wizard-textfield>`, | ||
html`<wizard-textfield | ||
label="type" | ||
.maybeValue=${content.type} | ||
helper="${translate('scl.type')}" | ||
minLength="${3}" | ||
pattern="AXN|BAT|MOT|FAN|FIL|PMP|TNK|VLV|E[A-Z]*" | ||
required | ||
></wizard-textfield>`, | ||
html`<wizard-checkbox | ||
label="virtual" | ||
.maybeValue=${content.virtual} | ||
helper="${translate('scl.virtual')}" | ||
nullable | ||
></wizard-checkbox>`, | ||
]; | ||
} |
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
112 changes: 112 additions & 0 deletions
112
test/integration/editors/substation/general-equipment-editor-wizard-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,112 @@ | ||
import { expect, fixture, html } from '@open-wc/testing'; | ||
|
||
import '../../../mock-wizard-editor.js'; | ||
import { MockWizardEditor } from '../../../mock-wizard-editor.js'; | ||
|
||
import '../../../../src/editors/substation/general-equipment-editor.js'; | ||
import { GeneralEquipmentEditor } from '../../../../src/editors/substation/general-equipment-editor.js'; | ||
import { WizardTextField } from '../../../../src/wizard-textfield.js'; | ||
|
||
describe('general-equipment-editor wizarding editing integration', () => { | ||
describe('edit wizard', () => { | ||
let doc: XMLDocument; | ||
let parent: MockWizardEditor; | ||
let element: GeneralEquipmentEditor | null; | ||
|
||
let nameField: WizardTextField; | ||
let descField: WizardTextField; | ||
let typeField: WizardTextField; | ||
let secondaryAction: HTMLElement; | ||
let primaryAction: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
doc = await fetch( | ||
'/test/testfiles/editors/substation/generalequipment.scd' | ||
) | ||
.then(response => response.text()) | ||
.then(str => new DOMParser().parseFromString(str, 'application/xml')); | ||
parent = <MockWizardEditor>( | ||
await fixture( | ||
html`<mock-wizard-editor | ||
><general-equipment-editor | ||
.element=${doc.querySelector('GeneralEquipment')} | ||
></general-equipment-editor | ||
></mock-wizard-editor>` | ||
) | ||
); | ||
|
||
element = parent.querySelector('general-equipment-editor'); | ||
await (<HTMLElement>( | ||
element?.shadowRoot?.querySelector('mwc-icon-button[icon="edit"]') | ||
)).click(); | ||
await parent.updateComplete; | ||
|
||
nameField = <WizardTextField>( | ||
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="name"]') | ||
); | ||
descField = <WizardTextField>( | ||
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="desc"]') | ||
); | ||
typeField = <WizardTextField>( | ||
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="type"]') | ||
); | ||
secondaryAction = <HTMLElement>( | ||
parent.wizardUI.dialog?.querySelector( | ||
'mwc-button[slot="secondaryAction"]' | ||
) | ||
); | ||
primaryAction = <HTMLElement>( | ||
parent.wizardUI.dialog?.querySelector( | ||
'mwc-button[slot="primaryAction"]' | ||
) | ||
); | ||
}); | ||
it('closes on secondary action', async () => { | ||
secondaryAction.click(); | ||
await new Promise(resolve => setTimeout(resolve, 100)); // await animation | ||
expect(parent.wizardUI.dialog).to.not.exist; | ||
}); | ||
it('does not change name attribute if not unique within parent element', async () => { | ||
const oldName = nameField.value; | ||
nameField.value = 'genSub2'; | ||
primaryAction.click(); | ||
await parent.updateComplete; | ||
expect( | ||
doc.querySelector('GeneralEquipment')?.getAttribute('name') | ||
).to.equal(oldName); | ||
}); | ||
it('changes name attribute on primary action', async () => { | ||
parent.wizardUI.inputs[0].value = 'newName'; | ||
primaryAction.click(); | ||
await parent.updateComplete; | ||
expect( | ||
doc.querySelector('GeneralEquipment')?.getAttribute('name') | ||
).to.equal('newName'); | ||
}); | ||
it('changes type attribute on primary action', async () => { | ||
parent.wizardUI.inputs[2].value = 'newAXN'; | ||
primaryAction.click(); | ||
await parent.updateComplete; | ||
expect( | ||
doc.querySelector('GeneralEquipment')?.getAttribute('type') | ||
).to.equal('newAXN'); | ||
}); | ||
it('changes desc attribute on primary action', async () => { | ||
descField.value = 'newDesc'; | ||
primaryAction.click(); | ||
await parent.updateComplete; | ||
expect( | ||
doc.querySelector('GeneralEquipment')?.getAttribute('desc') | ||
).to.equal('newDesc'); | ||
}); | ||
it('deletes desc attribute if wizard-textfield is deactivated', async () => { | ||
await new Promise(resolve => setTimeout(resolve, 100)); // await animation | ||
descField.nullSwitch!.click(); | ||
await parent.updateComplete; | ||
await primaryAction.click(); | ||
await parent.updateComplete; | ||
expect(doc.querySelector('GeneralEquipment')?.getAttribute('desc')).to.be | ||
.null; | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.