This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
browser.js
171 lines (146 loc) · 4.73 KB
/
browser.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
/**
* @file Render process entry point for Code.org Maker App.
* Creates/binds browser controls, including the webview that hosts Code.org
* curriculum.
*
* Process: Renderer
*/
// First step: Always be production, unless told otherwise.
if (process.env.NODE_ENV === undefined) process.env.NODE_ENV = 'production';
const {ipcRenderer} = require('electron');
const {
NAVIGATION_REQUESTED,
RELOAD_REQUESTED,
TOGGLE_DEV_TOOLS_REQUESTED,
} = require('./channelNames');
const {
HOME_URL,
MAKER_SETUP_URL,
CLEVER_LOGIN_URL,
MAKER_LOGIN_URL,
SIGN_IN_URL,
} = require('./defaults');
window.onresize = doLayout;
var isLoading = false;
// Handle requests from Electron menu items
ipcRenderer.on(RELOAD_REQUESTED, () => {
if (!isLoading) {
document.querySelector('webview').reload();
}
});
ipcRenderer.on(NAVIGATION_REQUESTED, (_, url) => navigateTo(url));
ipcRenderer.on(TOGGLE_DEV_TOOLS_REQUESTED, () => {
const webview = document.querySelector('webview');
if (webview.isDevToolsOpened()) {
webview.closeDevTools();
} else {
webview.openDevTools();
}
});
window.onload = function() {
var webview = document.querySelector('webview');
doLayout();
document.querySelector('#back').onclick = function() {
webview.goBack();
};
document.querySelector('#forward').onclick = function() {
webview.goForward();
};
document.querySelector('#home').onclick = function() {
navigateTo(HOME_URL);
};
document.querySelector('#reload').onclick = function() {
if (isLoading) {
webview.stop();
} else {
webview.reload();
}
};
document.querySelector('#reload').addEventListener(
'webkitAnimationIteration',
function() {
if (!isLoading) {
document.body.classList.remove('loading');
}
}
);
document.querySelector('#setup').onclick = function() {
navigateTo(MAKER_SETUP_URL);
};
document.querySelector('#clever-login').onclick = function() {
navigateTo(CLEVER_LOGIN_URL);
};
document.querySelector('#google-login').onclick = function() {
// Opens link in default browser
require('electron').shell.openExternal(SIGN_IN_URL + '?user_return_to=/maker/display_google_oauth_code&maker=true');
navigateTo(MAKER_LOGIN_URL);
};
webview.addEventListener('close', handleExit);
webview.addEventListener('did-start-loading', handleLoadStart);
webview.addEventListener('did-stop-loading', handleLoadStop);
webview.addEventListener('did-fail-load', handleLoadAbort);
webview.addEventListener('did-get-redirect-request', handleLoadRedirect);
webview.addEventListener('did-finish-load', handleLoadCommit);
navigateTo(HOME_URL);
};
function navigateTo(url) {
resetExitedState();
document.querySelector('webview').src = url;
}
function doLayout() {
var webview = document.querySelector('webview');
var controls = document.querySelector('#controls');
var controlsHeight = controls.offsetHeight;
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var webviewWidth = windowWidth;
var webviewHeight = windowHeight - controlsHeight;
webview.style.width = webviewWidth + 'px';
webview.style.height = webviewHeight + 'px';
var sadWebview = document.querySelector('#sad-webview');
sadWebview.style.width = webviewWidth + 'px';
sadWebview.style.height = webviewHeight * 2 / 3 + 'px';
sadWebview.style.paddingTop = webviewHeight / 3 + 'px';
}
function handleExit(event) {
console.log(event.type);
document.body.classList.add('exited');
if (event.type === 'abnormal') {
document.body.classList.add('crashed');
} else if (event.type === 'killed') {
document.body.classList.add('killed');
}
}
function resetExitedState() {
document.body.classList.remove('exited');
document.body.classList.remove('crashed');
document.body.classList.remove('killed');
}
function handleLoadCommit() {
resetExitedState();
var webview = document.querySelector('webview');
document.querySelector('#back').disabled = !webview.canGoBack();
document.querySelector('#forward').disabled = !webview.canGoForward();
}
function handleLoadStart(event) {
document.body.classList.add('loading');
if (process.env.OPEN_DEV_TOOLS) {
document.querySelector('webview').openDevTools();
}
isLoading = true;
resetExitedState();
}
function handleLoadStop(event) {
// We don't remove the loading class immediately, instead we let the animation
// finish, so that the spinner doesn't jerkily reset back to the 0 position.
isLoading = false;
}
function handleLoadAbort(event) {
console.log('LoadAbort');
console.log(' url: ' + event.url);
console.log(' isTopLevel: ' + event.isTopLevel);
console.log(' type: ' + event.type);
}
function handleLoadRedirect(event) {
resetExitedState();
}