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