max80.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. const eqs = c.indexOf("=");
  20. if (eqs > 0) {
  21. map.set(c.slice(0, eqs), c.slice(eqs+1));
  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. // Insert status data from a map
  78. function initstatus(map)
  79. {
  80. for (const [key,val] of map) {
  81. var e = document.getElementById(key);
  82. if (e) {
  83. e.innerText = val;
  84. }
  85. }
  86. }
  87. // Load status data
  88. function loaddata(url)
  89. {
  90. fetchconfig(url)
  91. .then(map => { initdata(map) })
  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 was_visible = pwd.getAttribute('type') == 'text';
  143. const new_type = was_visible ? 'password' : 'text';
  144. const new_icon = was_visible ? 'show' : 'hide';
  145. pwd.setAttribute('type', new_type);
  146. me.innerHTML = new_icon;
  147. }
  148. // Hack to include an HTML files. Sadly, does not support
  149. // including files with <script> tags.
  150. function inc(url) {
  151. var me = document.currentScript;
  152. fetch(url)
  153. .then((response) => response.text())
  154. .then((text) => { me.outerHTML = text; });
  155. }