-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MMM-json.js
183 lines (159 loc) · 5.08 KB
/
MMM-json.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Magic Mirror module for displaying JSON values
* By Daniel Habenicht
* MIT Licensed
*/
Module.register("MMM-json", {
// Default module config.
defaults: {
url: "https://jsonplaceholder.typicode.com/users",
refreshInterval: 1000 * 60 * 5, // 5 minutes
styleRules: []
},
start: function () {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.getData();
var self = this;
// Schedule updates
setInterval(function () {
self.getData();
self.updateDom();
}, this.config.refreshInterval);
},
// Import additional CSS Styles
getStyles: function () {
return ["mmm-json.css"];
},
// Contact node helper for data
getData: function () {
Log.info("MMM-json: getting data");
this.sendSocketNotification("MMM_JSON_REQUEST", {
config: this.config,
identifier: this.identifier
});
},
// Handle node helper response
socketNotificationReceived: function (notification, payload) {
if (
notification === "MMM_JSON_RESPONSE" &&
payload.identifier === this.identifier
) {
if (payload.error === true) {
console.error(
"MMM-JSON: An Error occured while fetching your response. Please have a look at the server log."
);
this.loaded = false;
} else {
this.loaded = true;
this.response = payload.data;
}
this.updateDom(1000);
}
},
// Override the Header generator
getHeader: function () {
// If an Icon should be displayed we need our own header
if (this.config.headerIcon) {
return "";
} else {
return this.data.header || "";
}
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
if (this.config.url == null || this.config.url === "") {
wrapper.innerHTML = "Missing configuration.";
return wrapper;
}
// Display loading while waiting for API response
if (!this.loaded) {
wrapper.innerHTML = "Loading...";
return wrapper;
}
var tb = document.createElement("table");
// Display our own header if we want an icon
if (this.config.headerIcon) {
var imgDiv = document.createElement("div");
var sTitle = document.createElement("p");
sTitle.innerHTML = this.data.header || "";
sTitle.className += "normal";
var icon = document.createElement("i");
icon.className = "fas " + this.config.headerIcon;
sTitle.style = "margin: 0px 0px 0px 15px;";
imgDiv.appendChild(icon);
imgDiv.appendChild(sTitle);
var divider = document.createElement("hr");
divider.className += "dimmed";
wrapper.appendChild(imgDiv);
wrapper.appendChild(divider);
}
for (var i = 0; i < this.response.length; i++) {
var row = document.createElement("tr");
var keyTd = document.createElement("td");
keyTd.className = "small light bright";
if (this.response[i].icon) {
var icon = document.createElement("i");
icon.className = this.response[i].icon;
keyTd.appendChild(icon);
}
var titleSpan = document.createElement("span");
if (this.response[i].title) {
titleSpan.className = "small regular bright";
titleSpan.innerHTML =
(this.response[i].title ? this.response[i].title : "") + ":";
}
keyTd.appendChild(titleSpan);
row.appendChild(keyTd);
for (var j = 0; j < this.response[i].value.length; j++) {
var dataTr = document.createElement("td");
// Add Prefix
if (
this.response[i].prefix != null &&
(!Array.isArray(this.response[i].prefix) ||
this.response[i].prefix[j])
) {
dataTr.innerHTML += Array.isArray(this.response[i].prefix)
? this.response[i].prefix[j]
: this.response[i].prefix;
dataTr.innerHTML += " ";
}
// Add Value
dataTr.innerHTML += this.response[i].value[j];
// Add Suffix
if (
this.response[i].suffix != null &&
(!Array.isArray(this.response[i].suffix) ||
this.response[i].suffix[j])
) {
dataTr.innerHTML += " ";
dataTr.innerHTML += Array.isArray(this.response[i].suffix)
? this.response[i].suffix[j]
: this.response[i].suffix;
}
// Add Matching Styles
if (
this.config.styleRules != null &&
this.config.styleRules.length > 0
) {
for (var k = 0; k < this.config.styleRules.length; k++) {
if (this.config.styleRules[k].match(this.response[i].value[j])) {
if (this.config.styleRules[k].style != null) {
dataTr.style = this.config.styleRules[k].style;
}
if (this.config.styleRules[k].class != null) {
dataTr.className += " " + this.config.styleRules[k].class;
}
}
}
}
dataTr.className = "small light bright";
row.appendChild(dataTr);
}
tb.appendChild(row);
}
wrapper.appendChild(tb);
return wrapper;
}
});