forked from natcl/electron-node-red
-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.js
74 lines (64 loc) · 1.74 KB
/
settings.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
const Store = require('electron-store');
const store = new Store();
const bcrypt = require('bcryptjs');
const { ipcRenderer } = require('electron')
let form = document.getElementById("settings")
let savedSettings = store.get("settings")
console.log(savedSettings)
formToJson = (elements) => {
let json = {}
elements.forEach(element => {
if (element.name !== "") json[element.name] = element.value
})
return json
}
jsonToForm = (settings, form) => {
Object.keys(settings).forEach(settingName => {
form.elements[settingName].value = settings[settingName]
})
form.elements["nodered-password"].value = ""
}
hasNRChanged = (settings) => {
var changed = false
if (savedSettings == null && settings != null){
changed = true
}
for (k in savedSettings) {
if (k.match(/nodered-.*/g)) {
if (savedSettings[k] != settings[k]){
changed = true
}
}
}
return changed
}
form.addEventListener("submit", (event) => {
event.preventDefault();
let settings = formToJson([...form.elements])
console.log(settings)
if (settings["nodered-password"] != ""){
settings["nodered-password"] = bcrypt.hashSync(settings["nodered-password"], 8)
} else {
settings["nodered-password"] = savedSettings["nodered-password"]
}
store.set({
"settings": settings
})
console.log(settings)
if (hasNRChanged(settings)){
var r = confirm("Node-RED Settings will only take effect on a restart, do you want to restart now");
if (r ==true) {
ipcRenderer.send('restart-request', 'restart')
} else{
close()
}
} else{
close()
}
})
window.addEventListener('load', (event) => {
if (savedSettings) {
jsonToForm(savedSettings, form)
console.log(savedSettings);
}
});