|
| 1 | +import DOMPurify from "dompurify"; |
| 2 | +import { proxyImageUrl } from "./events.js"; |
| 3 | + |
| 4 | +const backdropClass = "pf-v6-c-backdrop"; |
| 5 | + |
| 6 | +const modalTemplate = document.createElement("template"); |
| 7 | +modalTemplate.innerHTML = ` |
| 8 | + <div class="${backdropClass}" style="display: none;"> |
| 9 | + <div class="pf-v6-l-bullseye"> |
| 10 | + <div class="pf-v6-c-modal-box pf-m-md" role="dialog" aria-modal="true" |
| 11 | + aria-labelledby="event-detail-title" aria-describedby="event-detail-body"> |
| 12 | + <div class="pf-v6-c-modal-box__close"> |
| 13 | + <button class="pf-v6-c-button pf-m-plain" type="button" aria-label="Close" data-slot="close"> |
| 14 | + <span class="pf-v6-c-button__icon"> |
| 15 | + <i class="fas fa-times" aria-hidden="true"></i> |
| 16 | + </span> |
| 17 | + </button> |
| 18 | + </div> |
| 19 | + <header class="pf-v6-c-modal-box__header"> |
| 20 | + <div class="pf-v6-c-modal-box__header-main"> |
| 21 | + <h1 class="pf-v6-c-modal-box__title" id="event-detail-title" data-slot="title"></h1> |
| 22 | + </div> |
| 23 | + </header> |
| 24 | + <div class="pf-v6-c-modal-box__body" id="event-detail-body" tabindex="0" data-slot="body"> |
| 25 | + <div data-slot="content"> |
| 26 | + <div class="pf-v6-c-skeleton pf-v6-u-mb-md" style="--pf-v6-c-skeleton--Height: 10rem; border-radius: var(--pf-t--global--border--radius--medium);"></div> |
| 27 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 28 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50 pf-v6-u-mb-sm"></div> |
| 29 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 30 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50 pf-v6-u-mb-md"></div> |
| 31 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-v6-u-mb-sm"></div> |
| 32 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 33 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50"></div> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + <footer class="pf-v6-c-modal-box__footer"> |
| 37 | + <a data-slot="rsvp" target="_blank" rel="noopener" |
| 38 | + class="pf-v6-c-button pf-m-primary">RSVP on Luma</a> |
| 39 | + <button data-slot="cancel" type="button" |
| 40 | + class="pf-v6-c-button pf-m-link">Close</button> |
| 41 | + </footer> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + </div>`; |
| 45 | + |
| 46 | +const detailTemplate = document.createElement("template"); |
| 47 | +detailTemplate.innerHTML = ` |
| 48 | + <div> |
| 49 | + <img data-slot="cover" alt="" style="width: 100%; max-height: 16rem; object-fit: cover; |
| 50 | + border-radius: var(--pf-t--global--border--radius--medium); margin-bottom: 1rem;" /> |
| 51 | + <div class="pf-v6-c-description-list pf-m-horizontal pf-m-compact"> |
| 52 | + <div class="pf-v6-c-description-list__group" data-slot="date-group"> |
| 53 | + <dt class="pf-v6-c-description-list__term"><span class="pf-v6-c-description-list__text">Date</span></dt> |
| 54 | + <dd class="pf-v6-c-description-list__description"><div class="pf-v6-c-description-list__text" data-slot="date"></div></dd> |
| 55 | + </div> |
| 56 | + <div class="pf-v6-c-description-list__group" data-slot="time-group"> |
| 57 | + <dt class="pf-v6-c-description-list__term"><span class="pf-v6-c-description-list__text">Time</span></dt> |
| 58 | + <dd class="pf-v6-c-description-list__description"><div class="pf-v6-c-description-list__text" data-slot="time"></div></dd> |
| 59 | + </div> |
| 60 | + <div class="pf-v6-c-description-list__group" data-slot="venue-group"> |
| 61 | + <dt class="pf-v6-c-description-list__term"><span class="pf-v6-c-description-list__text">Venue</span></dt> |
| 62 | + <dd class="pf-v6-c-description-list__description"><div class="pf-v6-c-description-list__text"><span data-slot="venue"></span><br data-slot="map-br" /><span data-slot="map-link"></span></div></dd> |
| 63 | + </div> |
| 64 | + <div class="pf-v6-c-description-list__group" data-slot="hosts-group"> |
| 65 | + <dt class="pf-v6-c-description-list__term"><span class="pf-v6-c-description-list__text">Hosted by</span></dt> |
| 66 | + <dd class="pf-v6-c-description-list__description"><div class="pf-v6-c-description-list__text" data-slot="hosts"></div></dd> |
| 67 | + </div> |
| 68 | + <div class="pf-v6-c-description-list__group" data-slot="admission-group"> |
| 69 | + <dt class="pf-v6-c-description-list__term"><span class="pf-v6-c-description-list__text">Admission</span></dt> |
| 70 | + <dd class="pf-v6-c-description-list__description"><div class="pf-v6-c-description-list__text" data-slot="admission"></div></dd> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + <hr class="pf-v6-u-mt-md pf-v6-u-mb-md" data-slot="desc-divider" /> |
| 74 | + <div class="pf-v6-c-content" data-slot="description"></div> |
| 75 | + </div>`; |
| 76 | + |
| 77 | +customElements.define( |
| 78 | + "event-detail-modal", |
| 79 | + class extends HTMLElement { |
| 80 | + connectedCallback() { |
| 81 | + this.append(modalTemplate.content.cloneNode(true)); |
| 82 | + this._backdrop = this.querySelector(`.${backdropClass}`); |
| 83 | + this._$ = (s) => this.querySelector(`[data-slot="${s}"]`); |
| 84 | + |
| 85 | + this._$("close").addEventListener("click", () => this.close()); |
| 86 | + this._$("cancel").addEventListener("click", () => this.close()); |
| 87 | + this._backdrop.addEventListener("click", (e) => { |
| 88 | + if (e.target === this._backdrop) this.close(); |
| 89 | + }); |
| 90 | + this._keyHandler = (e) => { |
| 91 | + if (e.key === "Escape") this.close(); |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + async open(eventApiId, api) { |
| 96 | + this._backdrop.style.display = ""; |
| 97 | + document.documentElement.style.overflow = "hidden"; |
| 98 | + document.addEventListener("keydown", this._keyHandler); |
| 99 | + |
| 100 | + this._$("title").innerHTML = '<div class="pf-v6-c-skeleton pf-m-text-lg pf-m-width-50"></div>'; |
| 101 | + const content = this._$("content"); |
| 102 | + content.innerHTML = ` |
| 103 | + <div class="pf-v6-c-skeleton pf-v6-u-mb-md" style="--pf-v6-c-skeleton--Height: 10rem; border-radius: var(--pf-t--global--border--radius--medium);"></div> |
| 104 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 105 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50 pf-v6-u-mb-sm"></div> |
| 106 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 107 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50 pf-v6-u-mb-md"></div> |
| 108 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-v6-u-mb-sm"></div> |
| 109 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-75 pf-v6-u-mb-sm"></div> |
| 110 | + <div class="pf-v6-c-skeleton pf-m-text-sm pf-m-width-50"></div>`; |
| 111 | + this._$("rsvp").style.display = "none"; |
| 112 | + |
| 113 | + try { |
| 114 | + const data = await fetch( |
| 115 | + `${api}/events/detail?event_api_id=${encodeURIComponent(eventApiId)}`, |
| 116 | + ).then((r) => r.json()); |
| 117 | + |
| 118 | + this._$("title").textContent = data.name; |
| 119 | + this._$("rsvp").href = data.link; |
| 120 | + this._$("rsvp").style.display = ""; |
| 121 | + |
| 122 | + const detail = detailTemplate.content.cloneNode(true); |
| 123 | + const $ = (s) => detail.querySelector(`[data-slot="${s}"]`); |
| 124 | + |
| 125 | + const cover = $("cover"); |
| 126 | + if (data.cover_url) { |
| 127 | + cover.src = proxyImageUrl(data.cover_url, api, "luma"); |
| 128 | + cover.alt = data.name; |
| 129 | + } else { |
| 130 | + cover.remove(); |
| 131 | + } |
| 132 | + |
| 133 | + const start = new Date(data.start_at); |
| 134 | + const dateOpts = { |
| 135 | + weekday: "long", |
| 136 | + year: "numeric", |
| 137 | + month: "long", |
| 138 | + day: "numeric", |
| 139 | + timeZone: data.timezone || undefined, |
| 140 | + }; |
| 141 | + $("date").textContent = start.toLocaleDateString("en-CA", dateOpts); |
| 142 | + |
| 143 | + const timeOpts = { |
| 144 | + hour: "numeric", |
| 145 | + minute: "2-digit", |
| 146 | + timeZone: data.timezone || undefined, |
| 147 | + timeZoneName: data.timezone ? "short" : undefined, |
| 148 | + }; |
| 149 | + let timeText = start.toLocaleTimeString("en-CA", timeOpts); |
| 150 | + if (data.end_at) { |
| 151 | + const end = new Date(data.end_at); |
| 152 | + timeText += " – " + end.toLocaleTimeString("en-CA", timeOpts); |
| 153 | + } |
| 154 | + $("time").textContent = timeText; |
| 155 | + |
| 156 | + if (data.location) { |
| 157 | + const venueText = data.full_address |
| 158 | + ? `${data.location} — ${data.full_address}` |
| 159 | + : data.location; |
| 160 | + $("venue").textContent = venueText; |
| 161 | + |
| 162 | + if (data.map_url) { |
| 163 | + const mapLink = document.createElement("a"); |
| 164 | + mapLink.href = data.map_url; |
| 165 | + mapLink.target = "_blank"; |
| 166 | + mapLink.rel = "noopener"; |
| 167 | + mapLink.className = "pf-v6-c-button pf-m-link pf-m-inline pf-m-small"; |
| 168 | + mapLink.innerHTML = |
| 169 | + 'View on map <span class="pf-v6-c-button__icon pf-m-end"><icon-external></icon-external></span>'; |
| 170 | + $("map-link").append(mapLink); |
| 171 | + } else { |
| 172 | + $("map-br").remove(); |
| 173 | + $("map-link").remove(); |
| 174 | + } |
| 175 | + } else { |
| 176 | + $("venue-group").remove(); |
| 177 | + } |
| 178 | + |
| 179 | + if (data.hosts) { |
| 180 | + $("hosts").textContent = data.hosts; |
| 181 | + } else { |
| 182 | + $("hosts-group").remove(); |
| 183 | + } |
| 184 | + |
| 185 | + if (data.admission) { |
| 186 | + $("admission").textContent = data.admission; |
| 187 | + } else { |
| 188 | + $("admission-group").remove(); |
| 189 | + } |
| 190 | + |
| 191 | + if (data.description) { |
| 192 | + $("description").innerHTML = DOMPurify.sanitize(data.description); |
| 193 | + } else { |
| 194 | + $("desc-divider").remove(); |
| 195 | + $("description").remove(); |
| 196 | + } |
| 197 | + |
| 198 | + content.replaceChildren(detail); |
| 199 | + } catch { |
| 200 | + content.innerHTML = |
| 201 | + "<p>Could not load event details. Please try again later.</p>"; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + close() { |
| 206 | + this._backdrop.style.display = "none"; |
| 207 | + document.documentElement.style.overflow = ""; |
| 208 | + document.removeEventListener("keydown", this._keyHandler); |
| 209 | + } |
| 210 | + }, |
| 211 | +); |
0 commit comments