about summary refs log tree commit diff
path: root/users/grfn/bbbg/resources/public/main.js
blob: 87c0b64d0a37f88c1c0e81d1393eda89a7b4ecef (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
window.onload = () => {
  const input = document.getElementById("name-autocomplete");
  if (input != null) {
    const attendeeList = document.getElementById("attendees-list");
    const filterAttendees = (filter) => {
      if (filter == "") {
        for (let elt of attendeeList.querySelectorAll("li")) {
          elt.classList.remove("hidden");
        }

        return;
      }

      let re = "";
      for (let c of filter) {
        re += `${c}.*`;
      }
      let filterRe = new RegExp(re, "i");

      for (let elt of attendeeList.querySelectorAll("li")) {
        const attendee = JSON.parse(elt.dataset.attendee);
        if (attendee["bbbg.attendee/meetup-name"].match(filterRe) == null) {
          elt.classList.add("hidden");
        } else {
          elt.classList.remove("hidden");
        }
      }
    };

    const attendeeIDInput = document.getElementById("attendee-id");
    const submit = document.querySelector("#submit-button");
    const signupForm = document.getElementById("signup-form");

    input.oninput = (e) => {
      filterAttendees(e.target.value);
      attendeeIDInput.value = null;
      submit.classList.add("hidden");
      submit.setAttribute("disabled", "disabled");
      signupForm.setAttribute("disabled", "disabled");
    };

    attendeeList.addEventListener("click", (e) => {
      if (!(e.target instanceof HTMLLIElement)) {
        return;
      }
      if (e.target.dataset.attendee == null) {
        return;
      }

      const attendee = JSON.parse(e.target.dataset.attendee);
      input.value = attendee["bbbg.attendee/meetup-name"];
      attendeeIDInput.value = attendee["bbbg.attendee/id"];

      submit.classList.remove("hidden");
      submit.removeAttribute("disabled");
      signupForm.removeAttribute("disabled");
    });
  }

  document.querySelectorAll("form").forEach((form) => {
    form.addEventListener("submit", (e) => {
      if (e.target.attributes.disabled) {
        e.preventDefault();
      }

      const confirmMessage = e.target.dataset.confirm;
      if (confirmMessage != null && !confirm(confirmMessage)) {
        e.stopImmediatePropagation();
        e.preventDefault();
      }
    });
  });
};