aboutsummaryrefslogtreecommitdiff
path: root/static/js/forms.ts
blob: 2916536a00576d743611418d4e37389b38d92225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(function () {

  const cookiesDisabled = !navigator.cookieEnabled;

  if (cookiesDisabled) {
    document.cookie = "disabled";
    document.cookie.indexOf("disabled");
    return console.warn("WARNING: Cannot persist form state due to cookie restrictions");
  }

  ["URLChangedCustomEvent", "DOMContentLoaded"].forEach(function (event) {
    self.addEventListener(event, function (event) {
      const input = Array.prototype.slice.call(document.querySelectorAll("input"));
      const select = Array.prototype.slice.call(document.querySelectorAll("select"));
      const textarea = Array.prototype.slice.call(document.querySelectorAll("textarea"));

      const states = input.concat(select).concat(textarea);

      for (let i = 0; i < states.length; i++) {

        if (localStorage[states[i].id]) {
          if (states[i].type === "radio" || states[i].type === "checkbox") {
            if (localStorage[states[i].id] === "on") states[i].checked = true;
          } else states[i].value = localStorage[states[i].id];
          self.dispatchEvent(new Event("storage"));
        }

        states[i].addEventListener("change", function (event) {

          // console.log("INFO: STATE",    event.target);
          // console.log("INFO: ID:",      event.target.id);
          // console.log("INFO: NAME:",    event.target.name);
          // console.log("INFO: TYPE:",    event.target.type);
          // console.log("INFO: VALUE:",   event.target.value);
          // console.log("INFO: CHECKED:", event.target.checked);

          localStorage[event.target.id] = event.target.value;

          const group = document.querySelectorAll(`input[name='${event.target.name}']`);

          for (let i = 0; i < group.length; i++) {
            if ((group[i].type === "radio" || group[i].type === "checkbox") && !group[i].checked) {
              localStorage[group[i].id] = "off";
            }
          }
          self.dispatchEvent(new Event("storage"));
        });
      }
    });
  });

  self.addEventListener("storage", function () {
    let stylesheet = document.querySelector('link[href$="default-simple.css"]')

    if (localStorage["config.density.low"]  === "on") stylesheet.rel = "stylesheet"
    if (localStorage["config.density.high"] === "on") stylesheet.rel = "alternate stylesheet"
  });

})();