浏览代码

import files on models filedrop

Martin Kunz 1 年之前
父节点
当前提交
152297a1eb
共有 3 个文件被更改,包括 32 次插入32 次删除
  1. 11 3
      src/components/TheModels.vue
  2. 15 26
      src/ua/AddressSpace.ts
  3. 6 3
      src/ua/UANodeSet.ts

+ 11 - 3
src/components/TheModels.vue

@@ -49,13 +49,21 @@ async function newProject() {
   store.setAddressSpace(as);
   newDialogOpen.value=false;
   newDialogDisabled.value=false;
+}
 
+async function handleDrop(e:DragEvent) {
+  if(!e.dataTransfer)
+    return;
+  for(let file of e.dataTransfer.files) {
+      let xmlString= await file.text();
+      store.addressSpace?.addNodeset(await UANodeSet.parse(xmlString, file.name));
+  }
 }
 </script>
 
 <template>
-  <div v-if="store.addressSpace">
-    <div class="card">
+  <div v-if="store.addressSpace" >
+    <div class="card" @drop.prevent="handleDrop($event)" dropzone="copy" @dragover="$event.preventDefault();">
       <div class="card-body" v-if="store.addressSpace">
         <h5 class="card-title">Models</h5>
         <p class="card-text">
@@ -72,7 +80,7 @@ async function newProject() {
     <button class="btn btn-light" @click="newDialogOpen = true">New project</button>
   </div>
 
-  <v-dialog :open="newDialogOpen" @cancel="newDialogOpen=false" >
+  <v-dialog :open="newDialogOpen" @cancel="newDialogOpen=false">
     <p>New project</p>
     <div class="form-check">
       <input class="form-check-input" type="radio" name="projectType" id="empty" value="empty" v-model="projectType">

+ 15 - 26
src/ua/AddressSpace.ts

@@ -6,42 +6,31 @@ import JSZip from "jszip";
 export class AddressSpace{
 
     nodeMap:  Map<string, UABaseNode>;
+    nst: NamespaceTable;
+    nodesets: UANodeSet[];
 
-    constructor(public nodesets: UANodeSet[]) {
-        this.reIndex(nodesets);
-        this.nodeMap=this.buildNodeMap(nodesets);
-        this.resolveChildren(nodesets, this.nodeMap);
+    constructor(nodesets: UANodeSet[]) {
+        this.nst=new NamespaceTable();
+        this.nodeMap=new Map();
+        this.nodesets=[];
+        for(const nodeset of nodesets) {
+            this.addNodeset(nodeset);
+        }
     }
 
     public findNode(nodeId: string):UABaseNode|undefined {
         return this.nodeMap.get(nodeId)
     }
 
-
-    private reIndex(nodesets:UANodeSet[]) {
-        const nst=new NamespaceTable();
-        for(const nodeset of nodesets) {
-            nodeset.reIndex(nst);
-        }
-    }
-    
-    private buildNodeMap(nodesets:UANodeSet[]) {
-        const nm=new Map<string, UABaseNode>();
-        for(const nodeset of nodesets) {
-            for(const node of nodeset.nodes) {
-                nm.set(node.nodeId.toString(), node);
-            }
-        }
-        return nm;
-    }
-
-    private resolveChildren(nodesets:UANodeSet[], nm:Map<string, UABaseNode>) {
-        for(const nodeset of nodesets) {
-            nodeset.resolveChildren(nm);
+    public addNodeset(nodeset: UANodeSet) {
+        nodeset.reIndex(this.nst);
+        for(const node of nodeset.nodes) {
+            this.nodeMap.set(node.nodeId.toString(), node);
         }
+        nodeset.resolveChildren(this.nodeMap);
+        this.nodesets.push(nodeset);
     }
 
-
     static async load(files: string[]) {
         const promises:Promise<UANodeSet>[] = []
         for(const file of files) {

+ 6 - 3
src/ua/UANodeSet.ts

@@ -42,7 +42,12 @@ export class UANodeSet implements IToXML{
         return xmlUANodeSet;
     }
 
-    static async load(url: string) {
+    static async load(url: string): Promise<UANodeSet> {
+        const xml= await (await fetch(url)).text();
+        const fileName= url.split('/').pop()||url
+        return this.parse(xml, fileName);
+    }
+    static async parse(xml: string, fileName: string): Promise<UANodeSet> {
         const parseOptions:Partial<X2jOptions>={
             ignoreAttributes: false,
             numberParseOptions: {
@@ -66,7 +71,6 @@ export class UANodeSet implements IToXML{
             }
         }
         const parser = new XMLParser(parseOptions);
-        const xml= await (await fetch(url)).text();
         const xmlObj = parser.parse(xml);
         const models: Model[]=[];
         const xmlModels=xmlObj['UANodeSet']['Models'];
@@ -89,7 +93,6 @@ export class UANodeSet implements IToXML{
                 nst.addUri(nsUri)
             }
         }
-        const fileName= url.split('/').pop()||url
         return new UANodeSet(fileName, models, nodes, nst);
     }
 }