refactor(search): ♻️ make search action into a module

Signed-off-by: alistair3149 <alistair31494322@gmail.com>
This commit is contained in:
alistair3149 2023-08-29 20:05:44 -07:00
parent 680d84cfb0
commit dc072f4e5b
No known key found for this signature in database
GPG key ID: 16076C01E5C88864
3 changed files with 69 additions and 58 deletions

View file

@ -0,0 +1,66 @@
const config = require( './config.json' );
const htmlHelper = require( './htmlHelper.js' )();
function searchAction() {
return {
init: function ( typeaheadEl, itemGroupData ) {
const actionData = {
type: 'action',
size: 'sm'
};
itemGroupData.items = itemGroupData.items.map( ( item ) => ( { ...item, ...actionData } ) );
typeaheadEl.append( htmlHelper.getItemGroupElement( itemGroupData ) );
},
render: function ( typeaheadEl, searchQuery ) {
const itemGroupData = {
id: 'action',
items: []
};
// TODO: Save this in a separate JSON file
// Fulltext search
itemGroupData.items.push( {
// id: 'fulltext',
link: `${config.wgScriptPath}/index.php?title=Special:Search&fulltext=1&search=${searchQuery.valueHtml}`,
icon: 'articleSearch',
msg: 'citizen-search-fulltext'
} );
// Edit/create page
// TODO: Check if user has right, and whether the page exists
itemGroupData.items.push( {
// id: 'editpage',
link: `${config.wgScriptPath}/index.php?title=${searchQuery.valueHtml}&action=edit`,
icon: 'edit',
msg: 'citizen-search-editpage'
} );
// MediaSearch
if ( config.isMediaSearchExtensionEnabled ) {
itemGroupData.items.push( {
// id: 'mediasearch',
link: `${config.wgScriptPath}/index.php?title=Special:MediaSearch&type=image&search=${searchQuery.valueHtml}`,
icon: 'imageGallery',
msg: 'citizen-search-mediasearch'
} );
}
if ( !typeaheadEl.querySelector( '.citizen-typeahead-item-group[data-group="action"]' ) ) {
this.init( typeaheadEl, itemGroupData );
}
itemGroupData.items.forEach( ( item, index ) => {
const actionEl = document.getElementById( `citizen-typeahead-action-${index}` );
htmlHelper.updateItemElement( actionEl, {
link: item.link,
title: searchQuery.value,
/* eslint-disable-next-line mediawiki/msg-doc */
label: mw.message( item.msg )
} );
} );
}
};
}
/** @module searchAction */
module.exports = searchAction;

View file

@ -7,6 +7,7 @@ const config = require( './config.json' );
const htmlHelper = require( './htmlHelper.js' )();
const presult = require( './presult.js' )();
const searchAction = require( './searchAction.js' )();
const searchClient = require( './searchClient.js' )( config );
const searchHistory = require( './searchHistory.js' )( config );
const searchQuery = require( './searchQuery.js' )();
@ -442,66 +443,9 @@ function getSuggestions() {
*
*/
function updateTypeaheadItems() {
/**
* Get a list of tools for the typeahead footer
*/
const getTools = () => {
const itemGroupData = {
id: 'tool',
items: []
};
// TODO: Save this in a separate JSON file
// Fulltext search
itemGroupData.items.push( {
// id: 'fulltext',
link: `${config.wgScriptPath}/index.php?title=Special:Search&fulltext=1&search=${searchQuery.valueHtml}`,
icon: 'articleSearch',
msg: 'citizen-search-fulltext'
} );
// Edit/create page
// TODO: Check if user has right, and whether the page exists
itemGroupData.items.push( {
// id: 'editpage',
link: `${config.wgScriptPath}/index.php?title=${searchQuery.valueHtml}&action=edit`,
icon: 'edit',
msg: 'citizen-search-editpage'
} );
// MediaSearch
if ( config.isMediaSearchExtensionEnabled ) {
itemGroupData.items.push( {
// id: 'mediasearch',
link: `${config.wgScriptPath}/index.php?title=Special:MediaSearch&type=image&search=${searchQuery.valueHtml}`,
icon: 'imageGallery',
msg: 'citizen-search-mediasearch'
} );
}
const hasTools = !!document.querySelector( '.citizen-typeahead-item-group[data-group="tool"]' );
if ( !hasTools ) {
const toolData = {
type: 'tool',
size: 'sm'
};
itemGroupData.items = itemGroupData.items.map( ( item ) => ( { ...item, ...toolData } ) );
typeahead.element.append( htmlHelper.getItemGroupElement( itemGroupData ) );
}
itemGroupData.items.forEach( ( item, index ) => {
const toolEl = document.getElementById( `citizen-typeahead-tool-${index}` );
htmlHelper.updateItemElement( toolEl, {
link: item.link,
title: searchQuery.value,
/* eslint-disable-next-line mediawiki/msg-doc */
label: mw.message( item.msg )
} );
} );
};
if ( searchQuery.isValid ) {
presult.clear( typeahead.element );
getTools();
searchAction.render( typeahead.element, searchQuery );
getSuggestions();
} else {
typeahead.items.clear();

View file

@ -160,6 +160,7 @@
"resources/skins.citizen.search/htmlHelper.js",
"resources/skins.citizen.search/fetch.js",
"resources/skins.citizen.search/presult.js",
"resources/skins.citizen.search/searchAction.js",
"resources/skins.citizen.search/searchClient.js",
"resources/skins.citizen.search/searchHistory.js",
"resources/skins.citizen.search/searchQuery.js",