Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wizards/subfunction): add create wizard #733

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions src/editors/substation/function-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ import {
property,
customElement,
state,
query,
} from 'lit-element';

import '../../action-pane.js';
import './sub-function-editor.js';
import { getChildElementsByTagName } from '../../foundation.js';
import {
getChildElementsByTagName,
newWizardEvent,
SCLTag,
tags,
} from '../../foundation.js';
import { translate } from 'lit-translate';
import { emptyWizard, wizards } from '../../wizards/wizard-library.js';
import { Menu } from '@material/mwc-menu';
import { ListItem } from '@material/mwc-list/mwc-list-item';
import { IconButton } from '@material/mwc-icon-button';

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

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

/** Pane rendering `Function` element with its children */
@customElement('function-editor')
Expand All @@ -26,6 +45,19 @@ export class FunctionEditor extends LitElement {
return `${name}${desc ? ` - ${desc}` : ''}${type ? ` (${type})` : ''}`;
}

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

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

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

firstUpdated(): void {
this.addMenu.anchor = <HTMLElement>this.addButton;
JakobVogelsang marked this conversation as resolved.
Show resolved Hide resolved
}

private renderSubFunctions(): TemplateResult {
const subfunctions = getChildElementsByTagName(this.element, 'SubFunction');
return html` ${subfunctions.map(
Expand All @@ -36,13 +68,42 @@ export class FunctionEditor extends LitElement {
)}`;
}

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

