AddressSpace.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { UANodeSet } from "./UANodeSet";
  2. import { UABaseNode } from "./UABaseNode";
  3. import { NamespaceTable } from "./NameSpaceTable";
  4. import JSZip from "jszip";
  5. export class AddressSpace{
  6. nodeMap: Map<string, UABaseNode>;
  7. nst: NamespaceTable;
  8. nodesets: UANodeSet[];
  9. constructor(nodesets: UANodeSet[]) {
  10. this.nst=new NamespaceTable();
  11. this.nodeMap=new Map();
  12. this.nodesets=[];
  13. for(const nodeset of nodesets) {
  14. this.addNodeset(nodeset);
  15. }
  16. }
  17. public findNode(nodeId: string):UABaseNode|undefined {
  18. return this.nodeMap.get(nodeId)
  19. }
  20. public addNodeset(nodeset: UANodeSet) {
  21. nodeset.reIndex(this.nst);
  22. for(const node of nodeset.nodes) {
  23. this.nodeMap.set(node.nodeId.toString(), node);
  24. }
  25. nodeset.resolveReferences(this.nodeMap);
  26. this.nodesets.push(nodeset);
  27. }
  28. static async load(files: string[]) {
  29. const promises:Promise<UANodeSet>[] = []
  30. for(const file of files) {
  31. promises.push(UANodeSet.load(file));
  32. }
  33. const sets:UANodeSet[]= [];
  34. for(const p of promises) {
  35. sets.push(await p)
  36. }
  37. return new AddressSpace(sets);
  38. }
  39. public exportProject() {
  40. const zip = new JSZip();
  41. const fileNames:string[]=[];
  42. for(const ns of this.nodesets) {
  43. fileNames.push(ns.fileName);
  44. zip.file(ns.fileName, ns.toXML().toString());
  45. }
  46. zip.file("project.json", JSON.stringify(fileNames));
  47. return zip.generateAsync({type:'blob'});
  48. }
  49. }