2021-01-16 22:01:58 +00:00
|
|
|
var searchToggle = document.getElementById( 'search-checkbox' ),
|
|
|
|
searchInput = document.getElementById( 'searchInput' ),
|
|
|
|
pageReady = require( ( 'mediawiki.page.ready' ) );
|
|
|
|
|
|
|
|
/**
|
2021-04-12 19:55:59 +00:00
|
|
|
* Focus and unfocus in search box when search toggle checkbox is toggled.
|
2021-01-16 22:01:58 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function searchInputFocus() {
|
2021-04-12 19:55:59 +00:00
|
|
|
if ( searchToggle.checked === true ) {
|
2021-01-16 22:01:58 +00:00
|
|
|
searchInput.focus();
|
2021-04-12 20:22:16 +00:00
|
|
|
} else {
|
|
|
|
searchInput.blur();
|
2021-04-12 19:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:09:28 +00:00
|
|
|
/**
|
|
|
|
* Toggle search bar with slash
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-04-12 19:55:59 +00:00
|
|
|
function keyboardEvents() {
|
|
|
|
if ( searchToggle.checked === false ) {
|
2021-04-17 04:07:47 +00:00
|
|
|
if ( event.key === '/' &&
|
|
|
|
!event.target.matches( 'input' ) &&
|
|
|
|
!event.target.matches( 'textarea' )
|
2021-04-17 04:06:57 +00:00
|
|
|
) {
|
2021-04-12 20:22:16 +00:00
|
|
|
searchToggle.checked = true;
|
2021-04-12 19:55:59 +00:00
|
|
|
searchInput.focus();
|
|
|
|
searchInput.value = '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( event.key === 'Escape' ) {
|
2021-04-12 20:22:16 +00:00
|
|
|
searchToggle.checked = false;
|
2021-04-12 19:55:59 +00:00
|
|
|
searchInput.blur();
|
|
|
|
}
|
2021-04-12 19:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:01:58 +00:00
|
|
|
/**
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function init() {
|
2021-04-12 20:22:16 +00:00
|
|
|
searchToggle.addEventListener( 'change', searchInputFocus );
|
2021-04-12 19:55:59 +00:00
|
|
|
document.addEventListener( 'keyup', keyboardEvents );
|
2021-01-16 22:01:58 +00:00
|
|
|
pageReady.loadSearchModule(
|
|
|
|
// Decide between new Citizen implementation or core
|
|
|
|
mw.config.get( 'wgCitizenEnableSearch' ) ?
|
|
|
|
'skins.citizen.scripts.search' : 'mediawiki.searchSuggest'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init
|
|
|
|
};
|