Skip to content

Commit 7631312

Browse files
author
Dennis Labordus
committed
Small refactoring and added test for the plugin components.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 0c98e08 commit 7631312

File tree

10 files changed

+1158
-1851
lines changed

10 files changed

+1158
-1851
lines changed

src/locamation/LocamationLNEdit.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export class LocamationVMUEditElement extends LitElement {
2626
@property()
2727
nsdoc!: Nsdoc;
2828

29+
get inputs(): WizardInput[] {
30+
return Array.from(this.shadowRoot!.querySelectorAll(wizardInputSelector));
31+
}
32+
2933
save(): WizardAction[] {
30-
const inputs: WizardInput[] = Array.from(this.shadowRoot!.querySelectorAll(wizardInputSelector));
3134
const locamationPrivate = getPrivate(this.logicalNode);
3235

33-
if (!this.fieldsChanged(locamationPrivate, inputs) || !this.checkValidityInputs(inputs)) {
36+
if (!this.fieldsChanged(locamationPrivate, this.inputs) || !this.checkValidityInputs(this.inputs)) {
3437
return [];
3538
}
3639

@@ -39,19 +42,19 @@ export class LocamationVMUEditElement extends LitElement {
3942
title: get('locamation.vmu.updateAction', {lnName: lnHeader(this.logicalNode, this.nsdoc)}),
4043
};
4144

42-
complexAction.actions.push(...createEditorAction(locamationPrivate, 'IDENTIFIER', getInputFieldValue(inputs, 'identifier')));
45+
complexAction.actions.push(...createEditorAction(locamationPrivate, 'IDENTIFIER', getInputFieldValue(this.inputs, 'identifier')));
4346
if (hasPrivateElement(this.logicalNode, 'SUM')) {
44-
complexAction.actions.push(...createEditorAction(locamationPrivate, 'SUM', getInputFieldValue(inputs, 'sum')));
47+
complexAction.actions.push(...createEditorAction(locamationPrivate, 'SUM', getInputFieldValue(this.inputs, 'sum')));
4548
} else {
46-
complexAction.actions.push(...createEditorAction(locamationPrivate, 'CHANNEL', getInputFieldValue(inputs, 'channel')));
49+
complexAction.actions.push(...createEditorAction(locamationPrivate, 'CHANNEL', getInputFieldValue(this.inputs, 'channel')));
4750
}
48-
complexAction.actions.push(...createEditorAction(locamationPrivate, 'TRANSFORM-PRIMARY', getInputFieldValue(inputs, 'transformPrimary')));
49-
complexAction.actions.push(...createEditorAction(locamationPrivate, 'TRANSFORM-SECONDARY', getInputFieldValue(inputs, 'transformSecondary')));
51+
complexAction.actions.push(...createEditorAction(locamationPrivate, 'TRANSFORM-PRIMARY', getInputFieldValue(this.inputs, 'transformPrimary')));
52+
complexAction.actions.push(...createEditorAction(locamationPrivate, 'TRANSFORM-SECONDARY', getInputFieldValue(this.inputs, 'transformSecondary')));
5053

5154
return complexAction.actions.length ? [complexAction] : [];
5255
}
5356

54-
private fieldsChanged(locamationPrivate: Element, inputs: WizardInput[]): boolean {
57+
private fieldsChanged(locamationPrivate: Element | null, inputs: WizardInput[]): boolean {
5558
const oldIdentifier= getPrivateTextValue(locamationPrivate, 'IDENTIFIER');
5659
const oldChannel = getPrivateTextValue(locamationPrivate, 'CHANNEL');
5760
const oldSum = getPrivateTextValue(locamationPrivate, 'SUM');

src/locamation/foundation.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function lnHeader(ln: Element, nsDoc: Nsdoc): string {
2222

2323
const data = nsDoc.getDataDescription(ln);
2424

25-
return `${prefix != null ? `${prefix} - ` : ``} ${data.label} ${inst ? ` - ${inst}` : ``}`;
25+
return `${prefix != null ? `${prefix} - ` : ``}${data.label}${inst ? ` - ${inst}` : ``}`;
2626
}
2727

2828
export function lDeviceHeader(lDevice: Element): string {
@@ -34,9 +34,9 @@ export function lDeviceHeader(lDevice: Element): string {
3434

3535
export function iedHeader(ied: Element): string {
3636
const name = getNameAttribute(ied);
37-
const description = getDescriptionAttribute(ied);
37+
const desc = getDescriptionAttribute(ied);
3838

39-
return `${name}${description !== undefined ? ' (' + description + ')' : ''}`;
39+
return `${name}${desc ? ' (' + desc + ')' : ''}`;
4040
}
4141

4242

@@ -60,30 +60,35 @@ export function addPrefixAndNamespaceToDocument(element: Element): void {
6060
}
6161
}
6262

63-
export function getPrivate(element: Element): Element {
63+
export function getPrivate(element: Element): Element | null {
6464
return element.querySelector(`Private[type="${LOCAMATION_PRIVATE}"]`)!;
6565
}
6666

67-
export function createEditorAction(locamationPrivate: Element, fieldType: string, value: string | null): SimpleAction[] {
68-
let privateField = Array.from(locamationPrivate.querySelectorAll(`P[type="${fieldType}"]`))
69-
.filter(element => element.namespaceURI === LOCAMATION_NS)
70-
.pop();
71-
if (!privateField) {
72-
// Make sure the namespace is configured on the root element with the known prefix.
73-
addPrefixAndNamespaceToDocument(locamationPrivate);
74-
75-
privateField = locamationPrivate.ownerDocument.createElementNS(LOCAMATION_NS, "P");
76-
privateField.setAttribute("type", fieldType);
77-
privateField.textContent = value;
78-
return [{new: {parent: locamationPrivate, element: privateField}}];
79-
}
67+
export function createEditorAction(locamationPrivate: Element | null, fieldType: string, value: string | null): SimpleAction[] {
68+
if (locamationPrivate) {
69+
let privateField = Array.from(locamationPrivate.querySelectorAll(`P[type="${fieldType}"]`))
70+
.filter(element => element.namespaceURI === LOCAMATION_NS)
71+
.pop();
72+
if (!privateField) {
73+
// Make sure the namespace is configured on the root element with the known prefix.
74+
addPrefixAndNamespaceToDocument(locamationPrivate);
75+
76+
privateField = locamationPrivate.ownerDocument.createElementNS(LOCAMATION_NS, "P");
77+
privateField.setAttribute("type", fieldType);
78+
privateField.textContent = value;
79+
return [{new: {parent: locamationPrivate, element: privateField}}];
80+
}
8081

81-
const newPrivateField = cloneElement(privateField, {});
82-
newPrivateField.textContent = value;
83-
return [{old: {element: privateField}, new: {element: newPrivateField}}];
82+
if (privateField.textContent !== value) {
83+
const newPrivateField = cloneElement(privateField, {});
84+
newPrivateField.textContent = value;
85+
return [{old: {element: privateField}, new: {element: newPrivateField}}];
86+
}
87+
}
88+
return [];
8489
}
8590

86-
export function hasPrivateElement(locamationPrivate: Element, type: string): boolean {
91+
export function hasPrivateElement(locamationPrivate: Element | null, type: string): boolean {
8792
if (locamationPrivate) {
8893
return Array.from(locamationPrivate.querySelectorAll(`P[type="${type}"]`))
8994
.filter(element => element.namespaceURI === LOCAMATION_NS)
@@ -92,7 +97,7 @@ export function hasPrivateElement(locamationPrivate: Element, type: string): boo
9297
return false;
9398
}
9499

95-
export function getPrivateTextValue(locamationPrivate: Element, type: string): string | null {
100+
export function getPrivateTextValue(locamationPrivate: Element | null, type: string): string | null {
96101
if (locamationPrivate) {
97102
const privateElement =
98103
Array.from(locamationPrivate.querySelectorAll(`P[type="${type}"]`))

0 commit comments

Comments
 (0)