1 import {Component, OnInit, OnDestroy} from '@angular/core';
2 import {ActivatedRoute, Router, ParamMap} from '@angular/router';
3 import {Subscription} from 'rxjs';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {BibRecordService} from '@eg/share/catalog/bib-record.service';
7 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
8 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
9 import {StaffCatalogService} from '../catalog.service';
10 import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
13 selector: 'eg-catalog-cn-browse-results',
14 templateUrl: 'results.component.html'
16 export class CnBrowseResultsComponent implements OnInit, OnDestroy {
18 searchContext: CatalogSearchContext;
20 routeSub: Subscription;
23 private router: Router,
24 private route: ActivatedRoute,
25 private cat: CatalogService,
26 private bib: BibRecordService,
27 private catUrl: CatalogUrlService,
28 private staffCat: StaffCatalogService
32 this.searchContext = this.staffCat.searchContext;
33 this.routeSub = this.route.queryParamMap.subscribe(
34 (params: ParamMap) => this.browseByUrl(params)
39 this.routeSub.unsubscribe();
42 browseByUrl(params: ParamMap): void {
43 this.catUrl.applyUrlParams(this.searchContext, params);
44 const cbs = this.searchContext.cnBrowseSearch;
46 if (cbs.isSearchable()) {
48 this.cat.cnBrowse(this.searchContext)
49 .subscribe(results => this.processResults(results));
53 processResults(results: any[]) {
54 this.results = results;
56 const depth = this.searchContext.global ?
57 this.searchContext.org.root().ou_type().depth() :
58 this.searchContext.searchOrg.ou_type().depth();
60 const bibIds = this.results.map(r => r.record().id());
61 const distinct = (value: any, index: number, self: Array<number>) => {
62 return self.indexOf(value) === index;
65 const bres: IdlObject[] = [];
66 this.bib.getBibSummary(
67 bibIds.filter(distinct),
68 this.searchContext.searchOrg.id(), depth
71 // Response order not guaranteed. Match the summary
72 // object up with its response object. A bib may be
73 // linked to multiple call numbers
74 const bibResults = this.results.filter(
75 r => Number(r.record().id()) === summary.id);
77 bres.push(summary.record);
79 // Use _ since result is an 'acn' object.
80 bibResults.forEach(r => r._bibSummary = summary);
84 this.bib.fleshBibUsers(bres);
89 browseIsDone(): boolean {
90 return this.searchContext.searchState === CatalogSearchState.COMPLETE;
93 browseIsActive(): boolean {
94 return this.searchContext.searchState === CatalogSearchState.SEARCHING;
97 browseHasResults(): boolean {
98 return this.browseIsDone() && this.results.length > 0;
102 this.searchContext.cnBrowseSearch.offset--;
103 this.staffCat.cnBrowse();
107 this.searchContext.cnBrowseSearch.offset++;
108 this.staffCat.cnBrowse();
112 * Propagate the search params along when navigating to each record.
114 navigateToRecord(summary: BibRecordSummary) {
115 const params = this.catUrl.toUrlParams(this.searchContext);
117 this.router.navigate(
118 ['/staff/catalog/record/' + summary.id], {queryParams: params});