-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
132 lines (113 loc) · 2.98 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
const { app, ipcMain, BrowserWindow, dialog, Menu } = require('electron');
const path = require('path');
const url = require('url');
const settings = require('electron-settings');
const auth = require('./mapzen-util/src/js/authentication.js');
const config = require('./config.json');
require('electron-debug')({showDevTools: false});
let win;
const authOwner = {
loadLoginPage: (authUrl) => {
console.log('got a login message');
if (settings.get('clearAuthCache')) {
console.log('clearing previous cache to allow new login session');
win.webContents.session.clearStorageData();
}
console.log('authMain loading url', authUrl);
win.loadURL(authUrl);
}
};
if (!config.auth) {
throw new Error('No authentication configuration could be found.');
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
app.quit();
});
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1024,
height: 768
});
win.setTitle('Mapzen Search');
loadPage('intro');
win.on('closed', () => {
win = null;
});
const menuTemplate = [
{
label: 'Mapzen',
submenu: [
{
label: 'About ...',
click: () => {
console.log('About Clicked');
}
}, {
label: 'Quit',
click: () => {
app.quit();
}
}
]
}
];
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
}
ipcMain.on('authenticate', () => {
auth.authenticate(config.auth, authOwner, (err) => {
if (err) {
console.log(err.message);
}
loadPage('apiKey');
settings.set('clearAuthCache', false);
});
});
/**
* Automatically close the authentication window after a successful callback
*/
ipcMain.on('login-success', () => {
console.log('loading apiKey window after successful login');
loadPage('apiKey');
});
ipcMain.on('loadPage', (event, pageName) => {
loadPage(pageName);
});
ipcMain.on('openFile', (event, path) => {
const options = {
filters: [{ name: 'CSV', extensions: ['csv'] }],
properties: ['openFile']
};
dialog.showOpenDialog(win, options, function (fileNames) {
// fileNames is an array that contains all the selected
if (fileNames === undefined) {
return;
} else {
event.sender.send('openFileResults', fileNames[0]);
}
});
});
ipcMain.on('createFile', (event, path, defaultPath) => {
const options = {
defaultPath: defaultPath,
filters: [{ name: 'CSV', extensions: ['csv'] }],
properties: ['openFile']
};
dialog.showSaveDialog(win, options, function (fileName) {
// fileNames is an array that contains all the selected
if (fileName === undefined) {
return;
} else {
event.sender.send('createFileResults', fileName);
}
});
});
function loadPage(pageName) {
win.loadURL(url.format({
pathname: path.join(__dirname, 'static', `${pageName}.html`),
protocol: 'file:',
slashes: true
}));
}