-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
193 lines (163 loc) · 4.44 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
const electron = require('electron');
const { dialog, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const createMenuTree = require('./menu');
const loadData = require('./loadData');
require('electron-debug');
// Starts server with default options
if (process.argv.includes('--server')) {
require('datavoyager-server/build/server'); // eslint-disable-line global-require
}
const app = electron.app;
const Menu = electron.Menu;
let mainWindow;
let RENDERER_READY = false;
let dataQueue = [];
//
// Menu Related Event Handlers
//
const handlers = {
handleOpen: () => {
const options = {
properties: ['openFile'],
filters: [
{
name: 'Data Files',
extensions: ['json', 'csv', 'tsv'],
},
],
};
dialog.showOpenDialog(mainWindow, options, (filenames) => {
if (filenames && filenames.length > 0) {
const fp = filenames[0];
const filename = fp.replace(/^.*[\\\/]/, '');
// TODO Send 'loading' signal/message to renderer thread.
const data = loadData.load(fp);
if (RENDERER_READY) {
mainWindow.webContents.send('data', data, filename);
} else {
// This may be overkill.
dataQueue.push(data);
}
}
});
},
handleLoadSpec: () => {
const options = {
properties: ['openFile'],
filters: [
{
name: 'Vega Lite Specs',
extensions: ['json'],
},
],
};
dialog.showOpenDialog(mainWindow, options, (filenames) => {
if (filenames && filenames.length > 0) {
const fp = filenames[0];
// TODO Send 'loading' signal/message to renderer thread.
const spec = loadData.loadJSON(fp);
// Here is where you can manipulate the spec and resolve any relative path issues.
const filename = fp.replace(/^.*[\\\/]/, '');
mainWindow.webContents.send('spec', spec, filename);
}
});
},
handleTakeSession: () => {
mainWindow.webContents.send('applicationState', {
msg: 'getState',
});
},
handleRestoreSession: () => {
const options = {
properties: ['openFile'],
filters: [
{
name: 'Session Files',
extensions: ['json'],
},
],
};
dialog.showOpenDialog(mainWindow, options, (filenames) => {
if (filenames && filenames.length > 0) {
const fp = filenames[0];
const data = loadData.loadJSON(fp);
if (RENDERER_READY) {
mainWindow.webContents.send('applicationState', {
msg: 'setState',
payload: data,
});
}
}
});
},
};
//
// Initialize the app
//
function createWindow() {
// Create the browser window.
mainWindow = new electron.BrowserWindow({ width: 1024, height: 768 });
// Setup the menu
const menuTemplate = createMenuTree(app, handlers);
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true,
}));
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
//
// Communication between main thread and renderer thread.
//
ipcMain.on('status', (event, arg) => {
if (arg === 'ready') {
RENDERER_READY = true;
if (dataQueue.length > 0) {
dataQueue.forEach((datum) => {
mainWindow.webContents.send('data', datum);
});
dataQueue = [];
}
}
});
ipcMain.on('applicationState', (event, arg) => {
const { msg, payload } = arg;
switch (msg) {
case 'getState':
dialog.showSaveDialog(mainWindow, {
defaultPath: 'snapshot.vy.json',
},
(filePath) => {
if (filePath) {
fs.writeFileSync(filePath, JSON.stringify(payload, null, '\t'));
}
});
break;
default:
break;
}
});