Skip to content

Commit

Permalink
feat(search): ✨ reimplement multi search command experiment
Browse files Browse the repository at this point in the history
Search commands start withs slash and includes: action, rest, ask
  • Loading branch information
alistair3149 committed Aug 3, 2023
1 parent 4241f85 commit eec5c6f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions resources/skins.citizen.search/searchClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const searchClientsData = require( './searchClients/searchClients.json' );
function searchClient( config ) {
return {
active: null,
getData: function ( id ) {
const data = Object.values( searchClientsData ).find( ( item ) => item.id === id );
getData: function ( key, value ) {
const data = Object.values( searchClientsData ).find( ( item ) => item[ key ] === value );
return data;
},
setActive: function ( id ) {
const data = this.getData( id );
if ( data ) {
const data = this.getData( 'id', id );
if ( data && data !== this.active ) {
const client = require( `./searchClients/${data.id}.js` );
this.active = data;
this.active.client = client( config );
Expand Down
2 changes: 1 addition & 1 deletion resources/skins.citizen.search/searchClients/smwAskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function adaptApiResponse( config, query, response, showDescription ) {
height: 200
} : undefined
};
} )
} ) ?? []
};
}

Expand Down
3 changes: 3 additions & 0 deletions resources/skins.citizen.search/searchQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function searchQuery() {
},
checkValid: function ( s ) {
return s.length > 0;
},
removeCommand: function ( s ) {
this.setValue( this.value.replace( `/${s} `, '' ) );
}
};
}
Expand Down
16 changes: 14 additions & 2 deletions resources/skins.citizen.search/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ function getMenuItem( data ) {
*/
function updateTypeahead() {
searchQuery.setValue( searchInput.value );
searchClient.setActive( config.wgCitizenSearchGateway );

// Search command experiement
if ( searchInput.value.startsWith( '/' ) ) {
const command = searchInput.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 );
}
}
}

const placeholder = typeahead.querySelector( `.${ITEM_CLASS}-placeholder` );

Expand Down Expand Up @@ -491,8 +505,6 @@ function initTypeahead( searchForm, input ) {

// Init the value in case of undef error
updateActiveIndex();
// Set default active search client
searchClient.setActive( config.wgCitizenSearchGateway );

// Since searchInput is focused before the event listener is set up
onFocus();
Expand Down
3 changes: 2 additions & 1 deletion skin.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
"resources/skins.citizen.search/urlGenerator.js",
"resources/skins.citizen.search/searchClients/searchClients.json",
"resources/skins.citizen.search/searchClients/mwActionApi.js",
"resources/skins.citizen.search/searchClients/mwRestApi.js"
"resources/skins.citizen.search/searchClients/mwRestApi.js",
"resources/skins.citizen.search/searchClients/smwAskApi.js"
],
"messages": [
"citizen-search-fulltext",
Expand Down

0 comments on commit eec5c6f

Please sign in to comment.