Skip to content

Commit 7949d06

Browse files
author
Dennis Labordus
committed
Refactor open compas to make it more reusable for other menuitems.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent f2d8e83 commit 7949d06

25 files changed

+339
-261
lines changed

__snapshots__/clientln wizards.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394
icon=""
395395
label="[back]"
396396
slot="secondaryAction"
397+
style=""
397398
>
398399
</mwc-button>
399400
<mwc-button

__snapshots__/compas-scl-list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
mwc-list-item=""
2222
tabindex="0"
2323
>
24-
[compas.open.noScls]
24+
[compas.noScls]
2525
</mwc-list-item>
2626
</mwc-list>
2727

__snapshots__/compas-scltype-list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
mwc-list-item=""
2222
tabindex="0"
2323
>
24-
[compas.open.noSclTypes]
24+
[compas.noSclTypes]
2525
</mwc-list-item>
2626
</mwc-list>
2727

__snapshots__/compas-scltype-radiogroup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
mwc-list-item=""
2222
tabindex="0"
2323
>
24-
[compas.open.noSclTypes]
24+
[compas.noSclTypes]
2525
</mwc-list-item>
2626
</mwc-list>
2727

__snapshots__/compas-versions-plugin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
tabindex="0"
1313
>
1414
<span style="color: var(--base1)">
15-
[compas.versions.noScls]
15+
[compas.noSclVersions]
1616
</span>
1717
</mwc-list-item>
1818
</mwc-list>
@@ -45,7 +45,7 @@
4545
tabindex="0"
4646
>
4747
<span style="color: var(--base1)">
48-
[compas.versions.noScls]
48+
[compas.noSclVersions]
4949
</span>
5050
</mwc-list-item>
5151
</mwc-list>

__snapshots__/selectExtRefWizard.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
icon=""
213213
label="[back]"
214214
slot="secondaryAction"
215+
style=""
215216
>
216217
</mwc-button>
217218
<mwc-button

src/compas-editors/CompasVersions.ts

