max80.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. function boolcfg(str) { return str && !str.match(/^(0*|[fnd].*|of.*)$/i); }
  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 instanceof HTMLSelectElement) &&
  36. !field.classList.contains("noload")) {
  37. const val = map.get(field.name) || '';
  38. if (field.type == 'checkbox') {
  39. field.checked = !boolcfg(val);
  40. } else if (field.type == 'radio') {
  41. field.checked = (val == field.name);
  42. field.remove();
  43. } else {
  44. field.value = val;
  45. }
  46. } else if (field instanceof HTMLButtonElement &&
  47. field.type == 'submit') {
  48. button = field;
  49. }
  50. }
  51. if (button) {
  52. button.disabled = false; // All loaded, enable the submit button
  53. }
  54. }
  55. // Load form initialization data
  56. function loadform(form, url) {
  57. fetchconfig(url)
  58. .then(map => { initform(form, map) })
  59. .catch(() => {});
  60. }
  61. // Replace the contents of selected HTML elements based on a map with selectors
  62. function fillin(map,html)
  63. {
  64. for (const [key,val] of map) {
  65. try {
  66. for (var e of document.querySelectorAll(key))
  67. if (html) e.innerHTML = val; else e.innerText = val;
  68. } catch (error) { }
  69. }
  70. }
  71. // Load status or HTML data
  72. function load(url,html = false)
  73. {
  74. fetchconfig(url)
  75. .then(map => { fillin(map,html) })
  76. .catch(() => {});
  77. }
  78. // POST file upload with progress and response text
  79. function uploadfile(event) {
  80. event.preventDefault();
  81. var form = event.currentTarget;
  82. var elem = form.elements;
  83. var files = elem["file"].files;
  84. if (files.length != 1) {
  85. /* Show error */
  86. return;
  87. }
  88. var file = files[0];
  89. var xhr = new XMLHttpRequest();
  90. var progress = form.querySelector("progress");
  91. if (progress != undefined) {
  92. progress.value = 0;
  93. xhr.upload.addEventListener("progress", (event) => {
  94. if (event.lengthComputable) {
  95. progress.max = event.total * 1.05;
  96. progress.value = event.loaded;
  97. }
  98. });
  99. }
  100. var result = form.querySelector("pre.result");
  101. if (result != undefined) {
  102. result.className = "result hide";
  103. }
  104. xhr.addEventListener("loadend", (event) => {
  105. const ok = xhr.status >= 200 && xhr.status <= 299;
  106. if (progress != undefined) {
  107. progress.value = ok ? progress.max : 0;
  108. }
  109. if (result != undefined) {
  110. result.className = "result " + (ok ? "ok" : "err");
  111. result.innerText = xhr.responseText;
  112. }
  113. });
  114. xhr.open("POST", form.action);
  115. xhr.responseType = 'text';
  116. xhr.send(file);
  117. }
  118. // POST upload of a form as key=value pairs, *including* transmitting
  119. // unchecked checkboxes as name=0.
  120. // Enable a button (this ensures that the necessary scripts have been
  121. // run before one can submit)
  122. function enablebutton(id,on) {
  123. getelem(id).disabled = !on;
  124. }
  125. // Flip the status of an INPUT element between text and password
  126. function showpwd(id,me) {
  127. var pwd = getelem(id);
  128. const now_visible = me.classList.toggle('hide');
  129. me.classList.toggle('show',!now_visible);
  130. const new_type = now_visible ? 'text' : 'password';
  131. pwd.setAttribute('type', new_type);
  132. }
  133. // Hack to include an HTML files. Sadly, does not support
  134. // including files with <script> tags.
  135. function inc(url) {
  136. var me = document.currentScript;
  137. fetch(url, {redirect: "follow"})
  138. .then((response) => response.text())
  139. .then((text) => { me.outerHTML = text; });
  140. }
  141. // Insert translations
  142. function translate() {
  143. load('/sys/lang', true);
  144. }