mediawiki-skins-Citizen/resources/skins.citizen.scripts/search.js
2021-04-12 16:22:16 -04:00

54 lines
1.2 KiB
JavaScript

var searchToggle = document.getElementById( 'search-checkbox' ),
searchInput = document.getElementById( 'searchInput' ),
pageReady = require( ( 'mediawiki.page.ready' ) );
/**
* Focus and unfocus in search box when search toggle checkbox is toggled.
*
* @constructor
*/
function searchInputFocus() {
if ( searchToggle.checked === true ) {
searchInput.focus();
} else {
searchInput.blur();
}
}
/**
* Toggle search bar with slash
*
* @constructor
*/
function keyboardEvents() {
if ( searchToggle.checked === false ) {
if ( event.key === '/' && !event.target.matches( 'input' ) ) {
searchToggle.checked = true;
searchInput.focus();
searchInput.value = '';
}
} else {
if ( event.key === 'Escape' ) {
searchToggle.checked = false;
searchInput.blur();
}
}
}
/**
* @return {void}
*/
function init() {
searchToggle.addEventListener( 'change', searchInputFocus );
document.addEventListener( 'keyup', keyboardEvents );
pageReady.loadSearchModule(
// Decide between new Citizen implementation or core
mw.config.get( 'wgCitizenEnableSearch' ) ?
'skins.citizen.scripts.search' : 'mediawiki.searchSuggest'
);
}
module.exports = {
init: init
};