-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
192 lines (170 loc) · 5.41 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var browser = (function(configModule, tabsModule) {
var dce = function(str) { return document.createElement(str); };
var Browser = function(
controlsContainer,
back,
forward,
home,
reload,
locationForm,
locationBar,
tabContainer,
contentContainer,
newTabElement) {
this.controlsContainer = controlsContainer;
this.back = back;
this.forward = forward;
this.reload = reload;
this.home = home;
this.locationForm = locationForm;
this.locationBar = locationBar;
this.tabContainer = tabContainer;
this.contentContainer = contentContainer;
this.newTabElement = newTabElement;
this.tabs = new tabsModule.TabList(
'tabs',
this,
tabContainer,
contentContainer,
newTabElement);
this.init();
};
Browser.prototype.init = function() {
(function(browser) {
window.addEventListener('resize', function(e) {
browser.doLayout(e);
});
window.addEventListener('keydown', function(e) {
browser.doKeyDown(e);
});
browser.back.addEventListener('click', function(e) {
browser.tabs.getSelected().goBack();
});
browser.forward.addEventListener('click', function() {
browser.tabs.getSelected().goForward();
});
browser.home.addEventListener('click', function() {
browser.tabs.getSelected().navigateTo(configModule.homepage);
});
browser.reload.addEventListener('click', function() {
var tab = browser.tabs.getSelected();
if (tab.isLoading()) {
tab.stopNavigation();
} else {
tab.doReload();
}
});
browser.reload.addEventListener(
'webkitAnimationIteration',
function() {
// Between animation iterations: If loading is done, then stop spinning
if (!browser.tabs.getSelected().isLoading()) {
document.body.classList.remove('loading');
}
}
);
browser.locationForm.addEventListener('submit', function(e) {
e.preventDefault();
browser.tabs.getSelected().navigateTo(browser.locationBar.value);
});
browser.newTabElement.addEventListener(
'click',
function(e) { return browser.doNewTab(e); });
window.addEventListener('message', function(e) {
if (e.data) {
var data = JSON.parse(e.data);
if (data.name && data.title) {
browser.tabs.setLabelByName(data.name, data.title);
} else {
console.warn(
'Warning: Expected message from guest to contain {name, title}, but got:',
data);
}
} else {
console.warn('Warning: Message from guest contains no data');
}
});
var webview = dce('webview');
var tab = browser.tabs.append(webview);
// Global window.newWindowEvent may be injected by opener
if (window.newWindowEvent) {
window.newWindowEvent.window.attach(webview);
} else {
tab.navigateTo(configModule.homepage);
}
browser.tabs.selectTab(tab);
}(this));
};
Browser.prototype.doLayout = function(e) {
var controlsHeight = this.controlsContainer.offsetHeight;
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var contentWidth = windowWidth;
var contentHeight = windowHeight - controlsHeight;
var tab = this.tabs.getSelected();
var webview = tab.getWebview();
var webviewContainer = tab.getWebviewContainer();
var layoutElements = [
this.contentContainer,
webviewContainer,
webview];
for (var i = 0; i < layoutElements.length; ++i) {
layoutElements[i].style.width = contentWidth + 'px';
layoutElements[i].style.height = contentHeight + 'px';
}
};
// New window that is NOT triggered by existing window
Browser.prototype.doNewTab = function(e) {
var tab = this.tabs.append(dce('webview'));
tab.navigateTo(configModule.homepage);
this.tabs.selectTab(tab);
return tab;
};
Browser.prototype.doKeyDown = function(e) {
if (e.ctrlKey) {
switch(e.keyCode) {
// Ctrl+T
case 84:
this.doNewTab();
break;
// Ctrl+W
case 87:
e.preventDefault();
this.tabs.removeTab(this.tabs.getSelected());
break;
}
// Ctrl + [1-9]
if (e.keyCode >= 49 && e.keyCode <= 57) {
var idx = e.keyCode - 49;
if (idx < this.tabs.getNumTabs()) {
this.tabs.selectIdx(idx);
}
}
}
};
Browser.prototype.doTabNavigating = function(tab, url) {
if (tab.selected) {
document.body.classList.add('loading');
this.locationBar.value = url;
}
};
Browser.prototype.doTabNavigated = function(tab, url) {
this.updateControls();
};
Browser.prototype.doTabSwitch = function(oldTab, newTab) {
this.updateControls();
};
Browser.prototype.updateControls = function() {
var selectedTab = this.tabs.getSelected();
if (selectedTab.isLoading()) {
document.body.classList.add('loading');
}
var selectedWebview = selectedTab.getWebview();
this.back.disabled = !selectedWebview.canGoBack();
this.forward.disabled = !selectedWebview.canGoForward();
if (this.locationBar.value != selectedTab.url) {
this.locationBar.value = selectedTab.url;
}
};
return {'Browser': Browser};
})(config, tabs);