|
| 1 | +import {newLoadNsdocEvent} from "../Setting.js"; |
| 2 | +import {dispatchEventOnOpenScd} from "./foundation.js"; |
| 3 | + |
1 | 4 | import {createLogEvent} from "../compas-services/foundation.js";
|
2 | 5 | import {CompasSclValidatorService} from "../compas-services/CompasValidatorService.js";
|
3 | 6 |
|
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 | + } |
6 | 34 | }
|
7 | 35 |
|
| 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 | + */ |
8 | 40 | export async function loadNsdocFiles(): Promise<void> {
|
9 | 41 | await CompasSclValidatorService().listNsdocFiles()
|
10 | 42 | .then(response => {
|
11 | 43 | Array.from(response.querySelectorAll("NsdocFile") ?? [])
|
12 | 44 | .forEach(element => {
|
13 | 45 | const id = element.querySelector('Id')!.textContent ?? '';
|
14 | 46 | const nsdocId = element.querySelector('NsdocId')!.textContent ?? '';
|
| 47 | + const filename = element.querySelector('Filename')!.textContent ?? ''; |
15 | 48 | const checksum = element.querySelector('Checksum')!.textContent ?? '';
|
16 |
| - processNsdocFile(id, nsdocId, checksum); |
| 49 | + processNsdocFile(id, nsdocId, filename, checksum); |
17 | 50 | });
|
18 | 51 | })
|
19 | 52 | .catch(reason => {
|
|
0 commit comments