mediawiki-skins-Citizen/resources/skins.citizen.scripts/search.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

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();
2021-04-12 20:22:16 +00:00
} else {
searchInput.blur();
}
}
2021-04-12 19:09:28 +00:00
/**
* Toggle search bar with slash
*
* @constructor
*/
function keyboardEvents() {
if ( searchToggle.checked === false ) {
if ( event.key === '/' && !event.target.matches( 'input' ) ) {
2021-04-12 20:22:16 +00:00
searchToggle.checked = true;
searchInput.focus();
searchInput.value = '';
}
} else {
if ( event.key === 'Escape' ) {
2021-04-12 20:22:16 +00:00
searchToggle.checked = false;
searchInput.blur();
}
2021-04-12 19:09:28 +00:00
}
}
/**
* @return {void}
*/
function init() {
2021-04-12 20:22:16 +00:00
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
};