2
0

max80.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Read a key=value text file and return it as a Promise of a Map
  2. function fetchconfig(url) {
  3. var xdr = new XMLHttpRequest();
  4. return fetch(url, {cache: "no-store"})
  5. .then(res => {
  6. if (!res.ok) {
  7. throw new Error("HTTP error "+response.status);
  8. } else {
  9. return res.text();
  10. }
  11. })
  12. .then(text => {
  13. var map = new Map();
  14. for (const c of text.split(/[\r\n]+/)) {
  15. const eqs = c.indexOf("=");
  16. if (eqs > 0) {
  17. map.set(c.slice(0, eqs), c.slice(eqs+1));
  18. }
  19. }
  20. return map;
  21. });
  22. }
  23. // Initialize a form from a map
  24. function initform(root, map) {
  25. var form = root.elements;
  26. for (const [key,val] of map) {
  27. var field = form[key];
  28. if (field != undefined && field.tagName == "INPUT") {
  29. if (field.type == "checkbox") {
  30. field.checked = val && val != "0" && val != "off";
  31. } else {
  32. field.value = val;
  33. }
  34. }
  35. }
  36. }
  37. // Load form initialization data
  38. function loadform(form, url) {
  39. fetchconfig(url)
  40. .then(map => { initform(document.querySelector(form), map) })
  41. .catch(() => {});
  42. }
  43. // Insert status data from a map
  44. function initstatus(map)
  45. {
  46. for (const [key,val] of map) {
  47. var e = document.getElementById(key);
  48. if (e) {
  49. e.innerText = val;
  50. }
  51. }
  52. }
  53. // Load status data
  54. function loaddata(url)
  55. {
  56. fetchconfig(url)
  57. .then(map => { initdata(map) })
  58. .catch(() => {});
  59. }
  60. // POST file upload with progress and response text
  61. function uploadfile(event) {
  62. event.preventDefault();
  63. var form = event.currentTarget;
  64. var elem = form.elements;
  65. var files = elem["file"].files;
  66. if (files.length != 1) {
  67. /* Show error */
  68. return;
  69. }
  70. var file = files[0];
  71. var xhr = new XMLHttpRequest();
  72. var progress = form.querySelector("progress");
  73. if (progress != undefined) {
  74. progress.value = 0;
  75. xhr.upload.addEventListener("progress", (event) => {
  76. if (event.lengthComputable) {
  77. progress.max = event.total;
  78. progress.value = event.loaded;
  79. }
  80. });
  81. }
  82. var result = form.querySelector("pre.result");
  83. if (result != undefined) {
  84. result.className = "result hide";
  85. }
  86. xhr.addEventListener("loadend", (event) => {
  87. const ok = xhr.status >= 200 && xhr.status <= 299;
  88. if (progress != undefined) {
  89. progress.value = ok ? progress.max : 0;
  90. }
  91. if (result != undefined) {
  92. result.className = "result " + (ok ? "ok" : "err");
  93. result.innerText = xhr.responseText;
  94. }
  95. });
  96. xhr.open("POST", form.action);
  97. xhr.responseType = 'text';
  98. xhr.send(file);
  99. }
  100. // Flip the status of an INPUT element between text and password
  101. function showpwd(id,me) {
  102. var pwd = document.getElementById(id);
  103. const was_visible = pwd.getAttribute('type') == 'text';
  104. const new_type = was_visible ? 'password' : 'text';
  105. const new_icon = was_visible ? 'show' : 'hide';
  106. pwd.setAttribute('type', new_type);
  107. me.innerHTML = new_icon;
  108. }
  109. // Hack to include an HTML files. Sadly, does not support
  110. // including files with <script> tags.
  111. function inc(url) {
  112. var me = document.currentScript;
  113. fetch(url)
  114. .then((response) => response.text())
  115. .then((text) => { me.outerHTML = text; });
  116. }