diff --git a/src/wizards/sampledvaluecontrol.ts b/src/wizards/sampledvaluecontrol.ts index 103972589b..76761e85b8 100644 --- a/src/wizards/sampledvaluecontrol.ts +++ b/src/wizards/sampledvaluecontrol.ts @@ -12,11 +12,14 @@ import '../wizard-select.js'; import '../wizard-textfield.js'; import { cloneElement, + Delete, EditorAction, getValue, identity, isPublic, + newActionEvent, newSubWizardEvent, + newWizardEvent, selector, Wizard, WizardActor, @@ -43,6 +46,52 @@ function getSMV(element: Element): Element | null { ); } +export function removeSampledValueControlAction(element: Element): Delete[] { + if (!element.parentElement) return []; + + const dataSet = element.parentElement!.querySelector( + `DataSet[name="${element.getAttribute('datSet')}"]` + ); + const sMV = getSMV(element); + + const singleUse = + Array.from( + element.parentElement.querySelectorAll( + 'ReportControl, GSEControl, SampledValueControl' + ) + ).filter( + controlblock => + controlblock.getAttribute('datSet') === dataSet?.getAttribute('name') + ).length <= 1; + + const actions: Delete[] = []; + + actions.push({ + old: { + parent: element.parentElement!, + element, + }, + }); + + if (dataSet && singleUse) + actions.push({ + old: { + parent: element.parentElement!, + element: dataSet, + }, + }); + + if (sMV) + actions.push({ + old: { + parent: sMV.parentElement!, + element: sMV, + }, + }); + + return actions; +} + interface ContentOptions { name: string | null; desc: string | null; @@ -208,6 +257,17 @@ export function editSampledValueControlWizard(element: Element): Wizard { }}}" >` : html``, + html` { + const deleteActions = removeSampledValueControlAction(element); + deleteActions.forEach(deleteAction => + e.target?.dispatchEvent(newActionEvent(deleteAction)) + ); + e.target?.dispatchEvent(newWizardEvent()); + }} + >`, ], }, ]; diff --git a/test/integration/wizards/sampledvaluecontrol-wizarding-editing.test.ts b/test/integration/wizards/sampledvaluecontrol-wizarding-editing.test.ts index 02f5934620..af102339f3 100644 --- a/test/integration/wizards/sampledvaluecontrol-wizarding-editing.test.ts +++ b/test/integration/wizards/sampledvaluecontrol-wizarding-editing.test.ts @@ -238,6 +238,32 @@ describe('Wizards for SCL element SampledValueControl', () => { ?.textContent?.trim() ); }); + + it('removes the SampledValueControl element and its referenced elements on remove button click', async () => { + expect( + doc.querySelector( + 'IED[name="IED3"] SampledValueControl[name="MSVCB01"]' + ) + ).to.exist; + expect(doc.querySelector('IED[name="IED3"] DataSet[name="PhsMeas1"]')) + .to.exist; + expect(doc.querySelector('SMV[cbName="MSVCB01"]')).to.exist; + + const deleteButton =