confirm_action.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. export class confirm_action {
  2. #node;
  3. #action;
  4. constructor() {
  5. this.#node = null;
  6. this.#action = true;
  7. }
  8. get _description() {
  9. throw new TypeError("It must be overwriten.");
  10. }
  11. get _title() {
  12. return "You must confirm it.";
  13. }
  14. _action() {
  15. throw new TypeError("It must be overwriten.");
  16. }
  17. show() {
  18. if (this.#node !== null) {
  19. return;
  20. }
  21. this.#action = true;
  22. this.#node = this.#create_window();
  23. document.querySelector("body").appendChild(this.#node);
  24. setTimeout(() => {
  25. this.#node.style.opacity = "1";
  26. }, 100);
  27. }
  28. hide() {
  29. if (this.#node === null) {
  30. return;
  31. }
  32. this.#action = false;
  33. this.#node.style.opacity = "0";
  34. setTimeout(() => {
  35. if (this.#node === null) {
  36. return;
  37. }
  38. this.#node.remove();
  39. this.#node = null;
  40. }, 500);
  41. }
  42. #create_window() {
  43. const container = document.createElement("div");
  44. container.classList.add("confirm-window");
  45. container.style.transition = "opacity 0.5s";
  46. container.style.opacity = "0";
  47. const center = document.createElement("div");
  48. center.classList.add("center");
  49. container.appendChild(center);
  50. const title = document.createElement("div");
  51. title.classList.add("title");
  52. center.appendChild(title);
  53. const title_text = document.createElement("h3");
  54. title_text.innerText = this._title;
  55. title.appendChild(title_text);
  56. const description = document.createElement("div");
  57. description.classList.add("description");
  58. center.appendChild(description);
  59. const description_text = document.createElement("p");
  60. description_text.innerText = this._description;
  61. description.appendChild(description_text);
  62. const buttons = document.createElement("div");
  63. buttons.classList.add("buttons");
  64. center.appendChild(buttons);
  65. const cancel_button = document.createElement("button");
  66. cancel_button.classList.add("cancel");
  67. cancel_button.classList.add("material-icons");
  68. cancel_button.innerText = "clear";
  69. buttons.appendChild(cancel_button);
  70. const confirm_button = document.createElement("button");
  71. confirm_button.classList.add("confirm");
  72. confirm_button.classList.add("material-icons");
  73. confirm_button.innerText = "send";
  74. buttons.appendChild(confirm_button);
  75. cancel_button.addEventListener("click", () => {
  76. this.hide();
  77. });
  78. confirm_button.addEventListener("click", () => {
  79. if (this.#action === false) {
  80. return;
  81. }
  82. this._action();
  83. this.hide();
  84. });
  85. return container;
  86. }
  87. }