-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-TouchButton.js
328 lines (288 loc) · 11 KB
/
MMM-TouchButton.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* global Module
/* MagicMirror²
* Module: TouchButton
*
* By Tom Hirschberger
* MIT Licensed.
*/
Module.register('MMM-TouchButton', {
defaults: {
animationSpeed: 0,
classes: null,
buttons: [],
addEmptyTitle: false,
buttons: [],
refreshOnNotification: true,
refreshOnlyIfValueChanged: true,
notificationDelay: 3000,
notificationsAtStart: []
},
getScripts: function () {
return [this.file('node_modules/jsonpath-plus/dist/index-browser-umd.js'), this.file('node_modules/js-uuid/js-uuid.js'), this.file('node_modules/@iconify/iconify/dist/iconify.min.js')];
},
getStyles: function() {
return ['font-awesome.css', 'touch-button.css']
},
isAString: function(x) {
return Object.prototype.toString.call(x) === "[object String]"
},
validateCondition: function(source, value, type){
if (type == "eq"){
if ((typeof source === "number") || (this.isAString(source))){
return source == value
} else {
return JSON.stringify(source) === value
}
} else if (type == "incl"){
if (this.isAString(source)){
return source === value
} else {
return JSON.stringify(source).includes(value)
}
} else if (type == "mt") {
if (this.isAString(source)){
return new RegExp(value).test(source)
} else {
return new RegExp(value).test(JSON.stringify(source))
}
} else if (type == "lt"){
return source < value
} else if (type == "le"){
return source <= value
} else if (type == "gt"){
return source > value
} else if (type == "ge"){
return source >= value
}
return false
},
getCurrentButtonProps: function(buttonConfig, buttonResults){
const self = this
let icon = buttonConfig["icon"] || null
let imgIcon = buttonConfig["imgIcon"] || null
let classes = []
if (typeof buttonConfig["classes"] !== "undefined"){
classes = buttonConfig["classes"].split(" ")
}
if (typeof buttonConfig["conditions"] !== "undefined"){
for (let curCondition of buttonConfig["conditions"]){
let source = curCondition["source"] || null
let type = curCondition["type"] || null
let value = null
if (typeof curCondition["value"] !== "undefined"){
value = curCondition["value"]
}
let jsonpath = curCondition["jsonpath"] || null
if((source != null) && (type != null) && (value != null)){
let curSource
if(jsonpath != null){
curSource = buttonResults[source+jsonpath]
} else {
curSource = buttonResults[source]
}
if(curSource != null){
let valResult = self.validateCondition(curSource, value, type)
if(valResult){
icon = curCondition["icon"] || icon
imgIcon = curCondition["imgIcon"] || imgIcon
classes = []
if(typeof curCondition["classes"] !== "undefined"){
curCondition["classes"].split(" ").forEach(element => classes.push(element))
}
break
}
}
}
}
}
return [icon, imgIcon, classes]
},
getDom: function() {
const self = this
const wrapper = document.createElement('div')
let moduleClasses = []
if(self.config["classes"] != null){
self.config["classes"].split(" ").forEach(element => moduleClasses.push(element))
}
wrapper.classList.add("touchButtonRootWrapper")
moduleClasses.forEach(element => wrapper.classList.add(element))
for(let curId = 0; curId < self.config.buttons.length; curId++){
let curButtonConfig = self.config.buttons[curId]
if ((typeof curButtonConfig["profiles"] !== "undefined") && (self.currentProfile != null)){
if (!curButtonConfig["profiles"].includes(self.currentProfile)) {
continue
}
}
let curCondButtonConfig = self.getCurrentButtonProps(curButtonConfig, self.results[curId] || {})
var buttonWrapper = document.createElement("div")
buttonWrapper.className="touchButton buttonWrapper"
let curTitle = null
if ((typeof curButtonConfig.title === "undefined") ||
(curButtonConfig.title === null)){
if (self.config.addEmptyTitle){
curTitle = " "
}
} else {
curTitle = curButtonConfig.title
}
if (curTitle !== null){
let curTitleObj = document.createElement("div")
curTitleObj.className = "touchButton button title title-"+curButtonConfig.name
curTitleObj.innerHTML = curTitle
curCondButtonConfig[2].forEach(element => curTitleObj.classList.add(element))
buttonWrapper.appendChild(curTitleObj)
}
let curButton = null
if (curCondButtonConfig[1] != null ){
curButton = document.createElement("img")
curButton.setAttribute("src", curCondButtonConfig[1])
curButton.classList.add("imgIcon")
} else if (curCondButtonConfig[0] != null){
if(curCondButtonConfig[0].startsWith("fa ")){
curButton = document.createElement("i")
curButton.className = curCondButtonConfig[0]
curButton.setAttribute("aria-hidden", "true")
curButton.classList.add("icon")
} else {
curButton = document.createElement("span")
curButton.classList.add("iconify")
curButtonIconWrapper = document.createElement("span")
curButtonIconWrapper.classList.add("iconify-inline")
curButtonIconWrapper.setAttribute("data-icon", curCondButtonConfig[0])
curButton.appendChild(curButtonIconWrapper)
}
}
if(curButton != null){
curButton.classList.add("touchButton")
curButton.classList.add("button")
curButton.classList.add("button-"+curButtonConfig.name)
curCondButtonConfig[2].forEach(element => curButton.classList.add(element))
curButton.addEventListener("click", ()=>{ self.sendSocketNotification("BUTTON_PRESSED", {"moduleId": self.moduleId, "id": curId}) })
buttonWrapper.appendChild(curButton)
}
wrapper.appendChild(buttonWrapper)
}
return wrapper;
},
start: function () {
const self = this
self.moduleId = uuid.v4()
Log.info("Starting module: " + self.name);
self.sendSocketNotification('CONFIG', [self.moduleId, self.config])
self.results = {}
self.currentProfile = null
self.notifications = {}
self.watchNotifications = false
for(let curBtnId = 0; curBtnId < self.config.buttons.length; curBtnId++){
let curButtonConfig = self.config.buttons[curBtnId]
let curConditions = curButtonConfig["conditions"] || null
if (curConditions != null){
for (let curCondId = 0; curCondId < curConditions.length; curCondId++){
let curSource = curConditions[curCondId].source || null
if((curSource != null) && (curSource != "out") && (curSource != "err") && (curSource != "code")){
let curNotiObj = self.notifications[curSource] || []
let curResObj = {}
curResObj["id"] = curBtnId
curResObj["condition"] = curConditions[curCondId]
curNotiObj.push(curResObj)
self.notifications[curSource] = curNotiObj
self.watchNotifications = true
}
}
}
}
setTimeout(()=>{
for (let curNotiId = 0; curNotiId < self.config.notificationsAtStart.length; curNotiId++){
let curNotiObj = self.config.notificationsAtStart[curNotiId]
if (curNotiObj.length > 1){
self.sendNotification(curNotiObj[0], curNotiObj[1])
} else {
self.sendNotification(curNotiObj[0])
}
}
}, self.config.notificationDelay)
},
//https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string
tryParseJSONObject: function (jsonString) {
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
}
}
catch (e) { }
return false;
},
notificationReceived: function (notification, payload) {
const self = this
if (notification === "CHANGED_PROFILE") {
self.currentProfile = payload.to
self.updateDom(self.config.animationSpeed)
}
if(self.watchNotifications){
if (typeof self.notifications[notification] !== "undefined"){
let refreshNeeded = false
let curNotificationUsers = self.notifications[notification]
for(let curCondBtnIdx = 0; curCondBtnIdx < curNotificationUsers.length; curCondBtnIdx++){
let curId = curNotificationUsers[curCondBtnIdx].id
let curCondition = curNotificationUsers[curCondBtnIdx].condition
let curJsonpath = curCondition.jsonpath || null
let curResult
if (curJsonpath != null) {
let curParsedPayload = self.tryParseJSONObject(payload)
if(curParsedPayload){
curResult = JSONPath.JSONPath({ path: curJsonpath, json: curParsedPayload })[0];
} else {
curResult = payload
}
} else {
curResult = payload
}
let curResultExtendedNotification = notification
if (curJsonpath != null){
curResultExtendedNotification += curJsonpath
}
let oldResultObj = self.results[curId] || null
let oldResult = null
if(oldResultObj != null){
oldResult = oldResultObj[curResultExtendedNotification] || null
} else {
oldResultObj = {}
}
if (oldResult !== curResult) {
oldResultObj[curResultExtendedNotification] = curResult
self.results[curId] = oldResultObj
refreshNeeded = true
} else {
if (!self.config.refreshOnlyIfValueChanged) {
refreshNeeded = true
}
}
}
if(refreshNeeded){
self.updateDom(self.config.animationSpeed)
}
}
}
},
socketNotificationReceived: function (notification, payload) {
const self = this
if (self.moduleId === payload["moduleId"]){
if(notification === "SEND_NOTIFICATION"){
console.log(self.name+": Sending notification to all other modules")
if(typeof payload.payload !== "undefined"){
self.sendNotification(payload.notification, payload.payload)
} else {
self.sendNotification(payload.notification)
}
} else if (notification === "RESULT"){
self.results[payload.id] = payload
self.updateDom(self.config.animationSpeed)
}
}
},
})