1
0

ui.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //@ts-check
  2. // Helpers to work with html elements
  3. // by Humans for All
  4. //
  5. /**
  6. * Set the class of the children, based on whether it is the idSelected or not.
  7. * @param {HTMLDivElement} elBase
  8. * @param {string} idSelected
  9. * @param {string} classSelected
  10. * @param {string} classUnSelected
  11. */
  12. export function el_children_config_class(elBase, idSelected, classSelected, classUnSelected="") {
  13. for(let child of elBase.children) {
  14. if (child.id == idSelected) {
  15. child.className = classSelected;
  16. } else {
  17. child.className = classUnSelected;
  18. }
  19. }
  20. }
  21. /**
  22. * Create button and set it up.
  23. * @param {string} id
  24. * @param {(this: HTMLButtonElement, ev: MouseEvent) => any} callback
  25. * @param {string | undefined} name
  26. * @param {string | undefined} innerText
  27. */
  28. export function el_create_button(id, callback, name=undefined, innerText=undefined) {
  29. if (!name) {
  30. name = id;
  31. }
  32. if (!innerText) {
  33. innerText = id;
  34. }
  35. let btn = document.createElement("button");
  36. btn.id = id;
  37. btn.name = name;
  38. btn.innerText = innerText;
  39. btn.addEventListener("click", callback);
  40. return btn;
  41. }
  42. /**
  43. * Create a para and set it up. Optionaly append it to a passed parent.
  44. * @param {string} text
  45. * @param {HTMLElement | undefined} elParent
  46. * @param {string | undefined} id
  47. */
  48. export function el_create_append_p(text, elParent=undefined, id=undefined) {
  49. let para = document.createElement("p");
  50. para.innerText = text;
  51. if (id) {
  52. para.id = id;
  53. }
  54. if (elParent) {
  55. elParent.appendChild(para);
  56. }
  57. return para;
  58. }
  59. /**
  60. * Create a button which represents bool value using specified text wrt true and false.
  61. * When ever user clicks the button, it will toggle the value and update the shown text.
  62. *
  63. * @param {string} id
  64. * @param {{true: string, false: string}} texts
  65. * @param {boolean} defaultValue
  66. * @param {function(boolean):void} cb
  67. */
  68. export function el_create_boolbutton(id, texts, defaultValue, cb) {
  69. let el = document.createElement("button");
  70. el["xbool"] = defaultValue;
  71. el["xtexts"] = structuredClone(texts);
  72. el.innerText = el["xtexts"][String(defaultValue)];
  73. if (id) {
  74. el.id = id;
  75. }
  76. el.addEventListener('click', (ev)=>{
  77. el["xbool"] = !el["xbool"];
  78. el.innerText = el["xtexts"][String(el["xbool"])];
  79. cb(el["xbool"]);
  80. })
  81. return el;
  82. }
  83. /**
  84. * Create a div wrapped button which represents bool value using specified text wrt true and false.
  85. * @param {string} id
  86. * @param {string} label
  87. * @param {{ true: string; false: string; }} texts
  88. * @param {boolean} defaultValue
  89. * @param {(arg0: boolean) => void} cb
  90. * @param {string} className
  91. */
  92. export function el_creatediv_boolbutton(id, label, texts, defaultValue, cb, className="gridx2") {
  93. let div = document.createElement("div");
  94. div.className = className;
  95. let lbl = document.createElement("label");
  96. lbl.setAttribute("for", id);
  97. lbl.innerText = label;
  98. div.appendChild(lbl);
  99. let btn = el_create_boolbutton(id, texts, defaultValue, cb);
  100. div.appendChild(btn);
  101. return { div: div, el: btn };
  102. }
  103. /**
  104. * Create a select ui element, with a set of options to select from.
  105. * * options: an object which contains name-value pairs
  106. * * defaultOption: the value whose name should be choosen, by default.
  107. * * cb : the call back returns the name string of the option selected.
  108. *
  109. * @param {string} id
  110. * @param {Object<string,*>} options
  111. * @param {*} defaultOption
  112. * @param {function(string):void} cb
  113. */
  114. export function el_create_select(id, options, defaultOption, cb) {
  115. let el = document.createElement("select");
  116. el["xselected"] = defaultOption;
  117. el["xoptions"] = structuredClone(options);
  118. for(let cur of Object.keys(options)) {
  119. let op = document.createElement("option");
  120. op.value = cur;
  121. op.innerText = cur;
  122. if (options[cur] == defaultOption) {
  123. op.selected = true;
  124. }
  125. el.appendChild(op);
  126. }
  127. if (id) {
  128. el.id = id;
  129. el.name = id;
  130. }
  131. el.addEventListener('change', (ev)=>{
  132. let target = /** @type{HTMLSelectElement} */(ev.target);
  133. console.log("DBUG:UI:Select:", id, ":", target.value);
  134. cb(target.value);
  135. })
  136. return el;
  137. }
  138. /**
  139. * Create a div wrapped select ui element, with a set of options to select from.
  140. *
  141. * @param {string} id
  142. * @param {any} label
  143. * @param {{ [x: string]: any; }} options
  144. * @param {any} defaultOption
  145. * @param {(arg0: string) => void} cb
  146. * @param {string} className
  147. */
  148. export function el_creatediv_select(id, label, options, defaultOption, cb, className="gridx2") {
  149. let div = document.createElement("div");
  150. div.className = className;
  151. let lbl = document.createElement("label");
  152. lbl.setAttribute("for", id);
  153. lbl.innerText = label;
  154. div.appendChild(lbl);
  155. let sel = el_create_select(id, options,defaultOption, cb);
  156. div.appendChild(sel);
  157. return { div: div, el: sel };
  158. }
  159. /**
  160. * Create a input ui element.
  161. *
  162. * @param {string} id
  163. * @param {string} type
  164. * @param {any} defaultValue
  165. * @param {function(any):void} cb
  166. */
  167. export function el_create_input(id, type, defaultValue, cb) {
  168. let el = document.createElement("input");
  169. el.type = type;
  170. el.value = defaultValue;
  171. if (id) {
  172. el.id = id;
  173. }
  174. el.addEventListener('change', (ev)=>{
  175. cb(el.value);
  176. })
  177. return el;
  178. }
  179. /**
  180. * Create a div wrapped input.
  181. *
  182. * @param {string} id
  183. * @param {string} label
  184. * @param {string} type
  185. * @param {any} defaultValue
  186. * @param {function(any):void} cb
  187. * @param {string} className
  188. */
  189. export function el_creatediv_input(id, label, type, defaultValue, cb, className="gridx2") {
  190. let div = document.createElement("div");
  191. div.className = className;
  192. let lbl = document.createElement("label");
  193. lbl.setAttribute("for", id);
  194. lbl.innerText = label;
  195. div.appendChild(lbl);
  196. let el = el_create_input(id, type, defaultValue, cb);
  197. div.appendChild(el);
  198. return { div: div, el: el };
  199. }