-
Notifications
You must be signed in to change notification settings - Fork 119
/
browser-page.jsx
110 lines (96 loc) · 3.37 KB
/
browser-page.jsx
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
var pathlib = require('path')
var BrowserPageSearch = React.createClass({
componentDidUpdate: function (prevProps) {
if (!prevProps.isActive && this.props.isActive)
this.refs.input.getDOMNode().focus()
},
shouldComponentUpdate: function (nextProps, nextState) {
return (this.props.isActive != nextProps.isActive)
},
onKeyDown: function (e) {
if (e.keyCode == 13) {
e.preventDefault()
this.props.onPageSearch(e.target.value)
}
},
render: function () {
return <div id="browser-page-search" className={this.props.isActive ? 'visible' : 'hidden'}>
<input ref="input" type="text" placeholder="Search..." onKeyDown={this.onKeyDown} />
</div>
}
})
var BrowserPageStatus = React.createClass({
render: function () {
var status = this.props.page.statusText
if (!status && this.props.page.isLoading)
status = 'Loading...'
return <div id="browser-page-status" className={status ? 'visible' : 'hidden'}>{status}</div>
}
})
var BrowserPage = React.createClass({
componentDidMount: function () {
// setup resize events
window.addEventListener('resize', resize)
resize()
// attach webview events
for (var k in webviewEvents)
this.refs.webview.getDOMNode().addEventListener(k, webviewHandler(this, webviewEvents[k]))
// set location, if given
if (this.props.page.location)
this.navigateTo(this.props.page.location)
},
componentWillUnmount: function () {
window.removeEventListener('resize', resize)
},
navigateTo: function (l) {
var webview = this.refs.webview.getDOMNode()
webview.setAttribute('src', l)
},
onPageSearch: function (query) {
this.refs.webview.getDOMNode().executeJavaScript('window.find("'+query+'", 0, 0, 1)')
},
render: function () {
return <div className="sheta" >
<BrowserPageSearch isActive={this.props.page.isSearching} onPageSearch={this.onPageSearch} />
<webview ref="webview" onContextMenu={this.props.onContextMenu} />
<BrowserPageStatus page={this.props.page} />
</div>
}
})
function webviewHandler (self, fnName) {
return function (e) {
if (self.props[fnName])
self.props[fnName](e, self.props.page, self.props.pageIndex)
}
}
var webviewEvents = {
'load-commit': 'onLoadCommit',
'did-start-loading': 'onDidStartLoading',
'did-stop-loading': 'onDidStopLoading',
'did-finish-load': 'onDidFinishLoading',
'did-fail-load': 'onDidFailLoad',
'did-get-response-details': 'onDidGetResponseDetails',
'did-get-redirect-request': 'onDidGetRedirectRequest',
'dom-ready': 'onDomReady',
'page-title-set': 'onPageTitleSet',
'close': 'onClose',
'destroyed': 'onDestroyed',
'ipc-message': 'onIpcMessage',
'console-message': 'onConsoleMessage'
}
setTimeout(() => {
document.querySelector('webview').addEventListener('new-window', (e) => {
const protocol = require('url').parse(e.url).protocol
if (protocol === 'http:' || protocol === 'https:') {
let win = new BrowserWindow({icon: __dirname + '/build/1.ico',width: 800, height: 600})
win.loadURL(e.url);
}
})
}, 500);
function resize () {
Array.prototype.forEach.call(document.querySelectorAll('webview'), function (webview) {
var obj = webview && webview.querySelector('::shadow object')
if (obj)
obj.style.height = (window.innerHeight - 59) + 'px' // -61 to adjust for the tabs and navbar regions
})
}