Skip to content

Commit

Permalink
fix(editors/template): on id attribute update adopt references as well (
Browse files Browse the repository at this point in the history
#590)

* fix(editors/templates/lnodetype): make sure to update referenced lnType on id change

fixup-with-first

* fix(editors/template/dotype): make sure to uo update referenced type on id change

fixup-with-second

fixup-with-first

* fix(editors/template/datype): make sure to update ids references on id update

fixup-with-third

* fix(editors/template/enumtype): update id references on id attribute update

* fix(translations): in-cooperate review comments
  • Loading branch information
JakobVogelsang authored Mar 14, 2022
1 parent 8bc0549 commit af246dd
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 29 deletions.
39 changes: 37 additions & 2 deletions src/editors/templates/datype-wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SingleSelectedEvent } from '@material/mwc-list/mwc-list-foundation';

import '../../wizard-textfield.js';
import {
cloneElement,
Create,
createElement,
EditorAction,
Expand All @@ -21,6 +22,7 @@ import {
newSubWizardEvent,
newWizardEvent,
patterns,
Replace,
selector,
Wizard,
WizardActor,
Expand All @@ -31,9 +33,42 @@ import {
addReferencedDataTypes,
allDataTypeSelector,
unifyCreateActionArray,
updateIDNamingAction,
} from './foundation.js';

function updateDATpyeAction(element: Element): WizardActor {
return (inputs: WizardInput[]): EditorAction[] => {
const id = getValue(inputs.find(i => i.label === 'id')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);

if (
id === element.getAttribute('id') &&
desc === element.getAttribute('desc')
)
return [];

const newElement = cloneElement(element, { id, desc });

const actions: Replace[] = [];
actions.push({ old: { element }, new: { element: newElement } });

const oldId = element.getAttribute('id')!;
Array.from(
element.ownerDocument.querySelectorAll(
`DOType > DA[type="${oldId}"], DAType > BDA[type="${oldId}"]`
)
).forEach(oldDa => {
const newDa = <Element>oldDa.cloneNode(false);
newDa.setAttribute('type', id);

actions.push({ old: { element: oldDa }, new: { element: newDa } });
});

return [
{ title: get('datype.action.edit', { oldId, newId: id }), actions },
];
};
}

export function editDaTypeWizard(
dATypeIdentity: string,
doc: XMLDocument
Expand All @@ -51,7 +86,7 @@ export function editDaTypeWizard(
primary: {
icon: '',
label: get('save'),
action: updateIDNamingAction(datype),
action: updateDATpyeAction(datype),
},
content: [
html`<mwc-button
Expand Down
20 changes: 19 additions & 1 deletion src/editors/templates/dotype-wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
newActionEvent,
newSubWizardEvent,
newWizardEvent,
Replace,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -320,7 +321,24 @@ function updateDOTypeAction(element: Element): WizardActor {

const newElement = cloneElement(element, { id, desc, cdc });

return [{ old: { element }, new: { element: newElement } }];
const actions: Replace[] = [];
actions.push({ old: { element }, new: { element: newElement } });

const oldId = element.getAttribute('id')!;
Array.from(
element.ownerDocument.querySelectorAll(
`LNodeType > DO[type="${oldId}"], DOType > SDO[type="${oldId}"]`
)
).forEach(oldDo => {
const newDo = <Element>oldDo.cloneNode(false);
newDo.setAttribute('type', id);

actions.push({ old: { element: oldDo }, new: { element: newDo } });
});

return [
{ title: get('dotype.action.edit', { oldId, newId: id }), actions },
];
};
}

Expand Down
42 changes: 35 additions & 7 deletions src/editors/templates/enumtype-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ import {
newSubWizardEvent,
newWizardEvent,
patterns,
Replace,
selector,
Wizard,
WizardActor,
WizardInput,
} from '../../foundation.js';
import {
CreateOptions,
updateIDNamingAction,
UpdateOptions,
WizardOptions,
} from './foundation.js';
import { CreateOptions, UpdateOptions, WizardOptions } from './foundation.js';

function nextOrd(parent: Element): string {
const maxOrd = Math.max(
Expand Down Expand Up @@ -258,6 +254,38 @@ export function createEnumTypeWizard(
];
}

function updateEnumTpyeAction(element: Element): WizardActor {
return (inputs: WizardInput[]): EditorAction[] => {
const id = getValue(inputs.find(i => i.label === 'id')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);

if (
id === element.getAttribute('id') &&
desc === element.getAttribute('desc')
)
return [];

const newElement = cloneElement(element, { id, desc });

const actions: Replace[] = [];
actions.push({ old: { element }, new: { element: newElement } });

const oldId = element.getAttribute('id')!;
Array.from(
element.ownerDocument.querySelectorAll(
`DOType > DA[type="${oldId}"], DAType > BDA[type="${oldId}"]`
)
).forEach(oldDa => {
const newDa = <Element>oldDa.cloneNode(false);
newDa.setAttribute('type', id);

actions.push({ old: { element: oldDa }, new: { element: newDa } });
});

return [{ title: get('enum.action.edit', { oldId, newId: id }), actions }];
};
}

export function eNumTypeEditWizard(
eNumTypeIdentity: string,
doc: XMLDocument
Expand All @@ -272,7 +300,7 @@ export function eNumTypeEditWizard(
primary: {
icon: '',
label: get('save'),
action: updateIDNamingAction(enumtype),
action: updateEnumTpyeAction(enumtype),
},
content: [
html`<mwc-button
Expand Down
17 changes: 0 additions & 17 deletions src/editors/templates/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ export function isCreateOptions(
return (<CreateOptions>options).parent !== undefined;
}

export function updateIDNamingAction(element: Element): WizardActor {
return (inputs: WizardInput[]): EditorAction[] => {
const id = getValue(inputs.find(i => i.label === 'id')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);

if (
id === element.getAttribute('id') &&
desc === element.getAttribute('desc')
)
return [];

const newElement = cloneElement(element, { id, desc });

return [{ old: { element }, new: { element: newElement } }];
};
}

function containsCreateAction(actions: Create[], newAction: Create): boolean {
return !actions.some(
action =>
Expand Down
20 changes: 19 additions & 1 deletion src/editors/templates/lnodetype-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
newSubWizardEvent,
newWizardEvent,
patterns,
Replace,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -530,7 +531,24 @@ function updateLNodeTypeAction(element: Element): WizardActor {

const newElement = cloneElement(element, { id, desc, lnClass });

return [{ old: { element }, new: { element: newElement } }];
const actions: Replace[] = [];
actions.push({ old: { element }, new: { element: newElement } });

const oldId = element.getAttribute('id')!;
Array.from(
element.ownerDocument.querySelectorAll(
`LN0[lnType="${oldId}"], LN[lnType="${oldId}"]`
)
).forEach(oldAnyLn => {
const newAnyLn = <Element>oldAnyLn.cloneNode(false);
newAnyLn.setAttribute('lnType', id);

actions.push({ old: { element: oldAnyLn }, new: { element: newAnyLn } });
});

return [
{ title: get('lnodetype.action.edit', { oldId, newId: id }), actions },
];
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/translations/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ export const de: Translations = {
edit: 'EnumType bearbeiten',
},
},
action: {
edit: 'DAType ID "{{oldId}}" und deren DA-Referenzen geändert zu {{newId}} ',
},
},
datype: {
wizard: {
Expand All @@ -311,6 +314,9 @@ export const de: Translations = {
edit: 'DAType bearbeiten',
},
},
action: {
edit: 'EnumType ID "{{oldId}}" und deren DA-Referenzen geändert zu {{newId}} ',
},
},
bda: {
wizard: {
Expand Down Expand Up @@ -352,6 +358,9 @@ export const de: Translations = {
},
enums: 'Standard Enumerations',
},
action: {
edit: 'DOType ID "{{oldId}}" und deren DO-Referenzen geändert zu {{newId}} ',
},
},
lnodetype: {
wizard: {
Expand All @@ -361,6 +370,9 @@ export const de: Translations = {
select: 'Data Objects auswählen',
},
},
action: {
edit: 'LNodeType ID "{{oldId}}" und deren LN-Referenzen geändert zu {{newId}} ',
},
autoimport: 'Vordefinierte OpenSCD LN Klasse verwenden',
missinglnclass: 'Vordefinierte LN Klasse fehlt',
},
Expand Down
12 changes: 12 additions & 0 deletions src/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const en = {
edit: 'Edit EnumType',
},
},
action: {
edit: 'Change EnumType ID "{{oldId}}" and its DA references to {{newId}} ',
},
},
datype: {
wizard: {
Expand All @@ -307,6 +310,9 @@ export const en = {
edit: 'Edit DAType',
},
},
action: {
edit: 'Change DAType ID "{{oldId}}" and its DA references to {{newId}} ',
},
},
bda: {
wizard: {
Expand Down Expand Up @@ -348,6 +354,9 @@ export const en = {
},
enums: 'Default enumerations',
},
action: {
edit: 'Change DOType ID "{{oldId}}" and its DO references to {{newId}} ',
},
},
lnodetype: {
wizard: {
Expand All @@ -357,6 +366,9 @@ export const en = {
select: 'Select Data Objects',
},
},
action: {
edit: 'Change LNodeType ID "{{oldId}}" and its LN references to {{newId}} ',
},
autoimport: 'Use LN class from OpenSCD template',
missinglnclass: 'Missing pre-defined LN class',
},
Expand Down
Loading

0 comments on commit af246dd

Please sign in to comment.