mediawiki-skins-Citizen/resources/skins.citizen.search/searchPresults.js
github-actions 96171236d7 ci: 👷 lint code to MediaWiki standards
Check commit and GitHub actions for more details
2024-01-27 02:51:04 +00:00

66 lines
1.8 KiB
JavaScript

const config = require( './config.json' );
const htmlHelper = require( './htmlHelper.js' )();
const searchHistory = require( './searchHistory.js' )( config );
function searchPresults() {
return {
elements: undefined,
addSearchHistory: function ( fragment ) {
const historyData = searchHistory.get();
if ( !historyData?.length > 0 ) {
return fragment;
}
const itemGroupData = {
id: 'history',
items: []
};
historyData.forEach( ( result ) => {
const data = {
icon: 'history',
// TODO: Add option to prepend the result to input field
link: `${ config.wgScriptPath }/index.php?title=Special:Search&search=${ result }`,
type: 'history',
size: 'sm',
title: result
};
itemGroupData.items.push( data );
} );
fragment.append( htmlHelper.getItemGroupElement( itemGroupData ) );
return fragment;
},
render: function ( typeaheadEl ) {
const fragment = document.createDocumentFragment();
this.addSearchHistory( fragment );
if ( fragment.childNodes.length === 0 ) {
const data = {
icon: 'articlesSearch',
type: 'placeholder',
size: 'lg',
title: mw.message( 'searchsuggest-search' ).text(),
desc: mw.message( 'citizen-search-empty-desc' ).text()
};
fragment.append( htmlHelper.getItemElement( data ) );
}
typeaheadEl.querySelector( '.citizen-typeahead__item-placeholder' )?.remove();
typeaheadEl.append( fragment );
this.set( typeaheadEl );
},
set: function ( typeaheadEl ) {
// FIXME: Clean this up when we add top pages
this.elements = typeaheadEl.querySelectorAll( '.citizen-typeahead-item-group[data-group="history"]' );
},
clear: function ( typeaheadEl ) {
htmlHelper.removeItemGroup( typeaheadEl, 'history' );
this.elements = undefined;
}
};
}
/** @module searchPresults */
module.exports = searchPresults;