max80.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Get an element by id or an Element object
  2. function getelem(id) {
  3. return (id instanceof Element) ? id : document.getElementById(id);
  4. }
  5. // Read a key=value text file and return it as a Promise of a Map
  6. function fetchconfig(url) {
  7. return fetch(url, {redirect: "follow"})
  8. .then(res => {
  9. if (!res.ok) {
  10. throw new Error("HTTP error "+response.status);
  11. } else {
  12. return res.text();
  13. }
  14. })
  15. .then(text => {
  16. var map = new Map();
  17. for (const c of text.split(/[\r\n]+/)) {
  18. var m = c.match(/^\s*([\;\/]?)((?:"[^"]*"|[^"])*?)\s*=(.*)$/);
  19. if (m && m[1] == "") {
  20. var k = m[2].replaceAll(/(^"|"$|(")")/g, "$2");
  21. map.set(k, m[3]);
  22. }
  23. }
  24. return map;
  25. });
  26. }
  27. // Initialize a form from a map
  28. function initform(form,map) {
  29. var button = null;
  30. var clearers = new Map;
  31. form = getelem(form);
  32. for (var field of form.elements) {
  33. if (field instanceof HTMLInputElement &&
  34. !field.classList.contains("noload")) {
  35. var val = map.get(field.name);
  36. if (val == undefined)
  37. val = '';
  38. if (field.type == 'checkbox') {
  39. const checked = !val.match(/^(0*|[fn].*|of.*)$/i);
  40. field.checked = checked;
  41. if (checked) {
  42. const clearer_name = '-' + field.name;
  43. if (!clearers.has(clearer_name)) {
  44. clearers.set(clearer_name, 1);
  45. }
  46. }
  47. } else if (field.type == 'radio') {
  48. field.checked = (val == field.name);
  49. } else if (field.type == 'hidden') {
  50. clearers.set(field.name, 0);
  51. } else {
  52. field.value = val;
  53. }
  54. } else if (field instanceof HTMLButtonElement &&
  55. field.type == 'submit') {
  56. button = field;
  57. }
  58. }
  59. for (const [what, need] of clearers) {
  60. if (need) {
  61. var clearer = document.createElement('INPUT');
  62. clearer.type = 'hidden';
  63. clearer.name = what;
  64. form.prepend(clearer);
  65. }
  66. }
  67. if (button) {
  68. button.disabled = false; // All loaded, enable the submit button
  69. }
  70. }
  71. // Load form initialization data
  72. function loadform(form, url) {
  73. fetchconfig(url)
  74. .then(map => { initform(form, map) })
  75. .catch(() => {});
  76. }
  77. // Replace the contents of selected HTML elements based on a map with selectors
  78. function fillin(map,html)
  79. {
  80. for (const [key,val] of map) {
  81. try {
  82. for (var e of document.querySelectorAll(key))
  83. if (html) e.innerHTML = val; else e.innerText = val;
  84. } catch (error) { }
  85. }
  86. }
  87. // Load status or HTML data
  88. function load(url,html = false)
  89. {
  90. fetchconfig(url)
  91. .then(map => { fillin(map,html) })
  92. .catch(() => {});
  93. }
  94. // POST file upload with progress and response text
  95. function uploadfile(event) {
  96. event.preventDefault();
  97. var form = event.currentTarget;
  98. var elem = form.elements;
  99. var files = elem["file"].files;
  100. if (files.length != 1) {
  101. /* Show error */
  102. return;
  103. }
  104. var file = files[0];
  105. var xhr = new XMLHttpRequest();
  106. var progress = form.querySelector("progress");
  107. if (progress != undefined) {
  108. progress.value = 0;
  109. xhr.upload.addEventListener("progress", (event) => {
  110. if (event.lengthComputable) {
  111. progress.max = event.total;
  112. progress.value = event.loaded;
  113. }
  114. });
  115. }
  116. var result = form.querySelector("pre.result");
  117. if (result != undefined) {
  118. result.className = "result hide";
  119. }
  120. xhr.addEventListener("loadend", (event) => {
  121. const ok = xhr.status >= 200 && xhr.status <= 299;
  122. if (progress != undefined) {
  123. progress.value = ok ? progress.max : 0;
  124. }
  125. if (result != undefined) {
  126. result.className = "result " + (ok ? "ok" : "err");
  127. result.innerText = xhr.responseText;
  128. }
  129. });
  130. xhr.open("POST", form.action);
  131. xhr.responseType = 'text';
  132. xhr.send(file);
  133. }
  134. // Enable a button (this ensures that the necessary scripts have been
  135. // run before one can submit)
  136. function enablebutton(id,on) {
  137. getelem(id).disabled = !on;
  138. }
  139. // Flip the status of an INPUT element between text and password
  140. function showpwd(id,me) {
  141. var pwd = getelem(id);
  142. const now_visible = me.classList.toggle('hide');
  143. me.classList.toggle('show',!now_visible);
  144. const new_type = now_visible ? 'text' : 'password';
  145. pwd.setAttribute('type', new_type);
  146. }
  147. // Hack to include an HTML files. Sadly, does not support
  148. // including files with <script> tags.
  149. function inc(url) {
  150. var me = document.currentScript;
  151. fetch(url, {redirect: "follow"})
  152. .then((response) => response.text())
  153. .then((text) => { me.outerHTML = text; });
  154. }
  155. // Insert translations
  156. function translate() {
  157. load('/sys/lang', true);
  158. }