-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.js
190 lines (169 loc) · 5.48 KB
/
renderer.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
/* jshint -W104, -W119, -W097, -W067 */
/* jshint node: true */
/* jshint browser: true */
'use strict';
var electron = require('electron');
let waitingTime = 2000;
const forEach = (array, func) => Array.from(array).forEach(func);
document.querySelector('#settings').addEventListener('click', ()=> {
let infoSection = document.querySelector('#info');
infoSection.style.display = {"none": "block", "block": "none"}[infoSection.style.display || "none"];
});
document.addEventListener('dragover', function(event) {
event.preventDefault();
return false;
}, false);
document.addEventListener('drop', function(event) {
event.preventDefault();
return false;
}, false);
let format=(value)=>{
return Math.floor(value*10000)/10000;
};
forEach(
document.querySelectorAll('input[type="range"]'),
(slider)=>{
let name = slider.name;
let span = slider.parentElement.querySelector('span[name="' + name + '"]');
slider.addEventListener('input', (event) => {
let value = slider.value;
span.innerHTML = "(" + format(value) + ")";
});
slider.addEventListener('change', (event) => {
if (name === "waitingTime") {
waitingTime = slider.value;
}
let value = slider.value;
let configUpdate = {name: name, value: value};
electron.ipcRenderer.send('configUpdate', configUpdate);
});
}
);
const fireEvent = (el, eventName) => {
forEach(el.myListeners || [], (l)=> {
if (eventName === l.name) {
l.handler();
}
});
};
let currentButton = null;
electron.ipcRenderer.on('mousemove', (event, pos) => {
let el = document.elementFromPoint(pos.x, pos.y);
while(el !== null && el.tagName !== 'BUTTON') el = el.parentElement;
if (currentButton != el) {
if (currentButton) {
fireEvent(currentButton, "mouseleave");
currentButton.classList.remove('hover');
}
if (el) {
fireEvent(el, "mouseenter");
el.classList.add('hover');
}
currentButton = el;
}
});
electron.ipcRenderer.on('mouseleave', () => {
document.body.style.backgroundColor = null;
if (currentButton) {
fireEvent(currentButton, "mouseleave");
currentButton.classList.remove('hover');
currentButton = null;
}
});
electron.ipcRenderer.on('config', (event, config) => {
waitingTime = config.waitingTime.curr;
forEach(
document.querySelectorAll('input[type="range"]'),
(slider)=>{
let name = slider.getAttribute('name');
let value = config[name].curr;
slider.min = config[name].min;
slider.max = config[name].max;
slider.value = value;
let span = slider.parentElement.querySelector('span[name="' + name + '"]');
span.innerHTML = "(" + format(value) + ")";
}
);
});
let deactivateOtherClickButtons = (theButton) => {
forEach(
document.querySelectorAll("#clicks button"),
(button)=>{
if (button !== theButton) {
button.setAttribute('data-active', '0');
}
}
);
};
electron.ipcRenderer.on('clicked', ()=>{
deactivateOtherClickButtons(null);
currentButton = null;
});
function getModifiers() {
var modifiers = [];
[].forEach.call(
document.querySelectorAll('#modifiers button[data-active="1"]'),
(button)=>{
modifiers.push(button.getAttribute('data-symbol'));
}
);
return modifiers.join('');
}
function getClickSymbols() {
let button = document.querySelector('#clicks button[data-active="1"]');
return button ? button.getAttribute('data-symbol') : null;
}
let getSentence = () => {
let clickSymbols = getClickSymbols();
if (clickSymbols === null) return null;
let modSymbols = getModifiers();
let sentence = modSymbols + "." + clickSymbols + "." + modSymbols.toUpperCase().split('').reverse().join('');
return sentence;
};
let timer = (() => {
let timerId;
return {
start: (cb) => {
if (timerId) this.stop(timerId);
timerId = setTimeout(cb, waitingTime);
},
stop: ()=>{
clearTimeout(timerId);
}
};
})();
forEach(
document.querySelectorAll("#clicks button,#modifiers button"),
(button)=>{
button.addMyEventListener = (name, handler)=>{
let l = button.myListeners || [];
l.push({name, handler});
button.myListeners = l;
};
}
);
forEach(
['clicks', 'modifiers'],
(section) => {
forEach(
document.querySelectorAll("#" + section + " button"),
(button)=>{
button.addMyEventListener('mouseenter', () => {
timer.start(() => {
// toggles data-active attribute of button
let active = button.getAttribute('data-active');
active = {0:1, 1:0}[active || 0];
button.setAttribute('data-active', active);
if (section === 'clicks') {
deactivateOtherClickButtons(button);
electron.ipcRenderer.send('buttonPressed', getSentence());
}
});
});
button.addMyEventListener('mouseleave', () => {
timer.stop();
});
}
);
}
);