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
|
|
|
|
2023-08-03 00:16:39 +00:00
|
|
|
const searchClient = require( './searchClient.js' )( config );
|
|
|
|
const searchQuery = require( './searchQuery.js' )();
|
2023-08-02 23:56:09 +00:00
|
|
|
|
2023-08-03 23:25:41 +00:00
|
|
|
const typeahead = {
|
2023-08-03 23:30:57 +00:00
|
|
|
/** @type {HTMLElement | undefined} */
|
2023-08-04 20:27:37 +00:00
|
|
|
element: undefined,
|
2023-08-03 23:25:41 +00:00
|
|
|
form: {
|
2023-08-03 23:30:57 +00:00
|
|
|
/** @type {HTMLFormElement | undefined} */
|
2023-08-04 20:27:37 +00:00
|
|
|
element: undefined,
|
2023-08-03 23:25:41 +00:00
|
|
|
init: function ( formEl ) {
|
|
|
|
this.element = formEl;
|
|
|
|
this.element.setAttribute( 'aria-owns', typeahead.element.id );
|
|
|
|
this.element.append( typeahead.element );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
input: {
|
2023-08-03 23:30:57 +00:00
|
|
|
/** @type {HTMLInputElement | undefined} */
|
2023-08-04 20:27:37 +00:00
|
|
|
element: undefined,
|
2023-08-03 23:25:41 +00:00
|
|
|
// Trigger update only when character is composed (e.g. CJK IME)
|
|
|
|
isComposing: false,
|
|
|
|
init: function ( inputEl ) {
|
|
|
|
this.element = inputEl;
|
|
|
|
// Since searchInput is focused before the event listener is set up
|
|
|
|
this.onFocus();
|
|
|
|
this.element.setAttribute( 'aria-autocomplete', 'list' );
|
|
|
|
this.element.setAttribute( 'aria-controls', typeahead.element.id );
|
|
|
|
this.element.addEventListener( 'focus', this.onFocus );
|
|
|
|
},
|
|
|
|
onCompositionstart: function () {
|
2023-08-04 20:26:19 +00:00
|
|
|
typeahead.input.element.addEventListener( 'compositionend', typeahead.input.onCompositionend );
|
|
|
|
typeahead.input.isComposing = true;
|
2023-08-03 23:25:41 +00:00
|
|
|
},
|
|
|
|
onCompositionend: function () {
|
2023-08-04 20:26:19 +00:00
|
|
|
typeahead.input.isComposing = false;
|
|
|
|
typeahead.input.dispatchEvent( new Event( 'input' ) );
|
2023-08-03 23:25:41 +00:00
|
|
|
},
|
|
|
|
onFocus: function () {
|
|
|
|
// Refresh the typeahead since the query will be emptied when blurred
|
|
|
|
updateTypeahead();
|
|
|
|
typeahead.form.element.parentElement.classList.add( 'citizen-search__card--expanded' );
|
|
|
|
typeahead.input.element.addEventListener( 'keydown', keyboardEvents );
|
2023-08-04 07:37:28 +00:00
|
|
|
typeahead.input.element.addEventListener( 'input', typeahead.input.onInput );
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.input.element.addEventListener( 'blur', typeahead.onBlur );
|
|
|
|
},
|
|
|
|
onInput: function () {
|
2023-08-04 20:26:19 +00:00
|
|
|
typeahead.input.element.addEventListener( 'compositionstart', typeahead.input.onCompositionstart );
|
|
|
|
if ( typeahead.input.isComposing !== true ) {
|
2023-08-03 23:25:41 +00:00
|
|
|
mw.util.debounce( 100, updateTypeahead() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onBlur: function ( event ) {
|
|
|
|
if ( !typeahead.element.contains( event.relatedTarget ) ) {
|
|
|
|
// 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( () => {
|
|
|
|
typeahead.form.element.parentElement.classList.remove( 'citizen-search__card--expanded' );
|
|
|
|
typeahead.input.element.setAttribute( 'aria-activedescendant', '' );
|
|
|
|
typeahead.input.element.removeEventListener( 'keydown', keyboardEvents );
|
|
|
|
typeahead.input.element.removeEventListener( 'input', typeahead.input.onInput );
|
|
|
|
typeahead.input.element.removeEventListener( 'compositionstart', typeahead.input.onCompositionstart );
|
|
|
|
typeahead.input.element.removeEventListener( 'compositionend', typeahead.input.onCompositionend );
|
|
|
|
typeahead.input.element.removeEventListener( 'blur', this.onBlur );
|
|
|
|
}, 10 );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
init: function ( formEl, inputEl ) {
|
|
|
|
const template = mw.template.get( 'skins.citizen.search', 'resources/skins.citizen.search/templates/typeahead.mustache' );
|
|
|
|
const data = {
|
|
|
|
'msg-searchsuggest-search': mw.message( 'searchsuggest-search' ).text(),
|
|
|
|
'msg-citizen-search-empty-desc': mw.message( 'citizen-search-empty-desc' ).text()
|
|
|
|
};
|
|
|
|
this.element = template.render( data ).get()[ 1 ];
|
|
|
|
this.form.init( formEl );
|
|
|
|
this.input.init( inputEl );
|
|
|
|
|
|
|
|
// Init the value in case of undef error
|
|
|
|
updateActiveIndex();
|
|
|
|
|
|
|
|
// Run once in case there is searchQuery before eventlistener is attached
|
|
|
|
if ( this.input.element.value.length > 0 ) {
|
|
|
|
updateTypeahead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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() {
|
2023-08-03 23:25:41 +00:00
|
|
|
typeaheadItems = typeahead.element.querySelectorAll( `.${ITEM_CLASS}` );
|
2023-01-06 21:03:02 +00:00
|
|
|
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 );
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.input.element.setAttribute( 'aria-activedescendant', element.id );
|
2021-05-17 20:34:14 +00:00
|
|
|
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-08-03 23:25:41 +00:00
|
|
|
const typeaheadChildren = typeahead.element.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-08-03 23:25:41 +00:00
|
|
|
typeahead.element.innerHTML = '';
|
|
|
|
typeahead.element.append( fragment );
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove loading animation
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.input.element.parentNode.classList.remove( SEARCH_LOADING_CLASS );
|
|
|
|
typeahead.input.element.setAttribute( 'aria-activedescendant', '' );
|
2021-05-17 20:34:14 +00:00
|
|
|
activeIndex.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch suggestions from API and render the suggetions in HTML
|
|
|
|
*
|
2022-12-06 18:52:14 +00:00
|
|
|
* @param {HTMLElement} placeholder
|
2021-05-17 20:34:14 +00:00
|
|
|
*/
|
2023-08-02 23:56:09 +00:00
|
|
|
function getSuggestions( 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 ) => {
|
2023-08-02 23:56:09 +00:00
|
|
|
const regex = new RegExp( mw.util.escapeRegExp( searchQuery.valueHtml ), '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-04 01:20:47 +00:00
|
|
|
data.label = getRedirectLabel( result.title, result.label );
|
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 );
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.element.prepend( fragment );
|
2022-12-06 18:52:14 +00:00
|
|
|
} 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',
|
2023-08-02 23:56:09 +00:00
|
|
|
title: mw.message( 'citizen-search-noresults-title', searchQuery.valueHtml ).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
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.input.element.parentNode.classList.add( SEARCH_LOADING_CLASS );
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2023-08-02 23:56:09 +00:00
|
|
|
const { abort, fetch } = searchClient.active.client.fetchByTitle( searchQuery.value );
|
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-03 23:25:41 +00:00
|
|
|
typeahead.input.element.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-03 23:25:41 +00:00
|
|
|
typeahead.input.element.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-03 23:25:41 +00:00
|
|
|
typeahead.input.element.removeEventListener( 'input', abort );
|
|
|
|
typeahead.input.element.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() {
|
2023-08-03 23:25:41 +00:00
|
|
|
searchQuery.setValue( typeahead.input.element.value );
|
2023-08-03 02:07:35 +00:00
|
|
|
searchClient.setActive( config.wgCitizenSearchGateway );
|
|
|
|
|
|
|
|
// Search command experiement
|
2023-08-03 23:25:41 +00:00
|
|
|
if ( typeahead.input.element.value.startsWith( '/' ) ) {
|
|
|
|
const command = typeahead.input.element.value.split( ' ' )[ 0 ].slice( 1 );
|
2023-08-03 02:07:35 +00:00
|
|
|
if ( command.length > 0 ) {
|
|
|
|
const searchClientData = searchClient.getData( 'command', command );
|
|
|
|
// Multi-search clients experiment
|
|
|
|
if ( searchClientData ) {
|
|
|
|
searchClient.setActive( searchClientData.id );
|
|
|
|
searchQuery.removeCommand( command );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 23:56:09 +00:00
|
|
|
|
2023-08-03 23:25:41 +00:00
|
|
|
const placeholder = typeahead.element.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}`,
|
2023-08-02 23:56:09 +00:00
|
|
|
query = `<span class="citizen-typeahead__query">${searchQuery.valueHtml}</span>`,
|
|
|
|
itemLink = data.link + searchQuery.valueHtml,
|
2023-04-03 16:48:10 +00:00
|
|
|
/* 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
|
|
|
} );
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.element.append( item );
|
2023-04-03 16:48:10 +00:00
|
|
|
}
|
|
|
|
};
|
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',
|
2023-08-02 23:56:09 +00:00
|
|
|
msg: searchQuery.isValid ? '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',
|
2023-08-02 23:56:09 +00:00
|
|
|
msg: searchQuery.isValid ? 'citizen-search-mediasearch' : 'citizen-search-mediasearch-empty'
|
2023-04-03 16:48:10 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getTools();
|
2023-08-02 23:56:09 +00:00
|
|
|
if ( searchQuery.isValid ) {
|
|
|
|
getSuggestions( 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-03 23:30:57 +00:00
|
|
|
* @param {HTMLFormElement} formEl
|
|
|
|
* @param {HTMLInputElement} inputEl
|
2021-05-17 20:34:14 +00:00
|
|
|
*/
|
2023-08-03 23:30:57 +00:00
|
|
|
function initTypeahead( formEl, inputEl ) {
|
|
|
|
typeahead.init( formEl, inputEl );
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: initTypeahead
|
|
|
|
};
|