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 pathurlBarSuggestions.js
435 lines (396 loc) · 16.4 KB
/
urlBarSuggestions.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* 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/. */
const React = require('react')
const ReactDOM = require('react-dom')
const windowActions = require('../actions/windowActions')
const ImmutableComponent = require('./immutableComponent')
const config = require('../constants/config')
const top500 = require('./../data/top500')
const {aboutUrls, isNavigatableAboutPage, isSourceAboutUrl, isUrl} = require('../lib/appUrlUtil')
const Immutable = require('immutable')
const debounce = require('../lib/debounce')
const settings = require('../constants/settings')
const siteTags = require('../constants/siteTags')
const suggestionTypes = require('../constants/suggestionTypes')
const getSetting = require('../settings').getSetting
const eventUtil = require('../lib/eventUtil')
const cx = require('../lib/classSet')
const locale = require('../l10n')
const windowStore = require('../stores/windowStore')
const suggestion = require('../../app/renderer/lib/suggestion')
class UrlBarSuggestions extends ImmutableComponent {
constructor (props) {
super(props)
this.searchXHR = debounce(this.searchXHR.bind(this), 50)
}
get activeFrame () {
return windowStore.getFrame(this.props.activeFrameKey)
}
get activeIndex () {
if (this.props.suggestionList === null) {
return 0
}
return Math.abs(this.props.selectedIndex % (this.props.suggestionList.size + 1))
}
nextSuggestion () {
// If the user presses down and don't have an explicit selected index, skip to the 2nd one
if (this.props.locationValueSuffix && this.props.selectedIndex === null && this.props.suggestionList.size > 1) {
this.updateSuggestions(2)
return
}
this.updateSuggestions(this.props.selectedIndex + 1)
}
previousSuggestion () {
const suggestions = this.props.suggestionList
if (!suggestions) {
return
}
let newIndex = this.props.selectedIndex - 1
if (newIndex < 0) {
newIndex = suggestions.size
}
this.updateSuggestions(newIndex)
}
blur () {
window.removeEventListener('click', this)
windowActions.setUrlBarSuggestions(null, null)
windowActions.setUrlBarPreview(null)
}
clickSelected (e) {
this.ctrlKey = e.ctrlKey
this.metaKey = e.metaKey
this.shiftKey = e.shiftKey
const node = ReactDOM.findDOMNode(this)
if (node) {
node.getElementsByClassName('selected')[0].click()
}
}
// Whether the suggestions box should be rendered
shouldRender () {
return this.props.suggestionList && this.props.suggestionList.size > 0
}
render () {
window.removeEventListener('click', this)
if (!this.shouldRender()) {
return null
}
// Add an event listener on the window to hide suggestions when they are shown.
window.addEventListener('click', this)
// If there is a URL suffix that means there's an active autocomplete for the first element.
// We should show that as selected so the user knows what is being matched.
const suggestions = this.props.suggestionList
const bookmarkSuggestions = suggestions.filter((s) => s.type === suggestionTypes.BOOKMARK)
const historySuggestions = suggestions.filter((s) => s.type === suggestionTypes.HISTORY)
const aboutPagesSuggestions = suggestions.filter((s) => s.type === suggestionTypes.ABOUT_PAGES)
const tabSuggestions = suggestions.filter((s) => s.type === suggestionTypes.TAB)
const searchSuggestions = suggestions.filter((s) => s.type === suggestionTypes.SEARCH)
const topSiteSuggestions = suggestions.filter((s) => s.type === suggestionTypes.TOP_SITE)
let items = []
let index = 0
const addToItems = (suggestions, sectionKey, title, icon) => {
if (suggestions.size > 0) {
items.push(<li className='suggestionSection'>
{
icon
? <span className={cx({
suggestionSectionIcon: true,
[sectionKey]: true,
fa: true,
[icon]: true
})} />
: null
}
<span className='suggestionSectionTitle'>{title}</span>
</li>)
}
items = items.concat(suggestions.map((suggestion, i) => {
const currentIndex = index + i
const selected = this.activeIndex === currentIndex + 1 || (!this.activeIndex && currentIndex === 0 && this.props.locationValueSuffix)
return <li data-index={currentIndex + 1}
onMouseOver={this.onMouseOver.bind(this)}
onClick={suggestion.onClick}
key={`${suggestion.location}|${index + i}`}
ref={(node) => { selected && (this.selectedElement = node) }}
className={cx({
selected,
suggestionItem: true,
[suggestion.type]: true
})}>
{
suggestion.type !== suggestionTypes.TOP_SITE && suggestion.title
? <div className='suggestionTitle'>{suggestion.title}</div>
: null
}
{
suggestion.type !== suggestionTypes.SEARCH && suggestion.type !== suggestionTypes.ABOUT_PAGES
? <div className='suggestionLocation'>{suggestion.location}</div>
: null
}
</li>
}))
index += suggestions.size
}
addToItems(historySuggestions, 'historyTitle', locale.translation('historySuggestionTitle'), 'fa-clock-o')
addToItems(bookmarkSuggestions, 'bookmarksTitle', locale.translation('bookmarksSuggestionTitle'), 'fa-star-o')
addToItems(aboutPagesSuggestions, 'aboutPagesTitle', locale.translation('aboutPagesSuggestionTitle'), null)
addToItems(tabSuggestions, 'tabsTitle', locale.translation('tabsSuggestionTitle'), 'fa-external-link')
addToItems(searchSuggestions, 'searchTitle', locale.translation('searchSuggestionTitle'), 'fa-search')
addToItems(topSiteSuggestions, 'topSiteTitle', locale.translation('topSiteSuggestionTitle'), 'fa-link')
const documentHeight = Number.parseInt(window.getComputedStyle(document.querySelector(':root')).getPropertyValue('--navbar-height'), 10)
const menuHeight = this.props.menubarVisible ? 30 : 0
return <ul className='urlBarSuggestions' style={{
maxHeight: document.documentElement.offsetHeight - documentHeight - 2 - menuHeight
}}>
{items}
</ul>
}
onMouseOver (e) {
this.updateSuggestions(parseInt(e.target.dataset.index, 10))
}
componentDidMount () {
if (this.props.urlLocation.length > 0) {
this.suggestionList = this.getNewSuggestionList(this.props)
this.searchXHR(this.props, true)
}
}
componentWillUpdate (nextProps) {
if (this.selectedElement) {
this.selectedElement.scrollIntoView()
}
// If both the URL is the same and the number of sites to consider is
// the same then we don't need to regenerate the suggestions list.
if (this.props.urlLocation === nextProps.urlLocation &&
this.props.sites.size === nextProps.sites.size &&
// Make sure to update if there are online suggestions
this.props.searchResults.size === nextProps.searchResults.size) {
return
}
this.suggestionList = this.getNewSuggestionList(nextProps)
this.searchXHR(nextProps, !(this.props.urlLocation === nextProps.urlLocation))
}
getNewSuggestionList (props) {
props = props || this.props
if (!props.urlLocation && !props.urlPreview) {
return null
}
const navigateClickHandler = (formatUrl) => (site, e) => {
// We have a wonky way of fake clicking from keyboard enter,
// so remove the meta keys from the real event here.
e.metaKey = e.metaKey || this.metaKey
e.ctrlKey = e.ctrlKey || this.ctrlKey
e.shiftKey = e.shiftKey || this.shiftKey
delete this.metaKey
delete this.ctrlKey
delete this.shiftKey
const location = formatUrl(site)
// When clicked make sure to hide autocomplete
windowActions.setRenderUrlBarSuggestions(false)
if (eventUtil.isForSecondaryAction(e)) {
windowActions.newFrame({
location,
partitionNumber: site && site.get && site.get('partitionNumber') || undefined
}, !!e.shiftKey)
e.preventDefault()
windowActions.setNavBarFocused(true)
} else {
windowActions.loadUrl(this.activeFrame, location)
windowActions.setUrlBarActive(false)
windowActions.setUrlBarPreview(null)
this.blur()
}
}
const urlLocationLower = props.urlLocation.toLowerCase()
let suggestions = new Immutable.List()
const defaultme = (x) => x
const mapListToElements = ({data, maxResults, type, clickHandler = navigateClickHandler,
sortHandler = defaultme, formatTitle = defaultme, formatUrl = defaultme,
filterValue = (site) => 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) =>
suggestions.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 sortBasedOnLocationPos = (s1, s2) => {
const location1 = suggestion.normalizeLocation(s1.get('location'))
const location2 = suggestion.normalizeLocation(s2.get('location'))
const pos1 = location1.indexOf(urlLocationLower)
const pos2 = location2.indexOf(urlLocationLower)
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)
}
}
}
}
// history
if (getSetting(settings.HISTORY_SUGGESTIONS)) {
suggestions = suggestions.concat(mapListToElements({
data: props.sites,
maxResults: config.urlBarSuggestions.maxHistorySites,
type: suggestionTypes.HISTORY,
clickHandler: navigateClickHandler((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))
// Note: Bookmkark 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.
}
}))
}
// bookmarks
if (getSetting(settings.BOOKMARK_SUGGESTIONS)) {
suggestions = suggestions.concat(mapListToElements({
data: props.sites,
maxResults: config.urlBarSuggestions.maxBookmarkSites,
type: suggestionTypes.BOOKMARK,
clickHandler: navigateClickHandler((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
suggestions = suggestions.concat(mapListToElements({
data: aboutUrls.keySeq().filter((x) => isNavigatableAboutPage(x)),
maxResults: config.urlBarSuggestions.maxAboutPages,
type: suggestionTypes.ABOUT_PAGES,
clickHandler: navigateClickHandler((x) => x)}))
// opened frames
if (getSetting(settings.OPENED_TAB_SUGGESTIONS)) {
suggestions = suggestions.concat(mapListToElements({
data: windowStore.getFrames(),
maxResults: config.urlBarSuggestions.maxOpenedFrames,
type: suggestionTypes.TAB,
clickHandler: (frameProps) =>
windowActions.setActiveFrame(frameProps),
sortHandler: sortBasedOnLocationPos,
formatTitle: (frame) => frame.get('title'),
formatUrl: (frame) => frame.get('location'),
filterValue: (frame) => !isSourceAboutUrl(frame.get('location')) &&
frame.get('key') !== props.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) && props.searchResults) {
suggestions = suggestions.concat(mapListToElements({
data: props.searchResults,
maxResults: config.urlBarSuggestions.maxSearch,
type: suggestionTypes.SEARCH,
clickHandler: navigateClickHandler((searchTerms) => {
let searchURL = props.searchSelectEntry
? props.searchSelectEntry.search : props.searchDetail.get('searchURL')
return searchURL.replace('{searchTerms}', encodeURIComponent(searchTerms))
})
}))
}
// Alexa top 500
suggestions = suggestions.concat(mapListToElements({
data: top500,
maxResults: config.urlBarSuggestions.maxTopSites,
type: suggestionTypes.TOP_SITE,
clickHandler: navigateClickHandler((x) => x)}))
return suggestions
}
updateSuggestions (newIndex) {
const suggestions = this.suggestionList || this.props.suggestionList
if (!suggestions) {
return
}
// Update the urlbar preview content
if (newIndex === 0 || newIndex > suggestions.size) {
windowActions.setUrlBarPreview(null)
newIndex = null
} else {
const currentActive = suggestions.get(newIndex - 1)
if (currentActive && currentActive.title) {
windowActions.setUrlBarPreview(currentActive.title)
}
}
windowActions.setUrlBarSuggestions(suggestions, newIndex)
}
searchXHR (props, searchOnline) {
props = props || this.props
let autocompleteURL = props.searchSelectEntry
? props.searchSelectEntry.autocomplete : props.searchDetail.get('autocompleteURL')
if (!getSetting(settings.OFFER_SEARCH_SUGGESTIONS) || !autocompleteURL) {
windowActions.setUrlBarSuggestionSearchResults(Immutable.fromJS([]))
this.updateSuggestions(props.selectedIndex)
return
}
let urlLocation = props.urlLocation
if (!isUrl(urlLocation) && urlLocation.length > 0) {
if (props.searchSelectEntry) {
const replaceRE = new RegExp('^' + props.searchSelectEntry.shortcut + ' ', 'g')
urlLocation = urlLocation.replace(replaceRE, '')
}
// Render all suggestions asap
this.updateSuggestions(props.selectedIndex)
if (searchOnline) {
const xhr = new window.XMLHttpRequest({mozSystem: true})
xhr.open('GET', autocompleteURL
.replace('{searchTerms}', encodeURIComponent(urlLocation)), true)
xhr.responseType = 'json'
xhr.send()
xhr.onload = () => {
// Once we have the online suggestions, append them to the others
windowActions.setUrlBarSuggestionSearchResults(Immutable.fromJS(xhr.response[1]))
this.updateSuggestions(props.selectedIndex)
}
}
} else {
windowActions.setUrlBarSuggestionSearchResults(Immutable.fromJS([]))
this.updateSuggestions(props.selectedIndex)
}
}
}
module.exports = UrlBarSuggestions