Skip to content

Commit af246dd

Browse files
fix(editors/template): on id attribute update adopt references as well (openscd#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
1 parent 8bc0549 commit af246dd

File tree

11 files changed

+511
-29
lines changed

11 files changed

+511
-29
lines changed

src/editors/templates/datype-wizards.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SingleSelectedEvent } from '@material/mwc-list/mwc-list-foundation';
1212

1313
import '../../wizard-textfield.js';
1414
import {
15+
cloneElement,
1516
Create,
1617
createElement,
1718
EditorAction,
@@ -21,6 +22,7 @@ import {
2122
newSubWizardEvent,
2223
newWizardEvent,
2324
patterns,
25+
Replace,
2426
selector,
2527
Wizard,
2628
WizardActor,
@@ -31,9 +33,42 @@ import {
3133
addReferencedDataTypes,
3234
allDataTypeSelector,
3335
unifyCreateActionArray,
34-
updateIDNamingAction,
3536
} from './foundation.js';
3637

38+
function updateDATpyeAction(element: Element): WizardActor {
39+
return (inputs: WizardInput[]): EditorAction[] => {
40+
const id = getValue(inputs.find(i => i.label === 'id')!)!;
41+
const desc = getValue(inputs.find(i => i.label === 'desc')!);
42+
43+
if (
44+
id === element.getAttribute('id') &&
45+
desc === element.getAttribute('desc')
46+
)
47+
return [];
48+
49+
const newElement = cloneElement(element, { id, desc });
50+
51+
const actions: Replace[] = [];
52+
actions.push({ old: { element }, new: { element: newElement } });
53+
54+
const oldId = element.getAttribute('id')!;
55+
Array.from(
56+
element.ownerDocument.querySelectorAll(
57+
`DOType > DA[type="${oldId}"], DAType > BDA[type="${oldId}"]`
58+
)
59+
).forEach(oldDa => {
60+
const newDa = <Element>oldDa.cloneNode(false);
61+
newDa.setAttribute('type', id);
62+
63+
actions.push({ old: { element: oldDa }, new: { element: newDa } });
64+
});
65+
66+
return [
67+
{ title: get('datype.action.edit', { oldId, newId: id }), actions },
68+
];
69+
};
70+
}
71+
3772
export function editDaTypeWizard(
3873
dATypeIdentity: string,
3974
doc: XMLDocument
@@ -51,7 +86,7 @@ export function editDaTypeWizard(
5186
primary: {
5287
icon: '',
5388
label: get('save'),
54-
action: updateIDNamingAction(datype),
89+
action: updateDATpyeAction(datype),
5590
},
5691
content: [
5792
html`<mwc-button

src/editors/templates/dotype-wizards.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
newActionEvent,
2323
newSubWizardEvent,
2424
newWizardEvent,
25+
Replace,
2526
selector,
2627
Wizard,
2728
WizardActor,
@@ -320,7 +321,24 @@ function updateDOTypeAction(element: Element): WizardActor {
320321

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

323-
return [{ old: { element }, new: { element: newElement } }];
324+
const actions: Replace[] = [];
325+
actions.push({ old: { element }, new: { element: newElement } });
326+
327+
const oldId = element.getAttribute('id')!;
328+
Array.from(
329+
element.ownerDocument.querySelectorAll(
330+
`LNodeType > DO[type="${oldId}"], DOType > SDO[type="${oldId}"]`
331+
)
332+
).forEach(oldDo => {
333+
const newDo = <Element>oldDo.cloneNode(false);
334+
newDo.setAttribute('type', id);
335+
336+
actions.push({ old: { element: oldDo }, new: { element: newDo } });
337+
});
338+
339+
return [
340+
{ title: get('dotype.action.edit', { oldId, newId: id }), actions },
341+
];
324342
};
325343
}
326344

src/editors/templates/enumtype-wizard.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@ import {
2222
newSubWizardEvent,
2323
newWizardEvent,
2424
patterns,
25+
Replace,
2526
selector,
2627
Wizard,
2728
WizardActor,
2829
WizardInput,
2930
} from '../../foundation.js';
30-
import {
31-
CreateOptions,
32-
updateIDNamingAction,
33-
UpdateOptions,
34-
WizardOptions,
35-
} from './foundation.js';
31+
import { CreateOptions, UpdateOptions, WizardOptions } from './foundation.js';
3632

3733
function nextOrd(parent: Element): string {
3834
const maxOrd = Math.max(
@@ -258,6 +254,38 @@ export function createEnumTypeWizard(
258254
];
259255
}
260256

257+
function updateEnumTpyeAction(element: Element): WizardActor {
258+
return (inputs: WizardInput[]): EditorAction[] => {
259+
const id = getValue(inputs.find(i => i.label === 'id')!)!;
260+
const desc = getValue(inputs.find(i => i.label === 'desc')!);
261+
262+
if (
263+
id === element.getAttribute('id') &&
264+
desc === element.getAttribute('desc')
265+
)
266+
return [];
267+
268+
const newElement = cloneElement(element, { id, desc });
269+
270+
const actions: Replace[] = [];
271+
actions.push({ old: { element }, new: { element: newElement } });
272+
273+
const oldId = element.getAttribute('id')!;
274+
Array.from(
275+
element.ownerDocument.querySelectorAll(
276+
`DOType > DA[type="${oldId}"], DAType > BDA[type="${oldId}"]`
277+
)
278+
).forEach(oldDa => {
279+
const newDa = <Element>oldDa.cloneNode(false);
280+
newDa.setAttribute('type', id);
281+
282+
actions.push({ old: { element: oldDa }, new: { element: newDa } });
283+
});
284+
285+
return [{ title: get('enum.action.edit', { oldId, newId: id }), actions }];
286+
};
287+
}
288+
261289
export function eNumTypeEditWizard(
262290
eNumTypeIdentity: string,
263291
doc: XMLDocument
@@ -272,7 +300,7 @@ export function eNumTypeEditWizard(
272300
primary: {
273301
icon: '',
274302
label: get('save'),
275-
action: updateIDNamingAction(enumtype),
303+
action: updateEnumTpyeAction(enumtype),
276304
},
277305
content: [
278306
html`<mwc-button

src/editors/templates/foundation.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ export function isCreateOptions(
3030
return (<CreateOptions>options).parent !== undefined;
3131
}
3232

33-
export function updateIDNamingAction(element: Element): WizardActor {
34-
return (inputs: WizardInput[]): EditorAction[] => {
35-
const id = getValue(inputs.find(i => i.label === 'id')!)!;
36-
const desc = getValue(inputs.find(i => i.label === 'desc')!);
37-
38-
if (
39-
id === element.getAttribute('id') &&
40-
desc === element.getAttribute('desc')
41-
)
42-
return [];
43-
44-
const newElement = cloneElement(element, { id, desc });
45-
46-
return [{ old: { element }, new: { element: newElement } }];
47-
};
48-
}
49-
5033
function containsCreateAction(actions: Create[], newAction: Create): boolean {
5134
return !actions.some(
5235
action =>

src/editors/templates/lnodetype-wizard.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
newSubWizardEvent,
2727
newWizardEvent,
2828
patterns,
29+
Replace,
2930
selector,
3031
Wizard,
3132
WizardActor,
@@ -530,7 +531,24 @@ function updateLNodeTypeAction(element: Element): WizardActor {
530531

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

533-
return [{ old: { element }, new: { element: newElement } }];
534+
const actions: Replace[] = [];
535+
actions.push({ old: { element }, new: { element: newElement } });
536+
537+
const oldId = element.getAttribute('id')!;
538+
Array.from(
539+
element.ownerDocument.querySelectorAll(
540+
`LN0[lnType="${oldId}"], LN[lnType="${oldId}"]`
541+
)
542+
).forEach(oldAnyLn => {
543+
const newAnyLn = <Element>oldAnyLn.cloneNode(false);
544+
newAnyLn.setAttribute('lnType', id);
545+
546+
actions.push({ old: { element: oldAnyLn }, new: { element: newAnyLn } });
547+
});
548+
549+
return [
550+
{ title: get('lnodetype.action.edit', { oldId, newId: id }), actions },
551+
];
534552
};
535553
}
536554

src/translations/de.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ export const de: Translations = {
303303
edit: 'EnumType bearbeiten',
304304
},
305305
},
306+
action: {
307+
edit: 'DAType ID "{{oldId}}" und deren DA-Referenzen geändert zu {{newId}} ',
308+
},
306309
},
307310
datype: {
308311
wizard: {
@@ -311,6 +314,9 @@ export const de: Translations = {
311314
edit: 'DAType bearbeiten',
312315
},
313316
},
317+
action: {
318+
edit: 'EnumType ID "{{oldId}}" und deren DA-Referenzen geändert zu {{newId}} ',
319+
},
314320
},
315321
bda: {
316322
wizard: {
@@ -352,6 +358,9 @@ export const de: Translations = {
352358
},
353359
enums: 'Standard Enumerations',
354360
},
361+
action: {
362+
edit: 'DOType ID "{{oldId}}" und deren DO-Referenzen geändert zu {{newId}} ',
363+
},
355364
},
356365
lnodetype: {
357366
wizard: {
@@ -361,6 +370,9 @@ export const de: Translations = {
361370
select: 'Data Objects auswählen',
362371
},
363372
},
373+
action: {
374+
edit: 'LNodeType ID "{{oldId}}" und deren LN-Referenzen geändert zu {{newId}} ',
375+
},
364376
autoimport: 'Vordefinierte OpenSCD LN Klasse verwenden',
365377
missinglnclass: 'Vordefinierte LN Klasse fehlt',
366378
},

src/translations/en.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ export const en = {
299299
edit: 'Edit EnumType',
300300
},
301301
},
302+
action: {
303+
edit: 'Change EnumType ID "{{oldId}}" and its DA references to {{newId}} ',
304+
},
302305
},
303306
datype: {
304307
wizard: {
@@ -307,6 +310,9 @@ export const en = {
307310
edit: 'Edit DAType',
308311
},
309312
},
313+
action: {
314+
edit: 'Change DAType ID "{{oldId}}" and its DA references to {{newId}} ',
315+
},
310316
},
311317
bda: {
312318
wizard: {
@@ -348,6 +354,9 @@ export const en = {
348354
},
349355
enums: 'Default enumerations',
350356
},
357+
action: {
358+
edit: 'Change DOType ID "{{oldId}}" and its DO references to {{newId}} ',
359+
},
351360
},
352361
lnodetype: {
353362
wizard: {
@@ -357,6 +366,9 @@ export const en = {
357366
select: 'Select Data Objects',
358367
},
359368
},
369+
action: {
370+
edit: 'Change LNodeType ID "{{oldId}}" and its LN references to {{newId}} ',
371+
},
360372
autoimport: 'Use LN class from OpenSCD template',
361373
missinglnclass: 'Missing pre-defined LN class',
362374
},

0 commit comments

Comments
 (0)