forked from khrome/app-electron-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (120 loc) · 4.09 KB
/
app.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, BrowserWindow, Menu } = require('electron');
const Application = require('@protolus/terminal-app');
const Emitter = require('extended-emitter');
const Mangrove = require('mangrove');
const url = require("url");
let windowPrefs = {};
let applicationConfig = {};
function windowConfig(windowName, configs){
let newConfig = {};
let defaults = windowPrefs;
let prefix = windowName+'-';
configs.forEach(function(config){
//TODO: maybe copy to deref?
Object.keys(config).forEach(function(key){
if(key.indexOf(prefix) === 0){
if(!newConfig[key.substring(prefix.length)]){
newConfig[key.substring(prefix.length)] = config[key];
}
}else{
if(!newConfig[key]) newConfig[key] = config[key];
}
});
});
return newConfig;
};
//TODO: maybe someday allow multiple instances
const ElectronKit = function(name, options, cb){
var windows = {};
let application = new Application(name, options);
this.application = application;
this.windows = windows;
if(options.windowDefaults) windowPrefs = options.windowDefaults;
const Win = function(windowName, options){
let newConfig = {};
let defaults = windowPrefs;
let prefix = windowName+'-';
var configs = (options && options.stack) || [];
configs.forEach(function(config){
//TODO: maybe copy to deref?
Object.keys(config).forEach(function(key){
if(key.indexOf(prefix) === 0){
if(!newConfig[key.substring(prefix.length)]){
newConfig[key.substring(prefix.length)] = config[key];
}
}else{
if(!newConfig[key]) newConfig[key] = config[key];
}
});
});
let win = new BrowserWindow(windowConfig(name, [
applicationConfig, windowPrefs, {
width: options.width,
height: options.height
}
]));
var location = url.format(options.url);
win.loadURL(location);
win.on('closed', function(){
windows[name] = null
});
var ob = this;
this.save = function(){
application.save.apply({}, arguments);
}
this.electron = win;
}
var ob = this;
var handleData = function(cb){
//TODO: handle multiple
if(options.data){
ob.db = new Mangrove(options.data);
ob.db.ready(function(){
cb();
})
} else cb();
};
app.on('ready', function(){
if(cb){
application.config(function(err, conf, writeConfig){
application.save = function(){
let incomingConf = arguments.length > 1?arguments[0]:conf;
let callback = arguments.length > 1?arguments[1]:arguments[0];
writeConfig(incomingConf, function(){
if(callback) callback();
});
}
var menus = [];
application.menu = function(name, submenu){
menus.push({
label: name,
submenu: submenu
});
}
application.menus = function(name, submenu){
let menu = Menu.buildFromTemplate(menus);
Menu.setApplicationMenu(menu);
}
applicationConfig = conf;
handleData(function(){
if(cb) cb(null, ob, conf);
});
}, true, true); //enable dir read + auto create defaults
//TODO: support other async actions on init
}
});
var emitter = new Emitter();
emitter.onto(this);
app.on('window-all-closed', function(){
if (process.platform !== 'darwin') app.quit()
});
app.on('activate', function(){
emitter.emit('app-focus');
//TODO: update menus
});
this.electron = app;
this.Window = Win;
};
module.exports = {
Kit: ElectronKit
}