Commits
Giuseppe Digilio authored 2ceaba742f1
16 16 | import { RemoteData } from '../../data/remote-data'; |
17 17 | import { DSpaceObjectType } from '../dspace-object-type.model'; |
18 18 | import { SortDirection, SortOptions } from '../../cache/models/sort-options.model'; |
19 19 | import { RouteService } from '../../services/route.service'; |
20 20 | import { getAllSucceededRemoteDataPayload, getFirstSucceededRemoteData } from '../operators'; |
21 21 | import { hasNoValue, hasValue, isNotEmpty, isNotEmptyOperator } from '../../../shared/empty.util'; |
22 22 | import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils'; |
23 23 | import { SearchConfig, SortConfig } from './search-filters/search-config.model'; |
24 24 | import { SearchService } from './search.service'; |
25 25 | import { PaginationService } from '../../pagination/pagination.service'; |
26 + | import { ViewMode } from '../view-mode.model'; |
26 27 | |
27 28 | /** |
28 29 | * Service that performs all actions that have to do with the current search configuration |
29 30 | */ |
30 31 | @Injectable() |
31 32 | export class SearchConfigurationService implements OnDestroy { |
32 33 | |
33 34 | /** |
34 35 | * Default pagination id |
35 36 | */ |
189 190 | return this.routeService.getRouteParameterValue('fixedFilterQuery'); |
190 191 | } |
191 192 | |
192 193 | /** |
193 194 | * @returns {Observable<Params>} Emits the current active filters with their values as they are displayed in the frontend URL |
194 195 | */ |
195 196 | getCurrentFrontendFilters(): Observable<Params> { |
196 197 | return this.routeService.getQueryParamsWithPrefix('f.'); |
197 198 | } |
198 199 | |
200 + | /** |
201 + | * @returns {Observable<string>} Emits the current view mode |
202 + | */ |
203 + | getCurrentViewMode(defaultViewMode: ViewMode) { |
204 + | return this.routeService.getQueryParameterValue('view').pipe(map((viewMode) => { |
205 + | return viewMode || defaultViewMode; |
206 + | })); |
207 + | } |
208 + | |
199 209 | /** |
200 210 | * Creates an observable of SearchConfig every time the configuration stream emits. |
201 211 | * @param configuration The search configuration |
202 212 | * @param service The search service to use |
203 213 | * @param scope The search scope if exists |
204 214 | */ |
205 215 | getConfigurationSearchConfig(configuration: string, service: SearchService, scope?: string): Observable<SearchConfig> { |
206 216 | return service.getSearchConfigurationFor(scope, configuration).pipe( |
207 217 | getAllSucceededRemoteDataPayload() |
208 218 | ); |
278 288 | * @param {SearchOptions} defaults Default values for when no parameters are available |
279 289 | * @returns {Subscription} The subscription to unsubscribe from |
280 290 | */ |
281 291 | private subscribeToSearchOptions(defaults: SearchOptions): Subscription { |
282 292 | return observableMerge( |
283 293 | this.getConfigurationPart(defaults.configuration), |
284 294 | this.getScopePart(defaults.scope), |
285 295 | this.getQueryPart(defaults.query), |
286 296 | this.getDSOTypePart(), |
287 297 | this.getFiltersPart(), |
288 - | this.getFixedFilterPart() |
298 + | this.getFixedFilterPart(), |
299 + | this.getViewModePart(defaults.view) |
289 300 | ).subscribe((update) => { |
290 301 | const currentValue: SearchOptions = this.searchOptions.getValue(); |
291 302 | const updatedValue: SearchOptions = Object.assign(new PaginatedSearchOptions({}), currentValue, update); |
292 303 | this.searchOptions.next(updatedValue); |
293 304 | }); |
294 305 | } |
295 306 | |
296 307 | /** |
297 308 | * Sets up a subscription to all necessary parameters to make sure the paginatedSearchOptions emits a new value every time they update |
298 309 | * @param {string} paginationId The pagination ID |
301 312 | */ |
302 313 | private subscribeToPaginatedSearchOptions(paginationId: string, defaults: PaginatedSearchOptions): Subscription { |
303 314 | return observableMerge( |
304 315 | this.getConfigurationPart(defaults.configuration), |
305 316 | this.getPaginationPart(paginationId, defaults.pagination), |
306 317 | this.getSortPart(paginationId, defaults.sort), |
307 318 | this.getScopePart(defaults.scope), |
308 319 | this.getQueryPart(defaults.query), |
309 320 | this.getDSOTypePart(), |
310 321 | this.getFiltersPart(), |
311 - | this.getFixedFilterPart() |
322 + | this.getFixedFilterPart(), |
323 + | this.getViewModePart(defaults.view) |
312 324 | ).subscribe((update) => { |
313 325 | const currentValue: PaginatedSearchOptions = this.paginatedSearchOptions.getValue(); |
314 326 | const updatedValue: PaginatedSearchOptions = Object.assign(new PaginatedSearchOptions({}), currentValue, update); |
315 327 | this.paginatedSearchOptions.next(updatedValue); |
316 328 | }); |
317 329 | } |
318 330 | |
319 331 | /** |
320 332 | * Unsubscribe from all subscriptions related to the given paginationID |
321 333 | * @param paginationId The pagination id |
396 408 | * @returns {Observable<string>} Emits the current fixed filter as a partial SearchOptions object |
397 409 | */ |
398 410 | private getFixedFilterPart(): Observable<any> { |
399 411 | return this.getCurrentFixedFilter().pipe( |
400 412 | isNotEmptyOperator(), |
401 413 | map((fixedFilter) => { |
402 414 | return { fixedFilter }; |
403 415 | }), |
404 416 | ); |
405 417 | } |
418 + | |
419 + | /** |
420 + | * @returns {Observable<Params>} Emits the current view mode as a partial SearchOptions object |
421 + | */ |
422 + | private getViewModePart(defaultViewMode: ViewMode): Observable<any> { |
423 + | return this.getCurrentViewMode(defaultViewMode).pipe(map((view) => { |
424 + | return { view }; |
425 + | })); |
426 + | } |
406 427 | } |