This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathurlBarSuggestionsReducer.js
398 lines (373 loc) · 18 KB
/
urlBarSuggestionsReducer.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const windowConstants = require('../../../js/constants/windowConstants')
const getSetting = require('../../../js/settings').getSetting
const {isDefaultEntry} = require('../../../js/state/siteUtil')
const fetchSearchSuggestions = require('../fetchSearchSuggestions')
const {activeFrameStatePath, frameStatePath, getFrameByKey, getFrameKeyByTabId, getActiveFrame} = require('../../../js/state/frameStateUtil')
const searchProviders = require('../../../js/data/searchProviders')
const settings = require('../../../js/constants/settings')
const Immutable = require('immutable')
const config = require('../../../js/constants/config')
const top500 = require('../../../js/data/top500')
const {aboutUrls, isNavigatableAboutPage, isSourceAboutUrl, isUrl} = require('../../../js/lib/appUrlUtil')
const siteTags = require('../../../js/constants/siteTags')
const suggestion = require('../lib/suggestion')
const suggestionTypes = require('../../../js/constants/suggestionTypes')
const {navigateSiteClickHandler, frameClickHandler} = require('../suggestionClickHandlers')
// TODO: I think appStoreRenderer should just be collapsed into windowStore to avoid this
const appStoreRenderer = require('../../../js/stores/appStoreRenderer')
const updateSearchEngineInfoFromInput = (state, frameProps) => {
const input = frameProps.getIn(['navbar', 'urlbar', 'location'])
const frameSearchDetail = frameProps.getIn(['navbar', 'urlbar', 'searchDetail'])
const searchDetailPath = frameStatePath(state, frameProps.get('key')).concat(['navbar', 'urlbar', 'searchDetail'])
if (!input || !input.length || isUrl(input) || !input.startsWith(':')) {
state = state.deleteIn(searchDetailPath)
} else if (!frameSearchDetail || !input.startsWith(frameSearchDetail.get('shortcut') + ' ')) {
let entries = searchProviders.providers
for (let i = 0; i < entries.length; i++) {
const entry = entries[i]
if (input.startsWith(entry.shortcut + ' ')) {
state = state.setIn(
searchDetailPath,
Immutable.fromJS(Object.assign({}, entry, { activateSearchEngine: true })))
return state
}
}
state = state.deleteIn(searchDetailPath)
}
return state
}
const searchXHR = (state, frameProps, searchOnline) => {
const searchDetail = state.get('searchDetail')
const frameSearchDetail = frameProps.getIn(['navbar', 'urlbar', 'searchDetail'])
let autocompleteURL = frameSearchDetail
? frameSearchDetail.get('autocomplete')
: searchDetail.get('autocompleteURL')
if (!getSetting(settings.OFFER_SEARCH_SUGGESTIONS) || !autocompleteURL) {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']), Immutable.fromJS([]))
return state
}
let input = frameProps.getIn(['navbar', 'urlbar', 'location'])
if (!isUrl(input) && input.length > 0) {
if (searchDetail) {
const replaceRE = new RegExp('^' + searchDetail.get('shortcut') + ' ', 'g')
input = input.replace(replaceRE, '')
}
if (searchOnline) {
fetchSearchSuggestions(frameProps.get('tabId'), autocompleteURL, input)
}
} else {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']), Immutable.fromJS([]))
}
return state
}
const setUrlSuggestions = (state, suggestionList) => {
if (suggestionList !== undefined) {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList)
}
state = updateUrlSuffix(state, suggestionList)
return state
}
const setRenderUrlBarSuggestions = (state, enabled) => {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'shouldRender']), enabled)
if (!enabled) {
state = state.mergeIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions']), {
selectedIndex: null,
suggestionList: null
})
// Make sure to remove the suffix from the url bar
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex']), null)
state = updateUrlSuffix(state, undefined)
}
return state
}
/**
* Updates the active frame state with what the URL bar suffix should be.
* @param suggestionList - The suggestion list to use to figure out the suffix.
*/
const updateUrlSuffix = (state, suggestionList) => {
let selectedIndex = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex'])) || 0
const lastSuffix = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'urlSuffix']))
if (!selectedIndex && lastSuffix) {
selectedIndex = 0
}
const suggestion = suggestionList && suggestionList.get(selectedIndex)
let suffix = ''
if (suggestion) {
const autocompleteEnabled = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'autocompleteEnabled']))
if (autocompleteEnabled) {
const location = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'location']))
const index = suggestion.location.toLowerCase().indexOf(location.toLowerCase())
if (index !== -1) {
const beforePrefix = suggestion.location.substring(0, index)
if (beforePrefix.endsWith('://') || beforePrefix.endsWith('://www.') || index === 0) {
suffix = suggestion.location.substring(index + location.length)
}
}
}
}
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'urlSuffix']), suffix)
return state
}
const generateNewSuggestionsList = (state) => {
const activeFrameKey = state.get('activeFrameKey')
const urlLocation = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'location']))
let sites = appStoreRenderer.state.get('sites')
if (sites) {
// Filter out Brave default newtab sites and sites with falsey location
sites = sites.filter((site) => !isDefaultEntry(site) && site.get('location'))
}
const searchResults = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']))
const frameSearchDetail = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'searchDetail']))
const searchDetail = state.get('searchDetail')
if (!urlLocation) {
return state
}
const urlLocationLower = urlLocation.toLowerCase()
let suggestionsList = new Immutable.List()
const defaultme = (x) => x
const mapListToElements = ({data, maxResults, type, clickHandler = navigateSiteClickHandler.bind(this),
sortHandler = defaultme, formatTitle = defaultme, formatUrl = defaultme,
filterValue = (site) => {
return site.toLowerCase().includes(urlLocationLower)
}
}) => // Filter out things which are already in our own list at a smaller index
data
// Per suggestion provider filter
.filter(filterValue)
// Filter out things which are already in the suggestions list
.filter((site) =>
suggestionsList.findIndex((x) => (x.location || '').toLowerCase() === (formatUrl(site) || '').toLowerCase()) === -1 ||
// Tab autosuggestions should always be included since they will almost always be in history
type === suggestionTypes.TAB)
.sort(sortHandler)
.take(maxResults)
.map((site) => {
return {
onClick: clickHandler.bind(null, site),
title: formatTitle(site),
location: formatUrl(site),
type
}
})
const shouldNormalize = suggestion.shouldNormalizeLocation(urlLocationLower)
const urlLocationLowerNormalized = suggestion.normalizeLocation(urlLocationLower)
const sortBasedOnLocationPos = (s1, s2) => {
const location1 = shouldNormalize ? suggestion.normalizeLocation(s1.get('location')) : s1.get('location')
const location2 = shouldNormalize ? suggestion.normalizeLocation(s2.get('location')) : s2.get('location')
const pos1 = location1.indexOf(urlLocationLowerNormalized)
const pos2 = location2.indexOf(urlLocationLowerNormalized)
if (pos1 === -1 && pos2 === -1) {
return 0
} else if (pos1 === -1) {
return 1
} else if (pos2 === -1) {
return -1
} else {
if (pos1 - pos2 !== 0) {
return pos1 - pos2
} else {
// sort site.com higher than site.com/somepath
const sdnv1 = suggestion.simpleDomainNameValue(s1)
const sdnv2 = suggestion.simpleDomainNameValue(s2)
if (sdnv1 !== sdnv2) {
return sdnv2 - sdnv1
} else {
// If there's a tie on the match location, use the age
// decay modified access count
return suggestion.sortByAccessCountWithAgeDecay(s1, s2)
}
}
}
}
const historyFilter = (site) => {
if (!site) {
return false
}
const title = site.get('title') || ''
const location = site.get('location') || ''
// Note: Bookmark sites are now included in history. This will allow
// sites to appear in the auto-complete regardless of their bookmark
// status. If history is turned off, bookmarked sites will appear
// in the bookmark section.
return (title.toLowerCase().includes(urlLocationLower) ||
location.toLowerCase().includes(urlLocationLower)) &&
site.get('lastAccessedTime')
}
var historySites = sites.filter(historyFilter)
// potentially append virtual history items (such as www.google.com when
// searches have been made but the root site has not been visited)
historySites = historySites.concat(suggestion.createVirtualHistoryItems(historySites))
// history
if (getSetting(settings.HISTORY_SUGGESTIONS)) {
suggestionsList = suggestionsList.concat(mapListToElements({
data: historySites,
maxResults: config.urlBarSuggestions.maxHistorySites,
type: suggestionTypes.HISTORY,
clickHandler: navigateSiteClickHandler((site) => {
return site.get('location')
}),
sortHandler: sortBasedOnLocationPos,
formatTitle: (site) => site.get('title'),
formatUrl: (site) => site.get('location'),
filterValue: historyFilter
}))
}
// bookmarks
if (getSetting(settings.BOOKMARK_SUGGESTIONS)) {
suggestionsList = suggestionsList.concat(mapListToElements({
data: sites,
maxResults: config.urlBarSuggestions.maxBookmarkSites,
type: suggestionTypes.BOOKMARK,
clickHandler: navigateSiteClickHandler((site) => {
return site.get('location')
}),
sortHandler: sortBasedOnLocationPos,
formatTitle: (site) => site.get('title'),
formatUrl: (site) => site.get('location'),
filterValue: (site) => {
const title = site.get('title') || ''
const location = site.get('location') || ''
return (title.toLowerCase().includes(urlLocationLower) ||
location.toLowerCase().includes(urlLocationLower)) &&
site.get('tags') && site.get('tags').includes(siteTags.BOOKMARK)
}
}))
}
// about pages
suggestionsList = suggestionsList.concat(mapListToElements({
data: aboutUrls.keySeq().filter((x) => isNavigatableAboutPage(x)),
maxResults: config.urlBarSuggestions.maxAboutPages,
type: suggestionTypes.ABOUT_PAGES,
clickHandler: navigateSiteClickHandler((x) => x)}))
// opened frames
if (getSetting(settings.OPENED_TAB_SUGGESTIONS)) {
suggestionsList = suggestionsList.concat(mapListToElements({
data: state.get('frames'),
maxResults: config.urlBarSuggestions.maxOpenedFrames,
type: suggestionTypes.TAB,
clickHandler: frameClickHandler,
sortHandler: sortBasedOnLocationPos,
formatTitle: (frame) => frame.get('title'),
formatUrl: (frame) => frame.get('location'),
filterValue: (frame) => !isSourceAboutUrl(frame.get('location')) &&
frame.get('key') !== activeFrameKey &&
(
(frame.get('title') && frame.get('title').toLowerCase().includes(urlLocationLower)) ||
(frame.get('location') && frame.get('location').toLowerCase().includes(urlLocationLower))
)
}))
}
// Search suggestions
if (getSetting(settings.OFFER_SEARCH_SUGGESTIONS) && searchResults) {
suggestionsList = suggestionsList.concat(mapListToElements({
data: searchResults,
maxResults: config.urlBarSuggestions.maxSearch,
type: suggestionTypes.SEARCH,
clickHandler: navigateSiteClickHandler((searchTerms) => {
let searchURL = frameSearchDetail
? frameSearchDetail.get('search') : searchDetail.get('searchURL')
return searchURL.replace('{searchTerms}', encodeURIComponent(searchTerms))
})
}))
}
// Alexa top 500
suggestionsList = suggestionsList.concat(mapListToElements({
data: top500,
maxResults: config.urlBarSuggestions.maxTopSites,
type: suggestionTypes.TOP_SITE,
clickHandler: navigateSiteClickHandler((x) => x)}))
return setUrlSuggestions(state, suggestionsList)
}
const urlBarSuggestionsReducer = (state, action) => {
switch (action.actionType) {
case windowConstants.WINDOW_SET_NAVIGATED:
const key = action.key || state.get('activeFrameKey')
state = state.setIn(frameStatePath(state, key).concat(['navbar', 'urlbar', 'suggestions', 'shouldRender']), false)
const frame = getFrameByKey(state, key)
state = updateSearchEngineInfoFromInput(state, frame)
break
case windowConstants.WINDOW_SET_FINDBAR_SHOWN:
if (action.shown) {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'shouldRender']), false)
}
break
case windowConstants.WINDOW_PREVIOUS_URL_BAR_SUGGESTION_SELECTED: {
const selectedIndexPath = activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex'])
const suggestionList = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']))
const selectedIndex = state.getIn(selectedIndexPath)
const lastSuffix = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'urlSuffix']))
if (!selectedIndex && selectedIndex !== 0 && !lastSuffix) {
state = state.setIn(selectedIndexPath, 0)
} else if (selectedIndex > 0) {
state = state.setIn(selectedIndexPath, selectedIndex - 1)
}
state = updateUrlSuffix(state, state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList))
break
}
case windowConstants.WINDOW_NEXT_URL_BAR_SUGGESTION_SELECTED: {
const selectedIndexPath = activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex'])
const suggestionList = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']))
const selectedIndex = state.getIn(selectedIndexPath)
const lastSuffix = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'urlSuffix']))
if (!selectedIndex && selectedIndex !== 0 && !lastSuffix) {
state = state.setIn(selectedIndexPath, 0)
} else if (selectedIndex < suggestionList.size - 1) {
state = state.setIn(selectedIndexPath, selectedIndex + 1)
}
state = updateUrlSuffix(state, state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList))
break
}
case windowConstants.WINDOW_SEARCH_SUGGESTION_RESULTS_AVAILABLE:
const frameKey = getFrameKeyByTabId(state, action.tabId)
state = state.setIn(frameStatePath(state, frameKey).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']), action.searchResults)
state = generateNewSuggestionsList(state)
break
case windowConstants.WINDOW_SET_NAVBAR_INPUT:
const activeFrameProps = getActiveFrame(state)
state = updateSearchEngineInfoFromInput(state, activeFrameProps)
state = searchXHR(state, activeFrameProps, true)
state = generateNewSuggestionsList(state)
state = updateUrlSuffix(state, state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), action.suggestionList))
if (!action.location) {
state = setRenderUrlBarSuggestions(state, false)
}
break
case windowConstants.WINDOW_URL_BAR_AUTOCOMPLETE_ENABLED:
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'autocompleteEnabled']), action.enabled)
break
case windowConstants.WINDOW_SET_URL_BAR_SUGGESTIONS:
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex']), action.selectedIndex)
state = setUrlSuggestions(state, action.suggestionList)
break
case windowConstants.WINDOW_SET_URL_BAR_ACTIVE:
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'active']), action.isActive)
if (!action.isActive) {
state = state.setIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'shouldRender']), false)
state = state.mergeIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions']), {
selectedIndex: null,
suggestionList: null
})
}
break
case windowConstants.WINDOW_SET_RENDER_URL_BAR_SUGGESTIONS:
state = setRenderUrlBarSuggestions(state, action.enabled)
break
case windowConstants.WINDOW_ACTIVE_URL_BAR_SUGGESTION_CLICKED:
const selectedIndex = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'selectedIndex'])) || 0
const suggestionList = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']))
if (suggestionList.size > 0) {
// It's important this doesn't run sync or else the returned state below will overwrite anything done in the click handler
setImmediate(() =>
suggestionList.get(selectedIndex).onClick(action.isForSecondaryAction, action.shiftKey))
}
break
default:
return state
}
return state
}
module.exports = urlBarSuggestionsReducer