summaryrefslogtreecommitdiffstats
path: root/web/script.js
blob: b3179b3d90659c948cef67c4bc90b2daea6d65c4 (about) (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const dialog = document.querySelector("dialog");
const closeButton = document.querySelector("dialog button");
closeButton.addEventListener("click", () => {
    dialog.close();
});

let map = L.map("map");

L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);

let layerGroup = L.layerGroup().addTo(map);

document.forms["gpx-upload-form"].addEventListener("submit", (event) => {
    event.preventDefault();
    const gpxFileInput = document.getElementById("gpx-file-input");
    if (gpxFileInput.files.length !== 1) {
        alert("Voor de upload moet er exact één GPX-bestand zijn geselecteerd");
        return;
    }
    load(gpxFileInput.files[0]);
});

function el(name, attrs, ...children) {
    const node = document.createElement(name);
    for (const [k, v] of Object.entries(attrs)) {
        node.setAttribute(k, v);
    }
    node.replaceChildren(...children);
    return node;
}

function txt(s) {
    return document.createTextNode(s);
}

function tbl(rowcols) {
    return el("table", {},
              el("tbody", {},
                 ...rowcols.map((cols) =>
                     el("tr", {},
                        ...cols.map((col) => el("td", {}, col)),
                       ))));
}

async function showProblem(rsp) {
    const problem = await rsp.json();

    document.getElementById("dialog-title").innerText = problem.title;
    if (problem.detail)
        document.getElementById("dialog-message").innerText = problem.detail;
    else
        document.getElementById("dialog-message").innerText = "";
    dialog.showModal();

    let rows = [
        [txt("HTTP-statuscode"),
         el("a", { "href": "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/" + rsp.status },
            txt(rsp.status + " (" + rsp.statusText + ")"))],
        [txt("Probleemtype"),
         el("code", {}, txt(problem.type))],
        [txt("Trace-ID"),
         el("code", {}, txt(rsp.headers.get("X-Routemon-Trace-Id")))],
    ];
    if (problem.instance) {
        rows.push([txt("Instantie"),
                   el("code", {}, txt(problem.instance))]);
    }
    document.getElementById("dialog-more-info").replaceChildren(tbl(rows));

    return;
}

async function getSysinfo() {
    const rsp = await fetch("http://localhost:8284/sysinfo", {
        method: "GET",
        headers: {
            "Accept-Language": "nl-NL",
        },
    });
    if (!rsp.ok) {
        await showProblem(rsp);
        return;
    }

    const res = await rsp.json();
    document.getElementById("situation-publication-of").innerText = new Date(res.using_publication_of).toLocaleString();
}

async function load(gpxFile) {
    const rsp = await fetch("http://localhost:8284/gpx", {
        method: "POST",
        headers: {
            "Accept-Language": "nl-NL",
        },
        body: gpxFile,
    });
    if (!rsp.ok) {
        await showProblem(rsp);
        return;
    }

    const res = await rsp.json();
    layerGroup.clearLayers();

    let bounds = null;
    for (const track of res.tracks) {
        for (const segment of track.segments) {
            const polyline = L.polyline(segment.points, { color: "blue" }).addTo(layerGroup);
            if (bounds === null) {
                bounds = polyline.getBounds();
            } else {
                bounds.extend(polyline.getBounds());
            }
        }
    }
    if (bounds !== null) {
        map.fitBounds(bounds);
    }

    res.relevant_situations.forEach((sit) => {
        let firstComment = sit.comments[0] || "";
        const lfIndex = firstComment.indexOf("\n");
        if (lfIndex !== -1) {
            firstComment = firstComment.substring(0, lfIndex);
        }
        const commentEl =
              el("div", {},
                 el("code", {}, txt(sit.id)),
                 txt(": " + firstComment));

        const markerLayer = L.marker(sit.location);
        markerLayer.addTo(layerGroup).bindPopup(commentEl);

        sit.relevant_road_closures.forEach((rc) => {
            rc.relevant_lss.forEach((ls) => {
                const lineLayer = L.polyline(ls, {
                    color: "purple",
                    dashArray: "5, 10",
                    dashOffset: "0",
                });
                lineLayer.addTo(layerGroup);
            });
        });
    });
}

getSysinfo();