-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMMM-Transloc.js
More file actions
128 lines (108 loc) · 2.85 KB
/
MMM-Transloc.js
File metadata and controls
128 lines (108 loc) · 2.85 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
127
128
'use strict';
Module.register("MMM-Transloc", {
jsonData: null,
header: {},
// Default module config.
defaults: {
url: "",
arrayName: null,
tryFormatDate: false,
updateInterval: 15000,
stop_id: 4093250,
AgencyNum: 116,
Key: ""
},
start: function () {
this.sendSocketNotification("MMM-Transloc-start", this.config);
this.getJson();
this.scheduleUpdate();
},
scheduleUpdate: function () {
var self = this;
setInterval(function () {
self.getJson();
}, this.config.updateInterval);
},
// Request node_helper to get json from url
getJson: function () {
this.sendSocketNotification("MMM-JsonTable_GET_JSON", this.config);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-JsonTable_JSON_RESULT") {
// Only continue if the notification came from the request we made
// This way we can load the module more than once
if (payload.url === this.config.url)
{
this.jsonData = payload.data;
this.updateDom(500);
}
}
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
wrapper.className = "xsmall";
if (!this.jsonData) {
wrapper.innerHTML = "Awaiting json data...";
return wrapper;
}
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var items = [];
if (this.config.arrayName) {
items = this.jsonData[this.config.arrayName];
}
else {
items = this.jsonData;
}
// Check if items is of type array
if (!(items instanceof Array)) {
wrapper.innerHTML = "Json data is not of type array! " +
"Maybe the config arrayName is not used and should be, or is configured wrong";
return wrapper;
}
items.forEach(element => {
var row = this.getTableRow(element);
tbody.appendChild(row);
});
table.appendChild(tbody);
wrapper.appendChild(table);
return wrapper;
},
getTableRow: function (jsonObject) {
var row = document.createElement("tr");
for (var key in jsonObject) {
var cell = document.createElement("td");
var valueToDisplay = "";
if (key === "icon") {
cell.classList.add("fa", jsonObject[key]);
}
else if (this.config.tryFormatDate) {
valueToDisplay = this.getFormattedValue(jsonObject[key]);
}
else {
valueToDisplay = jsonObject[key];
}
var cellText = document.createTextNode(valueToDisplay);
cell.appendChild(cellText);
row.appendChild(cell);
}
return row;
},
// Format a date string or return the input
getFormattedValue: function (input) {
var m = moment(input);
if (typeof input === "string" && m.isValid()) {
// Show a formatted time if it occures today
if (m.isSame(new Date(), "day") && m.hours() !== 0 && m.minutes() !== 0 && m.seconds() !== 0) {
return m.format("HH:mm:ss");
}
else {
return m.format("YYYY-MM-DD");
}
}
else {
return input;
}
}
});