forked from mozilla-b2g/gaia
/
browser.js
168 lines (142 loc) · 4.9 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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
var Browser = {
currentTitle: '',
currentUrl: '',
GO: 0,
REFRESH: 1,
urlButtonMode: this.GO,
init: function browser_init() {
// Open global history database
GlobalHistory.db.open();
// Assign UI elements to variables
this.toolbarStart = document.getElementById('toolbar-start');
this.urlBar = document.getElementById('url-bar');
this.urlInput = document.getElementById('url-input');
this.urlButton = document.getElementById('url-button');
this.content = document.getElementById('browser-content');
this.backButton = document.getElementById('back-button');
this.forwardButton = document.getElementById('forward-button');
// Add event listeners
this.backButton.addEventListener('click', this.goBack.bind(this));
this.urlButton.addEventListener('click', this.go.bind(this));
this.forwardButton.addEventListener('click', this.goForward.bind(this));
this.urlInput.addEventListener('focus', this.urlFocus.bind(this));
this.urlInput.addEventListener('blur', this.urlBlur.bind(this));
window.addEventListener('submit', this);
window.addEventListener('keyup', this, true);
var browserEvents = ['loadstart', 'loadend', 'locationchange',
'titlechange'];
browserEvents.forEach((function attachBrowserEvent(type) {
this.content.addEventListener('mozbrowser' + type, this);
}).bind(this));
// Load homepage
var url = this.urlInput.value;
this.currentUrl = url;
this.navigate(url);
this.updateHistory(url);
},
handleEvent: function browser_handleEvent(evt) {
var urlInput = this.urlInput;
switch (evt.type) {
case 'submit':
this.go(evt);
break;
case 'keyup':
if (!SessionHistory.backLength() || evt.keyCode != evt.DOM_VK_ESCAPE)
break;
this.goBack();
evt.preventDefault();
break;
case 'mozbrowserloadstart':
this.currentTitle = '';
this.toolbarStart.classList.add('loading');
break;
case 'mozbrowserloadend':
this.toolbarStart.classList.remove('loading');
if (this.currentTitle)
urlInput.value = this.currentTitle;
else
urlInput.value = this.currentUrl;
this.setUrlButtonMode(this.REFRESH);
break;
case 'mozbrowserlocationchange':
this.locationChange(evt.detail);
break;
case 'mozbrowsertitlechange':
this.currentTitle = evt.detail;
if (!this.urlBar.querySelector(':focus'))
urlInput.value = this.currentTitle;
GlobalHistory.setPageTitle(this.currentUrl, this.currentTitle);
break;
}
},
navigate: function browser_navigate(url) {
this.content.setAttribute('src', url);
},
go: function browser_go(evt) {
evt.preventDefault();
if (this.urlButtonMode == this.REFRESH) {
this.navigate(this.currentUrl);
return;
}
var url = this.urlInput.value.trim();
var protocolRegexp = /^([a-z]+:)(\/\/)?/i;
var protocol = protocolRegexp.exec(url);
if (!protocol)
url = 'http://' + url;
if (url != this.currentUrl) {
this.urlInput.value = url;
this.currentUrl = url;
}
this.navigate(url);
this.urlInput.blur();
},
goBack: function browser_goBack() {
SessionHistory.back();
this.backButton.disabled = !SessionHistory.backLength();
this.forwardButton.disabled = !SessionHistory.forwardLength();
},
goForward: function browser_goForward() {
SessionHistory.forward();
this.backButton.disabled = !SessionHistory.backLength();
this.forwardButton.disabled = !SessionHistory.forwardLength();
},
updateHistory: function browser_updateHistory(url) {
SessionHistory.pushState(null, '', url);
GlobalHistory.addVisit(url);
this.backButton.disabled = !SessionHistory.backLength();
this.forwardButton.disabled = !SessionHistory.forwardLength();
},
locationChange: function browser_locationChange(url) {
if (url != this.currentUrl) {
this.currentUrl = url;
this.updateHistory(url);
}
},
urlFocus: function browser_urlFocus() {
this.urlInput.value = this.currentUrl;
this.urlInput.select();
this.setUrlButtonMode(this.GO);
},
urlBlur: function browser_urlBlur() {
if (this.currentTitle)
this.urlInput.value = this.currentTitle;
this.setUrlButtonMode(this.REFRESH);
},
setUrlButtonMode: function browser_setUrlButtonMode(mode) {
this.urlButtonMode = mode;
switch (mode) {
case this.GO:
this.urlButton.src = 'style/images/go.png';
break;
case this.REFRESH:
this.urlButton.src = 'style/images/refresh.png';
break;
}
}
};
window.addEventListener('load', function browserOnLoad(evt) {
window.removeEventListener('load', browserOnLoad);
Browser.init();
});