Bladeren bron

Update 'src/app/core/application/header-menu.js'

To read GEO with intersection point
New debug help methods "static printMap_hm (map)" and "static printObj_hm (vec1)"
Several changes on "onFileChanged ()"
- from "reader" to "obj_file_reader": "obj_file_reader.onload = function ()"
- from "map" to "content_map"
- from "result" to "str_file_content"
- from "parser" to "obj_file_parser"
- added "try/catch" at the end
leo 2 jaren geleden
bovenliggende
commit
74479e93b9
1 gewijzigde bestanden met toevoegingen van 73 en 13 verwijderingen
  1. 73 13
      src/app/core/application/header-menu.js

+ 73 - 13
src/app/core/application/header-menu.js

@@ -25,10 +25,13 @@ class HeaderMenu {
     $("body").on("change", "#input-file", this.onFileChanged.bind(this));
     }
 
+
+  // @see app/core/models/application/actions/action-manager.js
   save () {
     const layer = stageManager.getCurrentLayer();
     const file = layer.actionManager.save();
-    const name = prompt("Sallet como", layer.name.replace(/ /g, "_").toLowerCase());
+    const name = prompt("Gravar como", layer.name.replace(/ /g, "_").toLowerCase());
+    console.log("header-menu.js!save(): name=" + name + ", file=" + file); //D
     if (name == undefined)
       return;
     const a = document.createElement("a"), url = URL.createObjectURL(file);
@@ -46,25 +49,82 @@ class HeaderMenu {
     $("#input-file").click();
     }
 
+  static printMap_hm (map) {
+    var str = "";
+    for (let [key, value] of map) { str += "[" + key + ";" + value + "] "; }
+    return str;
+    }
+
+  static printObj_hm (vec1) { //DEBUG
+    var str = "";
+    var ii, tam = vec1.length;
+    console.log("header-menu.js: 1 #vec_obj_dynamic_obj=" + tam); //D + "\n * only ID =");
+    //D for (ii=0; ii<tam; ii++) { console.log(ii + ": id = " + vec1[ii].id); }
+    //D console.log(" * vec_obj_dynamic_obj=");
+    //D for (ii=0; ii<tam; ii++) { console.log(ii + ": obj = " + vec1[ii].obj); }
+    }
+  
+
+  // To read any GEO file, starts here
   onFileChanged () {
+    console.log("header-menu.js!onFileChanged(): starts"); //D
+    var auxEr = "";
+    var vec_obj_dynamic_obj = new Array(); // to store iGeom Dynamic Objects (use it to search DO by ID)
     const files = $("#input-file")[0].files;
+    var str_file_content = "-1"
     if (files == undefined || files.length === 0) return;
-    const reader = new FileReader();
-    reader.onload = function () {
-      var result = "-1", parser = "-1", map = "-1", orchestrator = "-1";
+    const obj_file_reader = new FileReader(); // JavaScript object FileReader allows web applications asynchronously read the contents of files
+    obj_file_reader.onload = function () {
+      console.log("header-menu.js!onload(): starts"); //D
+      var obj_file_parser = "-1", content_map = "-1", orchestrator = "-1";
       try {
-        result = reader.result;
-        parser = new FileParser(result);
-        map = parser.parse();
-        orchestrator = new ParserOrchestrator(map);
-        orchestrator.orchestrate();
+        auxEr += " 1, ";
+        if (obj_file_reader) {
+          str_file_content = obj_file_reader.result; // get
+          auxEr += "2, "; console.log("* #str_file_content=" + (str_file_content?str_file_content.length:0) + ", str_file_content = " + str_file_content);
+          }
+
+        var element = document.getElementById("messages"); // overwrite the message "File does not exist or it is empty""
+        element.innerHTML = "Construct inside area *"; // same text in "index.html"
+
+        obj_file_parser = new FileParser(str_file_content); // app/core/parser/file-parser.js: export class FileParser: only set "this.content = str_file_content"
+        auxEr += "3, ";
+
+        content_map = obj_file_parser.parse(vec_obj_dynamic_obj); // now parse each line producing a JS Map (additionally produce array "vec_obj_dynamic_obj")
+        auxEr += "4, ";
+
+        var aux = ""; // try { aux = JSON.stringify(Object.keys(content_map)) + " "; } catch (error) { aux = "error " + error + " "; }
+        auxEr += "5, "; // content_map = [id,1=type,0] [id,2=type,0] [id,3=type,13] [id,4=type,0] [id,5=type,13] [undefined=undefined] 
+        console.log("* #content_map=" + (content_map?content_map.length:0) + "=" + content_map.size + ", content_map = " + HeaderMenu.printMap_hm(content_map)); //D JSON.stringify(content_map) + "\n" + aux);
+        orchestrator = new ParserOrchestrator(content_map); // app/core/parser/parser-orchestrator.js
+        auxEr += "6, "; console.log("* #orchestrator=" + (orchestrator?orchestrator.length:0) + ", orchestrator = " + orchestrator);
+        auxEr += "7, ";
+        orchestrator.orchestrate(vec_obj_dynamic_obj); // Array() with {id, obj}
         }
       catch (error) {
-        console.log("header-menu.js!onFileChanged()!reader.onload: error " + error + "\n * result = " + result + "\n * parser = " + JSON.stringify(parser) + "\n * map = " + JSON.stringify(map));
+        console.log("header-menu.js!onFileChanged()!obj_file_reader.onload: error " + error + // "\n * str_file_content = " + str_file_content + "\n * obj_file_parser = " + JSON.stringify(obj_file_parser) + "\n * content_map = " + JSON.stringify(content_map));
+          auxEr);
         }
-      };
-    reader.readAsText(files[0]);
-    }
+      HeaderMenu.printObj_hm(vec_obj_dynamic_obj); //D only DEBUG!
+      console.log("header-menu.js!onload(): final"); //D
+      }; // obj_file_reader.onload = function()
+
+    // console.log("header-menu.js!onFileChanged(): obj_file_reader=" + obj_file_reader.name); //D
+    console.log("header-menu.js!onFileChanged(): files[0]=" + files[0].name); //D
+
+    var endereco, ind, basePath, pathFile = files[0].name, aux;
+    try {
+      basePath = window.location.href;
+      ind = basePath.lastIndexOf("/");
+      pathFile = basePath.substring(0, ind) + "/" + files[0].name;
+      var element = document.getElementById("messages");
+      element.innerHTML = "File '" + pathFile + "' does not exist or it is empty"; // "Construct inside area" = same text in "index.html"
+    } catch (error) { console.log("header-menu.js!onload(): file does not exists! Error "  + error + "\n* pathFile=" + pathFile + "\n* aux=" + aux); }
+
+    if (obj_file_reader)
+      obj_file_reader.readAsText(files[0]);
+    console.log("header-menu.js!onFileChanged(): final"); //D
+    } // onFileChanged()
   }
 
 export const headerMenu = new HeaderMenu();