Skip to content

Commit

Permalink
feat(templates): add val manipulation capability (#275)
Browse files Browse the repository at this point in the history
* feat(abstractda): create valAction and renderWizard to be used by both BDA and DA wizards

* fix(abstractda): name pattern and maxLength

* fix(abstractda): name attributes limits

* test(abstractda): update snapshot

* refactor(bda): move bDAWizard to wizard-library

* feat(bda): createBDaAction create Val

* refactor(datype-wizards): remove unused bda eizards and actions

* feat(da) add Val,dchg,achg,dupd to da wizards

* feat(da): add Val,dchg,qchg,dupd to createDaWizard

* fix(da and bda): minor fixes

* refactor(wizards): rename foundation folder
  • Loading branch information
JakobVogelsang committed Aug 3, 2021
1 parent 899988e commit 259ce39
Show file tree
Hide file tree
Showing 28 changed files with 5,896 additions and 3,724 deletions.
1,198 changes: 1,198 additions & 0 deletions __snapshots__/BDA wizarding editing integration.md

Large diffs are not rendered by default.

1,695 changes: 1,695 additions & 0 deletions __snapshots__/DA wizarding editing integration.md

Large diffs are not rendered by default.

1,154 changes: 0 additions & 1,154 deletions __snapshots__/DAType wizards.md

Large diffs are not rendered by default.

1,476 changes: 0 additions & 1,476 deletions __snapshots__/DOType wizards.md

Large diffs are not rendered by default.

521 changes: 521 additions & 0 deletions __snapshots__/abstractda wizards.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/editors/Templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './templates/enumtype-wizard.js';
import {
createDATypeWizard,
dATypeWizard,
editDaTypeWizard,
} from './templates/datype-wizards.js';
import {
createDOTypeWizard,
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class TemplatesPlugin extends LitElement {
}

openDATypeWizard(identity: string): void {
const wizard = dATypeWizard(identity, this.doc);
const wizard = editDaTypeWizard(identity, this.doc);
if (wizard) this.dispatchEvent(newWizardEvent(wizard));
}

Expand Down
307 changes: 17 additions & 290 deletions src/editors/templates/datype-wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { get, translate } from 'lit-translate';

import {
Create,
createElement,
EditorAction,
getReference,
getValue,
identity,
isPublic,
newActionEvent,
newWizardEvent,
patterns,
Expand All @@ -21,297 +19,26 @@ import {
import {
addReferencedDataTypes,
allDataTypeSelector,
buildListFromStringArray,
CreateOptions,
predefinedBasicTypeEnum,
unifyCreateActionArray,
updateIDNamingAction,
UpdateOptions,
valKindEnum,
WizardOptions,
} from './foundation.js';

import { List } from '@material/mwc-list';
import { ListItem } from '@material/mwc-list/mwc-list-item';
import { Select } from '@material/mwc-select';
import {
SelectedEvent,
SingleSelectedEvent,
} from '@material/mwc-list/mwc-list-foundation';

function updateBDaAction(element: Element): WizardActor {
return (inputs: WizardInput[]): EditorAction[] => {
const name = getValue(inputs.find(i => i.label === 'name')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);
const bType = getValue(inputs.find(i => i.label === 'bType')!)!;
const type =
bType === 'Enum' || bType === 'Struct'
? getValue(inputs.find(i => i.label === 'type')!)
: null;
const sAddr = getValue(inputs.find(i => i.label === 'sAddr')!);
const valKind =
getValue(inputs.find(i => i.label === 'valKind')!) !== ''
? getValue(inputs.find(i => i.label === 'valKind')!)
: null;
const valImport =
getValue(inputs.find(i => i.label === 'valImport')!) !== ''
? getValue(inputs.find(i => i.label === 'valImport')!)
: null;

const actions: EditorAction[] = [];
if (
name === element.getAttribute('name') &&
desc === element.getAttribute('desc') &&
bType === element.getAttribute('bType') &&
type === element.getAttribute('type') &&
sAddr === element.getAttribute('sAddr') &&
valKind === element.getAttribute('valKind') &&
valImport === element.getAttribute('valImprot')
) {
return [];
}

const newElement = <Element>element.cloneNode(false);
newElement.setAttribute('name', name);
if (desc === null) newElement.removeAttribute('desc');
else newElement.setAttribute('desc', desc);
newElement.setAttribute('bType', bType);
if (type === null) newElement.removeAttribute('type');
else newElement.setAttribute('type', type);
if (sAddr === null) newElement.removeAttribute('sAddr');
else newElement.setAttribute('sAddr', sAddr);
if (valKind === null) newElement.removeAttribute('valKind');
else newElement.setAttribute('valKind', valKind);
if (valImport === null) newElement.removeAttribute('valImport');
else newElement.setAttribute('valImport', valImport);
actions.push({
old: { element },
new: { element: newElement },
});

return actions;
};
}

function createBDaAction(parent: Element): WizardActor {
return (inputs: WizardInput[]): EditorAction[] => {
const name = getValue(inputs.find(i => i.label === 'name')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);
const bType = getValue(inputs.find(i => i.label === 'bType')!)!;
const type =
bType === 'Enum' || bType === 'Struct'
? getValue(inputs.find(i => i.label === 'type')!)
: null;
const sAddr = getValue(inputs.find(i => i.label === 'sAddr')!);
const valKind =
getValue(inputs.find(i => i.label === 'valKind')!) !== ''
? getValue(inputs.find(i => i.label === 'valKind')!)
: null;
const valImport =
getValue(inputs.find(i => i.label === 'valImport')!) !== ''
? getValue(inputs.find(i => i.label === 'valImport')!)
: null;

const actions: EditorAction[] = [];

const element = createElement(parent.ownerDocument, 'BDA', {
name,
desc,
bType,
type,
sAddr,
valKind,
valImport,
});
actions.push({
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

return actions;
};
}

function bDAWizard(options: WizardOptions): Wizard | undefined {
const doc = (<UpdateOptions>options).doc
? (<UpdateOptions>options).doc
: (<CreateOptions>options).parent.ownerDocument;
const bda =
Array.from(
doc.querySelectorAll(
selector('BDA', (<UpdateOptions>options).identity ?? NaN)
)
).find(isPublic) ?? null;

const [
title,
action,
type,
deleteButton,
name,
desc,
bTypeList,
sAddr,
valKindList,
valImportList,
] = bda
? [
get('bda.wizard.title.edit'),
updateBDaAction(bda),
bda.getAttribute('type'),
html`<mwc-button
icon="delete"
trailingIcon
label="${translate('delete')}"
@click=${(e: MouseEvent) => {
e.target!.dispatchEvent(newWizardEvent());
e.target!.dispatchEvent(
newActionEvent({
old: {
parent: bda.parentElement!,
element: bda,
reference: bda.nextSibling,
},
})
);
}}
fullwidth
></mwc-button> `,
bda.getAttribute('name'),
bda.getAttribute('desc'),
buildListFromStringArray(
predefinedBasicTypeEnum,
bda.getAttribute('bType')
),
bda.getAttribute('sAddr'),
buildListFromStringArray(valKindEnum, bda.getAttribute('valKind')),
buildListFromStringArray(
[null, 'true', 'false'],
bda.getAttribute('valImport')
),
]
: [
get('bda.wizard.title.add'),
createBDaAction((<CreateOptions>options).parent),
null,
html``,
'',
null,
buildListFromStringArray(predefinedBasicTypeEnum, 'Struct'),
null,
buildListFromStringArray(valKindEnum, null),
buildListFromStringArray([null, 'true', 'false'], null),
];

const types = Array.from(doc.querySelectorAll('DAType, EnumType'))
.filter(isPublic)
.filter(type => type.getAttribute('id'));

return [
{
title,
element: bda ?? undefined,
primary: { icon: '', label: get('save'), action: action },
content: [
deleteButton,
html`<wizard-textfield
label="name"
.maybeValue=${name}
helper="${translate('scl.name')}"
required
pattern="${patterns.alphanumericFirstLowerCase}"
dialogInitialFocus
>
></wizard-textfield
>`,
html`<wizard-textfield
label="desc"
helper="${translate('scl.desc')}"
.maybeValue=${desc}
nullable
pattern="${patterns.normalizedString}"
></wizard-textfield>`,
html`<mwc-select
fixedMenuPosition
label="bType"
helper="${translate('scl.bType')}"
required
@selected=${(e: SelectedEvent) => {
const bTypeOriginal = bda?.getAttribute('bType') ?? '';
const bType = (<Select>e.target).selected!.value!;
const typeUI = <Select>(
(<Select>e.target).parentElement!.querySelector(
'mwc-select[label="type"]'
)!
);
import { SingleSelectedEvent } from '@material/mwc-list/mwc-list-foundation';
import { createBDAWizard, editBDAWizard } from '../../wizards/bda.js';

Array.from(typeUI.children).forEach(child => {
(<ListItem>child).disabled = !child.classList.contains(bType);
(<ListItem>child).noninteractive =
!child.classList.contains(bType);
(<ListItem>child).style.display = !child.classList.contains(bType)
? 'none'
: '';
(<ListItem>child).selected =
bTypeOriginal === bType
? (<ListItem>child).value === type
: child.classList.contains(bType);
});
typeUI.disabled = !(bType === 'Enum' || bType === 'Struct');
typeUI.requestUpdate();
}}
>${bTypeList}</mwc-select
>`,
html`<mwc-select
fixedMenuPosition
label="type"
helper="${translate('scl.type')}"
>${types.map(
dataType =>
html`<mwc-list-item
class="${dataType.tagName === 'EnumType' ? 'Enum' : 'Struct'}"
value=${dataType.id}
?selected=${dataType.id === type}
>${dataType.id}</mwc-list-item
>`
)}</mwc-select
>`,
html`<wizard-textfield
label="sAddr"
helper="${translate('scl.sAddr')}"
.maybeValue=${sAddr}
nullable
pattern="${patterns.normalizedString}"
></wizard-textfield>`,
html`<mwc-select
label="valKind"
helper="${translate('scl.valKind')}"
fixedMenuPosition
>${valKindList}</mwc-select
>`,
html`<mwc-select
fixedMenuPosition
label="valImport"
helper="${translate('scl.valImport')}"
>${valImportList}</mwc-select
>`,
],
},
];
}

export function dATypeWizard(
export function editDaTypeWizard(
dATypeIdentity: string,
doc: XMLDocument
): Wizard | undefined {
const datype = doc.querySelector(selector('DAType', dATypeIdentity));
if (!datype) return undefined;

const id = datype.getAttribute('id');
const desc = datype.getAttribute('desc');

return [
{
title: get('datype.wizard.title.edit'),
Expand Down Expand Up @@ -343,7 +70,7 @@ export function dATypeWizard(
html`<wizard-textfield
label="id"
helper="${translate('scl.id')}"
.maybeValue=${datype.getAttribute('id')}
.maybeValue=${id}
required
maxlength="127"
minlength="1"
Expand All @@ -353,7 +80,7 @@ export function dATypeWizard(
html`<wizard-textfield
label="desc"
helper="${translate('scl.desc')}"
.maybeValue=${datype.getAttribute('desc')}
.maybeValue=${desc}
nullable
pattern="${patterns.normalizedString}"
></wizard-textfield>`,
Expand All @@ -363,21 +90,21 @@ export function dATypeWizard(
trailingIcon
label="${translate('scl.DA')}"
@click=${(e: Event) => {
const wizard = bDAWizard({
parent: datype,
});
if (wizard) e.target!.dispatchEvent(newWizardEvent(wizard));
if (datype)
e.target!.dispatchEvent(
newWizardEvent(createBDAWizard(datype))
);
e.target!.dispatchEvent(newWizardEvent());
}}
></mwc-button>
<mwc-list
style="margin-top: 0px;"
@selected=${(e: SingleSelectedEvent) => {
const wizard = bDAWizard({
identity: (<ListItem>(<List>e.target).selected).value,
doc,
});
if (wizard) e.target!.dispatchEvent(newWizardEvent(wizard));
const bdaIdentity = (<ListItem>(<List>e.target).selected).value;
const bda = doc.querySelector(selector('BDA', bdaIdentity));
if (bda)
e.target!.dispatchEvent(newWizardEvent(editBDAWizard(bda)));
e.target!.dispatchEvent(newWizardEvent());
}}
>
Expand Down
Loading

0 comments on commit 259ce39

Please sign in to comment.