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`,
|
|
|
|
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-12 18:11:06 +00:00
|
|
|
const typeaheadItem = require( './typeaheadItem.js' )();
|
2023-08-03 00:16:39 +00:00
|
|
|
const searchClient = require( './searchClient.js' )( config );
|
2023-08-04 23:49:02 +00:00
|
|
|
const searchHistory = require( './searchHistory.js' )( config );
|
2023-08-03 00:16:39 +00:00
|
|
|
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
|
2023-08-04 21:24:22 +00:00
|
|
|
typeahead.afterSeachQueryInput();
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.form.element.parentElement.classList.add( 'citizen-search__card--expanded' );
|
2023-08-12 18:11:06 +00:00
|
|
|
// FIXME: Should probably clean up this somehow
|
2023-08-04 23:49:02 +00:00
|
|
|
typeahead.element.addEventListener( 'click', typeahead.onClick );
|
2023-08-04 20:33:17 +00:00
|
|
|
typeahead.input.element.addEventListener( 'keydown', typeahead.input.onKeydown );
|
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-04 21:24:22 +00:00
|
|
|
mw.util.debounce( 100, typeahead.afterSeachQueryInput() );
|
2023-08-03 23:25:41 +00:00
|
|
|
}
|
2023-08-04 20:33:17 +00:00
|
|
|
},
|
|
|
|
onKeydown: function ( event ) {
|
|
|
|
if ( event.defaultPrevented ) {
|
|
|
|
return; // Do nothing if the event was already processed
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Moves the active item up and down */
|
|
|
|
if ( event.key === 'ArrowDown' || event.key === 'ArrowUp' ) {
|
|
|
|
if ( event.key === 'ArrowDown' ) {
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.increment( 1 );
|
2023-08-04 20:33:17 +00:00
|
|
|
} else {
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.increment( -1 );
|
2023-08-04 20:33:17 +00:00
|
|
|
}
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.toggle( typeahead.items.elements[ typeahead.items.index ] );
|
2023-08-04 20:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enter to click on the active item */
|
2023-08-12 18:11:06 +00:00
|
|
|
if ( typeahead.items.elements[ typeahead.items.index ] ) {
|
|
|
|
const link = typeahead.items.elements[ typeahead.items.index ].querySelector( `.${PREFIX}__content` );
|
2023-08-04 20:33:17 +00:00
|
|
|
if ( event.key === 'Enter' && link instanceof HTMLAnchorElement ) {
|
|
|
|
event.preventDefault();
|
|
|
|
link.click();
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 23:25:41 +00:00
|
|
|
}
|
|
|
|
},
|
2023-08-12 18:11:06 +00:00
|
|
|
items: {
|
|
|
|
/** @type {NodeList | undefined} */
|
|
|
|
elements: undefined,
|
|
|
|
index: -1,
|
|
|
|
max: 0,
|
|
|
|
setMax: function ( x ) {
|
|
|
|
this.max = x;
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
clearIndex: function () {
|
|
|
|
this.setIndex( -1 );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Sets 'citizen-typeahead__item--active' class on the element
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} item
|
|
|
|
*/
|
|
|
|
toggle: function ( item ) {
|
2023-08-12 18:20:53 +00:00
|
|
|
this.elements.forEach( ( element, index ) => {
|
|
|
|
if ( item !== element ) {
|
|
|
|
element.classList.remove( 'citizen-typeahead__item--active' );
|
2023-08-12 18:11:06 +00:00
|
|
|
} else {
|
|
|
|
if ( item.classList.contains( 'citizen-typeahead__item--active' ) ) {
|
|
|
|
item.classList.remove( 'citizen-typeahead__item--active' );
|
|
|
|
} else {
|
|
|
|
item.classList.add( 'citizen-typeahead__item--active' );
|
|
|
|
typeahead.input.element.setAttribute( 'aria-activedescendant', item.id );
|
2023-08-12 18:20:53 +00:00
|
|
|
this.setIndex( index );
|
2023-08-12 18:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-12 18:20:53 +00:00
|
|
|
} );
|
2023-08-12 18:11:06 +00:00
|
|
|
},
|
|
|
|
// So that mouse hover events are the same as keyboard hover events
|
|
|
|
bindMouseHoverEvent: function () {
|
|
|
|
this.elements.forEach( ( element ) => {
|
|
|
|
element.addEventListener( 'mouseenter', ( event ) => {
|
|
|
|
this.toggle( event.currentTarget );
|
|
|
|
} );
|
|
|
|
element.addEventListener( 'mouseleave', ( event ) => {
|
|
|
|
this.toggle( event.currentTarget );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
set: function () {
|
|
|
|
this.elements = typeahead.element.querySelectorAll( '.citizen-typeahead__item' );
|
|
|
|
this.bindMouseHoverEvent();
|
|
|
|
this.setMax( this.elements.length );
|
|
|
|
}
|
|
|
|
},
|
2023-08-07 21:00:56 +00:00
|
|
|
suggestions: {
|
|
|
|
/** @type {NodeList | undefined} */
|
|
|
|
elements: undefined,
|
|
|
|
set: function () {
|
|
|
|
this.elements = typeahead.element.querySelectorAll( '.citizen-typeahead__item-page' );
|
|
|
|
},
|
|
|
|
/* Remove all existing suggestions from typeahead */
|
|
|
|
clear: function () {
|
|
|
|
if ( !this.elements ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.elements.forEach( ( suggestion ) => {
|
|
|
|
suggestion.remove();
|
|
|
|
} );
|
|
|
|
this.set();
|
|
|
|
typeahead.input.element.setAttribute( 'aria-activedescendant', '' );
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.clearIndex();
|
2023-08-07 21:00:56 +00:00
|
|
|
// Remove loading animation
|
|
|
|
typeahead.form.element.classList.remove( SEARCH_LOADING_CLASS );
|
|
|
|
}
|
|
|
|
},
|
2023-08-03 23:25:41 +00:00
|
|
|
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', '' );
|
2023-08-04 23:49:02 +00:00
|
|
|
typeahead.element.removeEventListener( 'click', typeahead.onClick );
|
2023-08-04 20:33:17 +00:00
|
|
|
typeahead.input.element.removeEventListener( 'keydown', typeahead.input.onKeydown );
|
2023-08-03 23:25:41 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
},
|
2023-08-04 23:49:02 +00:00
|
|
|
onClick: function ( event ) {
|
|
|
|
// Extra safety so closest won't tranverse out of the typeahead
|
|
|
|
if ( typeahead.element.contains( event.target ) ) {
|
|
|
|
const item = event.target.closest( '.citizen-typeahead__item' );
|
|
|
|
if ( item instanceof HTMLElement ) {
|
|
|
|
let historyLabel;
|
|
|
|
|
|
|
|
// User click on a suggestion -> save the matched title > title
|
|
|
|
if ( item.classList.contains( 'citizen-typeahead__item-page' ) ) {
|
|
|
|
historyLabel = item.querySelector( '.citizen-typeahead__label' ).innerText ?? item.querySelector( '.citizen-typeahead__title' ).innerText;
|
|
|
|
} else {
|
|
|
|
historyLabel = searchQuery.value;
|
|
|
|
}
|
|
|
|
searchHistory.add( historyLabel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-08-04 21:24:22 +00:00
|
|
|
updateSearchClient: function () {
|
|
|
|
searchClient.setActive( config.wgCitizenSearchGateway );
|
|
|
|
|
|
|
|
// Search command experiement
|
|
|
|
if ( typeahead.input.element.value.startsWith( '/' ) ) {
|
|
|
|
const command = typeahead.input.element.value.split( ' ' )[ 0 ].slice( 1 );
|
|
|
|
if ( command.length > 0 ) {
|
|
|
|
const searchClientData = searchClient.getData( 'command', command );
|
|
|
|
// Multi-search clients experiment
|
|
|
|
if ( searchClientData ) {
|
|
|
|
searchClient.setActive( searchClientData.id );
|
|
|
|
searchQuery.removeCommand( command );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve( `Search client updated to ${searchClient.active.id}.` );
|
|
|
|
},
|
|
|
|
updateSearchQuery: function () {
|
|
|
|
if ( searchQuery.value === typeahead.input.element.value ) {
|
2023-08-04 21:25:01 +00:00
|
|
|
return Promise.reject( `Search query has not changed: ${searchQuery.value}.` );
|
2023-08-04 21:24:22 +00:00
|
|
|
}
|
|
|
|
searchQuery.setValue( typeahead.input.element.value );
|
|
|
|
return Promise.resolve( `Search query updated to ${searchQuery.value}.` );
|
|
|
|
},
|
|
|
|
afterSeachQueryInput: function () {
|
|
|
|
typeahead.updateSearchQuery().then( async () => {
|
|
|
|
await typeahead.updateSearchClient();
|
|
|
|
updateTypeaheadItems();
|
|
|
|
} )
|
2023-08-04 23:49:02 +00:00
|
|
|
.catch( () => {
|
2023-08-04 21:24:22 +00:00
|
|
|
// Don't do anything if search query has not changed.
|
2023-08-04 21:25:01 +00:00
|
|
|
} );
|
2023-08-04 21:24:22 +00:00
|
|
|
},
|
2023-08-03 23:25:41 +00:00
|
|
|
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 );
|
|
|
|
|
2023-08-04 23:49:02 +00:00
|
|
|
searchHistory.init();
|
|
|
|
|
2023-08-03 23:25:41 +00:00
|
|
|
// Init the value in case of undef error
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.set();
|
2023-08-03 23:25:41 +00:00
|
|
|
|
|
|
|
// Run once in case there is searchQuery before eventlistener is attached
|
|
|
|
if ( this.input.element.value.length > 0 ) {
|
2023-08-04 21:24:22 +00:00
|
|
|
this.afterSeachQueryInput();
|
2023-08-03 23:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
2023-08-12 18:11:06 +00:00
|
|
|
fragment.append( typeaheadItem.get( 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
|
2023-08-12 18:11:06 +00:00
|
|
|
typeaheadItem.update( placeholder, {
|
|
|
|
icon: 'articleNotFound',
|
|
|
|
type: 'placeholder',
|
|
|
|
size: 'lg',
|
|
|
|
title: mw.message( 'citizen-search-noresults-title', searchQuery.valueHtml ).text(),
|
|
|
|
desc: mw.message( 'citizen-search-noresults-desc' ).text()
|
|
|
|
} );
|
2022-12-14 05:45:43 +00:00
|
|
|
placeholder.classList.remove( HIDDEN_CLASS );
|
2022-12-06 18:52:14 +00:00
|
|
|
}
|
2023-08-07 21:00:56 +00:00
|
|
|
typeahead.suggestions.set();
|
|
|
|
// In case if somehow typeahead.suggestions.clear() didn't clear the loading animation
|
|
|
|
typeahead.form.element.classList.remove( SEARCH_LOADING_CLASS );
|
2023-08-12 18:11:06 +00:00
|
|
|
typeahead.items.set();
|
2021-05-17 20:34:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add loading animation
|
2023-08-07 21:00:56 +00:00
|
|
|
typeahead.form.element.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 );
|
2023-08-07 21:00:56 +00:00
|
|
|
typeahead.suggestions.clear();
|
2023-08-02 22:56:08 +00:00
|
|
|
if ( response.results !== null ) {
|
|
|
|
renderSuggestions( response.results );
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
} ).catch( ( error ) => {
|
2023-08-03 23:25:41 +00:00
|
|
|
typeahead.input.element.removeEventListener( 'input', abort );
|
2023-08-07 21:00:56 +00:00
|
|
|
typeahead.form.element.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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the typeahead element
|
|
|
|
*
|
|
|
|
*/
|
2023-08-04 21:24:22 +00:00
|
|
|
function updateTypeaheadItems() {
|
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?
|
2023-08-12 18:11:06 +00:00
|
|
|
typeaheadItem.update( item, {
|
|
|
|
link: itemLink,
|
|
|
|
desc: itemDesc
|
|
|
|
}
|
2023-04-03 16:48:10 +00:00
|
|
|
);
|
|
|
|
} else {
|
2023-08-12 18:11:06 +00:00
|
|
|
item = typeaheadItem.get( {
|
2023-04-03 16:48:10 +00:00
|
|
|
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 {
|
2023-08-07 21:00:56 +00:00
|
|
|
typeahead.suggestions.clear();
|
2022-12-06 18:52:14 +00:00
|
|
|
// Update placeholder with no query content
|
2023-08-12 18:11:06 +00:00
|
|
|
typeaheadItem.update( placeholder, {
|
|
|
|
icon: 'articlesSearch',
|
|
|
|
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-08-12 18:11:06 +00:00
|
|
|
typeahead.items.set();
|
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
|
|
|
|
};
|