2022-12-03 18:42:01 +00:00
|
|
|
const
|
2022-12-04 07:13:37 +00:00
|
|
|
PREFIX = 'citizen-typeahead',
|
2022-12-14 05:45:43 +00:00
|
|
|
SEARCH_LOADING_CLASS = 'citizen-loading',
|
2022-12-14 16:22:50 +00:00
|
|
|
ITEM_CLASS = `${PREFIX}__item`,
|
|
|
|
ACTIVE_CLASS = `${ITEM_CLASS}--active`,
|
|
|
|
HIDDEN_CLASS = `${ITEM_CLASS}--hidden`;
|
|
|
|
|
2023-08-02 22:56:08 +00:00
|
|
|
// Config object from getCitizenSearchResourceLoaderConfig()
|
2022-12-14 16:22:50 +00:00
|
|
|
const config = require( './config.json' );
|
2023-08-02 22:56:08 +00:00
|
|
|
|
|
|
|
// Search clients definition
|
|
|
|
const searchClients = require( './searchClients/searchClients.json' );
|
|
|
|
|
|
|
|
const searchClient = {
|
|
|
|
active: null,
|
|
|
|
getData: function ( id ) {
|
|
|
|
const data = Object.values( searchClients ).find( ( item ) => item.id === id );
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
setActive: function ( id ) {
|
|
|
|
const data = this.getData( id );
|
|
|
|
if ( data ) {
|
|
|
|
const client = require( `./searchClients/${data.id}.js` );
|
|
|
|
this.active = data;
|
|
|
|
this.active.client = client( config );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-05-17 20:34:14 +00:00
|
|
|
|
|
|
|
const activeIndex = {
|
|
|
|
index: -1,
|
2023-01-06 21:03:02 +00:00
|
|
|
max: 0,
|
2021-05-17 20:34:14 +00:00
|
|
|
setMax: function ( x ) {
|
2022-12-14 15:28:45 +00:00
|
|
|
this.max = x;
|
2021-05-17 20:34:14 +00:00
|
|
|
},
|
|
|
|
increment: function ( i ) {
|
|
|
|
this.index += i;
|
|
|
|
if ( this.index < 0 ) {
|
|
|
|
this.setIndex( this.max - 1 );
|
|
|
|
} // Index reaches top
|
|
|
|
if ( this.index === this.max ) {
|
|
|
|
this.setIndex( 0 );
|
|
|
|
} // Index reaches bottom
|
|
|
|
return this.index;
|
|
|
|
},
|
|
|
|
setIndex: function ( i ) {
|
|
|
|
if ( i <= this.max - 1 ) {
|
|
|
|
this.index = i;
|
|
|
|
}
|
|
|
|
return this.index;
|
|
|
|
},
|
|
|
|
clear: function () {
|
|
|
|
this.setIndex( -1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-14 16:39:18 +00:00
|
|
|
let /** @type {HTMLElement | undefined} */ typeahead;
|
|
|
|
let /** @type {HTMLElement | undefined} */ searchInput;
|
2023-01-06 21:03:02 +00:00
|
|
|
let /** @type {HTMLCollection | undefined} */ typeaheadItems;
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} MenuItemData
|
|
|
|
* @property {string} id
|
|
|
|
* @property {string} type
|
|
|
|
* @property {string} link
|
|
|
|
* @property {string} icon
|
|
|
|
* @property {string} thumbnail
|
|
|
|
* @property {string} title
|
|
|
|
* @property {string} label
|
2023-01-06 21:03:02 +00:00
|
|
|
* @property {string} desc
|
2022-12-14 16:39:18 +00:00
|
|
|
*/
|
|
|
|
|
2023-01-06 21:03:02 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the current list of item elements to update the active index
|
|
|
|
*/
|
|
|
|
function updateActiveIndex() {
|
|
|
|
typeaheadItems = typeahead.querySelectorAll( `.${ITEM_CLASS}` );
|
|
|
|
activeIndex.setMax( typeaheadItems.length );
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
|
|
|
* Sets an 'ACTIVE_CLASS' on the element
|
|
|
|
*
|
2021-05-17 20:34:14 +00:00
|
|
|
* @param {HTMLElement} element
|
|
|
|
*/
|
|
|
|
function toggleActive( element ) {
|
|
|
|
for ( let i = 0; i < typeaheadItems.length; i++ ) {
|
|
|
|
if ( element !== typeaheadItems[ i ] ) {
|
2022-12-06 18:52:14 +00:00
|
|
|
typeaheadItems[ i ].classList.remove( ACTIVE_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
} else {
|
2022-12-06 18:52:14 +00:00
|
|
|
if ( element.classList.contains( ACTIVE_CLASS ) ) {
|
|
|
|
element.classList.remove( ACTIVE_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
} else {
|
2022-12-06 18:52:14 +00:00
|
|
|
element.classList.add( ACTIVE_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
searchInput.setAttribute( 'aria-activedescendant', element.id );
|
|
|
|
activeIndex.setIndex( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyboard events: up arrow, down arrow and enter.
|
|
|
|
* moves the 'active' suggestion up and down.
|
|
|
|
*
|
|
|
|
* @param {Event} event
|
|
|
|
*/
|
|
|
|
function keyboardEvents( event ) {
|
|
|
|
if ( event.defaultPrevented ) {
|
|
|
|
return; // Do nothing if the event was already processed
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event.key === 'ArrowDown' || event.key === 'ArrowUp' ) {
|
|
|
|
if ( event.key === 'ArrowDown' ) {
|
|
|
|
activeIndex.increment( 1 );
|
|
|
|
} else {
|
|
|
|
activeIndex.increment( -1 );
|
|
|
|
}
|
|
|
|
toggleActive( typeaheadItems[ activeIndex.index ] );
|
|
|
|
}
|
2022-12-05 20:35:31 +00:00
|
|
|
|
2022-12-06 02:10:27 +00:00
|
|
|
if ( typeaheadItems[ activeIndex.index ] ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const link = typeaheadItems[ activeIndex.index ].querySelector( `.${PREFIX}__content` );
|
2022-12-06 02:10:27 +00:00
|
|
|
if ( event.key === 'Enter' && link instanceof HTMLAnchorElement ) {
|
|
|
|
event.preventDefault();
|
|
|
|
link.click();
|
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
2021-05-17 20:34:14 +00:00
|
|
|
* Bind mouseenter and mouseleave event to reproduce mouse hover event
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
*/
|
|
|
|
function bindMouseHoverEvent( element ) {
|
|
|
|
element.addEventListener( 'mouseenter', ( event ) => {
|
|
|
|
toggleActive( event.currentTarget );
|
|
|
|
} );
|
|
|
|
element.addEventListener( 'mouseleave', ( event ) => {
|
|
|
|
toggleActive( event.currentTarget );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all existing suggestions from typeahead
|
|
|
|
*/
|
|
|
|
function clearSuggestions() {
|
2023-01-06 21:03:02 +00:00
|
|
|
const typeaheadChildren = typeahead.children;
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2023-01-06 21:03:02 +00:00
|
|
|
if ( typeaheadChildren.length > 0 ) {
|
2022-12-14 05:45:43 +00:00
|
|
|
// Do all the work in document fragment then replace the whole list
|
|
|
|
// It is more performant this way
|
|
|
|
const
|
|
|
|
fragment = new DocumentFragment(),
|
2022-12-14 16:22:50 +00:00
|
|
|
template = document.getElementById( `${PREFIX}-template` );
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2023-01-06 21:03:02 +00:00
|
|
|
[ ...typeaheadChildren ].forEach( ( item ) => {
|
2023-03-01 21:31:42 +00:00
|
|
|
if ( !item.classList.contains( `${ITEM_CLASS}-page` ) ) {
|
2022-12-14 05:45:43 +00:00
|
|
|
fragment.append( item );
|
2021-05-18 16:25:34 +00:00
|
|
|
}
|
2022-12-14 05:45:43 +00:00
|
|
|
} );
|
|
|
|
fragment.append( template );
|
2023-06-21 18:30:45 +00:00
|
|
|
typeahead.innerHTML = '';
|
|
|
|
typeahead.append( fragment );
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove loading animation
|
2021-06-12 13:54:47 +00:00
|
|
|
searchInput.parentNode.classList.remove( SEARCH_LOADING_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
searchInput.setAttribute( 'aria-activedescendant', '' );
|
|
|
|
activeIndex.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch suggestions from API and render the suggetions in HTML
|
|
|
|
*
|
|
|
|
* @param {string} searchQuery
|
2022-12-06 18:52:14 +00:00
|
|
|
* @param {string} htmlSafeSearchQuery
|
|
|
|
* @param {HTMLElement} placeholder
|
2021-05-17 20:34:14 +00:00
|
|
|
*/
|
2022-12-06 18:52:14 +00:00
|
|
|
function getSuggestions( searchQuery, htmlSafeSearchQuery, placeholder ) {
|
2021-05-17 20:34:14 +00:00
|
|
|
const renderSuggestions = ( results ) => {
|
2022-12-06 18:52:14 +00:00
|
|
|
if ( results.length > 0 ) {
|
2023-08-02 22:56:08 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
|
|
|
* Return the redirect title with search query highlight
|
|
|
|
*
|
|
|
|
* @param {string} text
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2022-12-06 18:52:14 +00:00
|
|
|
const highlightTitle = ( text ) => {
|
|
|
|
const regex = new RegExp( mw.util.escapeRegExp( htmlSafeSearchQuery ), 'i' );
|
2022-12-14 16:22:50 +00:00
|
|
|
return text.replace( regex, `<span class="${PREFIX}__highlight">$&</span>` );
|
2022-12-03 19:12:01 +00:00
|
|
|
};
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
|
|
|
* Return the HTML of the redirect label
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} matchedTitle
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2022-12-06 18:52:14 +00:00
|
|
|
const getRedirectLabel = ( title, matchedTitle ) => {
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
|
|
|
* Check if the redirect is useful (T303013)
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2022-12-06 18:52:14 +00:00
|
|
|
const isRedirectUseful = () => {
|
|
|
|
// Change to lowercase then remove space and dashes
|
|
|
|
const cleanup = ( text ) => {
|
|
|
|
return text.toLowerCase().replace( /-|\s/g, '' );
|
|
|
|
};
|
2022-12-07 23:05:14 +00:00
|
|
|
const
|
|
|
|
cleanTitle = cleanup( title ),
|
|
|
|
cleanMatchedTitle = cleanup( matchedTitle );
|
|
|
|
|
2022-12-14 15:28:45 +00:00
|
|
|
return !(
|
|
|
|
cleanTitle.includes( cleanMatchedTitle ) ||
|
|
|
|
cleanMatchedTitle.includes( cleanTitle )
|
|
|
|
);
|
2022-12-06 18:52:14 +00:00
|
|
|
};
|
2022-12-03 19:12:01 +00:00
|
|
|
|
2022-12-06 18:52:14 +00:00
|
|
|
let html = '';
|
|
|
|
// Result is a redirect
|
|
|
|
// Show the redirect title and highlight it
|
|
|
|
if ( matchedTitle && isRedirectUseful() ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
html = `<div class="${PREFIX}__labelItem" title="${mw.message( 'search-redirect', matchedTitle ).plain()}">
|
|
|
|
<span class="citizen-ui-icon mw-ui-icon-wikimedia-articleRedirect"></span>
|
|
|
|
<span>${highlightTitle( matchedTitle )}</span>
|
|
|
|
</div>`;
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
2022-12-03 19:12:01 +00:00
|
|
|
|
2022-12-06 18:52:14 +00:00
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create suggestion items
|
|
|
|
results.forEach( ( result, index ) => {
|
2022-12-06 19:27:08 +00:00
|
|
|
const data = {
|
2022-12-14 16:22:50 +00:00
|
|
|
id: `${PREFIX}-suggestion-${index}`,
|
2022-12-14 04:52:54 +00:00
|
|
|
type: 'page',
|
2023-03-01 21:31:42 +00:00
|
|
|
size: 'md',
|
2023-08-02 22:56:08 +00:00
|
|
|
link: result.url,
|
2022-12-06 18:52:14 +00:00
|
|
|
title: highlightTitle( result.title ),
|
2023-08-02 22:56:08 +00:00
|
|
|
desc: result.description
|
2022-12-06 19:27:08 +00:00
|
|
|
};
|
2023-08-02 22:56:08 +00:00
|
|
|
if ( result.label ) {
|
|
|
|
data.label = getRedirectLabel( result.title, result.label );
|
2022-12-07 23:05:14 +00:00
|
|
|
}
|
2022-12-06 19:27:08 +00:00
|
|
|
if ( result.thumbnail ) {
|
2023-08-02 22:56:08 +00:00
|
|
|
data.thumbnail = result.thumbnail.url;
|
2022-12-06 19:27:08 +00:00
|
|
|
} else {
|
|
|
|
// Thumbnail placeholder icon
|
|
|
|
data.icon = 'image';
|
|
|
|
}
|
|
|
|
fragment.append( getMenuItem( data ) );
|
2022-12-06 18:52:14 +00:00
|
|
|
} );
|
|
|
|
// Hide placeholder
|
2022-12-14 05:45:43 +00:00
|
|
|
placeholder.classList.add( HIDDEN_CLASS );
|
2022-12-06 18:52:14 +00:00
|
|
|
typeahead.prepend( fragment );
|
|
|
|
} else {
|
|
|
|
// Update placeholder with no result content
|
|
|
|
updateMenuItem(
|
|
|
|
placeholder,
|
|
|
|
{
|
2022-12-06 19:48:18 +00:00
|
|
|
icon: 'articleNotFound',
|
2022-12-14 04:52:54 +00:00
|
|
|
type: 'placeholder',
|
2023-03-01 21:31:42 +00:00
|
|
|
size: 'lg',
|
2022-12-06 18:52:14 +00:00
|
|
|
title: mw.message( 'citizen-search-noresults-title', htmlSafeSearchQuery ).text(),
|
2023-01-06 21:03:02 +00:00
|
|
|
desc: mw.message( 'citizen-search-noresults-desc' ).text()
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
|
|
|
);
|
2022-12-14 05:45:43 +00:00
|
|
|
placeholder.classList.remove( HIDDEN_CLASS );
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add loading animation
|
2021-06-12 13:54:47 +00:00
|
|
|
searchInput.parentNode.classList.add( SEARCH_LOADING_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2023-08-02 22:56:08 +00:00
|
|
|
const { abort, fetch } = searchClient.active.client.fetchByTitle( searchQuery );
|
2021-05-17 20:34:14 +00:00
|
|
|
|
|
|
|
// Abort fetch if the input is detected
|
|
|
|
// So that fetch request won't be queued up
|
2023-08-01 00:59:50 +00:00
|
|
|
searchInput.addEventListener( 'input', abort, { once: true } );
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2023-08-02 22:56:08 +00:00
|
|
|
fetch?.then( ( response ) => {
|
2023-08-01 00:59:50 +00:00
|
|
|
searchInput.removeEventListener( 'input', abort );
|
2021-05-17 20:34:14 +00:00
|
|
|
clearSuggestions();
|
2023-08-02 22:56:08 +00:00
|
|
|
if ( response.results !== null ) {
|
|
|
|
renderSuggestions( response.results );
|
2023-01-06 21:03:02 +00:00
|
|
|
updateActiveIndex();
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
} ).catch( ( error ) => {
|
2023-08-01 00:59:50 +00:00
|
|
|
searchInput.removeEventListener( 'input', abort );
|
2021-06-12 13:54:47 +00:00
|
|
|
searchInput.parentNode.classList.remove( SEARCH_LOADING_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
// User can trigger the abort when the fetch event is pending
|
|
|
|
// There is no need for an error
|
|
|
|
if ( error.name !== 'AbortError' ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const message = `Uh oh, a wild error appears! ${error}`;
|
2021-05-17 20:34:14 +00:00
|
|
|
throw new Error( message );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:42:01 +00:00
|
|
|
/**
|
2022-12-06 18:52:14 +00:00
|
|
|
* Update menu item element
|
2022-12-03 18:42:01 +00:00
|
|
|
*
|
2022-12-06 18:52:14 +00:00
|
|
|
* @param {HTMLElement} item
|
2022-12-14 16:39:18 +00:00
|
|
|
* @param {MenuItemData} data
|
2022-12-03 18:42:01 +00:00
|
|
|
*/
|
2022-12-06 18:52:14 +00:00
|
|
|
function updateMenuItem( item, data ) {
|
2022-12-06 17:01:47 +00:00
|
|
|
if ( data.id ) {
|
|
|
|
item.setAttribute( 'id', data.id );
|
|
|
|
}
|
2022-12-14 04:52:54 +00:00
|
|
|
if ( data.type ) {
|
2023-03-01 21:31:42 +00:00
|
|
|
item.classList.add( `${ITEM_CLASS}-${data.type}` );
|
|
|
|
}
|
|
|
|
if ( data.size ) {
|
|
|
|
item.classList.add( `${ITEM_CLASS}-${data.size}` );
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
2022-12-03 18:42:01 +00:00
|
|
|
if ( data.link ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const link = item.querySelector( `.${PREFIX}__content` );
|
2022-12-03 18:42:01 +00:00
|
|
|
link.setAttribute( 'href', data.link );
|
|
|
|
}
|
2022-12-06 19:27:08 +00:00
|
|
|
if ( data.icon || data.thumbnail ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const thumbnail = item.querySelector( `.${PREFIX}__thumbnail` );
|
2022-12-06 19:27:08 +00:00
|
|
|
if ( data.thumbnail ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
thumbnail.style.backgroundImage = `url('${data.thumbnail}')`;
|
2022-12-06 19:27:08 +00:00
|
|
|
} else {
|
2022-12-14 16:22:50 +00:00
|
|
|
thumbnail.classList.add(
|
|
|
|
`${PREFIX}__thumbnail`,
|
|
|
|
'citizen-ui-icon',
|
|
|
|
`mw-ui-icon-wikimedia-${data.icon}`
|
|
|
|
);
|
2022-12-06 19:27:08 +00:00
|
|
|
}
|
2022-12-03 18:42:01 +00:00
|
|
|
}
|
2022-12-06 18:52:14 +00:00
|
|
|
if ( data.title ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const title = item.querySelector( `.${PREFIX}__title` );
|
2022-12-06 18:52:14 +00:00
|
|
|
title.innerHTML = data.title;
|
|
|
|
}
|
|
|
|
if ( data.label ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const label = item.querySelector( `.${PREFIX}__label` );
|
2022-12-06 18:52:14 +00:00
|
|
|
label.innerHTML = data.label;
|
|
|
|
}
|
2023-01-06 21:03:02 +00:00
|
|
|
if ( data.desc ) {
|
|
|
|
const desc = item.querySelector( `.${PREFIX}__description` );
|
|
|
|
desc.innerHTML = data.desc;
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate menu item HTML using the existing template
|
|
|
|
*
|
2022-12-14 16:39:18 +00:00
|
|
|
* @param {MenuItemData} data
|
2022-12-06 18:52:14 +00:00
|
|
|
* @return {HTMLElement|void}
|
|
|
|
*/
|
|
|
|
function getMenuItem( data ) {
|
2022-12-14 16:22:50 +00:00
|
|
|
const template = document.getElementById( `${PREFIX}-template` );
|
2022-12-03 18:42:01 +00:00
|
|
|
|
2022-12-06 18:52:14 +00:00
|
|
|
// Shouldn't happen but just to be safe
|
|
|
|
if ( !( template instanceof HTMLTemplateElement ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const
|
|
|
|
fragment = template.content.cloneNode( true ),
|
2022-12-14 16:22:50 +00:00
|
|
|
item = fragment.querySelector( `.${ITEM_CLASS}` );
|
2022-12-06 18:52:14 +00:00
|
|
|
updateMenuItem( item, data );
|
2022-12-14 15:28:45 +00:00
|
|
|
bindMouseHoverEvent( item );
|
2022-12-06 17:01:47 +00:00
|
|
|
return fragment;
|
2022-12-03 18:42:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
/**
|
|
|
|
* Update the typeahead element
|
|
|
|
*
|
|
|
|
*/
|
2023-04-03 15:52:38 +00:00
|
|
|
function updateTypeahead() {
|
2022-12-06 17:01:47 +00:00
|
|
|
const
|
|
|
|
searchQuery = searchInput.value,
|
2022-12-06 18:52:14 +00:00
|
|
|
htmlSafeSearchQuery = mw.html.escape( searchQuery ),
|
|
|
|
hasQuery = searchQuery.length > 0,
|
2023-03-01 21:31:42 +00:00
|
|
|
placeholder = typeahead.querySelector( `.${ITEM_CLASS}-placeholder` );
|
2022-12-06 17:01:47 +00:00
|
|
|
|
2022-12-14 16:39:18 +00:00
|
|
|
/**
|
2023-04-03 16:48:10 +00:00
|
|
|
* Get a list of tools for the typeahead footer
|
2022-12-14 16:39:18 +00:00
|
|
|
*/
|
2023-04-03 16:48:10 +00:00
|
|
|
const getTools = () => {
|
|
|
|
/**
|
|
|
|
* Update a tool item or create it if it does not exist
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
*/
|
|
|
|
const updateToolItem = ( data ) => {
|
|
|
|
const
|
|
|
|
itemId = `${PREFIX}-${data.id}`,
|
|
|
|
query = `<span class="citizen-typeahead__query">${htmlSafeSearchQuery}</span>`,
|
|
|
|
itemLink = data.link + htmlSafeSearchQuery,
|
|
|
|
/* eslint-disable-next-line mediawiki/msg-doc */
|
|
|
|
itemDesc = mw.message( data.msg, query );
|
|
|
|
|
|
|
|
let item = document.getElementById( itemId );
|
|
|
|
|
|
|
|
// Update existing element instead of creating a new one
|
|
|
|
if ( item ) {
|
|
|
|
// FIXME: Probably more efficient to just replace the query than the whole messaage?
|
|
|
|
updateMenuItem(
|
|
|
|
item,
|
|
|
|
{
|
|
|
|
link: itemLink,
|
|
|
|
desc: itemDesc
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
item = getMenuItem( {
|
|
|
|
icon: data.icon,
|
|
|
|
id: itemId,
|
|
|
|
type: 'tool',
|
|
|
|
size: 'sm',
|
2022-12-14 05:45:43 +00:00
|
|
|
link: itemLink,
|
2023-01-06 21:03:02 +00:00
|
|
|
desc: itemDesc
|
2023-04-03 16:48:10 +00:00
|
|
|
} );
|
|
|
|
typeahead.append( item );
|
|
|
|
}
|
|
|
|
};
|
2022-12-06 18:52:14 +00:00
|
|
|
|
2023-04-03 16:48:10 +00:00
|
|
|
// Fulltext search
|
2022-12-14 15:28:45 +00:00
|
|
|
updateToolItem( {
|
2023-04-03 16:48:10 +00:00
|
|
|
id: 'fulltext',
|
|
|
|
link: `${config.wgScriptPath}/index.php?title=Special:Search&fulltext=1&search=`,
|
|
|
|
icon: 'articleSearch',
|
|
|
|
msg: hasQuery ? 'citizen-search-fulltext' : 'citizen-search-fulltext-empty'
|
2022-12-14 15:28:45 +00:00
|
|
|
} );
|
|
|
|
|
2023-04-03 16:48:10 +00:00
|
|
|
// MediaSearch
|
|
|
|
if ( config.isMediaSearchExtensionEnabled ) {
|
|
|
|
updateToolItem( {
|
|
|
|
id: 'mediasearch',
|
|
|
|
link: `${config.wgScriptPath}/index.php?title=Special:MediaSearch&type=image&search=`,
|
|
|
|
icon: 'imageGallery',
|
|
|
|
msg: hasQuery ? 'citizen-search-mediasearch' : 'citizen-search-mediasearch-empty'
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getTools();
|
2022-12-06 18:52:14 +00:00
|
|
|
if ( hasQuery ) {
|
|
|
|
getSuggestions( searchQuery, htmlSafeSearchQuery, placeholder );
|
2021-05-17 20:34:14 +00:00
|
|
|
} else {
|
|
|
|
clearSuggestions();
|
2022-12-06 18:52:14 +00:00
|
|
|
// Update placeholder with no query content
|
|
|
|
updateMenuItem(
|
|
|
|
placeholder,
|
|
|
|
{
|
2022-12-06 19:48:18 +00:00
|
|
|
icon: 'articlesSearch',
|
2023-04-03 15:52:38 +00:00
|
|
|
title: mw.message( 'searchsuggest-search' ).text(),
|
|
|
|
desc: mw.message( 'citizen-search-empty-desc' ).text()
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
|
|
|
);
|
2022-12-14 05:45:43 +00:00
|
|
|
placeholder.classList.remove( HIDDEN_CLASS );
|
2023-01-06 21:03:02 +00:00
|
|
|
updateActiveIndex();
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} searchForm
|
|
|
|
* @param {HTMLInputElement} input
|
|
|
|
*/
|
|
|
|
function initTypeahead( searchForm, input ) {
|
2023-03-01 20:27:29 +00:00
|
|
|
const EXPANDED_CLASS = 'citizen-search__card--expanded';
|
2022-12-06 18:52:14 +00:00
|
|
|
|
2022-12-03 03:19:22 +00:00
|
|
|
const
|
2021-05-17 20:34:14 +00:00
|
|
|
template = mw.template.get(
|
|
|
|
'skins.citizen.search',
|
|
|
|
'resources/skins.citizen.search/templates/typeahead.mustache'
|
|
|
|
),
|
|
|
|
data = {
|
2023-04-03 15:52:38 +00:00
|
|
|
'msg-searchsuggest-search': mw.message( 'searchsuggest-search' ).text(),
|
|
|
|
'msg-citizen-search-empty-desc': mw.message( 'citizen-search-empty-desc' ).text()
|
2021-05-17 20:34:14 +00:00
|
|
|
};
|
|
|
|
|
2022-12-03 03:18:29 +00:00
|
|
|
const onBlur = ( event ) => {
|
2022-06-04 02:33:28 +00:00
|
|
|
const focusIn = typeahead.contains( event.relatedTarget );
|
|
|
|
|
|
|
|
if ( !focusIn ) {
|
|
|
|
// HACK: On Safari, users are unable to click any links because the blur
|
|
|
|
// event dismiss the links before it is clicked. This should fix it.
|
|
|
|
setTimeout( () => {
|
|
|
|
searchInput.setAttribute( 'aria-activedescendant', '' );
|
2023-03-01 20:27:29 +00:00
|
|
|
searchForm.parentElement.classList.remove( EXPANDED_CLASS );
|
2022-06-04 02:33:28 +00:00
|
|
|
searchInput.removeEventListener( 'keydown', keyboardEvents );
|
|
|
|
searchInput.removeEventListener( 'blur', onBlur );
|
|
|
|
}, 10 );
|
2021-05-18 16:23:05 +00:00
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
};
|
|
|
|
|
2022-12-03 03:18:29 +00:00
|
|
|
const onFocus = () => {
|
2021-05-18 17:26:00 +00:00
|
|
|
// Refresh the typeahead since the query will be emptied when blurred
|
2023-04-03 15:52:38 +00:00
|
|
|
updateTypeahead();
|
2023-03-01 20:27:29 +00:00
|
|
|
searchForm.parentElement.classList.add( EXPANDED_CLASS );
|
2021-05-18 16:25:34 +00:00
|
|
|
searchInput.addEventListener( 'keydown', keyboardEvents );
|
2021-05-18 17:14:51 +00:00
|
|
|
searchInput.addEventListener( 'blur', onBlur );
|
2021-05-18 16:25:34 +00:00
|
|
|
};
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
// Make them accessible outside of the function
|
|
|
|
typeahead = template.render( data ).get()[ 1 ];
|
|
|
|
searchInput = input;
|
|
|
|
|
|
|
|
searchForm.append( typeahead );
|
|
|
|
searchForm.setAttribute( 'aria-owns', 'searchform-suggestions' );
|
|
|
|
searchInput.setAttribute( 'aria-autocomplete', 'list' );
|
|
|
|
searchInput.setAttribute( 'aria-controls', 'searchform-suggestions' );
|
|
|
|
|
2023-01-06 21:03:02 +00:00
|
|
|
// Init the value in case of undef error
|
|
|
|
updateActiveIndex();
|
2023-08-02 22:56:08 +00:00
|
|
|
// Set default active search client
|
|
|
|
searchClient.setActive( config.wgCitizenSearchGateway );
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
// Since searchInput is focused before the event listener is set up
|
|
|
|
onFocus();
|
|
|
|
searchInput.addEventListener( 'focus', onFocus );
|
|
|
|
|
|
|
|
// Run once in case there is searchQuery before eventlistener is attached
|
|
|
|
if ( searchInput.value.length > 0 ) {
|
2023-04-03 15:52:38 +00:00
|
|
|
updateTypeahead();
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:48:10 +00:00
|
|
|
// Trigger update only when character is composed (e.g. CJK IME)
|
|
|
|
searchInput.composing = false;
|
|
|
|
searchInput.addEventListener( 'compositionstart', () => {
|
|
|
|
searchInput.composing = true;
|
|
|
|
} );
|
|
|
|
searchInput.addEventListener( 'compositionend', () => {
|
|
|
|
if ( searchInput.composing ) {
|
|
|
|
searchInput.composing = false;
|
|
|
|
searchInput.dispatchEvent( new Event( 'input' ) );
|
|
|
|
}
|
|
|
|
} );
|
2021-05-17 20:34:14 +00:00
|
|
|
searchInput.addEventListener( 'input', () => {
|
2023-04-03 16:48:10 +00:00
|
|
|
if ( searchInput.composing !== true ) {
|
|
|
|
mw.util.debounce( 100, updateTypeahead() );
|
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: initTypeahead
|
|
|
|
};
|