|
| 1 | +import { handleError, handleResponse } from './foundation.js'; |
| 2 | + |
| 3 | +interface NsDocFile { |
| 4 | + filename: string; |
| 5 | + id: string; |
| 6 | + name: string; |
| 7 | +} |
| 8 | + |
| 9 | +// Temporary solution to map to the old logic |
| 10 | +const nsDocfiles: NsDocFile[] = [ |
| 11 | + { |
| 12 | + filename: 'IEC_61850-7-2_2007B3-en.nsdoc', |
| 13 | + name: 'IEC 61850-7-2', |
| 14 | + id: '87e5bed8-2f27-4006-8673-f9d00b0a5426', |
| 15 | + }, |
| 16 | + { |
| 17 | + filename: 'IEC_61850-7-3_2007B3-en.nsdoc', |
| 18 | + name: 'IEC_61850-7-3', |
| 19 | + id: '315b02ac-c4aa-4495-9b4f-f7175a75c315', |
| 20 | + }, |
| 21 | + { |
| 22 | + filename: 'IEC_61850-7-4_2007B3-en.nsdoc', |
| 23 | + name: 'IEC 61850-7-4', |
| 24 | + id: 'da1b2ca0-1263-4b10-9b16-2f148ae3a1f1', |
| 25 | + }, |
| 26 | + { |
| 27 | + filename: 'IEC_61850-8-1_2003A2-en.nsdoc', |
| 28 | + name: 'IEC 61850-8-1', |
| 29 | + id: '0c052ea7-a010-4ca8-b2c7-caa665cabc46', |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +const createElement = ( |
| 34 | + name: string, |
| 35 | + textContent: string, |
| 36 | + document: XMLDocument |
| 37 | +): Element => { |
| 38 | + const element: Element = document.createElement(name); |
| 39 | + element.textContent = textContent; |
| 40 | + |
| 41 | + return element; |
| 42 | +}; |
| 43 | + |
| 44 | +/** TODO: Make this return JSON */ |
| 45 | +export function CompasNSDocFileService() { |
| 46 | + return { |
| 47 | + listNsdocFiles(): Promise<Document> { |
| 48 | + const document: XMLDocument = new DOMParser().parseFromString( |
| 49 | + '<NsdocListResponse></NsdocListResponse>', |
| 50 | + 'text/xml' |
| 51 | + ); |
| 52 | + |
| 53 | + nsDocfiles.forEach(nsDocFile => { |
| 54 | + const nsDocFileElement: Element = document.createElement('NsdocFile'); |
| 55 | + |
| 56 | + nsDocFileElement.appendChild( |
| 57 | + createElement('Id', nsDocFile.id, document) |
| 58 | + ); |
| 59 | + nsDocFileElement.appendChild( |
| 60 | + createElement('NsdocId', nsDocFile.name, document) |
| 61 | + ); |
| 62 | + nsDocFileElement.appendChild( |
| 63 | + createElement('Checksum', nsDocFile.id, document) |
| 64 | + ); |
| 65 | + nsDocFileElement.appendChild( |
| 66 | + createElement('Filename', nsDocFile.filename, document) |
| 67 | + ); |
| 68 | + |
| 69 | + document |
| 70 | + .querySelector('NsdocListResponse')! |
| 71 | + .appendChild(nsDocFileElement); |
| 72 | + }); |
| 73 | + |
| 74 | + return Promise.resolve(document); |
| 75 | + }, |
| 76 | + |
| 77 | + getNsdocFile(id: string): Promise<Document> { |
| 78 | + const nsDocFile: NsDocFile = nsDocfiles.find(f => f.id === id)!; |
| 79 | + |
| 80 | + if (!nsDocFile) { |
| 81 | + return Promise.reject(`Unable to find nsDoc file with id ${id}`); |
| 82 | + } |
| 83 | + return fetch(`./public/nsdoc/${nsDocFile.filename}`) |
| 84 | + .catch(handleError) |
| 85 | + .then(handleResponse) |
| 86 | + .then(res => { |
| 87 | + const document: XMLDocument = new DOMParser().parseFromString( |
| 88 | + '<NsdocResponse></NsdocResponse>', |
| 89 | + 'text/xml' |
| 90 | + ); |
| 91 | + |
| 92 | + document |
| 93 | + .querySelector('NsdocResponse')! |
| 94 | + .appendChild(createElement('NsdocFile', res, document)); |
| 95 | + |
| 96 | + return document; |
| 97 | + }); |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
0 commit comments