Files: ce8c9a23510ffcaae086aac41d0d04be17ab0af0 / lib / window.js
2505 bytesRaw
1 | var Path = require('path') |
2 | var electron = require('electron') |
3 | var extend = require('xtend/mutable') |
4 | |
5 | module.exports = function Window (context, path, opts) { |
6 | var window = new electron.BrowserWindow(extend({ |
7 | show: false |
8 | }, opts)) |
9 | |
10 | window.setMenu(null) |
11 | electron.ipcMain.on('ready-to-show', handleReadyToShow) |
12 | |
13 | window.webContents.on('dom-ready', function () { |
14 | window.webContents.executeJavaScript(` |
15 | var electron = require('electron') |
16 | var rootView = require(${JSON.stringify(path)}) |
17 | var insertCss = require('insert-css') |
18 | var h = require('../lib/h') |
19 | var createClient = require('ssb-client') |
20 | |
21 | require('../lib/context-menu') |
22 | insertCss(require('../styles')) |
23 | electron.webFrame.setZoomLevelLimits(1, 1) |
24 | |
25 | var config = ${JSON.stringify(context.config)} |
26 | var data = ${JSON.stringify(opts.data)} |
27 | var title = ${JSON.stringify(opts.title || 'Ferment')} |
28 | |
29 | document.documentElement.querySelector('head').appendChild( |
30 | h('title', title) |
31 | ) |
32 | |
33 | var shouldShow = ${opts.show !== false} |
34 | var shouldConnect = ${opts.connect !== false} |
35 | |
36 | if (shouldConnect) { |
37 | createClient(config.keys, config, (err, client) => { |
38 | if (err) { |
39 | electron.remote.getGlobal('console').log(err) |
40 | throw err |
41 | } else { |
42 | render(client) |
43 | } |
44 | }) |
45 | } else { |
46 | render() |
47 | } |
48 | |
49 | function render (client) { |
50 | try { |
51 | document.documentElement.replaceChild(h('body', [ |
52 | rootView(client, config, data) |
53 | ]), document.body) |
54 | } catch (ex) { |
55 | electron.ipcRenderer.send('ready-to-show') |
56 | throw ex |
57 | } |
58 | shouldShow && electron.ipcRenderer.send('ready-to-show') |
59 | } |
60 | `) |
61 | }) |
62 | |
63 | // setTimeout(function () { |
64 | // window.show() |
65 | // }, 3000) |
66 | |
67 | window.webContents.on('will-navigate', function (e, url) { |
68 | e.preventDefault() |
69 | electron.shell.openExternal(url) |
70 | }) |
71 | |
72 | window.webContents.on('new-window', function (e, url) { |
73 | e.preventDefault() |
74 | electron.shell.openExternal(url) |
75 | }) |
76 | |
77 | window.on('closed', function () { |
78 | electron.ipcMain.removeListener('ready-to-show', handleReadyToShow) |
79 | }) |
80 | |
81 | window.loadURL('file://' + Path.join(__dirname, '..', 'assets', 'base.html')) |
82 | return window |
83 | |
84 | // scoped |
85 | |
86 | function handleReadyToShow (ev) { |
87 | if (ev.sender === window) { |
88 | window.show() |
89 | electron.ipcMain.removeListener('ready-to-show', handleReadyToShow) |
90 | } |
91 | } |
92 | } |
93 |
Built with git-ssb-web