Skip to content

Commit 0be2d8d

Browse files
author
Dennis Labordus
committed
Implement check and retrieval of NSDoc File and using Event to load it.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent a78b712 commit 0be2d8d

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

src/Setting.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -184,39 +184,6 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
184184
if (changedProperties.has('settings')) use(this.settings.language);
185185
}
186186

187-
private renderFileSelect(): TemplateResult {
188-
return html `
189-
<input id="nsdoc-file" accept=".nsdoc" type="file" hidden required multiple
190-
@change=${(evt: Event) => this.uploadNsdocFile(evt)}}>
191-
<mwc-button label="${translate('settings.selectFileButton')}"
192-
id="selectFileButton"
193-
@click=${() => {
194-
const input = <HTMLInputElement | null>this.shadowRoot!.querySelector("#nsdoc-file");
195-
input?.click();
196-
}}>
197-
</mwc-button>
198-
`;
199-
}
200-
201-
private async uploadNsdocFile(evt: Event): Promise<void> {
202-
const files = Array.from(
203-
(<HTMLInputElement | null>evt.target)?.files ?? []
204-
);
205-
206-
if (files.length == 0) return;
207-
for (const file of files) {
208-
const text = await file.text();
209-
document
210-
.querySelector('open-scd')!
211-
.dispatchEvent(
212-
newLoadNsdocEvent(text, file.name)
213-
);
214-
}
215-
216-
this.nsdocFileUI.value = '';
217-
this.requestUpdate();
218-
}
219-
220187
private async onLoadNsdoc(event: LoadNsdocEvent) {
221188
const nsdocElement = this.parseToXmlObject(event.detail.nsdoc).querySelector('NSDoc');
222189

@@ -355,7 +322,6 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
355322
<wizard-divider></wizard-divider>
356323
<section>
357324
<h3>${translate('settings.loadNsdTranslations')}</h3>
358-
${this.renderFileSelect()}
359325
</section>
360326
<mwc-list id="nsdocList">
361327
${this.renderNsdocItem('IEC 61850-7-2')}

src/compas-services/CompasValidatorService.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ export function CompasSclValidatorService() {
3232
.then(parseXml);
3333
},
3434

35-
getNsdocFile(id: string): Promise<Document> {
35+
getNsdocFile(id: string): Promise<string> {
3636
const svsUrl = getCompasSettings().sclValidatorServiceUrl + '/nsdoc/v1/' + id;
3737
return fetch(svsUrl).catch(handleError)
38-
.then(handleResponse)
39-
.then(parseXml);
38+
.then(handleResponse);
4039
},
4140
}
4241
}

src/compas/CompasNsdoc.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,52 @@
1+
import {newLoadNsdocEvent} from "../Setting.js";
2+
import {dispatchEventOnOpenScd} from "./foundation.js";
3+
14
import {createLogEvent} from "../compas-services/foundation.js";
25
import {CompasSclValidatorService} from "../compas-services/CompasValidatorService.js";
36

4-
function processNsdocFile(id: string, nsdocId: string, checksum: string) {
5-
console.info(`Found NSDoc File '${nsdocId}' with ID '${id}'.`);
7+
/**
8+
* Load a single entry. Use the nsdocId to look in the Local Storage, if already loaded,
9+
* and if the checksum is the same.
10+
* If one of them isn't the case use the ID to retrieve the content of the NSDoc File and
11+
* fire the #newLoadNsdocEvent to add the content to the Local Storage.
12+
*
13+
* @param id - A unique id use to retrieve the content of NSDoc File from the SCL Validator Service.
14+
* @param nsdocId - The NSDoc ID to be used in the Local Storage.
15+
* @param filename - The name of the file, just for logging.
16+
* @param checksum - The checksum of the NSDoc File in the SCL Validator Service.
17+
*/
18+
async function processNsdocFile(id: string, nsdocId: string, filename: string, checksum: string): Promise<void> {
19+
const checksumKey = nsdocId + '.checksum';
20+
const checksumStored = localStorage.getItem(checksumKey);
21+
if (localStorage.getItem(nsdocId) === null || checksumStored === null || checksumStored !== checksum) {
22+
console.info(`Loading NSDoc File '${nsdocId}' with ID '${id}'.`);
23+
await CompasSclValidatorService().getNsdocFile(id)
24+
.then(nsdocContent => {
25+
dispatchEventOnOpenScd(newLoadNsdocEvent(nsdocContent, filename));
26+
localStorage.setItem(checksumKey, checksum);
27+
})
28+
.catch(reason => {
29+
createLogEvent(reason);
30+
});
31+
} else {
32+
console.debug(`Loading NSDoc File '${nsdocId}' skipped, already loaded.`);
33+
}
634
}
735

36+
/**
37+
* Call backend to get the list of available NSDoc Files on the SCL Validator Service.
38+
* Load each item found using the function #processNsdocFile.
39+
*/
840
export async function loadNsdocFiles(): Promise<void> {
941
await CompasSclValidatorService().listNsdocFiles()
1042
.then(response => {
1143
Array.from(response.querySelectorAll("NsdocFile") ?? [])
1244
.forEach(element => {
1345
const id = element.querySelector('Id')!.textContent ?? '';
1446
const nsdocId = element.querySelector('NsdocId')!.textContent ?? '';
47+
const filename = element.querySelector('Filename')!.textContent ?? '';
1548
const checksum = element.querySelector('Checksum')!.textContent ?? '';
16-
processNsdocFile(id, nsdocId, checksum);
49+
processNsdocFile(id, nsdocId, filename, checksum);
1750
});
1851
})
1952
.catch(reason => {

0 commit comments

Comments
 (0)