2023-08-24 00:44:01 +00:00
|
|
|
const config = require( './config.json' );
|
|
|
|
const searchHistory = require( './searchHistory.js' )( config );
|
|
|
|
|
2024-01-27 02:50:21 +00:00
|
|
|
function searchPresults() {
|
2023-08-24 00:44:01 +00:00
|
|
|
return {
|
2024-09-28 10:30:15 +00:00
|
|
|
renderHistory: function ( results, templates ) {
|
|
|
|
const items = [];
|
|
|
|
results.forEach( ( result, index ) => {
|
|
|
|
items.push( {
|
|
|
|
id: index,
|
|
|
|
href: `${ config.wgScriptPath }/index.php?title=Special:Search&search=${ result }`,
|
|
|
|
text: result,
|
|
|
|
icon: 'history'
|
|
|
|
} );
|
|
|
|
} );
|
2023-08-29 08:50:55 +00:00
|
|
|
|
2024-09-28 10:30:15 +00:00
|
|
|
const data = {
|
|
|
|
type: 'history',
|
|
|
|
'array-list-items': items
|
2023-08-29 08:50:55 +00:00
|
|
|
};
|
|
|
|
|
2024-09-28 10:30:15 +00:00
|
|
|
const partials = {
|
|
|
|
TypeaheadListItem: templates.TypeaheadListItem
|
|
|
|
};
|
2023-08-29 08:50:55 +00:00
|
|
|
|
2024-09-28 10:30:15 +00:00
|
|
|
document.getElementById( 'citizen-typeahead-list-history' ).outerHTML = templates.TypeaheadList.render( data, partials ).html();
|
|
|
|
document.getElementById( 'citizen-typeahead-group-history' ).hidden = false;
|
2023-08-24 00:44:01 +00:00
|
|
|
},
|
2024-09-28 21:31:43 +00:00
|
|
|
render: function ( templates ) {
|
|
|
|
const placeholderEl = document.getElementById( 'citizen-typeahead-placeholder' );
|
|
|
|
placeholderEl.innerHTML = '';
|
|
|
|
placeholderEl.hidden = true;
|
|
|
|
|
2024-09-28 10:30:15 +00:00
|
|
|
const historyResults = searchHistory.get();
|
|
|
|
if ( historyResults && historyResults.length > 0 ) {
|
|
|
|
this.renderHistory( historyResults, templates );
|
|
|
|
} else {
|
2023-08-24 00:44:01 +00:00
|
|
|
const data = {
|
|
|
|
icon: 'articlesSearch',
|
|
|
|
title: mw.message( 'searchsuggest-search' ).text(),
|
2024-09-28 21:31:43 +00:00
|
|
|
description: mw.message( 'citizen-search-empty-desc' ).text()
|
2023-08-24 00:44:01 +00:00
|
|
|
};
|
2024-09-28 21:31:43 +00:00
|
|
|
placeholderEl.innerHTML = templates.TypeaheadPlaceholder.render( data ).html();
|
|
|
|
placeholderEl.hidden = false;
|
2023-08-24 00:44:01 +00:00
|
|
|
}
|
2023-08-29 08:50:55 +00:00
|
|
|
},
|
2024-09-28 10:30:15 +00:00
|
|
|
clear: function () {
|
|
|
|
document.getElementById( 'citizen-typeahead-list-history' ).innerHTML = '';
|
|
|
|
document.getElementById( 'citizen-typeahead-group-history' ).hidden = true;
|
2023-08-24 00:44:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-27 02:50:21 +00:00
|
|
|
/** @module searchPresults */
|
|
|
|
module.exports = searchPresults;
|