aboutsummaryrefslogtreecommitdiff
path: root/static/user/user-additions.js
blob: 6d944b86c65c15891f3085b99fcc4f9386aa87df (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
window.formatters.set('description', (el, d) => {
    if (/<br\/?>/.exec(d)) {
        /* Assume that the text is HTML iff it contains a <br/> tag */
        let parser = new DOMParser();
        let doc = parser.parseFromString(d, 'text/html');
        el.replaceChildren(doc.body);
    } else {
        /* Otherwise it should be plain(er) text, parse "all" links
           (and reserved XML characters)
        */
        // TODO replace with something that doesn't use innerHTML */
        el.innerHTML = d
            .replaceAll(/</g, '&lt;')
            .replaceAll(/>/g, '&gt;')
            .replaceAll(/&/g, '&amp;')
            .replaceAll(/'/g, '&apos;')
            .replaceAll(/"/g, '&quot;')
            .replaceAll(/https?:\/\/\S+/g, '<a href="$&">$&</a>')
    }
})

/* This location formatter is generally not for general use.
   It holds a small lookup table of "all" locations at Linköping University,
   and makes location names from their calendar system clickable.

   To obtain salar.json, run scripts/fetch-liu-map-index.scm from calps source tree.
*/

window.salar = new Promise((resolve, reject) =>
    fetch('/static/user/salar.json')
    .then(d => d.json())
    .then(d => resolve(d)))


window.formatters.set('location', async function(el, d) {
    let rx = /Lokal: (.*)/
    let m = rx.exec(d)
    if (! m) {
        el.textContent = d;
        return;
    }

    let salar = await window.salar;

    let name = m[1]
    let frag = salar[name];
    if (frag) {
        let anch = document.createElement('a');
        anch.href = `https://old.liu.se/karta/${frag}`
        anch.target = '_blank'
        anch.textContent = name;
        el.append('Lokal: ');
        el.append(anch);
    } else {
        el.textContent = `Lokal: ${name}`
    }
})