-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
278 lines (233 loc) · 8.19 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
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
const electron = require('electron');
const dialog = electron.dialog;
const Tray = electron.Tray;
const path = require('path');
const glob = require('glob');
const { app, BrowserWindow } = require('electron');
const debug = /--debug/.test(process.argv[2]);
let websocketmode = false;
let websocketport = 0;
if (process.mas) app.setName('Electron APIs');
const ipcMain = require('electron').ipcMain;
let mainWindow = null;
const Menu = electron.Menu;
const MenuItem = electron.MenuItem;
const fs = require('fs');
const csv = require('csvtojson');
// only add update server if it's not being run from cli
if (require.main !== module) {
require('update-electron-app')({
logger: require('electron-log')
})
}
function initialize() {
makeSingleInstance();
function createWindow() {
const appIcon = new Tray(__dirname + '/src/public/css/icons/home.png');
const windowOptions = {
width: 1080,
minWidth: 680,
height: 840,
title: app.getName(),
icon: __dirname + '/src/public/css/icons/home.png'
};
// if (process.platform === 'linux') {
// windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
// }
ipcMain.on('not-data', (event, message) => {
if (message) {
mainWindow.webContents.send('errror-data');
} else {
openFile();
}
});
mainWindow = new BrowserWindow(windowOptions);
mainWindow.loadURL(path.join('file://', __dirname, '/src/mainWindow.html'));
mainWindow.on('closed', () => {
mainWindow = null
});
const menu = new Menu();
const menuTemplateFile = {
label: 'File',
submenu: [
{
label: 'load', click() {
openFile();
}
}]
};
const menuTemplateTools = {
label: 'Tools',
submenu: [
{
label: 'Select', submenu: [
{ label: 'Click Selection', click() { } }
]
},
{
label: 'Filter',
submenu: [
{ label: 'Filter By Data', click() { } },
{ label: 'Filter Selected Data', click() { } }
]
},
{ label: 'Zoom', click() { } },
{ label: 'Details on Demand', click() { } },
{ label: 'Sort', click() { } },
]
};
const menuTemplateDebug = {
label: 'Debug',
submenu: [
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.toggleDevTools()
}
}
]
};
menu.append(new MenuItem(menuTemplateFile));
menu.append(new MenuItem(menuTemplateTools));
menu.append(new MenuItem(menuTemplateDebug));
mainWindow.setMenu(menu);
//Work on Mac.
if (process.platform === 'darwin') {
mainWindow.on("focus", () => {
const menuTemplate = [menuTemplateFile, menuTemplateTools, menuTemplateDebug];
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
});
}
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
// app.quit();
});
}
app.on('ready', () => {
createWindow()
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
}
// Make this app a single instance app.
//
// The main window will be restored and focused instead of a second window
// opened when a person attempts to launch a second instance.
//
// Returns true if the current version of the app should quit instead of
// launching.
function makeSingleInstance() {
if (process.mas) return;
app.requestSingleInstanceLock();
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
}
//open and convert datafile
function openFile() {
dialog.showOpenDialog(function (fileNames) {
if (fileNames === undefined) return;
let fileName = fileNames[0];
let checkType = fileName.split('\\');
checkType = checkType[checkType.length - 1].split('.');
if (checkType[1] == 'csv') {
csv()
.fromFile(fileName)
.then((jsonObj) => {
for (let i = 0; i < jsonObj.length; i++) {
let obj = jsonObj[i];
for (let prop in obj) {
if (obj.hasOwnProperty(prop) && obj[prop] !== null && !isNaN(obj[prop])) {
obj[prop] = +obj[prop];
}
}
}
mainWindow.webContents.send("file-data", jsonObj)
})
}
if (checkType[1] === 'json') {
let jsonObj = require(fileName);
mainWindow.webContents.send("file-data", jsonObj)
}
if(checkType[1]=='tsv' || checkType[1]=='txt'){
let jsonObj = fs.readFileSync(fileName, "utf8");
jsonObj = tsvJSON(jsonObj);
mainWindow.webContents.send("file-data", jsonObj);
}
});
}
// tsv to Json convert
function tsvJSON(tsv) {
const lines = tsv.split('\n');
const headers = lines.slice(0, 1)[0].split('\t');
return lines.slice(1, lines.length).map(line => {
const data = line.split('\t');
return headers.reduce((obj, nextKey, index) => {
obj[nextKey] = data[index];
return obj;
}, {});
});
}
// Require each JS file in the main-process dir
function loadDemos() {
const files = glob.sync(path.join(__dirname, 'main-process/**/*.js'))
files.forEach((file) => { require(file) })
}
initialize();
ipcMain.on('document-ready', function (evt, msg) {
mainWindow.webContents.send('cl', 'APP is ready')
for (let arg of process.argv) {
if (arg === 'websocketmode=on') {
websocketmode = true
} else if (arg.startsWith('port=')) {
websocketport = +arg.replace("port=", "");
}
}
if (websocketmode) {
mainWindow.webContents.send('cl', 'websocketmode=' + websocketmode);
const WebSocket = require('ws');
const ws = new WebSocket(`ws://127.0.0.1:${websocketport}/`);
ws.on('open', function () {
mainWindow.webContents.send('cl', 'WS is open!');
ws.send("oi");
});
ws.on('message', function (message, flags) {
if (message && typeof message === 'string') {
try {
obj = JSON.parse(message);
mainWindow.webContents.send(obj.act, obj.msg);
mainWindow.webContents.send('cl', `WS Message: ${obj.act}`);
} catch (e) {
mainWindow.webContents.send('cl', 'WS Error: message is not a JSON')
}
}
});
ws.on('close', function (err) {
mainWindow.webContents.send('cl', 'WS Closed.');
console.log(err);
});
}
});
process.on('message', (message) => {
mainWindow.webContents.send('cl', 'foi de uma vcez')
// mainWindow.webContents.send("cl", message);
// console.log("message in: " + message);
// process.send("message in: " + message);
// if(mainWindow)
// mainWindow.webContents.send('add-vis', message);
});