]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/cnbrowse/results.component.ts
LP1819498 Angular staff catalog call number browse
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / cnbrowse / results.component.ts
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';
11
12 @Component({
13   selector: 'eg-catalog-cn-browse-results',
14   templateUrl: 'results.component.html'
15 })
16 export class CnBrowseResultsComponent implements OnInit, OnDestroy {
17
18     searchContext: CatalogSearchContext;
19     results: any[];
20     routeSub: Subscription;
21
22     constructor(
23         private router: Router,
24         private route: ActivatedRoute,
25         private cat: CatalogService,
26         private bib: BibRecordService,
27         private catUrl: CatalogUrlService,
28         private staffCat: StaffCatalogService
29     ) {}
30
31     ngOnInit() {
32         this.searchContext = this.staffCat.searchContext;
33         this.routeSub = this.route.queryParamMap.subscribe(
34             (params: ParamMap) => this.browseByUrl(params)
35         );
36     }
37
38     ngOnDestroy() {
39         this.routeSub.unsubscribe();
40     }
41
42     browseByUrl(params: ParamMap): void {
43         this.catUrl.applyUrlParams(this.searchContext, params);
44         const cbs = this.searchContext.cnBrowseSearch;
45
46         if (cbs.isSearchable()) {
47             this.results = [];
48             this.cat.cnBrowse(this.searchContext)
49                 .subscribe(results => this.processResults(results));
50         }
51     }
52
53     processResults(results: any[]) {
54         this.results = results;
55
56         const depth = this.searchContext.global ?
57             this.searchContext.org.root().ou_type().depth() :
58             this.searchContext.searchOrg.ou_type().depth();
59
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;
63         };
64
65         const bres: IdlObject[] = [];
66         this.bib.getBibSummary(
67             bibIds.filter(distinct),
68             this.searchContext.searchOrg.id(), depth
69         ).subscribe(
70             summary => {
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);
76
77                 bres.push(summary.record);
78
79                 // Use _ since result is an 'acn' object.
80                 bibResults.forEach(r => r._bibSummary = summary);
81             },
82             err => {},
83             ()  => {
84                 this.bib.fleshBibUsers(bres);
85             }
86         );
87     }
88
89     browseIsDone(): boolean {
90         return this.searchContext.searchState === CatalogSearchState.COMPLETE;
91     }
92
93     browseIsActive(): boolean {
94         return this.searchContext.searchState === CatalogSearchState.SEARCHING;
95     }
96
97     browseHasResults(): boolean {
98         return this.browseIsDone() && this.results.length > 0;
99     }
100
101     prevPage() {
102         this.searchContext.cnBrowseSearch.offset--;
103         this.staffCat.cnBrowse();
104     }
105
106     nextPage() {
107         this.searchContext.cnBrowseSearch.offset++;
108         this.staffCat.cnBrowse();
109     }
110
111     /**
112      * Propagate the search params along when navigating to each record.
113      */
114     navigateToRecord(summary: BibRecordSummary) {
115         const params = this.catUrl.toUrlParams(this.searchContext);
116
117         this.router.navigate(
118             ['/staff/catalog/record/' + summary.id], {queryParams: params});
119     }
120 }
121
122