-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
147 lines (123 loc) · 3.61 KB
/
index.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
"use strict";
// Import parts of electron to use
const { app, BrowserWindow, Menu, ipcMain, dialog } = require("electron");
const path = require("path");
const url = require("url");
const fs = require("fs");
require('electron-context-menu')({});
const CustomMenu = require("./src/utils/Menu");
// Import background workers
const NodeREDWorker = require("./src/background/NodeREDWorker");
const BlocklyWorker = require("./src/background/BlocklyWorker");
const MqttBroker = require("./src/background/MqttBroker");
const Settings = require("./src/background/Settings");
const Firmware = require("./src/background/Firmware");
const Gateway = require("./src/background/Gateway");
const Home = require("./src/background/Home");
let windows = [];
// Keep a reference for dev mode
let dev = false;
if (process.defaultApp || /[\\/]electron-prebuilt[\\/]/.test(process.execPath) || /[\\/]electron[\\/]/.test(process.execPath)) {
dev = true;
}
function createWindow() {
if (!app.isReady()) return;
let mainWindow;
mainWindow = new BrowserWindow({
width: 1000,
minWidth: 1000,
minHeight: 480,
show: false,
icon: path.join(__dirname, 'src', 'assets', 'icons', 'icon.png'),
//,titleBarStyle: 'hidden' future purpose?
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
CustomMenu.setup(!dev);
// Load the index.html of the app.
let indexPath;
if (dev && process.argv.indexOf("--noDevServer") === -1) {
indexPath = url.format({
protocol: "http:",
host: "localhost:8080",
pathname: "index.html",
slashes: true
});
} else {
indexPath = url.format({
protocol: "file:",
pathname: path.join(__dirname, "dist", "index.html"),
slashes: true
});
}
mainWindow.loadURL(indexPath);
mainWindow.once("ready-to-show", () => {
mainWindow.show();
// Open the DevTools automatically if developing
if (dev) {
mainWindow.webContents.openDevTools();
}
});
let timer = null;
mainWindow.on('close', (e) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(()=>{
dialog.showMessageBox(mainWindow, {
type: "warning",
buttons: ["Cancel", "Close without Saving"],
title: "Warning",
message: "Node-RED contains unsaved changes"
}).then((result)=>{
console.log('showMessageBox', 'response', result);
if (result.response == 1) {
mainWindow.send("iframe:node-red:visible", false);
timer = setTimeout(()=>{mainWindow.close()}, 100);
}
}).catch(err => {
console.error('showMessageBox', err);
})
}, 1000);
})
mainWindow.on("closed", function () {
if (timer) clearTimeout(timer);
const index = windows.indexOf(mainWindow);
windows.splice(index, 1);
mainWindow = null;
});
windows.push(mainWindow);
}
let userDataPath = app.getPath("userData");
if (!fs.existsSync(userDataPath)) {
fs.mkdirSync(userDataPath)
}
Settings.setup();
MqttBroker.setup();
Gateway.setup();
Firmware.setup();
NodeREDWorker.setup().finally(()=>{
for (let i in windows) {
windows[i].reload();
}
})
Home.setup();
BlocklyWorker.setup();
// Quit when all windows are closed.
app.on("window-all-closed", () => {
console.log("window-all-closed");
if (process.platform != 'darwin'){
console.log("app.quit");
app.quit();
}
});
app.on("app:window:new", createWindow);
app.on("ready", createWindow);
app.on("activate", () => {
if (windows.length == 0) {
createWindow();
}
});