-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
197 lines (175 loc) · 6.32 KB
/
main.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
const electron = require('electron');
const url = require('url');
const path = require('path');
// const d3 = require('d3');
var fs = require('fs');
// library to read the json files from URLs
const https = require('https');
//library to prompt the text input for the URLs
const prompt = require('electron-prompt');
const { dialog, app, BrowserWindow, Menu} = electron;
let mainWindow;
var json_data_loaded = false;
// Listen for app to be ready
app.on('ready', function () {
//create new window
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
}
});
mainWindow.setIcon(path.join(__dirname, 'img/other_icons/icon.png'));
// mainWindow.maximize()
// openninng dev tools
// mainWindow.openDevTools();
// load html in window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
//Quit app when closed
mainWindow.on('closed', function () {
app.quit();
})
// on resize: get the size and send it to the renderer process in case it's needed there
mainWindow.on('resize', function () {
var size = mainWindow.getSize();
var width = size[0];
var height = size[1];
if (json_data_loaded) {
mainWindow.webContents.send('resize', 'resize')
}
});
//build menu from tempalte defined below
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
// insert menu
Menu.setApplicationMenu(mainMenu);
});
// create menu tempalte
const mainMenuTemplate = [
{
label: 'File',
submenu: [
{
label: 'Open Mud File',
accelerator: process.platform == 'darwin' ? 'Command+O' : 'Ctrl+O',
click() {
var files_data = {};
dialog.showOpenDialog({properties: ["multiSelections", "openFile"]}, (fileNames) => {
// fileNames is an array that contains all the selected
if (fileNames === undefined) {
console.log("No file selected");
return;
}
for (var file_idx in fileNames) {
var filepath = fileNames[file_idx];
// fs.readFile(filepath, 'utf-8', (err, data) => {
// if(err){
// alert("An error ocurred reading the file :" + err.message);
// return;
// }
// global.sharedObj= data;
// mainWindow.webContents.send('draw', 'draw');
// });
var data = fs.readFileSync(filepath, 'utf-8');
files_data[file_idx] = data;
}
global.sharedObj = JSON.stringify(files_data);
mainWindow.webContents.send('draw', 'draw');
});
json_data_loaded = true;
}
},
{
label: 'Open MUD-URL',
accelerator: process.platform == 'darwin' ? 'Command+Shift+O' : 'Ctrl+Shift+O',
click(){
//prompt URL of the MUD file
prompt({
title: 'Insert the MUD file URL',
label: 'URL:',
value: 'https://example.com/mud_file.json',
inputAttrs: {
type: 'url'
},
type: 'input'
})
.then((r) => {
if(r === null) {
console.log('No URL inserted');
} else {
// collect the data using https
var data = "";
var test = https.get(r,{}, function(response){
response.on('data', (append) => {
data += append;
});
response.on('end', () => {
// when collect ends, show the result on the screen
addDevicesOnScreen({data});
});
});
}
})
.catch(console.error);
}
},
{ // label and shortcut for quititng the application
label: 'Quit',
accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
click() {
app.quit();
}
}
]
},
{
label: "Run",
submenu: [
{ // shortcut and label for resetting the application
label: "Reset",
accelerator: process.platform == 'darwin' ? 'Command+R' : 'Ctrl+R',
click() {
mainWindow.reload();
}
}
]
},
{ // help menu duh (what's About shortcut btw?)
label: "Help",
submenu: [
{
label: "About",
// accelerator: process.platform == 'darwin' ? 'Command+H' : 'Ctrl+H',
click() {
openAboutWindow();
}
}
]
}
];
let aboutWindow;
function openAboutWindow() {
//create new window
aboutWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}, // hardcoded size for the about window (small)
width: 300,
height: 250,
title: 'About'
});
aboutWindow.setMenu(null);
// load the local html in window from file
aboutWindow.loadURL(url.format({
pathname: path.join(__dirname, 'html/about.html'),
protocol: 'file:',
slashes: true
}));
}
function addDevicesOnScreen(devices_list){
// send the received json to renderer process
global.sharedObj = JSON.stringify(devices_list);
mainWindow.webContents.send('draw', 'draw');
}