formscreen.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { fullscreen } from "./fullscreen.js";
  2. export class formscreen extends fullscreen {
  3. #form;
  4. #result;
  5. constructor() {
  6. super();
  7. this.#form = null;
  8. this.#result = null;
  9. }
  10. get _name() {
  11. throw new TypeError("This is virtual getter!");
  12. }
  13. _process() {
  14. this._error = "This is abstract, and must be overwriten.";
  15. }
  16. _build_form() {
  17. throw new TypeError("This is virtual method!");
  18. }
  19. _get_input(name) {
  20. return this.get_query("input[name=\"" + name + "\"]");
  21. }
  22. get _has_submit() {
  23. return true;
  24. }
  25. _build_node() {
  26. const center = document.createElement("div");
  27. center.classList.add("center");
  28. const title = document.createElement("div");
  29. title.classList.add("title");
  30. center.appendChild(title);
  31. const title_content = document.createElement("h3");
  32. title_content.innerText = this._name;
  33. title.appendChild(title_content);
  34. const form = document.createElement("form");
  35. center.appendChild(form);
  36. form.addEventListener("click", () => {
  37. this._clear_results();
  38. });
  39. this.#form = document.createElement("div");
  40. this.#form.classList.add("content");
  41. form.appendChild(this.#form);
  42. this.#result = document.createElement("div");
  43. this.#result.classList.add("result");
  44. form.appendChild(this.#result);
  45. const bottom = document.createElement("div");
  46. bottom.classList.add("bottom");
  47. form.appendChild(bottom);
  48. const close_button = document.createElement("button");
  49. close_button.classList.add("close");
  50. close_button.classList.add("material-icons");
  51. close_button.innerText = "close";
  52. close_button.type = "button";
  53. bottom.appendChild(close_button);
  54. if (this._has_submit) {
  55. const send_button = document.createElement("button");
  56. send_button.classList.add("send");
  57. send_button.classList.add("material-icons");
  58. send_button.innerText = "send";
  59. send_button.type = "submit";
  60. bottom.appendChild(send_button);
  61. }
  62. close_button.addEventListener("click", () => {
  63. this.hide();
  64. });
  65. form.addEventListener("submit", (target) => {
  66. target.preventDefault();
  67. this._process();
  68. });
  69. this._refresh();
  70. return center;
  71. }
  72. _refresh() {
  73. while (this.#form.lastChild) {
  74. this.#form.lastChild.remove();
  75. }
  76. this._build_form();
  77. }
  78. _create_input(name, label_text, placeholder, worker = null) {
  79. const container = document.createElement("div");
  80. container.classList.add("input-container");
  81. container.classList.add("input-" + name);
  82. const label = document.createElement("label");
  83. label.htmlFor = name;
  84. label.innerText = label_text;
  85. container.appendChild(label);
  86. const input = document.createElement("input");
  87. input.type = "text";
  88. input.placeholder = placeholder;
  89. input.name = name;
  90. input.id = name;
  91. container.appendChild(input);
  92. if (worker !== null) {
  93. worker(input);
  94. }
  95. if (!this.#form) {
  96. throw new Error("Screen is not visible yet!");
  97. }
  98. this._append_child(container);
  99. return () => {
  100. return input.value;
  101. };
  102. }
  103. _append_child(target) {
  104. this.#form.appendChild(target);
  105. }
  106. _clear_results() {
  107. if (!this.#result) {
  108. return;
  109. }
  110. while (this.#result.lastChild) {
  111. this.#result.lastChild.remove();
  112. }
  113. }
  114. set _info(target) {
  115. this._clear_results();
  116. const info = document.createElement("p");
  117. info.classList.add("info");
  118. info.innerText = target;
  119. if (this.#result) {
  120. this.#result.appendChild(info);
  121. }
  122. }
  123. set _error(target) {
  124. this._clear_results();
  125. const info = document.createElement("p");
  126. info.classList.add("error");
  127. info.innerText = target;
  128. if (this.#result) {
  129. this.#result.appendChild(info);
  130. }
  131. }
  132. set _success(target) {
  133. this._clear_results();
  134. const info = document.createElement("p");
  135. info.classList.add("success");
  136. info.innerText = target;
  137. if (this.#result) {
  138. this.#result.appendChild(info);
  139. }
  140. }
  141. }