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