popup.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* exported Popup */
  2. // Add or remove a class from elem.classList, depending on cond.
  3. function setClass(elem, className, cond) {
  4. if (cond) {
  5. elem.classList.add(className);
  6. } else {
  7. elem.classList.remove(className);
  8. }
  9. }
  10. class Popup {
  11. constructor() {
  12. this.div = document.getElementById('active');
  13. this.statustext = document.getElementById('statustext');
  14. this.statusdesc = document.getElementById('statusdesc');
  15. this.img = document.getElementById('statusimg');
  16. }
  17. setEnabled(enabled) {
  18. setClass(this.img, 'on', enabled);
  19. }
  20. setActive(active) {
  21. setClass(this.img, 'running', active);
  22. }
  23. setStatusText(txt) {
  24. this.statustext.innerText = txt;
  25. }
  26. setStatusDesc(desc, error) {
  27. this.statusdesc.innerText = desc;
  28. setClass(this.statusdesc, 'error', error);
  29. }
  30. hideButton() {
  31. document.querySelector('.button').style.display = 'none';
  32. }
  33. setChecked(checked) {
  34. document.getElementById('enabled').checked = checked;
  35. }
  36. static fill(n, func) {
  37. switch(n.nodeType) {
  38. case 3: { // Node.TEXT_NODE
  39. const m = /^__MSG_([^_]*)__$/.exec(n.nodeValue);
  40. if (m) { n.nodeValue = func(m[1]); }
  41. break;
  42. }
  43. case 1: // Node.ELEMENT_NODE
  44. n.childNodes.forEach(c => Popup.fill(c, func));
  45. break;
  46. }
  47. }
  48. }