render(): TemplateResult {
return html`<action-pane
label="${this.header}"
icon="functions"
secondary
highlighted
>${this.renderSubFunctions()}</action-pane
>`;
>
<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"
@selected=${(e: Event) => {
const tagName = (<ListItem>(<Menu>e.target).selected).value;
this.openCreateWizard(tagName);
}}
>${this.renderAddButtons()}</mwc-menu
>
</abbr>
${this.renderSubFunctions()}
</action-pane>`;
}
}
67 changes: 65 additions & 2 deletions src/editors/substation/sub-function-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,34 @@ import {
property,
customElement,
state,
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 { 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 './sub-function-editor.js';
import { getChildElementsByTagName } from '../../foundation.js';
import {
getChildElementsByTagName,
newWizardEvent,
SCLTag,
tags,
} from '../../foundation.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
);
}

/** Pane rendering `SubFunction` element with its children */
@customElement('sub-function-editor')
Expand All @@ -26,6 +49,19 @@ export class SubFunctionEditor extends LitElement {
return `${name}${desc ? ` - ${desc}` : ''}${type ? ` (${type})` : ''}`;
}

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

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

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

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

private renderSubFunctions(): TemplateResult {
const subfunctions = getChildElementsByTagName(this.element, 'SubFunction');
return html` ${subfunctions.map(
Expand All @@ -36,8 +72,35 @@ export class SubFunctionEditor extends LitElement {
)}`;
}

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

render(): TemplateResult {
return html`<action-pane label="${this.header}" icon="functions" secondary
return html`<action-pane label="${this.header}" icon="functions" secondary>
<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"
@selected=${(e: Event) => {
const tagName = (<ListItem>(<Menu>e.target).selected).value;
this.openCreateWizard(tagName);
}}
>${this.renderAddButtons()}</mwc-menu
> </abbr
>${this.renderSubFunctions()}</action-pane
>`;
}
Expand Down
4 changes: 3 additions & 1 deletion src/wizards/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ interface ContentOptions {
reservedNames: string[];
}

function contentFunctionWizard(content: ContentOptions): TemplateResult[] {
export function contentFunctionWizard(
content: ContentOptions
): TemplateResult[] {
return [
html`<wizard-textfield
label="name"
Expand Down
56 changes: 56 additions & 0 deletions src/wizards/subfunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { get } from 'lit-translate';

import {
createElement,
getValue,
Wizard,
WizardActor,
WizardInputElement,
} from '../foundation.js';
import { contentFunctionWizard } from './function.js';

function createSubFunctionAction(parent: Element): WizardActor {
return (inputs: WizardInputElement[]) => {
const subFunctionAttrs: Record<string, string | null> = {};
const subFunctionKeys = ['name', 'desc', 'type'];
subFunctionKeys.forEach(key => {
subFunctionAttrs[key] = getValue(inputs.find(i => i.label === key)!);
});

const subFunction = createElement(
parent.ownerDocument,
'SubFunction',
subFunctionAttrs
);

return [{ new: { parent, element: subFunction } }];
};
}

export function createSubFunctionWizard(parent: Element): Wizard {
const name = '';
const desc = null;
const type = null;
const reservedNames = Array.from(parent.querySelectorAll('SubFunction')).map(
fUnction => fUnction.getAttribute('name')!
);

return [
{
title: get('wizard.title.add', { tagName: 'SubFunction' }),
primary: {
icon: 'save',
label: get('save'),
action: createSubFunctionAction(parent),
},
content: [
...contentFunctionWizard({
name,
desc,
type,
reservedNames,
}),
],
},
];
}
3 changes: 2 additions & 1 deletion src/wizards/wizard-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { editTrgOpsWizard } from './trgops.js';
import { createDaWizard } from './da.js';
import { editDAIWizard } from './dai.js';
import { createFunctionWizard } from './function.js';
import { createSubFunctionWizard } from './subfunction.js';

type SclElementWizard = (
element: Element,
Expand Down Expand Up @@ -476,7 +477,7 @@ export const wizards: Record<
},
SubFunction: {
edit: emptyWizard,
create: emptyWizard,
create: createSubFunctionWizard,
},
SubNetwork: {
edit: editSubNetworkWizard,
Expand Down
87 changes: 87 additions & 0 deletions test/integration/editors/substation/function-editor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { fixture, html, expect } from '@open-wc/testing';

import '../../../mock-wizard-editor.js';
import { MockWizardEditor } from '../../../mock-wizard-editor.js';

import '../../../../src/editors/substation/function-editor.js';
import { FunctionEditor } from '../../../../src/editors/substation/function-editor.js';
import { WizardTextField } from '../../../../src/wizard-textfield.js';

describe('function-editor wizarding editing integration', () => {
describe('open create wizard for element SubFunction', () => {
let doc: XMLDocument;
let parent: MockWizardEditor;
let element: FunctionEditor | null;

let nameField: WizardTextField;
let primaryAction: HTMLElement;

beforeEach(async () => {
doc = await fetch('/test/testfiles/zeroline/functions.scd')
.then(response => response.text())
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
parent = <MockWizardEditor>(
await fixture(
html`<mock-wizard-editor
><function-editor
.element=${doc.querySelector('Function')}
></function-editor
></mock-wizard-editor>`
)
);

element = parent.querySelector('function-editor');

(<HTMLElement>(
element?.shadowRoot?.querySelector('mwc-list-item[value="SubFunction"]')
)).click();
await parent.updateComplete;

nameField = <WizardTextField>(
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="name"]')
);

primaryAction = <HTMLElement>(
parent.wizardUI.dialog?.querySelector(
'mwc-button[slot="primaryAction"]'
)
);
});

it('does not add SubFunction if name attribute is not unique', async () => {
expect(
doc.querySelector(
'Substation > Function > SubFunction[name="mySubFunc"]'
)
).to.exist;

nameField.value = 'mySubFunc';
primaryAction.click();
await parent.updateComplete;

expect(
doc.querySelectorAll(
'Substation > Function > SubFunction[name="mySubFunc"]'
).length
).to.equal(1);
});

it('does add SubFunction if name attribute is unique', async () => {
expect(
doc.querySelector(
'Substation > Function > SubFunction[name="someNewFunction"]'
)
).to.not.exist;

nameField.value = 'someNewFunction';
await parent.updateComplete;
primaryAction.click();

expect(
doc.querySelector(
'Substation > Function > SubFunction[name="someNewFunction"]'
)
).to.exist;
});
});
});
Loading