2023-08-30 03:05:44 +00:00
|
|
|
const config = require( './config.json' );
|
|
|
|
const htmlHelper = require( './htmlHelper.js' )();
|
|
|
|
|
|
|
|
function searchAction() {
|
|
|
|
return {
|
2023-09-06 21:33:30 +00:00
|
|
|
userRights: undefined,
|
2024-05-22 02:14:58 +00:00
|
|
|
// eslint-disable-next-line es-x/no-async-functions
|
2023-09-06 21:33:30 +00:00
|
|
|
getUserRights: async function () {
|
|
|
|
// Get and cache user rights
|
2024-05-22 02:14:58 +00:00
|
|
|
this.userRights = await mw.user.getRights();
|
2023-09-06 21:33:30 +00:00
|
|
|
return this.userRights;
|
|
|
|
},
|
2023-08-30 03:05:44 +00:00
|
|
|
init: function ( typeaheadEl, itemGroupData ) {
|
|
|
|
const actionData = {
|
|
|
|
type: 'action',
|
2023-11-03 21:25:30 +00:00
|
|
|
size: 'chip'
|
2023-08-30 03:05:44 +00:00
|
|
|
};
|
|
|
|
itemGroupData.items = itemGroupData.items.map( ( item ) => ( { ...item, ...actionData } ) );
|
|
|
|
typeaheadEl.append( htmlHelper.getItemGroupElement( itemGroupData ) );
|
|
|
|
},
|
2024-05-22 02:14:58 +00:00
|
|
|
// eslint-disable-next-line es-x/no-async-functions
|
2023-09-06 21:33:30 +00:00
|
|
|
render: async function ( typeaheadEl, searchQuery ) {
|
2023-08-30 03:05:44 +00:00
|
|
|
const itemGroupData = {
|
|
|
|
id: 'action',
|
|
|
|
items: []
|
|
|
|
};
|
|
|
|
|
2024-05-22 01:43:59 +00:00
|
|
|
const searchQueryEncoded = encodeURIComponent( searchQuery.valueHtml );
|
2023-08-30 03:05:44 +00:00
|
|
|
// TODO: Save this in a separate JSON file
|
|
|
|
// Fulltext search
|
|
|
|
itemGroupData.items.push( {
|
|
|
|
// id: 'fulltext',
|
2024-05-22 01:43:59 +00:00
|
|
|
link: `${ config.wgScriptPath }/index.php?title=Special:Search&fulltext=1&search=${ searchQueryEncoded }`,
|
2023-08-30 03:05:44 +00:00
|
|
|
icon: 'articleSearch',
|
|
|
|
msg: 'citizen-search-fulltext'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// MediaSearch
|
|
|
|
if ( config.isMediaSearchExtensionEnabled ) {
|
|
|
|
itemGroupData.items.push( {
|
|
|
|
// id: 'mediasearch',
|
2024-05-22 01:43:59 +00:00
|
|
|
link: `${ config.wgScriptPath }/index.php?title=Special:MediaSearch&type=image&search=${ searchQueryEncoded }`,
|
2023-08-30 03:05:44 +00:00
|
|
|
icon: 'imageGallery',
|
|
|
|
msg: 'citizen-search-mediasearch'
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-09-06 21:33:30 +00:00
|
|
|
/*
|
|
|
|
For some reason title.exists() always returns null
|
|
|
|
const title = mw.Title.newFromUserInput( searchQuery.value );
|
|
|
|
console.log( title.exists() );
|
|
|
|
*/
|
|
|
|
|
|
|
|
const userRights = this.userRights ?? await this.getUserRights();
|
|
|
|
if ( userRights.includes( 'createpage', 'edit' ) ) {
|
|
|
|
// Edit/create page
|
|
|
|
// TODO: Check whether the page exists
|
|
|
|
itemGroupData.items.push( {
|
|
|
|
// id: 'editpage',
|
2024-05-22 01:43:59 +00:00
|
|
|
link: `${ config.wgScriptPath }/index.php?title=${ searchQueryEncoded }&action=edit`,
|
2023-09-06 21:33:30 +00:00
|
|
|
icon: 'edit',
|
|
|
|
msg: 'citizen-search-editpage'
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-05-22 02:14:58 +00:00
|
|
|
if ( !typeaheadEl.querySelector( '.citizen-typeahead-item-group[data-mw-citizen-typeahead-group="action"]' ) ) {
|
2023-08-30 03:05:44 +00:00
|
|
|
this.init( typeaheadEl, itemGroupData );
|
|
|
|
}
|
|
|
|
|
|
|
|
itemGroupData.items.forEach( ( item, index ) => {
|
2024-01-27 02:51:04 +00:00
|
|
|
const actionEl = document.getElementById( `citizen-typeahead-action-${ index }` );
|
2023-08-30 03:05:44 +00:00
|
|
|
htmlHelper.updateItemElement( actionEl, {
|
|
|
|
link: item.link,
|
|
|
|
/* eslint-disable-next-line mediawiki/msg-doc */
|
|
|
|
label: mw.message( item.msg )
|
|
|
|
} );
|
|
|
|
} );
|
2023-09-06 21:33:30 +00:00
|
|
|
},
|
|
|
|
clear: function ( typeaheadEl ) {
|
2024-01-27 02:50:21 +00:00
|
|
|
htmlHelper.removeItemGroup( typeaheadEl, 'action' );
|
2023-08-30 03:05:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @module searchAction */
|
|
|
|
module.exports = searchAction;
|