/
index.js
104 lines (82 loc) · 2.54 KB
/
index.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
if (typeof Proxy !== 'object' || typeof WeakMap !== 'function') {
throw new Error ('AppJS requires Node is run with the --harmony command line flag');
}
var Router = require('./router').Router,
Window = require('./window'),
App = require('./bindings').App,
NativeWindow = require('./bindings').NativeWindow,
WindowSettings = require('./settings').WindowSettings,
AppSettings = require('./settings').AppSettings;
var init = require('./bindings').init,
decorate = require('./utils').decorate,
isObject = require('./utils').isObject,
staticRouter = require('./router').staticRouter;
var concat = Array.prototype.concat,
_createWindow = App.prototype.createWindow;
var events = ['create', 'minimize', 'maximize', 'fullscreen', 'restore', 'move', 'resize'];
NativeWindow.prototype.__proto__ = process.EventEmitter.prototype;
decorate(NativeWindow.prototype, [
function pipe(event, target){
var emit = target.emit;
this.on(event, function(){
emit.apply(target, concat.apply(event, arguments));
});
}
]);
decorate(App.prototype, [
function createWindow(options){
var self = this;
if (typeof options === 'string') {
if (isObject(arguments[1])) {
arguments[1].url = options;
options = arguments[1];
} else {
options = { url: options };
}
}
if (!(options instanceof WindowSettings)) {
options = new WindowSettings(options);
}
var nativeWindow = _createWindow.call(this, options),
window = new Window(nativeWindow);
nativeWindow.name = options.name;
this.windows[options.name] = window;
nativeWindow.on('close', function(){
window.emit('close');
delete self.windows[options.name];
});
events.forEach(function(event){
nativeWindow.pipe(event, window);
});
return window;
},
function exit(){
Object.keys(this.windows).forEach(function(name){
var window = this.windows[name];
if (window && window.close) {
window.close();
}
}, this);
},
function serveFilesFrom(root){
this.router.use(staticRouter(root));
return this;
},
function on(type, listener){
process.on('appjs-'+type, listener.bind(this));
}
]);
var app = init(new AppSettings);
decorate(app, {
screenWidth: App.screenWidth,
screenHeight: App.screenHeight,
windows: {},
router: new Router(app)
});
app.on('exit',function(){
app.exit();
process.nextTick(function(){
process.kill(process.pid);
});
});
module.exports = app;