-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreload.js
324 lines (257 loc) · 10.5 KB
/
preload.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
const { ipcRenderer } = require('electron');
const { ipcEvents } = require('./ipcEvents');
/**
* Contains the table cells which have active classes applied and the timeout ID's to cancel the automatic removal of those classes.
* @type {Map<HTMLTableCellElement, number>}
*/
const changeMap = new Map();
window.addEventListener('DOMContentLoaded', () => {
/** @type {HTMLButtonElement} */
const goBtn = document.querySelector('#go'),
/** @type {HTMLButtonElement} */
stopBtn = document.querySelector('#stop'),
/** @type {HTMLButtonElement} */
connectBtn = document.querySelector('#connectBTN'),
/** @type {HTMLSelectElement} */
portList = document.querySelector('#portList'),
/** @type {HTMLButtonElement} */
refreshPortsBtn = document.querySelector('#refreshPortsBTN'),
/** @type {HTMLButtonElement} */
messagesTabBtn = document.querySelector('#messages-tab'),
/** @type {HTMLButtonElement} */
adapterTabBtn = document.querySelector('#adapter-tab'),
/** @type {HTMLButtonElement} */
disconnectBtn = document.querySelector('#disconnectBTN'),
/** @type {HTMLDivElement} */
updateAlert = document.querySelector('#autoUpdateAlert'),
/** @type {HTMLButtonElement} */
exportTable = document.querySelector('#exportTableBTN'),
/** @type {HTMLTableElement} */
table = document.querySelector('table'),
clickEvent = new Event('click', {
bubbles: true
});
ipcRenderer.on(ipcEvents.serialConnected, () => {
messagesTabBtn.classList.remove('disabled');
disconnectBtn.removeAttribute('disabled');
});
ipcRenderer.on(ipcEvents.serialDisconnect, (e, path) => {
connectBtn.removeAttribute('disabled');
portList.removeAttribute('disabled');
messagesTabBtn.classList.add('disabled');
goBtn.removeAttribute('disabled');
stopBtn.setAttribute('disabled', 'disabled');
disconnectBtn.setAttribute('disabled', 'disabled');
adapterTabBtn.dispatchEvent(clickEvent);
ipcRenderer.send(ipcEvents.performPortList);
});
ipcRenderer.on(ipcEvents.CANMessage, (e, d) => {
renderMessages(d);
});
ipcRenderer.on(ipcEvents.debugMessage, (e, d) => {
console.log(d);
});
ipcRenderer.on(ipcEvents.init, (e, d) => {
ipcRenderer.send(ipcEvents.performPortList);
if (d.connected) {
messagesTabBtn.classList.remove('disabled');
messagesTabBtn.dispatchEvent(clickEvent);
connectBtn.setAttribute('disabled', 'disabled');
portList.setAttribute('disabled', 'disabled');
disconnectBtn.removeAttribute('disabled');
}
if (d.path) {
// if available ports haven't been bound yet set the selected port as a data attribute to be added later
if (portList.options.length)
portList.value = d.path;
else
portList.dataset.path = d.path;
}
document.querySelector('.version').textContent = d.version;
});
ipcRenderer.on(ipcEvents.portList, (e, ports) => {
const templateData = ports.map(p => {
return {
value: p.path,
label: `${p.manufacturer || 'N/A'} : (${p.path})`
};
});
const element = renderTemplate('#port-list-item-template', templateData);
const selectedValue = portList.value;
portList.innerHTML = '';
portList.appendChild(element);
if (selectedValue)
portList.value = selectedValue;
if (portList.dataset.path) {
portList.value = portList.dataset.path;
delete portList.dataset.path;
}
});
ipcRenderer.on(ipcEvents.updateAvailable, () => {
updateAlert.querySelector('span').textContent = 'A new version is available and being downlaoded. You will be notified when its ready to install';
updateAlert.classList.remove('d-none');
});
ipcRenderer.on(ipcEvents.updateDownloaded, () => {
updateAlert.querySelector('span').textContent = 'A new version has been downloaded. It will automaitcally be installed next time you use this app';
const button = updateAlert.querySelector('button');
button.addEventListener('click', () => ipcRenderer.send(ipcEvents.performUpdate));
button.classList.remove('d-none');
updateAlert.classList.remove('d-none', 'alert-info');
updateAlert.classList.add('alert-success');
});
connectBtn.addEventListener('click', (e) => {
const path = portList.value;
if (!path)
return;
ipcRenderer.send(ipcEvents.performSerialConnect, path);
connectBtn.setAttribute('disabled', 'disabled');
portList.setAttribute('disabled', 'disabled');
});
goBtn.addEventListener('click', (e) => {
goBtn.setAttribute('disabled', 'disabled');
stopBtn.removeAttribute('disabled');
ipcRenderer.send(ipcEvents.performStartCANListening);
});
stopBtn.addEventListener('click', (e) => {
stopBtn.setAttribute('disabled', 'disabled');
goBtn.removeAttribute('disabled');
ipcRenderer.send(ipcEvents.performStopCANListening);
});
refreshPortsBtn.addEventListener('click', () => {
ipcRenderer.send(ipcEvents.performPortList);
});
disconnectBtn.addEventListener('click', () => {
ipcRenderer.send(ipcEvents.performSerialDisconnect);
});
exportTable.addEventListener('click', (e) => {
const tableData = Array.from(table.tBodies[0].rows)
.map(row => {
return {
messageId: row.cells[0].textContent,
D0: row.cells[1].textContent,
D1: row.cells[2].textContent,
D2: row.cells[3].textContent,
D3: row.cells[4].textContent,
D4: row.cells[5].textContent,
D5: row.cells[6].textContent,
D6: row.cells[7].textContent,
D7: row.cells[8].textContent,
notes: row.cells[9].children[0].value
}
});
ipcRenderer.send(ipcEvents.performExport, tableData);
})
ipcRenderer.send(ipcEvents.performInit);
});
/**
* Sets the CAN message value against its table cell. Adds a highlight class which will automatically be removed after a period of time.
*
* This is not used for first time messages but for updating existing CAN messages.
* @param {HTMLTableCellElement} td
* @param {string} v
* @returns
*/
function setValue(td, v) {
if (td.textContent === v)
return;
td.textContent = v;
td.classList.add('changed');
if (changeMap.has(td))
clearTimeout(changeMap.get(td));
const timeoutID = ((e) => setTimeout(() => {
e.classList.remove('changed');
}, 2500))(td);
changeMap.set(td, timeoutID);
}
/**
* Renders the CAN messages recieved
*
* @param {object[]} messages
*/
function renderMessages(messages) {
const tbody = document.querySelector('tbody');
messages.forEach(message => {
const existingCells = Array.from(tbody.querySelectorAll(`[data-message-id="${message.id}"] td.data-block`));
if (existingCells.length) {
// If the can message has already been recieved only update the actual can data blocks
existingCells
.forEach((cell, i) => setValue(cell, message.dataBlocks[i]));
} else {
const clone = renderTemplate('#table-row-template', {
messageId: message.id,
d1: message.dataBlocks[0] || '',
d2: message.dataBlocks[1] || '',
d3: message.dataBlocks[2] || '',
d4: message.dataBlocks[3] || '',
d5: message.dataBlocks[4] || '',
d6: message.dataBlocks[5] || '',
d7: message.dataBlocks[6] || '',
d8: message.dataBlocks[7] || ''
});
tbody.appendChild(clone);
// Add the message ID to the filter dropdown list to hide messages of this id
if (!document.querySelector(`.dropdown-menu [data-message-id="${message.id}"]`)) {
const messageFilterClone = renderTemplate('#message-filter-item-template', {
messageId: message.id
});
document.querySelector('.dropdown-menu').appendChild(messageFilterClone);
}
}
});
}
/**
* Prevent XSS by escaping html.
* Very unlikely that we will get an XSS attack over a serial port but who knows.
* @param {string} str
* @returns {string}
*/
function escapeHtml(str) {
return String(str)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/\//g, "/")
}
/**
*
* @param {string} templateSelector
* @param {(object|object[])} datas
* @returns {HTMLTemplateElement}
*/
function renderTemplate(templateSelector, templateData) {
const template = document.querySelector(templateSelector),
rootClone = template.content.cloneNode(true);
function renderItem(clone, data) {
let html = clone.firstElementChild.innerHTML;
// build a lookup of attributes that have template keys as their value
const attributesMap = Array.from(clone.firstElementChild.attributes)
.reduce((accu, cur) => {
if (cur.value.startsWith('{{'))
accu[cur.value] = cur;
return accu;
}, {});
for (const key in data) {
const templateKey = `{{${key}}}`,
regex = new RegExp(templateKey, 'g'),
value = escapeHtml(data[key]);
html = html.replace(regex, value);
if (templateKey in attributesMap)
attributesMap[templateKey].value = value;
}
clone.firstElementChild.innerHTML = html;
return clone;
}
if (!Array.isArray(templateData))
templateData = [templateData];
// render the first item as root
renderItem(rootClone, templateData[0]);
// then append all others after the root item
for (const data of templateData.splice(1)) {
const clone = template.content.cloneNode(true);
renderItem(clone, data);
rootClone.firstElementChild.after(clone);
}
return rootClone;
}