123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /*
- * iGeom by LInE
- * Free software to student private data
- *
- * http://www.matematica.br/igeom
- * http://www.usp.br/line
- *
- * Treat actions associated to iGeomJS menu (File | Edit)
- *
- * ./app/core/application/header-menu.js
- * @version 2025/03/25: Fixed reading any GEO file (treat tag "messages" in "src/index.html")
- * @version 2023/07/14: Fixed reading GEO file with midpoint
- */
- import { stageManager } from "./stage-manager";
- import { FileParser } from "../parser/file-parser";
- import { ParserOrchestrator } from "../parser/parser-orchestrator";
- class HeaderMenu {
- constructor () { }
- bootstrap () {
- $("body").on("click", "#save", this.save.bind(this));
- $("body").on("click", "#open", this.open.bind(this));
- $("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("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);
- a.href = url;
- a.download = `${name}.geo`;
- document.body.appendChild(a);
- a.click();
- setTimeout(function () {
- document.body.removeChild(a);
- window.URL.revokeObjectURL(url);
- }, 0);
- }
- open () {
- $("#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;
- for (ii=0; ii<tam; ii++) { str += "id = " + vec1[ii].id + "; "; }
- console.log("header-menu.js!printObj_hm(.): #vec_obj_dynamic_obj=" + tam + ":: " + str); //D + "\n * only 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 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 {
- 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);
- }
- // src/index.html: must have tag <li><span class="level-1" id="messages">iGeom version</span></li>
- var element = document.getElementById("messages"); // overwrite the message "File does not exist or it is empty""
- auxEr += "[2.1] ";
- if (element != undefined) { // security (case id="messages" was removed from "src/index.html"
- console.log("header-menu.js!onFileChanged(): [2.2] element=" + element);
- element.innerHTML = "Construct inside area *"; // same text in "index.html"
- auxEr += "[2.2] ";
- }
- else
- console.log("header-menu.js!onFileChanged(): [2.2] Error: src/index.html missing id=messages!");
- 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()!obj_file_reader.onload: error " + error + auxEr);
- // "\n * str_file_content = " + str_file_content + "\n * obj_file_parser = " + JSON.stringify(obj_file_parser) + "\n * content_map = " + JSON.stringify(content_map));
- }
- 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();
|