header-menu.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 a = document.createElement("a"),
  15. url = URL.createObjectURL(file);
  16. a.href = url;
  17. a.download = `${layer.name}.geo`;
  18. document.body.appendChild(a);
  19. a.click();
  20. setTimeout(function() {
  21. document.body.removeChild(a);
  22. window.URL.revokeObjectURL(url);
  23. }, 0);
  24. }
  25. open() {
  26. $("#input-file").click();
  27. }
  28. onFileChanged() {
  29. const files = $("#input-file")[0].files;
  30. if (files == undefined || files.length === 0) return;
  31. const reader = new FileReader();
  32. reader.onload = function() {
  33. const result = reader.result;
  34. const parser = new FileParser(result);
  35. const map = parser.parse();
  36. const orchestrator = new ParserOrchestrator(map);
  37. orchestrator.orchestrate();
  38. };
  39. reader.readAsText(files[0]);
  40. }
  41. }
  42. export const headerMenu = new HeaderMenu();