Skip to content

Commit 7936227

Browse files
JakobVogelsangca-d
andauthored
refactor(foundation): add cloneElement function (#284)
* feat(foundation): add cloneElement function * refactor(zeroline/foundation): change cloneElement to avoid collision * refactor(zeroline-pane): move updateNameAction to wizards foundation * refactor(communication): use cloneElement * refactor(wizards/templaterelated): use cloneElement * refactor(wizards/iededitorrelated): use cloneElement * docs(foundation): clarify cloneElement doc comment Co-authored-by: cad <[email protected]>
1 parent db49745 commit 7936227

23 files changed

+173
-170
lines changed

src/editors/communication/subnetwork-editor.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
compareNames,
2222
createElement,
2323
getReference,
24+
cloneElement,
2425
} from '../../foundation.js';
2526

2627
import { styles, WizardOptions, isCreateOptions } from './foundation.js';
@@ -64,10 +65,9 @@ function getBitRateAction(
6465
},
6566
};
6667

67-
const newBitRate = <Element>oldBitRate.cloneNode(false);
68+
const newBitRate = cloneElement(oldBitRate, { multiplier });
6869
newBitRate.textContent = BitRate;
69-
if (multiplier === null) newBitRate.removeAttribute('multiplier');
70-
else newBitRate.setAttribute('multiplier', multiplier);
70+
7171
return {
7272
old: { element: oldBitRate },
7373
new: { element: newBitRate },
@@ -92,12 +92,7 @@ export function updateSubNetworkAction(element: Element): WizardActor {
9292
) {
9393
subNetworkAction = null;
9494
} else {
95-
const newElement = <Element>element.cloneNode(false);
96-
newElement.setAttribute('name', name);
97-
if (desc === null) newElement.removeAttribute('desc');
98-
else newElement.setAttribute('desc', desc);
99-
if (type === null) newElement.removeAttribute('type');
100-
else newElement.setAttribute('type', type);
95+
const newElement = cloneElement(element, { name, desc, type });
10196
subNetworkAction = { old: { element }, new: { element: newElement } };
10297
}
10398

src/editors/templates/dotype-wizards.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from 'lit-html';
22
import { get, translate } from 'lit-translate';
33

44
import {
5+
cloneElement,
56
Create,
67
createElement,
78
EditorAction,
@@ -49,15 +50,8 @@ function updateSDoAction(element: Element): WizardActor {
4950
return [];
5051
}
5152

52-
const newElement = <Element>element.cloneNode(false);
53-
newElement.setAttribute('name', name);
54-
if (desc === null) newElement.removeAttribute('desc');
55-
else newElement.setAttribute('desc', desc);
56-
newElement.setAttribute('type', type);
57-
actions.push({
58-
old: { element },
59-
new: { element: newElement },
60-
});
53+
const newElement = cloneElement(element, { name, desc, type });
54+
actions.push({ old: { element }, new: { element: newElement } });
6155

6256
return actions;
6357
};
@@ -297,11 +291,7 @@ function updateDOTypeAction(element: Element): WizardActor {
297291
)
298292
return [];
299293

300-
const newElement = <Element>element.cloneNode(false);
301-
newElement.setAttribute('id', id);
302-
if (desc === null) newElement.removeAttribute('desc');
303-
else newElement.setAttribute('desc', desc);
304-
newElement.setAttribute('cdc', cdc);
294+
const newElement = cloneElement(element, { id, desc, cdc });
305295

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

src/editors/templates/enumtype-wizard.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from 'lit-element';
22
import { get, translate } from 'lit-translate';
33

44
import {
5+
cloneElement,
56
createElement,
67
EditorAction,
78
getReference,
@@ -80,10 +81,7 @@ function updateEnumValAction(element: Element): WizardActor {
8081
)
8182
return [];
8283

83-
const newElement = <Element>element.cloneNode(false);
84-
if (desc === null) newElement.removeAttribute('desc');
85-
else newElement.setAttribute('desc', desc);
86-
newElement.setAttribute('ord', ord);
84+
const newElement = cloneElement(element, { desc, ord });
8785
newElement.textContent = value;
8886

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

src/editors/templates/foundation.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { css, html, TemplateResult } from 'lit-element';
22
import { ifDefined } from 'lit-html/directives/if-defined';
33

44
import {
5+
cloneElement,
56
Create,
67
EditorAction,
78
getReference,
@@ -40,10 +41,7 @@ export function updateIDNamingAction(element: Element): WizardActor {
4041
)
4142
return [];
4243

43-
const newElement = <Element>element.cloneNode(false);
44-
newElement.setAttribute('id', id);
45-
if (desc === null) newElement.removeAttribute('desc');
46-
else newElement.setAttribute('desc', desc);
44+
const newElement = cloneElement(element, { id, desc });
4745

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

src/editors/templates/lnodetype-wizard.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from 'lit-element';
22
import { get, translate } from 'lit-translate';
33

44
import {
5+
cloneElement,
56
Create,
67
createElement,
78
EditorAction,
@@ -47,7 +48,6 @@ function updateDoAction(element: Element): WizardActor {
4748
? getValue(inputs.find(i => i.label === 'transient')!)
4849
: null;
4950

50-
const actions: EditorAction[] = [];
5151
if (
5252
name === element.getAttribute('name') &&
5353
desc === element.getAttribute('desc') &&
@@ -58,21 +58,15 @@ function updateDoAction(element: Element): WizardActor {
5858
return [];
5959
}
6060

61-
const newElement = <Element>element.cloneNode(false);
62-
newElement.setAttribute('name', name);
63-
if (desc === null) newElement.removeAttribute('desc');
64-
else newElement.setAttribute('desc', desc);
65-
newElement.setAttribute('type', type);
66-
if (accessControl === null) newElement.removeAttribute('accessControl');
67-
else newElement.setAttribute('accessControl', accessControl);
68-
if (transient === null) newElement.removeAttribute('transient');
69-
else newElement.setAttribute('transient', transient);
70-
actions.push({
71-
old: { element },
72-
new: { element: newElement },
61+
const newElement = cloneElement(element, {
62+
name,
63+
desc,
64+
type,
65+
accessControl,
66+
transient,
7367
});
7468

75-
return actions;
69+
return [{ old: { element }, new: { element: newElement } }];
7670
};
7771
}
7872

@@ -547,11 +541,7 @@ function updateLNodeTypeAction(element: Element): WizardActor {
547541
)
548542
return [];
549543

550-
const newElement = <Element>element.cloneNode(false);
551-
newElement.setAttribute('id', id);
552-
if (desc === null) newElement.removeAttribute('desc');
553-
else newElement.setAttribute('desc', desc);
554-
newElement.setAttribute('lnClass', lnClass);
544+
const newElement = cloneElement(element, { id, desc, lnClass });
555545

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

src/foundation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,19 @@ export function createElement(
23642364
return element;
23652365
}
23662366

2367+
/** @returns a clone of `element` with attributes set to values from `attrs`. */
2368+
export function cloneElement(
2369+
element: Element,
2370+
attrs: Record<string, string | null>
2371+
): Element {
2372+
const newElement = <Element>element.cloneNode(false);
2373+
Object.entries(attrs).forEach(([name, value]) => {
2374+
if (value === null) newElement.removeAttribute(name);
2375+
else newElement.setAttribute(name, value);
2376+
});
2377+
return newElement;
2378+
}
2379+
23672380
/** A directive rendering its argument `rendered` only if `rendered !== {}`. */
23682381
export const ifImplemented = directive(rendered => (part: Part) => {
23692382
if (Object.keys(rendered).length) part.setValue(rendered);

src/wizards/bay.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
WizardActor,
1111
WizardInput,
1212
} from '../foundation.js';
13-
14-
import { updateNamingAction } from '../zeroline/foundation.js';
13+
import { updateNamingAction } from './foundation/actions.js';
1514

1615
function render(name: string | null, desc: string | null): TemplateResult[] {
1716
return [

src/wizards/bda.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from 'lit-html';
22
import { get, translate } from 'lit-translate';
33

44
import {
5+
cloneElement,
56
createElement,
67
EditorAction,
78
getReference,
@@ -48,19 +49,15 @@ export function updateBDaAction(element: Element): WizardActor {
4849
) {
4950
bdaAction = null;
5051
} else {
51-
const newElement = <Element>element.cloneNode(false);
52-
newElement.setAttribute('name', name);
53-
if (desc === null) newElement.removeAttribute('desc');
54-
else newElement.setAttribute('desc', desc);
55-
newElement.setAttribute('bType', bType);
56-
if (type === null) newElement.removeAttribute('type');
57-
else newElement.setAttribute('type', type);
58-
if (sAddr === null) newElement.removeAttribute('sAddr');
59-
else newElement.setAttribute('sAddr', sAddr);
60-
if (valKind === null) newElement.removeAttribute('valKind');
61-
else newElement.setAttribute('valKind', valKind);
62-
if (valImport === null) newElement.removeAttribute('valImport');
63-
else newElement.setAttribute('valImport', valImport);
52+
const newElement = cloneElement(element, {
53+
name,
54+
desc,
55+
bType,
56+
type,
57+
sAddr,
58+
valKind,
59+
valImport,
60+
});
6461
bdaAction = { old: { element }, new: { element: newElement } };
6562
}
6663

src/wizards/conductingequipment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { html, TemplateResult } from 'lit-element';
22
import { get, translate } from 'lit-translate';
33

4-
import { updateNamingAction } from '../zeroline/foundation.js';
54
import {
65
createElement,
76
EditorAction,
@@ -12,6 +11,7 @@ import {
1211
WizardActor,
1312
WizardInput,
1413
} from '../foundation.js';
14+
import { updateNamingAction } from './foundation/actions.js';
1515

1616
const types: Partial<Record<string, string>> = {
1717
// standard

src/wizards/da.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html, TemplateResult } from 'lit-html';
22
import { get, translate } from 'lit-translate';
33

44
import {
5+
cloneElement,
56
createElement,
67
EditorAction,
78
getReference,
@@ -115,26 +116,19 @@ export function updateDaAction(element: Element): WizardActor {
115116
) {
116117
daAction = null;
117118
} else {
118-
const newElement = <Element>element.cloneNode(false);
119-
newElement.setAttribute('name', name);
120-
if (desc === null) newElement.removeAttribute('desc');
121-
else newElement.setAttribute('desc', desc);
122-
newElement.setAttribute('bType', bType);
123-
if (type === null) newElement.removeAttribute('type');
124-
else newElement.setAttribute('type', type);
125-
if (sAddr === null) newElement.removeAttribute('sAddr');
126-
else newElement.setAttribute('sAddr', sAddr);
127-
if (valKind === null) newElement.removeAttribute('valKind');
128-
else newElement.setAttribute('valKind', valKind);
129-
if (valImport === null) newElement.removeAttribute('valImport');
130-
else newElement.setAttribute('valImport', valImport);
131-
newElement.setAttribute('fc', fc);
132-
if (dchg === null) newElement.removeAttribute('dchg');
133-
else newElement.setAttribute('dchg', dchg);
134-
if (qchg === null) newElement.removeAttribute('qchg');
135-
else newElement.setAttribute('qchg', qchg);
136-
if (dupd === null) newElement.removeAttribute('dupd');
137-
else newElement.setAttribute('dupd', dupd);
119+
const newElement = cloneElement(element, {
120+
name,
121+
desc,
122+
bType,
123+
type,
124+
sAddr,
125+
valKind,
126+
valImport,
127+
fc,
128+
dchg,
129+
qchg,
130+
dupd,
131+
});
138132
daAction = { old: { element }, new: { element: newElement } };
139133
}
140134

0 commit comments

Comments
 (0)