Lines changed: 77 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ export default class CompasVersionsPlugin extends LitElement {
119119

120120
private async getVersion(version: string) {
121121
const type = getTypeFromDocName(this.docName);
122-
return await CompasSclDataService().getSclDocumentVersion(type, this.docId, version)
123-
.then(response => {
124-
const sclData = response.querySelectorAll("SclData").item(0).textContent;
125-
const sclDocument = new DOMParser().parseFromString(sclData ?? '', 'application/xml');
122+
return CompasSclDataService().getSclDocumentVersion(type, this.docId, version)
123+
.then(sclDocument => {
126124
return Promise.resolve(sclDocument.documentElement);
127125
});
128126
}
@@ -138,7 +136,7 @@ export default class CompasVersionsPlugin extends LitElement {
138136
return html `
139137
<mwc-list>
140138
<mwc-list-item>
141-
<span style="color: var(--base1)">${translate('compas.versions.noScls')}</span>
139+
<span style="color: var(--base1)">${translate('compas.noSclVersions')}</span>
142140
</mwc-list-item>
143141
</mwc-list>`
144142
}
@@ -261,82 +259,31 @@ export default class CompasVersionsPlugin extends LitElement {
261259
`;
262260
}
263261

264-
function openScl(docName: string, docId: string, version: string) {
265-
return function () {
266-
const openScd = getOpenScdElement();
267-
const type = getTypeFromDocName(docName);
268-
269-
CompasSclDataService().getSclDocumentVersion(type, docId, version)
270-
.then(response => {
271-
// Copy the SCL Result from the Response and create a new Document from it.
272-
const sclData = response.querySelectorAll("SclData").item(0).textContent;
273-
const sclDocument = new DOMParser().parseFromString(sclData??'', 'application/xml');
274-
275-
updateDocumentInOpenSCD(sclDocument);
276-
277-
openScd.dispatchEvent(
278-
newLogEvent({
279-
kind: 'info',
280-
title: get('compas.versions.restoreVersionSuccess', {version : version})
281-
}));
282-
})
283-
.catch(createLogEvent);
284-
285-
// Close the Restore Dialog.
286-
openScd.dispatchEvent(newWizardEvent());
287-
288-
return [];
289-
}
290-
}
291-
292-
function deleteScl(docName: string, docId: string) {
293-
return function () {
294-
const openScd = getOpenScdElement();
295-
const type = getTypeFromDocName(docName);
296-
297-
CompasSclDataService()
298-
.deleteSclDocument(type, docId)
299-
.then (() => {
300-
openScd.docId = '';
301-
openScd.dispatchEvent(
302-
newLogEvent({
303-
kind: 'info',
304-
title: get('compas.versions.deleteSuccess')
305-
}));
306-
})
307-
.catch(createLogEvent);
308-
309-
// Close the Restore Dialog.
310-
openScd.dispatchEvent(newWizardEvent());
311-
312-
return [];
313-
}
314-
}
315-
316-
function deleteSclVersion(docName: string, docId: string, version: string) {
317-
return function () {
318-
const openScd = getOpenScdElement();
319-
const type = getTypeFromDocName(docName);
320-
321-
CompasSclDataService()
322-
.deleteSclDocumentVersion(type, docId, version)
323-
.then(() => {
324-
openScd.dispatchEvent(
325-
newLogEvent({
326-
kind: 'info',
327-
title: get('compas.versions.deleteVersionSuccess', {version : version})
328-
}));
329-
})
330-
.catch(createLogEvent);
331-
332-
// Close the Restore Dialog.
333-
openScd.dispatchEvent(newWizardEvent());
334-
335-
return [];
262+
function confirmDeleteCompasWizard(docName: string, docId: string): Wizard {
263+
function deleteScl(docName: string, docId: string) {
264+
return function () {
265+
const openScd = getOpenScdElement();
266+
const type = getTypeFromDocName(docName);
267+
268+
CompasSclDataService()
269+
.deleteSclDocument(type, docId)
270+
.then (() => {
271+
openScd.docId = '';
272+
openScd.dispatchEvent(
273+
newLogEvent({
274+
kind: 'info',
275+
title: get('compas.versions.deleteSuccess')
276+
}));
277+
})
278+
.catch(createLogEvent);
279+
280+
// Close the Restore Dialog.
281+
openScd.dispatchEvent(newWizardEvent());
282+
283+
return [];
284+
}
336285
}
337-
}
338286

339-
function confirmDeleteCompasWizard(docName: string, docId: string): Wizard {
340287
return [
341288
{
342289
title: get('compas.versions.confirmDeleteTitle'),
@@ -353,6 +300,34 @@ function confirmDeleteCompasWizard(docName: string, docId: string): Wizard {
353300
}
354301

355302
function confirmRestoreVersionCompasWizard(docName: string, docId: string, version: string): Wizard {
303+
function openScl(docName: string, docId: string, version: string) {
304+
return function () {
305+
const openScd = getOpenScdElement();
306+
const type = getTypeFromDocName(docName);
307+
308+
CompasSclDataService().getSclDocumentVersion(type, docId, version)
309+
.then(response => {
310+
// Copy the SCL Result from the Response and create a new Document from it.
311+
const sclData = response.querySelectorAll("SclData").item(0).textContent;
312+
const sclDocument = new DOMParser().parseFromString(sclData??'', 'application/xml');
313+
314+
updateDocumentInOpenSCD(sclDocument);
315+
316+
openScd.dispatchEvent(
317+
newLogEvent({
318+
kind: 'info',
319+
title: get('compas.versions.restoreVersionSuccess', {version : version})
320+
}));
321+
})
322+
.catch(createLogEvent);
323+
324+
// Close the Restore Dialog.
325+
openScd.dispatchEvent(newWizardEvent());
326+
327+
return [];
328+
}
329+
}
330+
356331
return [
357332
{
358333
title: get('compas.versions.confirmRestoreTitle'),
@@ -369,6 +344,29 @@ function confirmRestoreVersionCompasWizard(docName: string, docId: string, versi
369344
}
370345

371346
function confirmDeleteVersionCompasWizard(docName: string, docId: string, version: string): Wizard {
347+
function deleteSclVersion(docName: string, docId: string, version: string) {
348+
return function () {
349+
const openScd = getOpenScdElement();
350+
const type = getTypeFromDocName(docName);
351+
352+
CompasSclDataService()
353+
.deleteSclDocumentVersion(type, docId, version)
354+
.then(() => {
355+
openScd.dispatchEvent(
356+
newLogEvent({
357+
kind: 'info',
358+
title: get('compas.versions.deleteVersionSuccess', {version : version})
359+
}));
360+
})
361+
.catch(createLogEvent);
362+
363+
// Close the Restore Dialog.
364+
openScd.dispatchEvent(newWizardEvent());
365+
366+
return [];
367+
}
368+
}
369+
372370
return [
373371
{
374372
title: get('compas.versions.confirmDeleteVersionTitle'),

src/compas-services/CompasSclDataService.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {CompasSettings} from "../compas/CompasSettings.js";
2-
import {handleError, handleResponse, parseXml} from "./foundation.js";
2+
import {extractSclFromResponse, handleError, handleResponse, parseXml} from "./foundation.js";
33

44
export const SDS_NAMESPACE = 'https://www.lfenergy.org/compas/SclDataService/v1';
55

@@ -49,53 +49,54 @@ export function CompasSclDataService() {
4949
},
5050

5151
listScls(type: string): Promise<Document> {
52-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/list';
52+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/list';
5353
return fetch(sclUrl)
5454
.catch(handleError)
5555
.then(handleResponse)
5656
.then(parseXml);
5757
},
5858

5959
listVersions(type: string, id: string): Promise<Document> {
60-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id + "/versions";
60+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + "/versions";
6161
return fetch(sclUrl)
6262
.catch(handleError)
6363
.then(handleResponse)
6464
.then(parseXml);
6565
},
6666

6767
getSclDocument(type: string, id: string): Promise<Document> {
68-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id;
68+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
6969
return fetch(sclUrl)
7070
.catch(handleError)
7171
.then(handleResponse)
72-
.then(parseXml);
72+
.then(parseXml)
73+
.then(extractSclFromResponse);
7374
},
7475

7576
getSclDocumentVersion(type: string, id: string, version: string): Promise<Document> {
76-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id + '/' + version;
77+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version;
7778
return fetch(sclUrl)
7879
.catch(handleError)
7980
.then(handleResponse)
8081
.then(parseXml);
8182
},
8283

8384
deleteSclDocumentVersion(type: string, id: string, version: string): Promise<string> {
84-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id + '/' + version;
85+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version;
8586
return fetch(sclUrl, {method: 'DELETE'})
8687
.catch(handleError)
8788
.then(handleResponse);
8889
},
8990

9091
deleteSclDocument(type: string, id: string): Promise<string> {
91-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id;
92+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
9293
return fetch(sclUrl, {method: 'DELETE'})
9394
.catch(handleError)
9495
.then(handleResponse);
9596
},
9697

9798
addSclDocument(type: string, body: CreateRequestBody): Promise<Document> {
98-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase();
99+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type;
99100
return fetch(sclUrl, {
100101
method: 'POST',
101102
headers: {
@@ -109,11 +110,12 @@ export function CompasSclDataService() {
109110
</sds:CreateRequest>`
110111
}).catch(handleError)
111112
.then(handleResponse)
112-
.then(parseXml);
113+
.then(parseXml)
114+
.then(extractSclFromResponse);
113115
},
114116

115117
updateSclDocument(type: string, id: string, body: UpdateRequestBody): Promise<Document> {
116-
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type?.toUpperCase() + '/' + id;
118+
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
117119
return fetch(sclUrl, {
118120
method: 'PUT',
119121
headers: {
@@ -127,7 +129,8 @@ export function CompasSclDataService() {
127129
</sds:UpdateRequest>`
128130
}).catch(handleError)
129131
.then(handleResponse)
130-
.then(parseXml);
132+
.then(parseXml)
133+
.then(extractSclFromResponse);
131134
}
132135
}
133136
}

src/compas-services/foundation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export function parseXml(textContent: string): Promise<Document> {
5858
return Promise.resolve(new DOMParser().parseFromString(textContent, 'application/xml'));
5959
}
6060

61+
export function extractSclFromResponse(response: Document): Promise<Document> {
62+
// Extract the SCL Result from the Response and create a new Document from it.
63+
const sclData = response.querySelectorAll("SclData").item(0).textContent;
64+
const sclDocument = new DOMParser().parseFromString(sclData??'', 'application/xml');
65+
return Promise.resolve(sclDocument);
66+
}
67+
6168
export function handleError(error: Error): Promise<never> {
6269
return Promise.reject({type: SERVER_ERROR, message: error.message});
6370
}

0 commit comments

Comments
 (0)