-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
126 lines (116 loc) · 3.08 KB
/
Copy pathpopup.js
File metadata and controls
126 lines (116 loc) · 3.08 KB
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
const config = new Config({ stops: "T4" }, "folichrome");
let data = {};
let error = null;
let showSettings = false;
function parseStops() {
return (config.data.stops || "")
.split(/[^a-z0-9]+/gi)
.filter(s => s.length)
.sort();
}
function refresh() {
const stops = parseStops();
if (!stops.length) {
data = {};
return Promise.resolve(true);
}
const newData = {};
const promises = stops.map(stop =>
fetch(`http://data.foli.fi/siri/sm/${stop}`)
.then(r => r.json())
.then(stopData => {
newData[stop] = stopData;
})
);
return Promise.all(promises)
.then(() => {
data = newData;
error = null;
m.redraw();
})
.catch(err => {
data = {};
error = err;
m.redraw();
});
}
function formatLine(i) {
const d = new Date(i.expecteddeparturetime * 1000);
const minutesToDeparture = (d - +new Date()) / 1000 / 60;
if (minutesToDeparture < 0) return null;
const sel = minutesToDeparture < 5 ? "tr.soon" : "tr";
// prettier-ignore
return m(sel, [
m("td.stop", i.stop),
m("td.line", i.lineref),
m("td.dest", i.destinationdisplay),
m("td.deptime", `${d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`),
m("td.mintodep", `\u25BB ${minutesToDeparture.toFixed(0)} min`),
]);
}
function departureComparer(a, b) {
if (a.stop !== b.stop) return a.stop < b.stop ? -1 : +1;
return a.expecteddeparturetime - b.expecteddeparturetime;
}
class FoliChrome {
oninit(v) {
config.load().then(() => {
// Chrome gets confused when the layout of the popup changes too quickly.
setTimeout(refresh, 150);
});
setInterval(refresh, 60000);
setInterval(() => {
if (!showSettings) m.redraw();
}, 15000);
}
renderRegular() {
const allInfo = Object.keys(data)
.reduce((acc, stop) => acc.concat(data[stop].result.map(datum => Object.assign({ stop }, datum))), [])
.sort(departureComparer);
const departureTable = allInfo.length
? m("table.departures", m("tbody", allInfo.map(formatLine)))
: m("div.nodep", "No departures to show");
const settingsBar = m(
"div.bar",
m(
"button",
{
onclick() {
showSettings = true;
}
},
"Settings"
)
);
return m("div", settingsBar, departureTable);
}
renderSettings() {
return m("div.settings", [
m("label", "Which stops do you want to show?"),
m("input", {
placeholder: "T4, 301",
value: config.data.stops,
oninput: m.withAttr("value", v => {
config.data.stops = v;
})
}),
m(
"button",
{
onclick() {
config.data.stops = parseStops(config.data.stops).join(", ");
config.save();
showSettings = false;
refresh();
}
},
"Save"
)
]);
}
view() {
const body = showSettings ? this.renderSettings() : this.renderRegular();
return body;
}
}
document.addEventListener("DOMContentLoaded", () => m.mount(document.body, FoliChrome));