summaryrefslogtreecommitdiffstats
path: root/VegaData/wwwroot/index.js
blob: 1a07ccb8038af2bfe7d6430cc850db72ec3c5528 (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
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
import { Temporal } from "temporal-polyfill";
import { gqp, sqp, t } from "./framework.js";

let _;

async function getShows() {
    if (_) return _;
    const response = await fetch("/shows");
    _ = await response.json();
    return _;
}

const ulShows = document.getElementById("ulShows");
const search = document.getElementById("search");
const renderShowsBtn = document.getElementById("renderShowsBtn");

renderShowsBtn.addEventListener("click", () => {
    renderShows("");
    search.value = "";
});

function dateString(date, small = false) {
    const instant = Temporal.Instant.from(`${date}Z`)
    const stringOptions = {
        weekday: "long",
        hour: "2-digit",
        timeZone: "UTC",
        minute: "2-digit",
        month: "long",
        calendar: "gregory",
        day: "numeric"
    }

    if (small) {
        return instant.toLocaleString("nb-NO", stringOptions);
    }

    return instant.toLocaleString("nb-NO", {
        year: "numeric",
        era: "long",
        ...stringOptions
    });
}

function copyLink(e) {
    if ("clipboard" in navigator) {
        navigator.clipboard.writeText(`${location.origin}/index.html#${urlId(e)}`)
    }
}

function vegascene(e) {
    if (e.movieVersion.startsWith("KUL")) return `https://www.vegascene.no/teater/${e.movieVersion}`
    return `https://www.vegascene.no/film/${e.movieVersion}`
}

if (gqp("q")) search.value = gqp("q")
search.addEventListener("input", e => renderShows(e.currentTarget.value));

function urlId(e) {
    return encodeURIComponent(`${e.movieVersion}-${e.startDateTime}`)
}

async function renderShows(query = search.value) {
    query = query?.trim();
    sqp({ q: query ?? "" })

    const shows = (await getShows()).reduce((acc, curr) => {
        const key = curr.title;
        if (!acc[key]) {
            acc[key] = [];
        }
        acc[key].push(curr);
        return acc;
    }, {});

    async function share(show) {
        const shareData = {
            title: `${show.title} ${dateString(show.startDateTime, true)} på vega`,
            url: urlId(show),
        };
        await navigator.share(shareData);
    }

    const lis = [];

    for (const showKey of Object.keys(shows).sort((a, b) => a.localeCompare(b))) {
        const times = shows[showKey].sort((a, b) => Temporal.PlainDate.compare(Temporal.PlainDate.from(a.startDateTime), Temporal.PlainDate.from(b.startDateTime)))
        if (query) {
            const words = [showKey.toLowerCase()]
            for (const time of times) {
                words.push(dateString(time.startDateTime))
                words.push(time.scene)
                words.push(time.tags.join(" "))
            }
            if (!words.some((word) => word.match(query.toLowerCase()))) continue;
        }

        lis.push(
            t("li", { class: "show", id: showKey }, [
                t("span", { class: "title italic" }, showKey),
                t("ul", undefined, [
                    t("li", undefined,
                        times.filter(e => e.ticketUrl !== "").map(e => {
                            const tagLine = `${e.scene} - ${[e.type, ...e.tags].join(", ")}`
                            return t("li", { class: `time time-${e.id}`, id: urlId(e) }, [
                                t("div", undefined, [
                                    t("span", { title: e.startDateTime }, dateString(e.startDateTime)),
                                    t("span", undefined, tagLine),
                                    t("div", { class: "actions" }, [
                                        t("a", { href: e.ticketUrl }, "Billetter"),
                                        "share" in navigator ? t("button", { onclick: () => share(e) }, "Del tid") : null,
                                        t("button", { onclick: () => copyLink(e) }, "Kopier lenke"),
                                        t("a", { href: vegascene(e) }, "Åpne på vegascene.no")
                                    ])
                                ])
                            ])
                        }))
                ])
            ])
        )
    }

    if (!lis.length) ulShows.replaceChildren(...[t("i", undefined, "Dessverre")])
    else ulShows.replaceChildren(...lis);
}

renderShows();