File size: 12,547 Bytes
187ef74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Checkout modal controller.
//
// On an event page, links marked data-modal-target="checkout" fetch the
// modal partial (?fragment=1) and present it as an in-page overlay with
// history integration (pushState to the checkout URL, Back closes). The
// form submits over fetch with Accept: application/json and paints a
// confirmation pane. Every step degrades: without JS the same links are
// ordinary navigations to the standalone /checkout/{id} page, whose form
// posts and redirects to the receipt.
(function () {
  "use strict";

  var overlay = null;
  var lastFocus = null;
  var onEventPage = !!document.querySelector(".event-detail");

  function money(cents) {
    return "$" + (cents / 100).toFixed(2);
  }

  function wireQuantity(root) {
    var qty = root.querySelector("#qty");
    if (!qty) return;
    var unit = parseInt(qty.getAttribute("data-unit-price-cents"), 10);
    var stepper = root.querySelector("[data-stepper]");

    function sync() {
      var value = parseInt(qty.value, 10) || 1;
      var max = qty.options.length;
      if (value < 1) { value = 1; qty.value = "1"; }
      if (value > max) { value = max; qty.value = String(max); }
      if (!isNaN(unit)) {
        root.querySelectorAll("[data-order-total]").forEach(function (el) {
          el.textContent = money(unit * value);
        });
      }
      var sq = root.querySelector("[data-summary-qty]");
      if (sq) sq.textContent = String(value);
      if (stepper) {
        stepper.querySelector('[data-step="-1"]').disabled = value <= 1;
        stepper.querySelector('[data-step="1"]').disabled = value >= max;
      }
    }
    qty.addEventListener("change", sync);
    if (stepper) {
      stepper.querySelectorAll("[data-step]").forEach(function (btn) {
        btn.addEventListener("click", function () {
          qty.value = String((parseInt(qty.value, 10) || 1) +
            parseInt(btn.getAttribute("data-step"), 10));
          sync();
        });
      });
    }
    sync();
  }

  function wireOptions(root) {
    var options = root.querySelector("[data-checkout-options]");
    if (!options) return;
    var registration = root.querySelector("[data-checkout-registration]");
    var summary = root.querySelector("[data-options-summary]");
    var formQty = root.querySelector("#qty");
    var optionsQty = root.querySelector("[data-options-qty]");

    function focusVisible(selector) {
      var target = root.querySelector(selector);
      if (target && target.offsetParent !== null && !target.disabled) {
        target.focus();
      }
    }

    function openSection(name) {
      options.classList.remove("is-date", "is-time", "is-tickets");
      options.classList.add("is-" + name);
      options.querySelectorAll("[data-option-section]").forEach(function (section) {
        var open = section.getAttribute("data-option-section") === name;
        section.classList.toggle("is-open", open);
        var trigger = section.querySelector("[data-option-trigger]");
        var panel = section.querySelector("[data-option-panel]");
        if (trigger) trigger.setAttribute("aria-expanded", String(open));
        if (panel) panel.hidden = !open;
      });
      var main = root.querySelector("[data-checkout-main]");
      if (main) main.scrollTop = 0;
    }

    options.querySelectorAll("[data-option-trigger]").forEach(function (trigger) {
      trigger.addEventListener("click", function () {
        openSection(trigger.getAttribute("data-option-trigger"));
      });
    });
    var date = options.querySelector("[data-option-date]");
    if (date) {
      date.addEventListener("click", function () {
        var selected = options.querySelector("[data-selected-date]");
        if (selected) selected.hidden = false;
        openSection("time");
        focusVisible("[data-option-time]");
      });
    }
    var time = options.querySelector("[data-option-time]");
    if (time) {
      time.addEventListener("click", function () {
        var selected = options.querySelector("[data-selected-time]");
        if (selected) selected.hidden = false;
        if (summary) summary.hidden = false;
        openSection("tickets");
        focusVisible("[data-options-qty]");
      });
    }
    if (optionsQty && formQty) {
      optionsQty.addEventListener("change", function () {
        formQty.value = optionsQty.value;
        formQty.dispatchEvent(new Event("change", { bubbles: true }));
      });
    }
    var seeMore = options.querySelector("[data-ticket-see-more]");
    if (seeMore) {
      seeMore.addEventListener("click", function () {
        var shortText = options.querySelector("[data-ticket-description-short]");
        var moreText = options.querySelector("[data-ticket-description-more]");
        var expanded = seeMore.getAttribute("aria-expanded") === "true";
        if (shortText) shortText.hidden = !expanded;
        if (moreText) moreText.hidden = expanded;
        seeMore.setAttribute("aria-expanded", String(!expanded));
        seeMore.textContent = expanded ? "See more" : "See less";
      });
    }
    var moreDates = options.querySelector("[data-calendar-more]");
    var calendarMonths = options.querySelector(".checkout-calendar__months");
    if (moreDates && calendarMonths) {
      moreDates.addEventListener("click", function () {
        calendarMonths.scrollBy({ top: 220, behavior: "smooth" });
      });
    }
    var register = options.querySelector("[data-options-register]");
    if (register && registration) {
      register.addEventListener("click", function () {
        options.hidden = true;
        registration.hidden = false;
        if (summary) summary.hidden = false;
        var main = root.querySelector("[data-checkout-main]");
        if (main) main.scrollTop = 0;
        var first = registration.querySelector("input:not([type='hidden'])");
        if (first) first.focus();
      });
    }
  }

  function visibleFocusables(container) {
    return Array.prototype.slice.call(container.querySelectorAll(
      "a[href], button:not([disabled]), input:not([disabled]), " +
      "select:not([disabled]), textarea:not([disabled]), [tabindex]"
    )).filter(function (item) {
      return item.getAttribute("tabindex") !== "-1" &&
        item.getAttribute("aria-hidden") !== "true" &&
        item.offsetParent !== null;
    });
  }

  function showErrors(root, errors) {
    var box = root.querySelector("[data-checkout-errors]");
    if (!box) return;
    var list = box.querySelector("ul");
    list.innerHTML = "";
    errors.forEach(function (message) {
      var item = document.createElement("li");
      item.textContent = message;
      list.appendChild(item);
    });
    box.hidden = false;
    box.scrollIntoView({ block: "nearest" });
  }

  function showConfirmation(root, data, quantity) {
    var main = root.querySelector("[data-checkout-main]");
    if (!main) return;
    main.innerHTML =
      '<div class="checkout-confirmation" role="status">' +
      '<svg class="icon" aria-hidden="true"><use href="#i-check"/></svg>' +
      "<h2>You're in!</h2>" +
      "<p>" + quantity + " ticket(s) confirmed" +
      (data.total ? " · " + data.total : "") + "</p>" +
      '<p>Order reference <strong>' + data.reference + "</strong></p>" +
      '<a class="eds-btn eds-btn--fill" href="' + data.receipt_url +
      '">View your order</a></div>';
    var link = main.querySelector("a.eds-btn");
    if (link) link.focus();
  }

  function wireForm(root) {
    var form = root.querySelector("[data-checkout-form]");
    if (!form || !window.fetch) return;
    function onSubmit(event) {
      event.preventDefault();
      var button = form.querySelector('button[type="submit"]');
      button.disabled = true;
      var quantity = parseInt(form.querySelector("#qty").value, 10) || 1;
      fetch(form.action, {
        method: "POST",
        headers: { "Accept": "application/json" },
        body: new URLSearchParams(new FormData(form)),
        credentials: "same-origin",
      }).then(function (response) {
        return response.json().then(function (data) {
          if (data.redirect) { window.location.assign(data.redirect); return; }
          if (data.status === "created") {
            showConfirmation(root, data, quantity);
            return;
          }
          showErrors(root, data.errors || ["Something went wrong."]);
          button.disabled = false;
        });
      }).catch(function () {
        // Network/parse failure: fall back to the ordinary form post.
        form.removeEventListener("submit", onSubmit);
        form.submit();
      });
    }
    form.addEventListener("submit", onSubmit);
  }

  function trapFocus(container) {
    container.addEventListener("keydown", function (event) {
      if (event.key !== "Tab") return;
      var focusable = visibleFocusables(container);
      if (!focusable.length) return;
      var first = focusable[0];
      var last = focusable[focusable.length - 1];
      if (focusable.indexOf(document.activeElement) === -1) {
        event.preventDefault();
        (event.shiftKey ? last : first).focus();
      } else if (event.shiftKey && document.activeElement === first) {
        event.preventDefault(); last.focus();
      } else if (!event.shiftKey && document.activeElement === last) {
        event.preventDefault(); first.focus();
      }
    });
  }

  function closeOverlay(viaHistory) {
    if (!overlay) return;
    overlay.remove();
    overlay = null;
    document.body.classList.remove("has-checkout-open");
    if (lastFocus && lastFocus.focus) lastFocus.focus();
    if (!viaHistory) history.back();
  }

  function openModal(checkoutUrl) {
    var fragmentUrl = checkoutUrl +
      (checkoutUrl.indexOf("?") >= 0 ? "&" : "?") + "fragment=1";
    fetch(fragmentUrl, { credentials: "same-origin" })
      .then(function (response) {
        if (!response.ok) throw new Error("fragment " + response.status);
        return response.text();
      })
      .then(function (html) {
        lastFocus = document.activeElement;
        overlay = document.createElement("div");
        overlay.className = "checkout-overlay";
        overlay.innerHTML = html;
        document.body.appendChild(overlay);
        document.body.classList.add("has-checkout-open");
        history.pushState({ checkout: true }, "", checkoutUrl);
        var modal = overlay.querySelector('[data-modal="checkout"]');
        wireQuantity(overlay);
        wireOptions(overlay);
        wireForm(overlay);
        trapFocus(overlay);
        var initial = visibleFocusables(modal)[0];
        if (initial) initial.focus();
        else modal.focus();
        overlay.addEventListener("click", function (event) {
          if (event.target === overlay) closeOverlay(false);
        });
        overlay.querySelectorAll("[data-checkout-close]").forEach(function (close) {
          close.addEventListener("click", function (event) {
            event.preventDefault();
            closeOverlay(false);
          });
        });
      })
      .catch(function () {
        // Fetch failed: fall back to full-page navigation.
        window.location.assign(checkoutUrl);
      });
  }

  if (onEventPage && window.fetch) {
    document.querySelectorAll('a[href^="/checkout/"]').forEach(function (link) {
      link.addEventListener("click", function (event) {
        event.preventDefault();
        openModal(link.getAttribute("href"));
      });
    });
    window.addEventListener("keydown", function (event) {
      if (event.key === "Escape" && overlay) closeOverlay(false);
    });
    window.addEventListener("popstate", function () {
      if (overlay) closeOverlay(true);
    });

    var shareButton = document.querySelector(
      '.event-toolbar button[aria-label="Share event"]');
    var shareStatus = document.querySelector(".event-share-status");
    if (shareButton) {
      shareButton.addEventListener("click", function () {
        if (!shareStatus) return;
        shareStatus.textContent = "Sharing is unavailable in this local clone";
        window.setTimeout(function () {
          shareStatus.textContent = "";
        }, 2200);
      });
    }
  }

  // Standalone /checkout/{id} page: wire quantity + fetch submit in place.
  var pageModal = document.querySelector(
    ".checkout-overlay.is-page [data-modal='checkout']");
  if (pageModal) {
    wireQuantity(pageModal);
    wireOptions(pageModal);
    wireForm(pageModal);
    var pageRoot = document.querySelector(".checkout-overlay.is-page");
    pageRoot.classList.add("is-static");
  }
})();