2019-07-02 21:10:10 +00:00
|
|
|
module.exports = function () {
|
2024-03-24 20:53:43 +00:00
|
|
|
const mobile = require( 'mobile.startup' );
|
|
|
|
const SearchOverlay = mobile.search.SearchOverlay;
|
|
|
|
const SearchGateway = mobile.search.SearchGateway;
|
|
|
|
const overlayManager = mobile.getOverlayManager();
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
const $searchInput = $( '#searchInput' );
|
|
|
|
const placeholder = $searchInput.attr( 'placeholder' );
|
|
|
|
const defaultSearchPage = $searchInput.siblings( 'input[name=title]' ).val();
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
const $searchBar = $( '#searchInput, #searchIcon, .skin-minerva-search-trigger' );
|
|
|
|
const searchRoute = new RegExp( /\/search/ );
|
|
|
|
let searchOverlayInstance;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
// Only continue on mobile devices as it breaks desktop search
|
|
|
|
// See https://phabricator.wikimedia.org/T108432
|
|
|
|
if ( mw.config.get( 'skin' ) !== 'minerva' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:34 +00:00
|
|
|
/**
|
|
|
|
* Hide the search overlay on pageload before the search route
|
|
|
|
* is registered with the overlayManager.
|
|
|
|
* Allows the usage of history.back() to close searchOverlay by
|
|
|
|
* preventing the situation described in https://phabricator.wikimedia.org/T102946
|
|
|
|
*/
|
|
|
|
function removeSearchOnPageLoad() {
|
|
|
|
if ( searchRoute.test( overlayManager.router.getPath() ) ) {
|
|
|
|
// TODO: replace when router supports replaceState https://phabricator.wikimedia.org/T189173
|
|
|
|
history.replaceState( '', document.title, window.location.pathname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSearchOverlay() {
|
|
|
|
if ( !searchOverlayInstance ) {
|
|
|
|
searchOverlayInstance = new SearchOverlay( {
|
|
|
|
router: overlayManager.router,
|
|
|
|
gatewayClass: SearchGateway,
|
|
|
|
api: new mw.Api(),
|
2023-01-31 02:09:27 +00:00
|
|
|
autocapitalize: $searchInput.attr( 'autocapitalize' ),
|
2019-01-09 13:57:26 +00:00
|
|
|
searchTerm: $searchInput.val(),
|
2021-04-01 16:36:18 +00:00
|
|
|
placeholderMsg: placeholder,
|
|
|
|
defaultSearchPage: defaultSearchPage
|
2018-03-20 14:17:34 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
return searchOverlayInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSearchOnPageLoad();
|
|
|
|
overlayManager.add( searchRoute, getSearchOverlay );
|
|
|
|
|
|
|
|
// Apparently needed for main menu to work correctly.
|
2019-01-09 13:57:26 +00:00
|
|
|
$searchBar.prop( 'readonly', true );
|
2018-03-20 14:17:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger overlay on touchstart so that the on-screen keyboard on iOS
|
|
|
|
* can be triggered immidiately after on touchend. The keyboard can't be
|
|
|
|
* triggered unless the element is already visible.
|
|
|
|
* Touchstart makes the overlay visible, touchend brings up the keyboard afterwards.
|
|
|
|
*/
|
2019-01-09 13:57:26 +00:00
|
|
|
$searchBar.on( 'touchstart click', function ( ev ) {
|
2018-03-20 14:17:34 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
overlayManager.router.navigate( '/search' );
|
|
|
|
} );
|
|
|
|
|
2019-01-09 13:57:26 +00:00
|
|
|
$searchBar.on( 'touchend', function ( ev ) {
|
2018-03-20 14:17:34 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
/**
|
|
|
|
* Manually triggering focus event because on-screen keyboard only
|
|
|
|
* opens when `focus()` is called from a "user context event",
|
|
|
|
* Calling it from the route callback above (which calls SearchOverlay#show)
|
|
|
|
* doesn't work.
|
|
|
|
* http://stackoverflow.com/questions/6837543/show-virtual-keyboard-on-mobile-phones-in-javascript
|
|
|
|
*/
|
|
|
|
getSearchOverlay().showKeyboard();
|
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-07-02 21:10:10 +00:00
|
|
|
};
|