-
Notifications
You must be signed in to change notification settings - Fork 5
/
MMM-Navigate.js
167 lines (143 loc) · 6.76 KB
/
MMM-Navigate.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
//MMM-Navigate.js:
var locked = false;
var vconfirm = 0;
Module.register("MMM-Navigate",{
// Default module config.
defaults: {
Alias: [ 'Seite vorwärts','Seite zurück'],
Action: [{type: "notification", title: 'Good morning!'},{type: "notification", title: 'Good morning!'}],
GPIOPins: [26,20,19]//rotary cw, rotary ccw, rotary press (BCM Numbering)
},
getStyles: function() {
return [
this.file('MMM-Navigate.css'), //load css
];
},
sendAction: function(description) {
this.show(0,{force: true});
if((description.notification == "SHELLCOMMAND") && (vconfirm==0)){
vconfirm = 1;
this.sendNotification("SHOW_ALERT",{type:"notification",message:"Ausführen von SHELLCOMMAND "+ description.payload +" bitte durch 2.Klick bestätigen"});
}else if((description.notification == "SHELLCOMMAND") && (vconfirm==1)){
vconfirm = 0;
this.sendSocketNotification(description.notification, description.payload);
}else{
vconfirm = 0;
this.sendNotification(description.notification, description.payload);
}
this.hide(10000);
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.sendConfig();//pass config to node_helper.js
},
// Override dom generator.
getDom: function() {
//Div for loading
if (this.loading) {
var loading = document.createElement("div");
loading.innerHTML = this.translate("LOADING");
loading.className = "dimmed light small";
wrapper.appendChild(loading);
return wrapper;
}
var self = this;//makes variables usable in functions
//Div after loading
var parent = document.createElement("div");
parent.className = "xsmall bright";
//build navigation from array
for (let index = 0; index < this.config.Action.length; index++) {
var naviItem = document.createElement("li");
var link = document.createElement('a');
link.setAttribute('href', '');
link.innerHTML = this.config.Alias[index];
naviItem.setAttribute('id', index);
if(index==0){//first li gets class="selected"
naviItem.setAttribute('class', 'selected');
}
naviItem.appendChild(link);
parent.appendChild(naviItem);
}
return parent;
},
sendConfig: function() {
this.sendSocketNotification("BUTTON_CONFIG", {
config: this.config
});
},
naviaction: function(payload){
var self = this;
if(payload.inputtype === 'CW' || payload.inputtype === 'CCW' || payload.inputtype === 'PRESSED'){
navigationmove(payload.inputtype);
}
function fselectedid(){//get ID and return it
for (let index = 0; index < self.config.Action.length; index++) {
var test = document.getElementsByTagName('li')[index].getAttribute('class');
if(test=='selected' || test=='selected locked' || test=='selected locked fa-lock1'){//axled lock icon
var selectedid = document.getElementsByTagName('li')[index].getAttribute('id');
return selectedid;
}
}
}
function navigationmove(input){
self.show(0);
selectedid = fselectedid();
if(input==='CW' || input==='CCW'){
vconfirm = 0;
if(input==='CW'){
navistep = 1;
actionstep = 0;
}else if(input==='CCW'){
navistep = -1;
actionstep = 1;
}
if(locked==true){
self.sendAction(self.config.Action[selectedid][parseInt(actionstep)]);
}else if(locked==false){
document.getElementsByTagName('li')[selectedid].setAttribute('class', '');//CW&CCW
if(selectedid==0 && input==='CW'){//mark next row
document.getElementsByTagName('li')[parseInt(selectedid)+1].setAttribute('class', 'selected');//CW
}else if(selectedid==0 && input==='CCW'){//mark last row
document.getElementsByTagName('li')[self.config.Action.length-1].setAttribute('class', 'selected');//CCW
}else if(selectedid==self.config.Action.length-1 && input==='CW'){//mark first row
document.getElementsByTagName('li')[0].setAttribute('class', 'selected');//CW
}else if(selectedid==self.config.Action.length-1 && input==='CCW'){//mark prev row
document.getElementsByTagName('li')[parseInt(selectedid)-1].setAttribute('class', 'selected');//CCW
}else{//mark next one in selected direction
document.getElementsByTagName('li')[parseInt(selectedid)+navistep].setAttribute('class', 'selected');
}
}
}else if(input === 'PRESSED'){
if(locked==false){//Menu not locked so ... (see below)
if(Array.isArray(self.config.Action[selectedid])){//if selected entry Action is array - lock it
locked = true;
document.getElementsByTagName('li')[selectedid].setAttribute('class', 'selected locked fa-lock1');//axled lock icon
}else{//if selected entry Action is object - so there is nothing to lock - execute it
self.show(0,{force: true});
self.sendAction(self.config.Action[selectedid]);
}
}else{//Menu locked so unlock it
locked = false;
document.getElementsByTagName('li')[selectedid].setAttribute('class', 'selected');
}
}
}
return parent;
},
//Helper, to use module without Rotary Encoder and without GPIO Pins, like developing in Pixel VM
notificationReceived: function(notification, payload) {
if(notification === "CW" || notification === "CCW" || notification === "PRESSED"){
this.naviaction({inputtype: ""+ notification +""});
}
if(notification === "SHELLCOMMAND"){
this.sendSocketNotification(notification, payload);
}
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if(notification === 'CW' || notification === 'CCW' || notification === 'PRESSED'){
this.naviaction(payload);
}
},
});