-
Notifications
You must be signed in to change notification settings - Fork 31
/
MMM-JsonTable.js
155 lines (132 loc) · 3.92 KB
/
MMM-JsonTable.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
/* global Module moment */
Module.register("MMM-JsonTable", {
jsonData: null,
// Default module config.
defaults: {
url: "",
arrayName: null,
noDataText:
"Json data is not of type array! Maybe the config arrayName is not used and should be, or is configured wrong.",
keepColumns: [],
size: 0,
tryFormatDate: false,
updateInterval: 15000,
animationSpeed: 500,
descriptiveRow: null
},
start () {
this.getJson();
this.scheduleUpdate();
},
scheduleUpdate () {
const self = this;
setInterval(() => {
self.getJson();
}, this.config.updateInterval);
},
// Request node_helper to get json from url
getJson () {
this.sendSocketNotification("MMM-JsonTable_GET_JSON", this.config.url);
},
socketNotificationReceived (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(this.config.animationSpeed);
}
}
},
// Override dom generator.
getDom () {
const wrapper = document.createElement("div");
wrapper.className = "xsmall";
if (!this.jsonData) {
wrapper.innerHTML = "Awaiting json data...";
return wrapper;
}
const table = document.createElement("table");
const tbody = document.createElement("tbody");
let 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 = this.config.noDataText;
return wrapper;
}
items.forEach((element) => {
const row = this.getTableRow(element);
tbody.appendChild(row);
});
// Add in Descriptive Row Header
if (this.config.descriptiveRow) {
const header = table.createTHead();
header.innerHTML = this.config.descriptiveRow;
}
table.appendChild(tbody);
wrapper.appendChild(table);
return wrapper;
},
getTableRow (jsonObject) {
const row = document.createElement("tr");
Object.entries(jsonObject).forEach(([key, value]) => {
const cell = document.createElement("td");
let valueToDisplay = "";
let cellValue = "";
if (value.constructor === Object) {
if ("value" in value) {
cellValue = value.value;
} else {
cellValue = "";
}
if ("color" in value) {
cell.style.color = value.color;
}
} else {
cellValue = value;
}
if (key === "icon") {
cell.classList.add("fa", cellValue);
} else if (this.config.tryFormatDate) {
valueToDisplay = this.getFormattedValue(cellValue);
} else if (
this.config.keepColumns.length === 0 ||
this.config.keepColumns.indexOf(key) >= 0
) {
valueToDisplay = cellValue;
}
const cellText = document.createTextNode(valueToDisplay);
if (this.config.size > 0 && this.config.size < 9) {
const heading = document.createElement(`H${this.config.size}`);
heading.appendChild(cellText);
cell.appendChild(heading);
} else {
cell.appendChild(cellText);
}
row.appendChild(cell);
});
return row;
},
// Format a date string or return the input
getFormattedValue (input) {
const momentObj = moment(input);
if (typeof input === "string" && momentObj.isValid()) {
// Show a formatted time if it occures today
if (
momentObj.isSame(new Date(Date.now()), "day") &&
momentObj.hours() !== 0 &&
momentObj.minutes() !== 0 &&
momentObj.seconds() !== 0
) {
return momentObj.format("HH:mm:ss");
}
return momentObj.format("YYYY-MM-DD");
}
return input;
}
});