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