-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-TFL.js
94 lines (81 loc) · 2.42 KB
/
MMM-TFL.js
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
Module.register("MMM-TFL", {
// Default module config.
defaults: {
lines: "all",
modes: ["tube", "overground", "tram"],
updateTime: 600000,
app_id: null,
app_key: null
},
start: function() {
var self = this;
this.map = null;
setInterval(function() {
self.updateDom();
}, this.config.updateTime);
},
getDom: function() {
return this.fetchTubeStatus();
},
fetchTubeStatus: function() {
const ul = document.createElement("ul");
ul.setAttribute("class", "MMM-TFL");
const lines = this.config.lines;
let url = "https://api.tfl.gov.uk/line/mode/";
this.config.modes.forEach(mode => {
url += `${mode},`;
});
url += "/status";
if (this.config.app_id && this.config.app_key) {
url += `?app_id=${this.config.app_id}&app_key=${this.config.app_key}`;
}
try {
fetch(url /*, {}*/)
.then(result => result.json())
.then(result => {
if (lines !== "all") {
result = result.filter(line => lines.includes(line.id));
}
result.forEach(line => {
const li = document.createElement("li");
const lineName = document.createElement("span");
lineName.innerHTML = line.name;
lineName.setAttribute("class", line.id);
lineName.classList.add("line");
if (line.modeName === "national-rail") {
lineName.classList.add("national-rail");
}
const status = document.createElement("span");
status.innerHTML = this.getLineStatusText(line);
status.setAttribute("class", this.getLineStatusClass(line));
status.classList.add("status");
status.classList.add("status-problem");
li.appendChild(lineName);
li.appendChild(status);
ul.appendChild(li);
});
});
this.map = ul;
return ul;
} catch (err) {
console.log(err);
return this.map;
}
},
getStyles: function() {
return ["MMM-TFL.css"];
},
getLineStatusText: function(line) {
let text = "";
line.lineStatuses.forEach(status => {
text += status.statusSeverityDescription;
text += "\n";
});
return `${line.lineStatuses[0].statusSeverityDescription}`;
},
getLineStatusClass: function(line) {
return line.lineStatuses[0].statusSeverityDescription
.toLowerCase()
.replace(" ", "-");
}
});