-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
219 lines (172 loc) · 5.1 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
'use strict'
const electron = require('electron')
const dialog = electron.dialog
const app = electron.app // Module to control application life.
const BrowserWindow = electron.BrowserWindow // Module to create native browser window.
const globalShortcut = electron.globalShortcut
const ipcMain = electron.ipcMain
var ishttpURL = function(url){
if(/(http|ftp|https|file):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(url)){
return true
}
return false
}
// EG: 'file://xxx/index.html/#view/xxx' will return 'index.html/#view'
var getRouteURL = function(url){
var tmp = /[^/]*\.html#\/[^/]*/.exec(url)
return tmp ? tmp[0] : url
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null
var locale = 'en'
var debug = true
// Functions
var onCloseEvent = function(event){
var self = this
self.webContents.send('close-confirm', '')
var msg = {
'quit': {
'en': 'Quit Now?',
'zh-cn': '确定退出么?',
'jp': 'やめるか?'
},
'ok': {
'en': ' Yes ',
'zh-cn': '是',
'jp': 'はい'
},
'cancel': {
'en': ' No ',
'zh-cn': '否',
'jp': 'いいえ'
}
}
var choice = dialog.showMessageBox(self, {
type: 'question',
buttons: [
msg['ok'][locale]?msg['ok'][locale]:msg['ok']['en'],
msg['cancel'][locale]?msg['cancel'][locale]:msg['cancel']['en']
],
title: 'DJI Assistant 2',
message: msg['quit'][locale]?msg['quit'][locale]:msg['quit']['en'],
noLink: true,
cancelId: 2
})
if(choice !== 0){
event.preventDefault()
return false
}
self.webContents.send('close', '')
// If user closing the main window, close all opened window.
if(self == mainWindow){
var windowset = BrowserWindow.getAllWindows()
windowset.forEach(function(ele, idx){
if(ele && ele.close && ele != mainWindow) ele.close()
})
}
}
// For Browser Plugins
app.commandLine.appendSwitch('ppapi-out-of-process','')
app.commandLine.appendSwitch('register-pepper-plugins', '../Browser/DJIViewerPlugin.dll;plugin/dji_viewer' + ', '
+ '../Browser/DJILiveVideoPlugin.dll;plugin/dji_live_video')
// Quit when all windows are closed.
app.on('window-all-closed', function(){
// 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()
// }
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function(){
mainWindow = new BrowserWindow({
width: 1080, height: 650, minWidth: 1080, minHeight: 650, useContentSize: true, title: 'DJI Assistant 2', backgroundColor: '#fff', autoHideMenuBar: true,
'web-preferences': {
'plugins': true
}
})
if(!debug) mainWindow.setMenu(null)
mainWindow.setMenuBarVisibility(false)
if(debug) {
mainWindow.loadURL('file://' + __dirname + '/index.html#/debug/1')
} else {
mainWindow.loadURL('file://' + __dirname + '/index.html#/debug/0')
}
// Open the DevTools.
mainWindow.webContents.openDevTools()
mainWindow.on('close', onCloseEvent)
// Emitted when the window is closed.
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
})
/*dialog.showMessageBox(mainWindow, {
type: 'question',
buttons: [
'ok',
'cancel'
],
title: 'DJI Assistant 2',
message: JSON.stringify(process.argv),
noLink: true,
cancelId: 2
})*/
})
ipcMain.on('asynchronous-message', function(event, arg){
switch(arg.type){
case 'openWindow':
var opts = arg.options
// Check whether window already exist
var already_exist = false
var windowset = BrowserWindow.getAllWindows()
for(var i = windowset.length - 1; i >= 0; i--){
var ele = windowset[i]
if(ele && ele.webContents){
var cur_url = ele.webContents.getURL()
var go_url = ishttpURL(arg.url) ? arg.url : getRouteURL(arg.url)
cur_url = ishttpURL(cur_url) ? cur_url : getRouteURL(cur_url)
console.log('go_url',go_url)
console.log('cur_url',cur_url)
if(cur_url == go_url){
already_exist = true
ele.focus()
break
}
}
}
if(already_exist) return false
if(ishttpURL(arg.url)){
var url = arg.url
opts['node-integration'] = false
}else{
var url = 'file://' + __dirname + '/' + arg.url
}
opts['web-preferences'] = {'plugins': true}
var win = new BrowserWindow(opts)
if(opts['needCloseConfirm']) win.on('close', onCloseEvent)
win.on('closed', function(){
win = null
})
if(!debug) win.setMenu(null)
win.setMenuBarVisibility(false)
win.loadURL(url)
win.show()
break
default:
break
}
event.sender.send('asynchronous-reply', 'success')
})
ipcMain.on('set-locale', function(event, arg){
locale = arg
})
ipcMain.on('open-devtools', function(event, arg){
console.log('receive open-devtools event:',arg);
if(!!arg){
//mainWindow.webContents.closeDevTools();
}
})