header-menu.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * iGeom by LInE
  3. * Free software to student private data
  4. *
  5. * http://www.matematica.br/igeom
  6. * http://www.usp.br/line
  7. *
  8. * Treat actions associated to iGeomJS menu (File | Edit)
  9. *
  10. * ./app/core/application/header-menu.js
  11. * @version 2025/03/25: Fixed reading any GEO file (treat tag "messages" in "src/index.html")
  12. * @version 2023/07/14: Fixed reading GEO file with midpoint
  13. */
  14. import { stageManager } from "./stage-manager";
  15. import { FileParser } from "../parser/file-parser";
  16. import { ParserOrchestrator } from "../parser/parser-orchestrator";
  17. class HeaderMenu {
  18. constructor () { }
  19. bootstrap () {
  20. $("body").on("click", "#save", this.save.bind(this));
  21. $("body").on("click", "#open", this.open.bind(this));
  22. $("body").on("change", "#input-file", this.onFileChanged.bind(this));
  23. }
  24. // @see app/core/models/application/actions/action-manager.js
  25. save () {
  26. const layer = stageManager.getCurrentLayer();
  27. const file = layer.actionManager.save();
  28. const name = prompt("Gravar como", layer.name.replace(/ /g, "_").toLowerCase());
  29. console.log("header-menu.js!save(): name=" + name + ", file=" + file); //D
  30. if (name == undefined)
  31. return;
  32. const a = document.createElement("a"), url = URL.createObjectURL(file);
  33. a.href = url;
  34. a.download = `${name}.geo`;
  35. document.body.appendChild(a);
  36. a.click();
  37. setTimeout(function () {
  38. document.body.removeChild(a);
  39. window.URL.revokeObjectURL(url);
  40. }, 0);
  41. }
  42. open () {
  43. $("#input-file").click();
  44. }
  45. static printMap_hm (map) {
  46. var str = "";
  47. for (let [key, value] of map) { str += "[" + key + ";" + value + "] "; }
  48. return str;
  49. }
  50. static printObj_hm (vec1) { //DEBUG
  51. var str = "";
  52. var ii, tam = vec1.length;
  53. for (ii=0; ii<tam; ii++) { str += "id = " + vec1[ii].id + "; "; }
  54. console.log("header-menu.js!printObj_hm(.): #vec_obj_dynamic_obj=" + tam + ":: " + str); //D + "\n * only ID =");
  55. //D console.log(" * vec_obj_dynamic_obj=");
  56. //D for (ii=0; ii<tam; ii++) { console.log(ii + ": obj = " + vec1[ii].obj); }
  57. }
  58. // To read any GEO file, starts here
  59. onFileChanged () {
  60. console.log("header-menu.js!onFileChanged(): starts"); //D
  61. var auxEr = "";
  62. var vec_obj_dynamic_obj = new Array(); // to store iGeom Dynamic Objects (use it to search DO by ID)
  63. const files = $("#input-file")[0].files;
  64. var str_file_content = "-1"
  65. if (files == undefined || files.length === 0) return;
  66. const obj_file_reader = new FileReader(); // JavaScript object FileReader allows web applications asynchronously read the contents of files
  67. obj_file_reader.onload = function () {
  68. console.log("header-menu.js!onload(): starts"); //D
  69. var obj_file_parser = "-1", content_map = "-1", orchestrator = "-1";
  70. try {
  71. auxEr += " [1] ";
  72. if (obj_file_reader) {
  73. str_file_content = obj_file_reader.result; // get
  74. auxEr += "[2] "; console.log("* #str_file_content=" + (str_file_content?str_file_content.length:0) + ", str_file_content = " + str_file_content);
  75. }
  76. // src/index.html: must have tag <li><span class="level-1" id="messages">iGeom version</span></li>
  77. var element = document.getElementById("messages"); // overwrite the message "File does not exist or it is empty""
  78. auxEr += "[2.1] ";
  79. if (element != undefined) { // security (case id="messages" was removed from "src/index.html"
  80. console.log("header-menu.js!onFileChanged(): [2.2] element=" + element);
  81. element.innerHTML = "Construct inside area *"; // same text in "index.html"
  82. auxEr += "[2.2] ";
  83. }
  84. else
  85. console.log("header-menu.js!onFileChanged(): [2.2] Error: src/index.html missing id=messages!");
  86. obj_file_parser = new FileParser(str_file_content); // app/core/parser/file-parser.js: export class FileParser: only set "this.content = str_file_content"
  87. auxEr += "[3] ";
  88. 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")
  89. auxEr += "[4] ";
  90. var aux = ""; // try { aux = JSON.stringify(Object.keys(content_map)) + " "; } catch (error) { aux = "error " + error + " "; }
  91. 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]
  92. 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);
  93. orchestrator = new ParserOrchestrator(content_map); // app/core/parser/parser-orchestrator.js
  94. auxEr += "[6] "; console.log("* #orchestrator=" + (orchestrator?orchestrator.length:0) + ", orchestrator = " + orchestrator);
  95. auxEr += "[7] ";
  96. orchestrator.orchestrate(vec_obj_dynamic_obj); // Array() with {id, obj}
  97. }
  98. catch (error) {
  99. console.log("header-menu.js!onFileChanged()!obj_file_reader.onload: error " + error + auxEr);
  100. // "\n * str_file_content = " + str_file_content + "\n * obj_file_parser = " + JSON.stringify(obj_file_parser) + "\n * content_map = " + JSON.stringify(content_map));
  101. }
  102. HeaderMenu.printObj_hm(vec_obj_dynamic_obj); //D only DEBUG!
  103. console.log("header-menu.js!onload(): final"); //D
  104. }; // obj_file_reader.onload = function()
  105. // console.log("header-menu.js!onFileChanged(): obj_file_reader=" + obj_file_reader.name); //D
  106. console.log("header-menu.js!onFileChanged(): files[0]=" + files[0].name); //D
  107. var endereco, ind, basePath, pathFile = files[0].name, aux;
  108. try {
  109. basePath = window.location.href;
  110. ind = basePath.lastIndexOf("/");
  111. pathFile = basePath.substring(0, ind) + "/" + files[0].name;
  112. var element = document.getElementById("messages");
  113. element.innerHTML = "File '" + pathFile + "' does not exist or it is empty"; // "Construct inside area" = same text in "index.html"
  114. } catch (error) { console.log("header-menu.js!onload(): file does not exists! Error " + error + "\n* pathFile=" + pathFile + "\n* aux=" + aux); }
  115. if (obj_file_reader)
  116. obj_file_reader.readAsText(files[0]);
  117. console.log("header-menu.js!onFileChanged(): final"); //D
  118. } // onFileChanged()
  119. }
  120. export const headerMenu = new HeaderMenu();