-
Notifications
You must be signed in to change notification settings - Fork 25
/
SearchOverlay.js
317 lines (290 loc) · 8.98 KB
/
SearchOverlay.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
var
mfExtend = require( '../mfExtend' ),
Overlay = require( '../Overlay' ),
util = require( '../util' ),
searchHeader = require( './searchHeader' ),
SearchResultsView = require( './SearchResultsView' ),
WatchstarPageList = require( '../watchstar/WatchstarPageList' ),
SEARCH_DELAY = 300,
SEARCH_SPINNER_DELAY = 2000;
/**
* Overlay displaying search results
*
* @class SearchOverlay
* @extends Overlay
* @uses SearchGateway
* @uses Icon
*
* @param {Object} params Configuration options
* @param {string} params.placeholderMsg Search input placeholder text.
* @param {string} [params.action] of form defaults to the value of wgScript
* @param {string} [params.defaultSearchPage] The default search page e.g. Special:Search
* @param {SearchGateway} [params.gateway]
*/
function SearchOverlay( params ) {
var header = searchHeader(
params.placeholderMsg,
params.action || mw.config.get( 'wgScript' ),
function ( query ) {
this.performSearch( query );
}.bind( this ),
params.defaultSearchPage || '',
params.autocapitalize
),
options = util.extend( true, {
headerChrome: true,
isBorderBox: false,
className: 'overlay search-overlay',
headers: [ header ],
events: {
'click .search-content': 'onClickSearchContent',
'click .overlay-content': 'onClickOverlayContent',
'click .overlay-content > div': function ( ev ) {
ev.stopPropagation();
},
'touchstart .results': 'hideKeyboardOnScroll',
'mousedown .results': 'hideKeyboardOnScroll',
'click .results a': 'onClickResult'
}
},
params );
this.header = header;
Overlay.call( this, options );
this.api = options.api;
// eslint-disable-next-line new-cap
this.gateway = options.gateway || new options.gatewayClass( this.api );
this.router = options.router;
this.currentSearchId = null;
}
mfExtend( SearchOverlay, Overlay, {
/**
* Initialize 'search within pages' functionality
*
* @memberof SearchOverlay
* @instance
*/
onClickSearchContent: function () {
var
$form = this.$el.find( 'form' ),
$el = $form[ 0 ].parentNode;
// Add fulltext input to force fulltext search
this.parseHTML( '<input>' )
.attr( {
type: 'hidden',
name: 'fulltext',
value: 'search'
} )
.appendTo( $form );
// history.back queues a task so might run after this call. Thus we use setTimeout
// http://www.w3.org/TR/2011/WD-html5-20110113/webappapis.html#queue-a-task
setTimeout( function () {
// Firefox doesn't allow submission of a form not in the DOM
// so temporarily re-add it if it's gone.
if ( !$form[ 0 ].parentNode ) {
$form.appendTo( $el );
}
$form.trigger( 'submit' );
}, 0 );
},
/**
* Tapping on background only should hide the overlay
*
* @memberof SearchOverlay
* @instance
*/
onClickOverlayContent: function () {
this.$el.find( '.cancel' ).trigger( 'click' );
},
/**
* Hide the keyboard when scrolling starts (avoid weird situation when
* user taps on an item, the keyboard hides and wrong item is clicked).
*
* @memberof SearchOverlay
* @instance
*/
hideKeyboardOnScroll: function () {
this.$input.trigger( 'blur' );
},
/**
* Handle the user clicking a result.
*
* @memberof SearchOverlay
* @instance
* @param {jQuery.Event} ev
*/
onClickResult: function ( ev ) {
var
self = this,
$link = this.$el.find( ev.currentTarget );
/**
* Fired when the user clicks a search result
*
* @type {Object}
* @property {jQuery.Object} result The jQuery-wrapped DOM element that
* the user clicked
* @property {number} resultIndex The zero-based index of the
* result in the set of results
* @property {jQuery.Event} originalEvent The original event
*/
// FIXME: ugly hack that removes search from browser history
// when navigating to search results
ev.preventDefault();
this.router.back().then( function () {
// T308288: Appends the current search id as a url param on clickthroughs
if ( this.currentSearchId ) {
var clickUri = new mw.Uri( location.href );
clickUri.query.searchToken = this.currentSearchId;
self.router.navigateTo( document.title, {
path: clickUri.toString(),
useReplaceState: true
} );
this.currentSearchId = null;
}
// Router.navigate does not support changing href.
// FIXME: Needs upstream change T189173
// eslint-disable-next-line no-restricted-properties
window.location.href = $link.attr( 'href' );
} );
},
/**
* @inheritdoc
* @memberof SearchOverlay
* @instance
*/
postRender: function () {
var self = this,
searchResults = new SearchResultsView( {
searchContentLabel: mw.msg( 'mobile-frontend-search-content' ),
noResultsMsg: mw.msg( 'mobile-frontend-search-no-results' ),
searchContentNoResultsMsg: mw.message( 'mobile-frontend-search-content-no-results' ).parse()
} ),
timer;
this.$el.find( '.overlay-content' ).append( searchResults.$el );
Overlay.prototype.postRender.call( this );
// FIXME: `this.$input` should not be set. Isolate to searchHeader function
this.$input = this.$el.find( this.header ).find( 'input' );
// FIXME: `this.$searchContent` should not be set. Isolate to SearchResultsView class.
this.$searchContent = searchResults.$el.hide();
// FIXME: `this.$resultContainer` should not be set. Isolate to SearchResultsView class.
this.$resultContainer = searchResults.$el.find( '.results-list-container' );
// On iOS a touchstart event while the keyboard is open will result in a scroll
// leading to an accidental click (T299846)
// Stopping propagation when the input is focused will prevent scrolling while
// the keyboard is collapsed.
this.$resultContainer[ 0 ].addEventListener( 'touchstart', ( ev ) => {
if ( document.activeElement === this.$input[0] ) {
ev.stopPropagation();
}
} );
/**
* Hide the spinner and abort timed spinner shows.
* FIXME: Given this manipulates SearchResultsView this should be moved into that class
*/
function clearSearch() {
self.$spinner.hide();
clearTimeout( timer );
}
// Show a spinner on top of search results
// FIXME: Given this manipulates SearchResultsView this should be moved into that class
this.$spinner = searchResults.$el.find( '.spinner-container' );
this.on( 'search-start', function ( searchData ) {
if ( timer ) {
clearSearch();
}
timer = setTimeout( function () {
self.$spinner.show();
}, SEARCH_SPINNER_DELAY - searchData.delay );
} );
this.on( 'search-results', clearSearch );
},
/**
* Trigger a focus() event on search input in order to
* bring up the virtual keyboard.
*
* @memberof SearchOverlay
* @instance
*/
showKeyboard: function () {
var len = this.$input.val().length;
this.$input.trigger( 'focus' );
// Cursor to the end of the input
if ( this.$input[0].setSelectionRange ) {
this.$input[0].setSelectionRange( len, len );
}
},
/**
* @inheritdoc
* @memberof SearchOverlay
* @instance
*/
show: function () {
// Overlay#show defines the actual overlay visibility.
Overlay.prototype.show.apply( this, arguments );
this.showKeyboard();
},
/**
* Perform search and render results inside current view.
* FIXME: Much of the logic for caching and pending queries inside this function should
* actually live in SearchGateway, please move out.
*
* @memberof SearchOverlay
* @instance
* @param {string} query
*/
performSearch: function ( query ) {
var
self = this,
api = this.api,
delay = this.gateway.isCached( query ) ? 0 : SEARCH_DELAY;
// it seems the input event can be fired when virtual keyboard is closed
// (Chrome for Android)
if ( query !== this.lastQuery ) {
if ( self._pendingQuery ) {
self._pendingQuery.abort();
}
clearTimeout( this.timer );
if ( query.length ) {
this.timer = setTimeout( function () {
var xhr;
xhr = self.gateway.search( query );
self._pendingQuery = xhr.then( function ( data ) {
this.currentSearchId = data.searchId;
// FIXME: Given this manipulates SearchResultsView
// this should be moved into that class
// check if we're getting the rights response in case of out of
// order responses (need to get the current value of the input)
if ( data && data.query === self.$input.val() ) {
self.$el.toggleClass( 'no-results', data.results.length === 0 );
self.$searchContent
.show()
.find( 'p' )
.hide()
.filter( data.results.length ? '.with-results' : '.without-results' )
.show();
// eslint-disable-next-line no-new
new WatchstarPageList( {
api: api,
funnel: 'search',
pages: data.results,
el: self.$resultContainer
} );
self.$results = self.$resultContainer.find( 'li' );
}
} ).promise( { abort: function () { xhr.abort(); } } );
}, delay );
} else {
self.resetSearch();
}
this.lastQuery = query;
}
},
/**
* Clear results
*
* @private
*/
resetSearch: function () {
this.$el.find( '.overlay-content' ).children().hide();
}
} );
module.exports = SearchOverlay;