confirm_action.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. get _info() {
  18. return false;
  19. }
  20. show() {
  21. if (this.#node !== null) {
  22. return;
  23. }
  24. this.#action = true;
  25. this.#node = this.#create_window();
  26. document.querySelector("body").appendChild(this.#node);
  27. setTimeout(() => {
  28. this.#node.style.opacity = "1";
  29. }, 100);
  30. }
  31. hide() {
  32. if (this.#node === null) {
  33. return;
  34. }
  35. this.#action = false;
  36. this.#node.style.opacity = "0";
  37. setTimeout(() => {
  38. if (this.#node === null) {
  39. return;
  40. }
  41. this.#node.remove();
  42. this.#node = null;
  43. }, 500);
  44. }
  45. #create_window() {
  46. const container = document.createElement("div");
  47. container.classList.add("confirm-window");
  48. container.style.transition = "opacity 0.5s";
  49. container.style.opacity = "0";
  50. const center = document.createElement("div");
  51. center.classList.add("center");
  52. container.appendChild(center);
  53. const title = document.createElement("div");
  54. title.classList.add("title");
  55. center.appendChild(title);
  56. const title_text = document.createElement("h3");
  57. title_text.innerText = this._title;
  58. title.appendChild(title_text);
  59. const description = document.createElement("div");
  60. description.classList.add("description");
  61. center.appendChild(description);
  62. const description_text = document.createElement("p");
  63. description_text.innerText = this._description;
  64. description.appendChild(description_text);
  65. const buttons = document.createElement("div");
  66. buttons.classList.add("buttons");
  67. center.appendChild(buttons);
  68. const cancel_button = document.createElement("button");
  69. cancel_button.classList.add("cancel");
  70. cancel_button.classList.add("material-icons");
  71. cancel_button.innerText = "close";
  72. buttons.appendChild(cancel_button);
  73. cancel_button.addEventListener("click", () => {
  74. this.hide();
  75. });
  76. if (!this._info) {
  77. const confirm_button = document.createElement("button");
  78. confirm_button.classList.add("confirm");
  79. confirm_button.classList.add("material-icons");
  80. confirm_button.innerText = "send";
  81. buttons.appendChild(confirm_button);
  82. confirm_button.addEventListener("click", () => {
  83. if (this.#action === false) {
  84. return;
  85. }
  86. this._action();
  87. this.hide();
  88. });
  89. }
  90. return container;
  91. }
  92. }