LP1869906 Angular staff cat browse links
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / browse / results.component.ts
1 import {Component, OnInit, OnDestroy} from '@angular/core';
2 import {ActivatedRoute, ParamMap} from '@angular/router';
3 import {Subscription} from 'rxjs';
4 import {CatalogService} from '@eg/share/catalog/catalog.service';
5 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
6 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
7 import {StaffCatalogService} from '../catalog.service';
8
9 @Component({
10   selector: 'eg-catalog-browse-results',
11   templateUrl: 'results.component.html'
12 })
13 export class BrowseResultsComponent implements OnInit, OnDestroy {
14
15     searchContext: CatalogSearchContext;
16     results: any[];
17     routeSub: Subscription;
18
19     constructor(
20         private route: ActivatedRoute,
21         private cat: CatalogService,
22         private catUrl: CatalogUrlService,
23         private staffCat: StaffCatalogService
24     ) {}
25
26     ngOnInit() {
27         this.searchContext = this.staffCat.searchContext;
28         this.routeSub = this.route.queryParamMap.subscribe(
29             (params: ParamMap) => this.browseByUrl(params)
30         );
31     }
32
33     ngOnDestroy() {
34         this.routeSub.unsubscribe();
35     }
36
37     browseByUrl(params: ParamMap): void {
38         this.catUrl.applyUrlParams(this.searchContext, params);
39         const bs = this.searchContext.browseSearch;
40
41         // SearchContext applies a default fieldClass value of 'keyword'.
42         // Replace with 'title', since there is no 'keyword' browse.
43         if (bs.fieldClass === 'keyword') {
44             bs.fieldClass = 'title';
45         }
46
47         if (bs.isSearchable()) {
48             this.results = [];
49             this.cat.browse(this.searchContext)
50                 .subscribe(result => this.addResult(result));
51         }
52     }
53
54     addResult(result: any) {
55
56         result.compiledHeadings = [];
57
58         // Avoi dupe headings per see
59         const seen: any = {};
60
61         result.sees.forEach(sees => {
62             if (!sees.control_set) { return; }
63
64             sees.headings.forEach(headingStruct => {
65                 const fieldId = Object.keys(headingStruct)[0];
66                 const heading = headingStruct[fieldId][0];
67
68                 const inList = result.list_authorities.filter(
69                     id => Number(id) === Number(heading.target))[0];
70
71                 if (   heading.target
72                     && heading.main_entry
73                     && heading.target_count
74                     && !inList
75                     && !seen[heading.target]) {
76
77                     seen[heading.target] = true;
78
79                     result.compiledHeadings.push({
80                         heading: heading.heading,
81                         target: heading.target,
82                         target_count: heading.target_count,
83                         type: heading.type
84                     });
85                 }
86             });
87         });
88
89         this.results.push(result);
90     }
91
92     browseIsDone(): boolean {
93         return this.searchContext.searchState === CatalogSearchState.COMPLETE;
94     }
95
96     browseIsActive(): boolean {
97         return this.searchContext.searchState === CatalogSearchState.SEARCHING;
98     }
99
100     browseHasResults(): boolean {
101         return this.browseIsDone() && this.results.length > 0;
102     }
103
104     prevPage() {
105         const firstResult = this.results[0];
106         if (firstResult) {
107             this.searchContext.browseSearch.pivot = firstResult.pivot_point;
108             this.staffCat.browse();
109         }
110     }
111
112     nextPage() {
113         const lastResult = this.results[this.results.length - 1];
114         if (lastResult) {
115             this.searchContext.browseSearch.pivot = lastResult.pivot_point;
116             this.staffCat.browse();
117         }
118     }
119
120     searchByBrowseEntryParams(result) {
121         const ctx = this.searchContext.clone();
122         ctx.browseSearch.reset(); // we're done browsing
123         ctx.termSearch.hasBrowseEntry =
124             result.browse_entry + ',' + result.fields;
125         return this.catUrl.toUrlParams(ctx);
126     }
127
128     // NOTE: to test unauthorized heading display in concerto
129     // browse for author = kab
130     newBrowseFromHeadingParams(heading) {
131         const ctx = this.searchContext.clone();
132         ctx.browseSearch.value = heading.heading;
133         return this.catUrl.toUrlParams(ctx);
134     }
135 }
136
137