header-menu.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { stageManager } from "./stage-manager";
  2. import { FileParser } from "../parser/file-parser";
  3. import { ParserOrchestrator } from "../parser/parser-orchestrator";
  4. class HeaderMenu {
  5. constructor() { }
  6. bootstrap() {
  7. $("body").on("click", "#save", this.save.bind(this));
  8. $("body").on("click", "#open", this.open.bind(this));
  9. $("body").on("change", "#input-file", this.onFileChanged.bind(this));
  10. }
  11. save() {
  12. const layer = stageManager.getCurrentLayer();
  13. const file = layer.actionManager.save();
  14. const name = prompt("Sallet como", layer.name.replace(/ /g, "_").toLowerCase());
  15. if (name == undefined) return;
  16. const a = document.createElement("a"),
  17. url = URL.createObjectURL(file);
  18. a.href = url;
  19. a.download = `${name}.geo`;
  20. document.body.appendChild(a);
  21. a.click();
  22. setTimeout(function () {
  23. document.body.removeChild(a);
  24. window.URL.revokeObjectURL(url);
  25. }, 0);
  26. }
  27. open() {
  28. $("#input-file").click();
  29. }
  30. onFileChanged() {
  31. const files = $("#input-file")[0].files;
  32. if (files == undefined || files.length === 0) return;
  33. const reader = new FileReader();
  34. reader.onload = function () {
  35. const result = reader.result;
  36. const parser = new FileParser(result);
  37. const map = parser.parse();
  38. const orchestrator = new ParserOrchestrator(map);
  39. orchestrator.orchestrate();
  40. };
  41. reader.readAsText(files[0]);
  42. }
  43. }
  44. export const headerMenu = new HeaderMenu();