-
Notifications
You must be signed in to change notification settings - Fork 10
/
MMM-Buttons.js
127 lines (107 loc) · 3.34 KB
/
MMM-Buttons.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
/* global Module */
/* Magic Mirror
* Module: Buttons
*
* By Joseph Bethge
* MIT Licensed.
*/
Module.register("MMM-Buttons", {
requiresVersion: "2.1.0",
// Default module config.
defaults: {
buttons: [],
minShortPressTime: 0,
maxShortPressTime: 500,
minLongPressTime: 3000,
bounceTimeout: 300
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.sendConfig();
this.intervals = [];
this.alerts = [];
for (var i = 0; i < this.config.buttons.length; i++)
{
this.intervals.push(undefined);
this.alerts.push(false);
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
return wrapper;
},
/* sendConfig()
* intialize backend
*/
sendConfig: function() {
this.sendSocketNotification("BUTTON_CONFIG", {
config: this.config
});
},
buttonUp: function(index, duration) {
if (this.alerts[index]) {
// alert already shown, clear interval to update it and hide it
if (this.intervals[index] !== undefined) {
clearInterval(this.intervals[index]);
}
this.alerts[index] = false;
this.sendNotification("HIDE_ALERT");
} else {
// no alert shown, clear time out for showing it
if (this.intervals[index] !== undefined) {
clearTimeout(this.intervals[index]);
}
}
this.intervals[index] = undefined;
var min = this.config.minShortPressTime;
var max = this.config.maxShortPressTime;
var shortPress = this.config.buttons[index].shortPress
var longPress = this.config.buttons[index].longPress
if (shortPress && min <= duration && duration <= max)
{
this.sendAction(shortPress);
}
min = this.config.minLongPressTime;
if (longPress && min <= duration)
{
this.sendAction(longPress);
}
},
sendAction: function(description) {
this.sendNotification(description.notification, description.payload);
},
buttonDown: function(index) {
var self = this;
if (self.config.buttons[index].longPress && self.config.buttons[index].longPress.title)
{
this.intervals[index] = setTimeout(function () {
self.startAlert(index);
}, this.config.maxShortPressTime);
}
},
showAlert: function (index) {
// display the message
this.sendNotification("SHOW_ALERT", {
title: this.config.buttons[index].longPress.title,
message: this.config.buttons[index].longPress.message,
imageFA: this.config.buttons[index].longPress.imageFA
});
},
startAlert: function(index) {
this.alerts[index] = true;
this.showAlert(index);
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "BUTTON_UP")
{
this.buttonUp(payload.index, payload.duration);
}
if (notification === "BUTTON_DOWN")
{
this.buttonDown(payload.index);
}
},
});