Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/search.js

raw symbols SCIP
1 module.exports = function () {
2 const mobile = require( 'mobile.startup' );
3 const SearchOverlay = mobile.search.SearchOverlay;
4 const SearchGateway = mobile.search.SearchGateway;
5 const overlayManager = mobile.getOverlayManager();
6 // eslint-disable-next-line no-jquery/no-global-selector
7 const $searchInput = $( '#searchInput' );
8 const placeholder = $searchInput.attr( 'placeholder' );
9 const defaultSearchPage = $searchInput.siblings( 'input[name=title]' ).val();
10 // eslint-disable-next-line no-jquery/no-global-selector
11 const $searchBar = $( '#searchInput, #searchIcon, .skin-minerva-search-trigger' );
12 const searchRoute = new RegExp( /\/search/ );
13 let searchOverlayInstance;
14
15 // Only continue on mobile devices as it breaks desktop search
16 // See https://phabricator.wikimedia.org/T108432
17 if ( mw.config.get( 'skin' ) !== 'minerva' ) {
18 return;
19 }
20
21 /**
22 * Hide the search overlay on pageload before the search route
23 * is registered with the overlayManager.
24 * Allows the usage of history.back() to close searchOverlay by
25 * preventing the situation described in https://phabricator.wikimedia.org/T102946
26 */
27 function removeSearchOnPageLoad() {
28 if ( searchRoute.test( overlayManager.router.getPath() ) ) {
29 // TODO: replace when router supports replaceState https://phabricator.wikimedia.org/T189173
30 history.replaceState( '', document.title, window.location.pathname );
31 }
32 }
33
34 function getSearchOverlay() {
35 if ( !searchOverlayInstance ) {
36 searchOverlayInstance = new SearchOverlay( {
37 router: overlayManager.router,
38 gatewayClass: SearchGateway,
39 api: new mw.Api(),
40 autocapitalize: $searchInput.attr( 'autocapitalize' ),
41 searchTerm: $searchInput.val(),
42 placeholderMsg: placeholder,
43 defaultSearchPage: defaultSearchPage
44 } );
45 }
46 return searchOverlayInstance;
47 }
48
49 removeSearchOnPageLoad();
50 overlayManager.add( searchRoute, getSearchOverlay );
51
52 // Apparently needed for main menu to work correctly.
53 $searchBar.prop( 'readonly', true );
54
55 /**
56 * Trigger overlay on touchstart so that the on-screen keyboard on iOS
57 * can be triggered immidiately after on touchend. The keyboard can't be
58 * triggered unless the element is already visible.
59 * Touchstart makes the overlay visible, touchend brings up the keyboard afterwards.
60 */
61 $searchBar.on( 'touchstart click', ( ev ) => {
62 ev.preventDefault();
63 overlayManager.router.navigate( '/search' );
64 } );
65
66 $searchBar.on( 'touchend', ( ev ) => {
67 ev.preventDefault();
68 /**
69 * Manually triggering focus event because on-screen keyboard only
70 * opens when `focus()` is called from a "user context event",
71 * Calling it from the route callback above (which calls SearchOverlay#show)
72 * doesn't work.
73 * http://stackoverflow.com/questions/6837543/show-virtual-keyboard-on-mobile-phones-in-javascript
74 */
75 getSearchOverlay().showKeyboard();
76 } );
77
78 };