11import { Injectable } from '@angular/core' ;
22import { PaginatedList } from '@dspace/core/data/paginated-list.model' ;
3+ import { RemoteData } from '@dspace/core/data/remote-data' ;
34import {
5+ getFirstSucceededRemoteData ,
46 getFirstSucceededRemoteDataPayload ,
57 getFirstSucceededRemoteListPayload ,
68} from '@dspace/core/shared/operators' ;
@@ -24,6 +26,7 @@ import {
2426 merge ,
2527 mergeMap ,
2628 scan ,
29+ tap ,
2730} from 'rxjs/operators' ;
2831
2932import {
@@ -90,6 +93,12 @@ export class VocabularyTreeviewService {
9093 */
9194 private hideSearchingWhenUnsubscribed$ = new Observable ( ( ) => ( ) => this . loading . next ( false ) ) ;
9295
96+ public currentPage = 1 ;
97+ public totalPages = 1 ;
98+ public queryInProgress = '' ;
99+ public showNextPageSubject = new BehaviorSubject < boolean > ( false ) ;
100+ public showPreviousPageSubject = new BehaviorSubject < boolean > ( false ) ;
101+
93102 /**
94103 * Initialize instance variables
95104 *
@@ -197,20 +206,51 @@ export class VocabularyTreeviewService {
197206 }
198207
199208 /**
200- * Perform a search operation by query
209+ * Initiates a vocabulary search using the provided query term and selection, starting from the first page.
210+ *
211+ * @param query - The text input to search for within the vocabulary.
212+ * @param selectedItems - Currently selected vocabulary item IDs to retain in the result.
201213 */
202214 searchByQuery ( query : string , selectedItems : string [ ] ) {
215+ this . searchByQueryAndPage ( query , selectedItems , 1 ) ;
216+ }
217+
218+ /**
219+ * Executes a paginated vocabulary search with the given query, selection, and page number.
220+ * Updates pagination state, loading indicators, and triggers the vocabulary tree rebuild.
221+ *
222+ * @param query - The search term to filter vocabulary entries.
223+ * @param selectedItems - IDs of items currently selected in the tree.
224+ * @param page - The page number to fetch (1-based index).
225+ */
226+ searchByQueryAndPage ( query : string , selectedItems : string [ ] , page : number = 1 ) {
203227 this . loading . next ( true ) ;
228+ this . queryInProgress = query ;
229+ this . currentPage = page ;
230+
204231 if ( isEmpty ( this . storedNodes ) ) {
205232 this . storedNodes = this . dataChange . value ;
206233 this . storedNodeMap = this . nodeMap ;
207234 }
208235 this . nodeMap = new Map < string , TreeviewNode > ( ) ;
209236 this . dataChange . next ( [ ] ) ;
210237
211- this . vocabularyService . getVocabularyEntriesByValue ( query , false , this . vocabularyOptions , new PageInfo ( ) ) . pipe (
238+ const pageInfo = new PageInfo ( {
239+ elementsPerPage : 20 ,
240+ currentPage : page ,
241+ totalElements : 0 ,
242+ totalPages : 0 ,
243+ } ) ;
244+
245+ this . vocabularyService . getVocabularyEntriesByValue ( query , false , this . vocabularyOptions , pageInfo ) . pipe (
246+ getFirstSucceededRemoteData ( ) ,
247+ tap ( ( rd : RemoteData < PaginatedList < VocabularyEntry > > ) => {
248+ this . totalPages = rd . payload . pageInfo . totalPages ;
249+ this . showPreviousPageSubject . next ( rd . payload . pageInfo . currentPage > 1 ) ;
250+ this . showNextPageSubject . next ( rd . payload . pageInfo . currentPage < this . totalPages ) ;
251+ } ) ,
212252 getFirstSucceededRemoteListPayload ( ) ,
213- mergeMap ( ( result : VocabularyEntry [ ] ) => ( result . length > 0 ) ? result : of ( null ) ) ,
253+ mergeMap ( ( result : VocabularyEntry [ ] ) => result . length > 0 ? result : of ( null ) ) ,
214254 mergeMap ( ( entry : VocabularyEntry ) =>
215255 this . vocabularyService . findEntryDetailById ( entry . otherInformation . id , this . vocabularyName ) . pipe (
216256 getFirstSucceededRemoteDataPayload ( ) ,
0 commit comments