Skip to content

Commit aee7691

Browse files
committed
101623: Replace hardcoded vocabulary with BrowseDefinition in BrowseByTaxonomyPage
1 parent d1c91b8 commit aee7691

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/app/browse-by/browse-by-taxonomy-page/browse-by-taxonomy-page.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
(deselect)="onDeselect($event)">
77
</ds-vocabulary-treeview>
88
</div>
9-
<a class="btn btn-primary" [routerLink]="['/search']" [queryParams]="{ 'f.subject': filterValues }">{{ 'browse.taxonomy.button' | translate }}</a>
9+
<a class="btn btn-primary" [routerLink]="['/search']" [queryParams]="queryParams">{{ 'browse.taxonomy.button' | translate }}</a>
1010
</div>

src/app/browse-by/browse-by-taxonomy-page/browse-by-taxonomy-page.component.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, Inject, OnDestroy } from '@angular/core';
22
import { VocabularyOptions } from '../../core/submission/vocabularies/models/vocabulary-options.model';
33
import { VocabularyEntryDetail } from '../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
4+
import { ActivatedRoute } from '@angular/router';
5+
import { Observable, Subscription } from 'rxjs';
6+
import { BrowseDefinition } from '../../core/shared/browse-definition.model';
7+
import { GenericConstructor } from '../../core/shared/generic-constructor';
8+
import { BROWSE_BY_COMPONENT_FACTORY } from '../browse-by-switcher/browse-by-decorator';
9+
import { map } from 'rxjs/operators';
10+
import { ThemeService } from 'src/app/shared/theme-support/theme.service';
11+
import { HierarchicalBrowseDefinition } from '../../core/shared/hierarchical-browse-definition.model';
412

513
@Component({
614
selector: 'ds-browse-by-taxonomy-page',
@@ -10,7 +18,7 @@ import { VocabularyEntryDetail } from '../../core/submission/vocabularies/models
1018
/**
1119
* Component for browsing items by metadata in a hierarchical controlled vocabulary
1220
*/
13-
export class BrowseByTaxonomyPageComponent implements OnInit {
21+
export class BrowseByTaxonomyPageComponent implements OnInit, OnDestroy {
1422

1523
/**
1624
* The {@link VocabularyOptions} object
@@ -27,8 +35,48 @@ export class BrowseByTaxonomyPageComponent implements OnInit {
2735
*/
2836
filterValues: string[];
2937

30-
ngOnInit() {
31-
this.vocabularyOptions = { name: 'srsc', closed: true };
38+
/**
39+
* The facet the use when filtering
40+
*/
41+
facetType: string;
42+
43+
/**
44+
* The used vocabulary
45+
*/
46+
vocabularyName: string;
47+
48+
/**
49+
* The parameters used in the URL
50+
*/
51+
queryParams: any;
52+
53+
/**
54+
* Resolved browse-by component
55+
*/
56+
browseByComponent: Observable<any>;
57+
58+
/**
59+
* Subscriptions to track
60+
*/
61+
browseByComponentSubs: Subscription[] = [];
62+
63+
public constructor( protected route: ActivatedRoute,
64+
protected themeService: ThemeService,
65+
@Inject(BROWSE_BY_COMPONENT_FACTORY) private getComponentByBrowseByType: (browseByType, theme) => GenericConstructor<any>) {
66+
}
67+
68+
ngOnInit(): void {
69+
this.browseByComponent = this.route.data.pipe(
70+
map((data: { browseDefinition: BrowseDefinition }) => {
71+
this.getComponentByBrowseByType(data.browseDefinition.getRenderType(), this.themeService.getThemeName());
72+
return data.browseDefinition;
73+
})
74+
);
75+
this.browseByComponentSubs.push(this.browseByComponent.subscribe((browseDefinition: HierarchicalBrowseDefinition) => {
76+
this.facetType = browseDefinition.facetType;
77+
this.vocabularyName = browseDefinition.vocabulary;
78+
this.vocabularyOptions = { name: this.vocabularyName, closed: true };
79+
}));
3280
}
3381

3482
/**
@@ -41,10 +89,30 @@ export class BrowseByTaxonomyPageComponent implements OnInit {
4189
this.selectedItems.push(detail);
4290
this.filterValues = this.selectedItems
4391
.map((item: VocabularyEntryDetail) => `${item.value},equals`);
92+
this.updateQueryParams();
4493
}
4594

95+
/**
96+
* Removes detail from selectedItems and filterValues.
97+
*
98+
* @param detail VocabularyEntryDetail to be removed
99+
*/
46100
onDeselect(detail: VocabularyEntryDetail): void {
47101
this.selectedItems = this.selectedItems.filter((entry: VocabularyEntryDetail) => { return entry !== detail; });
48102
this.filterValues = this.filterValues.filter((value: string) => { return value !== `${detail.value},equals`; });
103+
this.updateQueryParams();
104+
}
105+
106+
/**
107+
* Updates queryParams based on the current facetType and filterValues.
108+
*/
109+
private updateQueryParams(): void {
110+
this.queryParams = {
111+
['f.' + this.facetType]: this.filterValues
112+
};
113+
}
114+
115+
ngOnDestroy(): void {
116+
this.browseByComponentSubs.forEach((sub: Subscription) => sub.unsubscribe());
49117
}
50118
}

0 commit comments

Comments
 (0)