Skip to content

Commit 5690ac7

Browse files
author
Dennis Labordus
committed
Small refactoring and added unit test for new parts.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 1c168c3 commit 5690ac7

25 files changed

+648
-146
lines changed

src/compas/CompasExistsIn.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { property, PropertyValues, state } from 'lit-element';
22

33
import { LitElementConstructor, Mixin } from '../foundation.js';
44

5-
import { CompasSclDataService } from '../compas-services/CompasSclDataService.js';
65
import { getTypeFromDocName } from './foundation.js';
6+
import { CompasSclDataService } from '../compas-services/CompasSclDataService.js';
7+
import { NOT_FOUND_ERROR } from '../compas-services/foundation.js';
78

89
export type CompasExistsInElement = Mixin<typeof CompasExistsIn>;
910

@@ -32,7 +33,7 @@ export function CompasExistsIn<TBase extends LitElementConstructor>(
3233
}
3334
}
3435

35-
callService(docType: string, docId: string) {
36+
callService(docType: string, docId: string): Promise<Document> {
3637
// Use the versions call to check if any exist, because then the document also exists
3738
// And it saves bandwidth not to retrieve the whole document.
3839
return CompasSclDataService().listVersions(docType, docId);
@@ -43,8 +44,10 @@ export function CompasExistsIn<TBase extends LitElementConstructor>(
4344
const docType = getTypeFromDocName(this.docName);
4445
this.callService(docType, this.docId)
4546
.then(() => (this.existInCompas = true))
46-
.catch(() => {
47-
this.existInCompas = false;
47+
.catch(reason => {
48+
if (reason.type === NOT_FOUND_ERROR) {
49+
this.existInCompas = false;
50+
}
4851
});
4952
} else {
5053
this.existInCompas = false;

src/compas/CompasImportFromApi.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { customElement, html, LitElement, TemplateResult } from 'lit-element';
22

3-
import '@material/mwc-button';
3+
import '@material/mwc-list/mwc-list-item';
44

55
import {
66
newOpenDocEvent,
77
newPendingStateEvent,
88
newWizardEvent,
99
} from '../foundation.js';
1010

11+
import '../filtered-list.js';
12+
1113
import {
1214
createLogEvent,
1315
handleError,
1416
handleResponse,
1517
parseXml,
1618
} from '../compas-services/foundation.js';
1719

18-
import '../WizardDivider.js';
19-
import './CompasSclTypeList.js';
20-
import './CompasScl.js';
2120
import { CompasCimMappingService } from '../compas-services/CompasCimMappingService.js';
2221

2322
@customElement('compas-import-from-api')

src/compas/CompasLabelsField.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
export class CompasLabelsFieldElement extends LitElement {
3232
@property()
3333
set privateElement(privateElement: Element) {
34-
this.originalLabelsElement = getLabels(privateElement) ?? undefined;
34+
this.originalLabelsElement = getLabels(privateElement);
3535
if (this.originalLabelsElement) {
3636
this.newLabelsElement = <Element>(
3737
this.originalLabelsElement.cloneNode(true)
@@ -42,7 +42,7 @@ export class CompasLabelsFieldElement extends LitElement {
4242
}
4343

4444
@property()
45-
originalLabelsElement: Element | undefined;
45+
originalLabelsElement: Element | null = null;
4646

4747
@property()
4848
newLabelsElement!: Element;

src/compas/CompasOpen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { newPendingStateEvent } from '../foundation.js';
1414

1515
import { CompasSclDataService } from '../compas-services/CompasSclDataService.js';
1616
import { createLogEvent } from '../compas-services/foundation.js';
17-
import { SclSelectedEvent } from './CompasScl.js';
17+
import { SclSelectedEvent } from './CompasSclList.js';
1818
import { TypeSelectedEvent } from './CompasSclTypeList.js';
1919

2020
import '../WizardDivider.js';
2121
import './CompasSclTypeList.js';
22-
import './CompasScl.js';
22+
import './CompasSclList.js';
2323

2424
/* Event that will be used when a SCL Document is retrieved. */
2525
export interface DocRetrievedDetail {

src/compas/CompasScl.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/compas/CompasSclList.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
customElement,
3+
html,
4+
LitElement,
5+
property,
6+
TemplateResult,
7+
} from 'lit-element';
8+
import { translate } from 'lit-translate';
9+
10+
import '@material/mwc-list';
11+
import '@material/mwc-list/mwc-list-item';
12+
13+
import {
14+
CompasSclDataService,
15+
SDS_NAMESPACE,
16+
} from '../compas-services/CompasSclDataService.js';
17+
18+
/* Event that will be used when a SCL is selected from a list of SCL Documents. */
19+
export interface SclSelectedDetail {
20+
docId: string;
21+
}
22+
export type SclSelectedEvent = CustomEvent<SclSelectedDetail>;
23+
export function newSclSelectedEvent(docId: string): SclSelectedEvent {
24+
return new CustomEvent<SclSelectedDetail>('sclSelected', {
25+
bubbles: true,
26+
composed: true,
27+
detail: { docId },
28+
});
29+
}
30+
31+
@customElement('compas-scl-list')
32+
export class CompasSclList extends LitElement {
33+
@property({ type: String })
34+
type = '';
35+
36+
@property()
37+
scls!: Element[];
38+
39+
firstUpdated(): void {
40+
this.fetchData();
41+
}
42+
43+
fetchData(): void {
44+
CompasSclDataService()
45+
.listScls(this.type)
46+
.then(xmlResponse => {
47+
this.scls = Array.from(xmlResponse.querySelectorAll('Item') ?? []);
48+
});
49+
}
50+
51+
render(): TemplateResult {
52+
if (!this.scls) {
53+
return html` <compas-loading></compas-loading> `;
54+
}
55+
if (this.scls?.length <= 0) {
56+
return html` <mwc-list>
57+
<mwc-list-item><i>${translate('compas.noScls')}</i></mwc-list-item>
58+
</mwc-list>`;
59+
}
60+
return html` <mwc-list>
61+
${this.scls.map(item => {
62+
const id =
63+
item.getElementsByTagNameNS(SDS_NAMESPACE, 'Id').item(0)!
64+
.textContent ?? '';
65+
let name =
66+
item.getElementsByTagNameNS(SDS_NAMESPACE, 'Name').item(0)!
67+
.textContent ?? '';
68+
if (name === '') {
69+
name = id;
70+
}
71+
const version =
72+
item.getElementsByTagNameNS(SDS_NAMESPACE, 'Version').item(0)!
73+
.textContent ?? '';
74+
return html`<mwc-list-item
75+
tabindex="0"
76+
@click=${() => this.dispatchEvent(newSclSelectedEvent(id))}
77+
>
78+
${name} (${version})
79+
</mwc-list-item>`;
80+
})}
81+
</mwc-list>`;
82+
}
83+
}

src/compas/private.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ export function createCompasSclName(parent: Element, value: string): Element {
3737
return newSclNameElement;
3838
}
3939

40-
export function getLabels(parent: Element): Element | undefined {
41-
return Array.from(parent.querySelectorAll(`:scope > Labels`)).find(
42-
element => element.namespaceURI === COMPAS_NAMESPACE
40+
export function getLabels(privateElement: Element): Element | null {
41+
return (
42+
Array.from(privateElement.querySelectorAll(`:scope > Labels`)).find(
43+
element => element.namespaceURI === COMPAS_NAMESPACE
44+
) ?? null
4345
);
4446
}
4547

src/menu/CompasOpen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class CompasOpenMenuPlugin extends LitElement {
2727
detail: { docId: undefined },
2828
})
2929
);
30+
plugin.dispatchEvent(newWizardEvent());
3031
} else {
3132
updateDocumentInOpenSCD(plugin, event.detail.doc, event.detail.docName);
3233
plugin.dispatchEvent(newWizardEvent());
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<SCL xmlns:compas="https://www.lfenergy.org/compas/v1" xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
2+
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
33
<Private type="compas_scl">
4-
<compas:SclFileType>CID</compas:SclFileType>
54
</Private>
65
<Header id="380b5e70-4753-4b59-b5b4-d51ceb26a30c" version="3.0.0" revision="Revision" toolID="toolID" nameStructure="IEDName"/>
76
</SCL>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<SCL xmlns:compas="https://www.lfenergy.org/compas/v1" xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
2+
<SCL xmlns:compas="https://www.lfenergy.org/compas/extension/v1" xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
33
<Private type="compas_scl">
44
<compas:SclName>existing</compas:SclName>
55
<compas:SclFileType>CID</compas:SclFileType>
6+
<compas:Labels>
7+
<compas:Label>Label 1</compas:Label>
8+
</compas:Labels>
69
</Private>
710
<Header id="380b5e70-4753-4b59-b5b4-d51ceb26a30c" version="3.0.0" revision="Revision" toolID="toolID" nameStructure="IEDName"/>
811
</SCL>

0 commit comments

Comments
 (0)