-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-TouchNotifications.js
274 lines (239 loc) · 7.37 KB
/
MMM-TouchNotifications.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* global Module */
/* Magic Mirror
* Module: MMM-TouchNotifications
*
* By Brian Janssen
* MIT Licensed.
*/
Module.register("MMM-TouchNotifications", {
// Default module config.
defaults: {
// Determines if the border around the buttons should be shown.
showBorder: true,
// The minimum width for all the buttons.
minWidth: "0px",
// The minimum height for all the buttons.
minHeight: "0px",
// The location of the symbol relative to the text.
picturePlacement: "left",
// The direction of the menu.
direction: "row",
// All the different buttons in the menu.
buttons: [{
// this is a button with only a single notification
text: "Example 1",
symbol: "ban",
notification: {
type: "SHOW_ALERT",
payload: {
type: "notification",
title: "Example 1",
message: "Hello World!"
}
}
}, {
// this is a button with multiple notifications
text: "Example 2",
notification: [{
// the first notification
type: "SHOW_ALERT",
payload: {
type: "notification",
title: "Example 2",
message: "Hello"
}
}, {
// the second notification
type: "SHOW_ALERT",
payload: {
type: "notification",
title: "Example 2",
message: "World!"
}
}]
}]
},
// Define required styles.
getStyles: function() {
return ["font-awesome.css", this.file("MMM-TouchNotifications.css")];
},
start: function() {
this.config.picturePlacement = {
"right": "row-reverse",
"left": "row",
"top": "column",
"bottom": "column-reverse"
}[this.config.picturePlacement];
for (var i = 0; i < this.config.buttons.length; i++) {
if (typeof this.config.buttons[i].notification === "string" && this.config.buttons[i].select === undefined) {
this.config.buttons[i].select = this.config.buttons[i].notification;
}
}
},
notificationReceived: function(notification, payload, sender) {
var event;
if (window.CustomEvent) {
event = new CustomEvent("touch-notification", {
detail: {
"notification": notification,
"payload": payload,
"sender": sender
}
});
} else {
event = document.createEvent("CustomEvent");
event.initCustomEvent("touch-notification", true, true, {
"notification": notification,
"payload": payload,
"sender": sender
});
}
var element = document.getElementById(this.identifier + "_menu");
if (element) {
// element.dispatchEvent(event); argh events don't pass down to children
element.notification(event);
}
},
// Override dom generator.
getDom: function() {
var menu = document.createElement("span");
menu.className = "notification-menu";
menu.id = this.identifier + "_menu";
menu.style.flexDirection = this.config.direction;
for (var index in this.config.buttons) {
menu.appendChild(this.createButton(this, index, this.config.buttons[index]));
}
menu.notification = function(event) {
var childs = menu.children;
for (var i = 0; i < childs.length; i++) {
childs[i].dispatchEvent(event);
}
};
return menu;
},
createButton: function(self, index, data) {
var item = document.createElement("span");
item.id = self.identifier + "_button_" + index;
item.className = "notification-button";
item.style.minWidth = self.config.minWidth;
item.style.minHeight = self.config.minHeight;
item.style.flexDirection = self.config.picturePlacement;
if (!self.config.showBorder) {
item.style.borderColor = "black";
}
if (data.notification) {
self.buttonClick(self, item, data.notification);
}
if (data.select) {
self.buttonEvent(self, item, data.select);
}
self.buttonPicture(self, item, data);
self.buttonText(self, item, data);
return item;
},
buttonPicture: function(self, button, data) {
if (data.symbol) {
var symbol = document.createElement("span");
symbol.className = "notification-picture fa fa-" + data.symbol;
if (data.size) {
symbol.className += " fa-" + data.size;
symbol.className += data.size == 1 ? "g" : "x";
}
if (data.text && self.config.picturePlacement === "row") { // row = left
symbol.style.marginRight = "10px";
}
button.appendChild(symbol);
} else if (data.img) {
var image = document.createElement("img");
image.className = "notification-picture";
image.src = data.img;
if (data.width) image.width = data.width;
if (data.height) image.height = data.height;
if (data.text && self.config.picturePlacement === "row") { // row = left
image.style.marginRight = "10px";
}
button.appendChild(image);
}
},
buttonText: function(self, button, data) {
if (data.text) {
var text = document.createElement("span");
text.className = "notification-text";
text.innerHTML = data.text;
if ((data.symbol || data.img) && self.config.picturePlacement === "row-reverse") { // right = row-reverse
text.style.marginRight = "10px";
}
button.appendChild(text);
}
},
buttonClick: function(self, button, notification) {
if (typeof notification !== "string") {
if (notification.type && (notification.payload || notification.payload == 0)) {
button.addEventListener("click", function() {
self.sendNotification(notification.type, notification.payload);
});
} else {
button.addEventListener("click", function() {
for (var index in notification) {
self.buttonClick(self, button, notification[index]);
}
});
}
return;
}
var data = notification.split(" ");
switch (data[0]) {
case "profile":
button.addEventListener("click", function() {
self.sendNotification("CURRENT_PROFILE", data[1]);
});
break;
case "show":
case "hide":
// TODO
break;
}
},
buttonEvent: function(self, button, select) {
if (typeof select === "function") {
button.addEventListener("touch-notification", function(e) {
var result = select(e.detail.notification, e.detail.payload,
button.className === "notification-button selected", e.detail.sender);
if (result < 0) {
button.className = "notification-button";
} else if (result > 0) {
button.className = "notification-button selected";
}
});
return;
}
var data = select.split(" ");
switch (data[0]) {
case "profile":
self.buttonEvent(self, button, function(type, payload) {
if (type === "CHANGED_PROFILE") {
if (payload.to === data[1]) {
return 1;
} else {
return -1;
}
}
return 0;
});
break;
case "click":
button.addEventListener("click", function() {
if (button.className === "notification-button selected") {
button.className = "notification-button";
} else {
button.className = "notification-button selected";
}
});
break;
case "show":
case "hide":
// TODO
break;
}
}
});