-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-AvfallshentingOslo.js
153 lines (141 loc) · 4.48 KB
/
MMM-AvfallshentingOslo.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
Module.register("MMM-AvfallshentingOslo", {
defaults: {
address: "Maridalsveien 52",
dateFormat: "dddd Do MMM",
useHumanFormat: "by_week", // Accepts "strict" and "by_week"
showHeader: true,
updateSpeed: 1000,
refresh: 3600,
displayIcons: true,
displayWasteType: false,
exclusions: ["Restavfall"],
},
getScripts: function() {
return [ "moment.js" ];
},
getStyles: function () {
return [this.file("style.css")];
},
getTranslations: function() {
return {
nb: "translations/nb.json",
en: "translations/en.json"
};
},
start: function() {
var self = this;
moment.locale(config.language);
this.journeys = [];
this.getPickupDates();
setInterval( function(){
self.getPickupDates();
}, this.config.refresh*1000);
},
getPickupDates: function() {
Log.info(`Fetching pickup dates for ${this.config.address}`);
this.sendSocketNotification("GET_PICKUP_DATES", this.config.address);
},
getImage: function(wasteType) {
src = "/modules/MMM-AvfallshentingOslo/img/"
switch(wasteType) {
case 'Restavfall':
src += "restavfall.svg";
break;
case "Restavfall til forbrenning":
src += "brennbar-rest.jpg";
break;
case "Papir":
src += "papir.svg";
break;
}
let icon = document.createElement("img");
icon.src = src;
icon.className += ' icon';
return icon;
},
getDateString: function(date) {
const dateObj = moment(date);
const currentDate = moment();
const isToday = dateObj.isSame(currentDate, 'days');
const isTomorrow = dateObj.diff(currentDate.startOf('day'), 'days') == 1;
if(isToday) {
return this.translate("today");
} else if(isTomorrow) {
return this.translate("tomorrow");
} else if(this.config.useHumanFormat == "by_week") {
const weeksUntil = dateObj.week() - currentDate.week();
if(weeksUntil === 0) {
return dateObj.format("dddd");
} else if (weeksUntil === 1) {
return this.translate("next_dow", {day: dateObj.format("dddd")});
} else {
return dateObj.format(this.config.dateFormat);
}
} else if(this.config.useHumanFormat == "strict") {
let daysUntil = dateObj.diff(currentDate, "days");
if(daysUntil < 7) {
return dateObj.format("dddd");
} else if (daysUntil < 13) {
return this.translate("next_dow", {day: dateObj.format("dddd")});
} else {
return dateObj.format(this.config.dateFormat);
}
}
return dateObj.format(this.config.dateFormat);
},
getCell: function(cellText, className) {
let cell = document.createElement("td");
if (!!className) {
cell.className = className;
}
cell.innerHTML = cellText;
return cell;
},
getDom: function(){
let wrapper = document.createElement("div");
wrapper.className = `light bright`;
if (this.pickupDates && Object.keys(this.pickupDates).length > 0){
let table = document.createElement("table");
if (this.config.showHeader){
let hrow = document.createElement("div");
hrow.className = "light small align-right";
hrow.innerHTML = this.translate('waste_pickup_header')
wrapper.appendChild(hrow);
}
if(Object.keys(this.pickupDates).includes('error')) {
let row = document.createElement("tr");
row.appendChild(this.getCell(this.pickupDates.error));
table.appendChild(row);
} else {
for (const [wasteType, pickupDate] of Object.entries(this.pickupDates)){
let exclusions = this.config.exclusions.map( (excl) => { return excl.toLowerCase(); } );
if (exclusions.includes(wasteType.toLowerCase())){
continue;
}
let row = document.createElement("tr");
row.className += "medium";
if(this.config.displayIcons) {
let cell = document.createElement("td");
cell.appendChild(this.getImage(wasteType))
row.appendChild(cell);
}
if(this.config.displayWasteType) {
row.appendChild(this.getCell(wasteType, "align-left small wasteType light"));
}
row.appendChild(this.getCell(this.getDateString(pickupDate), 'date'));
table.appendChild(row);
}
}
wrapper.appendChild(table);
} else {
wrapper.innerHTML = this.translate("LOADING");
}
return wrapper;
},
socketNotificationReceived: function(message, payload){
if (message === "PICKUP_DATES"){
this.pickupDates = payload;
this.updateDom(this.config.updateSpeed);
}
}
});