forked from htilburgs/MMM-MyGarbage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-MyGarbage.js
178 lines (151 loc) · 5.37 KB
/
MMM-MyGarbage.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
Module.register('MMM-MyGarbage', {
// Default values
defaults: {
alert: false,
weeksToDisplay: 2,
limitTo: 99,
dateFormat: "dddd D MMMM",
fade: true,
fadePoint: 0.25 // Start on 1/4th of the list.
},
// Define stylesheet
getStyles: function () {
return ["MMM-MyGarbage.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
// Define required translations.
getTranslations: function () {
return {
en: "translations/en.json",
nl: "translations/nl.json",
de: "translations/de.json",
sv: "translations/sv.json"
}
},
capFirst: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
start: function () {
Log.info('Starting module: ' + this.name);
this.sendSocketNotification('MMM-MYGARBAGE-CONFIG', this.config);
this.nextPickups = [];
this.getPickups();
this.timer = null;
},
// Read garbage_schedule.csv file
getPickups: function () {
clearTimeout(this.timer);
this.timer = null;
this.sendSocketNotification("MMM-MYGARBAGE-GET", { weeksToDisplay: this.config.weeksToDisplay, instanceId: this.identifier });
//Set check times
var self = this;
this.timer = setTimeout(function () {
self.getPickups();
}, 60 * 60 * 1000); //update once an hour
},
socketNotificationReceived: function (notification, payload) {
if (notification == "MMM-MYGARBAGE-RESPONSE" + this.identifier && payload.length > 0) {
this.nextPickups = payload;
this.updateDom(1000);
} else if (notification == "MMM-MYGARBAGE-NOENTRIES") { //Pass Alert on
this.sendNotification("SHOW_ALERT", {
title: this.translate("GARBAGEENTRIESLEFT", { entriesLeft: payload }),
message: this.translate("REMEMBERADDINGPICKUPS"),
imageFA: "recycle",
timer: "3000"
});
}
},
// Create Garbage Icons from garbage_icons.svg
svgIconFactory: function (color) {
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttributeNS(null, "class", "garbage-icon");
//Switch for Legacy files
switch (color) {
case 'GreenBin':
svg.setAttributeNS(null, "style", "fill: #00A651");
break;
case 'GarbageBin':
svg.setAttributeNS(null, "style", "fill: #787878");
break;
case 'PaperBin':
svg.setAttributeNS(null, "style", "fill: #0059ff");
break;
default:
svg.setAttributeNS(null, "style", "fill: " + color);
break;
}
// Set icon id
var iconId = ( color == 'blue' ) ? 'bag_icon.svg#bag' : 'garbage_icons.svg#bin';
var use = document.createElementNS('http://www.w3.org/2000/svg', "use");
use.setAttributeNS("http://www.w3.org/1999/xlink", "href", this.file(iconId));
svg.appendChild(use);
return (svg);
},
getDom: function () {
var wrapper = document.createElement("div");
if (this.nextPickups.length == 0) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
// Start Fade effect
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startFade = Math.min(this.nextPickups.length, this.config.limitTo) * this.config.fadePoint;
var fadeSteps = Math.min(this.nextPickups.length, this.config.limitTo) - startFade;
}
var currentFadeStep = 0;
// End Fade effect
// this.nextPickups.forEach( function(pickup) {
for (i = 0; i < this.nextPickups.length; i++) {
if (i == this.config.limitTo) {
break;
}
var pickup = this.nextPickups[i];
//Create CSS Elements
var pickupContainer = document.createElement("div");
pickupContainer.classList.add("garbage-container");
//Add date to Garbage Pickup
var dateContainer = document.createElement("span");
dateContainer.classList.add("garbage-date");
//Formats Garbage Pickup Date
moment.locale();
var today = moment().startOf("day");
var pickUpDate = moment(pickup.pickupDate);
if (today.isSame(pickUpDate)) {
dateContainer.innerHTML = this.translate("TODAY");
} else if (moment(today).add(1, "days").isSame(pickUpDate)) {
dateContainer.innerHTML = this.translate("TOMORROW");
} else if (moment(today).add(7, "days").isAfter(pickUpDate)) {
dateContainer.innerHTML = this.capFirst(pickUpDate.format("dddd"));
} else {
dateContainer.innerHTML = this.capFirst(pickUpDate.format(this.config.dateFormat));
}
pickupContainer.appendChild(dateContainer);
//Add Garbage icons
var iconContainer = document.createElement("span");
iconContainer.classList.add("garbage-icon-container");
for (var key in pickup) {
//Convert date strings to moment.js Date objects
if (key != "pickupDate" && key != "WeekStarting")
if (pickup[key])
iconContainer.appendChild(this.svgIconFactory(key)); //TODO COLORS
}
pickupContainer.appendChild(iconContainer);
wrapper.appendChild(pickupContainer);
// Start Fading
if (i >= startFade) { //fading
currentFadeStep = i - startFade;
pickupContainer.style.opacity = 1 - (1 / fadeSteps * currentFadeStep);
}
// End Fading
};
return wrapper;
}
});