max80.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const PassiveListener = { passive: true };
  2. // Get an element by id or an Element object
  3. function getelem(id) {
  4. return (id instanceof Element) ? id : document.getElementById(id);
  5. }
  6. // Find a child with a specific tag
  7. function chi(me,tag) { return me.getElementsByTagName(tag)[0]; }
  8. // Find a sibling element with a specific tag
  9. function sib(me,tag) { return chi(me.parentElement, tag); }
  10. // Read a key=value text file and return it as a Promise of a Map
  11. function fetchconfig(url) {
  12. return fetch(url, {redirect: "follow"})
  13. .then(res => {
  14. if (!res.ok) {
  15. throw new Error("HTTP error "+response.status);
  16. } else {
  17. return res.text();
  18. }
  19. })
  20. .then(text => {
  21. var map = new Map();
  22. for (const c of text.split(/[\r\n]+/)) {
  23. var m = c.match(/^\s*([\;\/]?)((?:"[^"]*"|[^"])*?)\s*=(.*)$/);
  24. if (m && m[1] == "") {
  25. var k = m[2].replaceAll(/(^"|"$|(")")/g, "$2");
  26. map.set(k, m[3]);
  27. }
  28. }
  29. return map;
  30. });
  31. }
  32. // Parse a string for a valid boolean truth value
  33. function cfgbool(str) {
  34. return str && !str.match(/^(0*|[fnd].*|of.*)$/i);
  35. }
  36. // Initialize a form from a map. Checkboxes take a cfgbool string;
  37. // if in the HTML their value is set to a false cfgbool string then
  38. // the checkbox is inverted logic.
  39. function initform(form,map) {
  40. var button = null;
  41. var clearers = new Set;
  42. form = getelem(form);
  43. for (var e of form.elements) {
  44. if (e.classList.contains('noinit')) {
  45. continue;
  46. } else if (e instanceof HTMLInputElement ||
  47. e instanceof HTMLSelectElement) {
  48. const val = map.get(e.name);
  49. if (val == null)
  50. continue;
  51. if (e.type == 'checkbox') {
  52. e.checked = cfgbool(val) == cfgbool(e.value);
  53. } else if (e.type == 'radio') {
  54. e.checked = (val == e.name);
  55. } else {
  56. e.value = val;
  57. }
  58. } else if (e instanceof HTMLButtonElement &&
  59. e.type == 'submit') {
  60. button = e;
  61. }
  62. }
  63. if (button) {
  64. button.disabled = false; // All loaded, enable the submit button
  65. }
  66. }
  67. // Load form initialization data
  68. function loadform(form, url) {
  69. fetchconfig(url)
  70. .then(map => { initform(form, map) })
  71. .catch(() => {});
  72. }
  73. // Replace the contents of selected HTML elements based on a map with
  74. // selectors.
  75. function fillin(map,html = false,top = document)
  76. {
  77. for (const [key,val] of map) {
  78. try {
  79. const m = key.match(/(.*?)(?:\;([\w.-]*)(\??))?$/);
  80. for (var e of top.querySelectorAll(m[1])) {
  81. try {
  82. if (!html) e.textContents = val;
  83. else if (!m[2]) e.innerHTML = val;
  84. else if (!m[3]) e.setAttribute(m[2],val);
  85. else if (!cfgbool(val)) e.removeAttribute(m[2]);
  86. else e.setAttribute(m[2],'');
  87. } catch(e) { };
  88. }
  89. } catch(e) { };
  90. }
  91. }
  92. // Load status or HTML data
  93. function load(url,html = false)
  94. {
  95. fetchconfig(url)
  96. .then(map => fillin(map,html))
  97. .catch(() => {});
  98. }
  99. // POST upload of data from within a form, with progress and response text
  100. function upload(form,data) {
  101. var xhr = new XMLHttpRequest();
  102. var progress = chi(form,'progress');
  103. if (progress) {
  104. progress.value = 0;
  105. xhr.upload.addEventListener('progress', (e) => {
  106. if (!e.lengthComputable)
  107. return;
  108. progress.max = e.total * 1.05;
  109. progress.value = e.loaded;
  110. }, PassiveListener);
  111. }
  112. var result = chi(form,'output');
  113. if (result)
  114. result.classList.remove('result');
  115. xhr.addEventListener('readystatechange', (e) => {
  116. if (xhr.readyState != XMLHttpRequest.DONE)
  117. return;
  118. const ok = xhr.status >= 200 && xhr.status < 400;
  119. if (progress)
  120. progress.value = ok ? progress.max : 0;
  121. if (result) {
  122. var msg = xhr.responseText.trimEnd();
  123. if (!msg)
  124. msg = xhr.status + ' ' + xhr.statusText;
  125. result.textContent = msg;
  126. result.classList.toggle('ok', ok);
  127. result.classList.toggle('err', !ok);
  128. result.classList.add('result');
  129. }
  130. const rf = parseInt(xhr.getResponseHeader('Refresh'));
  131. if (rf && ok)
  132. setTimeout(() => window.location.reload(true), rf * 1000);
  133. }, PassiveListener);
  134. xhr.open(form.method, form.action);
  135. xhr.responseType = 'text';
  136. xhr.send(data);
  137. return xhr;
  138. }
  139. // Upload a data file blob
  140. function uploadfile() {
  141. event.preventDefault();
  142. const form = event.target.form || event.target;
  143. var files = form.elements['file'];
  144. return (files.files.length == 1)
  145. ? upload(form,files.files[0]) : files.click();
  146. }
  147. // key=value formatting of form data; including inverted checkboxes
  148. function textformdata(form) {
  149. var data = '';
  150. for (var e of form.elements) {
  151. var val = e.value;
  152. if (val == undefined || !e.name || e instanceof HTMLButtonElement) {
  153. continue;
  154. } else if (e instanceof HTMLInputElement) {
  155. if (e.type == 'checkbox')
  156. val = e.checked == cfgbool(val) ? '1' : '0';
  157. else if (e.type == 'radio' && !e.checked)
  158. continue;
  159. }
  160. data += e.name + '=' + val + "\r\n";
  161. }
  162. return data;
  163. }
  164. // POST form contents upload with response text
  165. function uploadform() {
  166. event.preventDefault();
  167. const form = event.target.form || event.target;
  168. return upload(form,textformdata(form));
  169. }
  170. // Flip the status of an INPUT element between text and password
  171. function showpwd(me = event.target) {
  172. const now_visible = me.classList.toggle('hide');
  173. const new_type = now_visible ? 'text' : 'password';
  174. me.classList.toggle('show',!now_visible);
  175. sib(me,'input').setAttribute('type', new_type);
  176. }
  177. // Insert translations as needed
  178. var translations = null;
  179. var delay_translate = false;
  180. function translate(top = document) {
  181. delay_translate = (!translations || document.readyState != 'complete');
  182. if (!delay_translate)
  183. fillin(translations, true, top);
  184. }
  185. document.addEventListener('load', (e) => translate(), PassiveListener);
  186. fetchconfig('/sys/lang')
  187. .then((map) => {
  188. translations = map;
  189. if (delay_translate)
  190. translate();
  191. })
  192. .catch(() => {});
  193. // Hack to include an HTML files. Sadly, does not support
  194. // including files with <script> tags.
  195. function inc(url) {
  196. var me = document.currentScript;
  197. fetch(url, {redirect: "follow"})
  198. .then((response) => response.text())
  199. .then((text) => {
  200. const p = me.parentElement;
  201. me.outerHTML = text;
  202. translate(p);
  203. });
  204. }