Skip to content

Commit

Permalink
feat(substation/general-equipment-editor):add_button_general-equipmen…
Browse files Browse the repository at this point in the history
…t_container (openscd#1110)

* feat(substation/general-equipment-editor):add_button_general-equipment_container

* fix(../substation/general-equipment-editor.ts):set_the_anchor_for_the_menu

* fix(general-equipment-editor.ts):merge_conflicts
  • Loading branch information
marcvanraalte authored Jan 4, 2023
1 parent b3def87 commit 812ff94
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/editors/substation/general-equipment-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import {
property,
state,
css,
query,
} from 'lit-element';

import { translate } from 'lit-translate';

import '@material/mwc-icon-button';
import '@material/mwc-list/mwc-list-item';
import '@material/mwc-menu';
import '@material/mwc-fab';
import { IconButton } from '@material/mwc-icon-button';
import { ListItem } from '@material/mwc-list/mwc-list-item';
import { Menu } from '@material/mwc-menu';

import '../../action-pane.js';
import '../../editors/substation/eq-function-editor.js';
Expand All @@ -19,9 +27,18 @@ import {
getChildElementsByTagName,
newActionEvent,
newWizardEvent,
SCLTag,
tags,
} from '../../foundation.js';
import { translate } from 'lit-translate';
import { wizards } from '../../wizards/wizard-library.js';
import { emptyWizard, wizards } from '../../wizards/wizard-library.js';

function childTags(element: Element | null | undefined): SCLTag[] {
if (!element) return [];

return tags[<SCLTag>element.tagName].children.filter(
child => wizards[child].create !== emptyWizard
);
}

@customElement('general-equipment-editor')
export class GeneralEquipmentEditor extends LitElement {
Expand All @@ -44,11 +61,25 @@ export class GeneralEquipmentEditor extends LitElement {
return `${name} ${desc ? `— ${desc}` : ''}`;
}

@query('mwc-menu') addMenu!: Menu;
@query('mwc-icon-button[icon="playlist_add"]') addButton!: IconButton;

openEditWizard(): void {
const wizard = wizards['GeneralEquipment'].edit(this.element);
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
}

private openCreateWizard(tagName: string): void {
const wizard = wizards[<SCLTag>tagName].create(this.element!);

if (wizard) this.dispatchEvent(newWizardEvent(wizard));
}

firstUpdated(): void {
if (this.addMenu && this.addButton)
this.addMenu.anchor = <HTMLElement>this.addButton;
}

remove(): void {
if (this.element.parentElement)
this.dispatchEvent(
Expand Down Expand Up @@ -91,6 +122,15 @@ export class GeneralEquipmentEditor extends LitElement {
: html``;
}

private renderAddButtons(): TemplateResult[] {
return childTags(this.element).map(
child =>
html`<mwc-list-item value="${child}"
><span>${child}</span></mwc-list-item
>`
);
}

render(): TemplateResult {
if (this.showfunctions)
return html`<action-pane label=${this.header}>
Expand All @@ -106,6 +146,25 @@ export class GeneralEquipmentEditor extends LitElement {
@click=${() => this.remove()}
></mwc-icon-button>
</abbr>
<abbr
slot="action"
style="position:relative;"
title="${translate('add')}"
>
<mwc-icon-button
icon="playlist_add"
@click=${() => (this.addMenu.open = true)}
></mwc-icon-button
><mwc-menu
corner="BOTTOM_RIGHT"
menuCorner="END"
@action=${(e: Event) => {
const tagName = (<ListItem>(<Menu>e.target).selected).value;
this.openCreateWizard(tagName);
}}
>${this.renderAddButtons()}</mwc-menu
></abbr
>
${this.renderLNodes()} ${this.renderEqFunctions()}
</action-pane>`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@ import { expect, fixture, html } from '@open-wc/testing';
import '../../../mock-wizard-editor.js';
import { MockWizardEditor } from '../../../mock-wizard-editor.js';

import { ListItemBase } from '@material/mwc-list/mwc-list-item-base';

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';
import { MenuBase } from '@material/mwc-menu/mwc-menu-base.js';

const openAndCancelMenu: (
parent: MockWizardEditor,
element: GeneralEquipmentEditor
) => Promise<void> = (
parent: MockWizardEditor,
element: GeneralEquipmentEditor
): Promise<void> =>
new Promise(async resolve => {
expect(parent.wizardUI.dialog).to.be.undefined;

element?.shadowRoot
?.querySelector<MenuBase>("mwc-icon-button[icon='playlist_add']")!
.click();
const lnodMenuItem: ListItemBase =
element?.shadowRoot?.querySelector<ListItemBase>(
`mwc-list-item[value='LNode']`
)!;
lnodMenuItem.click();
await new Promise(resolve => setTimeout(resolve, 100)); // await animation

expect(parent.wizardUI.dialog).to.exist;

const secondaryAction: HTMLElement = <HTMLElement>(
parent.wizardUI.dialog?.querySelector(
'mwc-button[slot="secondaryAction"][dialogaction="close"]'
)
);

secondaryAction.click();
await new Promise(resolve => setTimeout(resolve, 100)); // await animation

expect(parent.wizardUI.dialog).to.be.undefined;

return resolve();
});

describe('general-equipment-editor wizarding editing integration', () => {
let doc: XMLDocument;
Expand Down Expand Up @@ -136,4 +175,37 @@ describe('general-equipment-editor wizarding editing integration', () => {
});
});
});

describe('Open add wizard', () => {
let doc: XMLDocument;
let parent: MockWizardEditor;
let element: GeneralEquipmentEditor | null;

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')}
?showfunctions=${true}
></general-equipment-editor
></mock-wizard-editor>`
)
);

element = parent.querySelector('general-equipment-editor');

await parent.updateComplete;
});

it('Should open the same wizard for the second time', async () => {
await openAndCancelMenu(parent, element!);
await openAndCancelMenu(parent, element!);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ snapshots["Editor web component for GeneralEquipment SCL element rendered as act
<mwc-icon-button icon="delete">
</mwc-icon-button>
</abbr>
<abbr
slot="action"
style="position:relative;"
title="[add]"
>
<mwc-icon-button icon="playlist_add">
</mwc-icon-button>
<mwc-menu
corner="BOTTOM_RIGHT"
menucorner="END"
>
<mwc-list-item
aria-disabled="false"
mwc-list-item=""
role="menuitem"
tabindex="-1"
value="LNode"
>
<span>
LNode
</span>
</mwc-list-item>
<mwc-list-item
aria-disabled="false"
mwc-list-item=""
role="menuitem"
tabindex="-1"
value="EqFunction"
>
<span>
EqFunction
</span>
</mwc-list-item>
</mwc-menu>
</abbr>
<div class="container lnode">
<l-node-editor>
</l-node-editor>
Expand Down

0 comments on commit 812ff94

Please sign in to comment.