-
-
Notifications
You must be signed in to change notification settings - Fork 680
/
browserUI.js
233 lines (184 loc) · 5.52 KB
/
browserUI.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var statistics = require('js/statistics.js')
var searchEngine = require('js/util/searchEngine.js')
var urlParser = require('js/util/urlParser.js')
/* common actions that affect different parts of the UI (webviews, tabstrip, etc) */
var settings = require('util/settings/settings.js')
var webviews = require('webviews.js')
var focusMode = require('focusMode.js')
var tabBar = require('navbar/tabBar.js')
var tabEditor = require('navbar/tabEditor.js')
var searchbar = require('searchbar/searchbar.js')
/* creates a new task */
function addTask () {
tasks.setSelected(tasks.add())
tabBar.updateAll()
addTab()
}
/* creates a new tab */
/*
options
options.enterEditMode - whether to enter editing mode when the tab is created. Defaults to true.
options.openInBackground - whether to open the tab without switching to it. Defaults to false.
*/
function addTab (tabId = tabs.add(), options = {}) {
/*
adding a new tab should destroy the current one if either:
* The current tab is an empty, non-private tab, and the new tab is private
* The current tab is empty, and the new tab has a URL
*/
if (!options.openInBackground && !tabs.get(tabs.getSelected()).url && ((!tabs.get(tabs.getSelected()).private && tabs.get(tabId).private) || tabs.get(tabId).url)) {
destroyTab(tabs.getSelected())
}
tabBar.addTab(tabId)
webviews.add(tabId)
if (!options.openInBackground) {
switchToTab(tabId, {
focusWebview: options.enterEditMode === false
})
if (options.enterEditMode !== false) {
tabEditor.show(tabId)
}
} else {
tabBar.getTab(tabId).scrollIntoView()
}
}
/* destroys a task object and the associated webviews */
function destroyTask (id) {
var task = tasks.get(id)
task.tabs.forEach(function (tab) {
webviews.destroy(tab.id)
})
tasks.destroy(id)
}
/* destroys the webview and tab element for a tab */
function destroyTab (id) {
tabBar.removeTab(id)
tabs.destroy(id) // remove from state - returns the index of the destroyed tab
webviews.destroy(id) // remove the webview
}
/* destroys a task, and either switches to the next most-recent task or creates a new one */
function closeTask (taskId) {
var previousCurrentTask = tasks.getSelected().id
destroyTask(taskId)
if (taskId === previousCurrentTask) {
// the current task was destroyed, find another task to switch to
if (tasks.getLength() === 0) {
// there are no tasks left, create a new one
return addTask()
} else {
// switch to the most-recent task
var recentTaskList = tasks.map(function (task) {
return { id: task.id, lastActivity: tasks.getLastActivity(task.id) }
})
const mostRecent = recentTaskList.reduce(
(latest, current) => current.lastActivity > latest.lastActivity ? current : latest
)
return switchToTask(mostRecent.id)
}
}
}
/* destroys a tab, and either switches to the next tab or creates a new one */
function closeTab (tabId) {
/* disabled in focus mode */
if (focusMode.enabled()) {
focusMode.warn()
return
}
if (tabId === tabs.getSelected()) {
var currentIndex = tabs.getIndex(tabs.getSelected())
var nextTab =
tabs.getAtIndex(currentIndex - 1) || tabs.getAtIndex(currentIndex + 1)
destroyTab(tabId)
if (nextTab) {
switchToTab(nextTab.id)
} else {
addTab()
}
} else {
destroyTab(tabId)
}
}
/* changes the currently-selected task and updates the UI */
function switchToTask (id) {
tasks.setSelected(id)
tabBar.updateAll()
var taskData = tasks.get(id)
if (taskData.tabs.count() > 0) {
var selectedTab = taskData.tabs.getSelected()
// if the task has no tab that is selected, switch to the most recent one
if (!selectedTab) {
selectedTab = taskData.tabs.get().sort(function (a, b) {
return b.lastActivity - a.lastActivity
})[0].id
}
switchToTab(selectedTab)
} else {
addTab()
}
}
/* switches to a tab - update the webview, state, tabstrip, etc. */
function switchToTab (id, options) {
options = options || {}
tabEditor.hide()
tabs.setSelected(id)
tabBar.setActiveTab(id)
webviews.setSelected(id, {
focus: options.focusWebview !== false
})
}
webviews.bindEvent('did-create-popup', function (tabId, popupId) {
var popupTab = tabs.add({
private: tabs.get(tabId).private
})
tabBar.addTab(popupTab)
webviews.add(popupTab, popupId)
switchToTab(popupTab)
})
webviews.bindEvent('new-tab', function (tabId, url) {
var newTab = tabs.add({
url: url,
private: tabs.get(tabId).private // inherit private status from the current tab
})
addTab(newTab, {
enterEditMode: false,
openInBackground: !settings.get('openTabsInForeground')
})
})
webviews.bindIPC('close-window', function (tabId, args) {
closeTab(tabId)
})
searchbar.events.on('url-selected', function (data) {
var searchbarQuery = searchEngine.getSearch(urlParser.parse(data.url))
if (searchbarQuery) {
statistics.incrementValue('searchCounts.' + searchbarQuery.engine)
}
if (data.background) {
var newTab = tabs.add({
url: data.url,
private: tabs.get(tabs.getSelected()).private
})
addTab(newTab, {
enterEditMode: false,
openInBackground: true
})
} else {
webviews.update(tabs.getSelected(), data.url)
tabEditor.hide()
}
})
tabBar.events.on('tab-selected', function (id) {
switchToTab(id)
})
tabBar.events.on('tab-closed', function (id) {
closeTab(id)
})
module.exports = {
addTask,
addTab,
destroyTask,
destroyTab,
closeTask,
closeTab,
switchToTask,
switchToTab
}