8505e768f3284164102a536d6e12988728c8ee65
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / result / record.component.ts
1 import {Component, OnInit, OnDestroy, Input} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {Router} from '@angular/router';
4 import {OrgService} from '@eg/core/org.service';
5 import {NetService} from '@eg/core/net.service';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {CatalogService} from '@eg/share/catalog/catalog.service';
8 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
10 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
11 import {StaffCatalogService} from '../catalog.service';
12 import {BasketService} from '@eg/share/catalog/basket.service';
13
14 @Component({
15   selector: 'eg-catalog-result-record',
16   templateUrl: 'record.component.html',
17   styleUrls: ['record.component.css']
18 })
19 export class ResultRecordComponent implements OnInit, OnDestroy {
20
21     @Input() index: number;  // 0-index display row
22     @Input() summary: BibRecordSummary;
23
24     // Optional call number (acn) object to highlight
25     // Assumed prefix/suffix are fleshed
26     // Used by call number browse.
27     @Input() callNumber: IdlObject;
28
29     searchContext: CatalogSearchContext;
30     isRecordSelected: boolean;
31     basketSub: Subscription;
32
33     constructor(
34         private router: Router,
35         private org: OrgService,
36         private net: NetService,
37         private bib: BibRecordService,
38         private cat: CatalogService,
39         private catUrl: CatalogUrlService,
40         private staffCat: StaffCatalogService,
41         private basket: BasketService
42     ) {}
43
44     ngOnInit() {
45         this.searchContext = this.staffCat.searchContext;
46         this.summary.getHoldCount();
47         this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
48
49         // Watch for basket changes caused by other components
50         this.basketSub = this.basket.onChange.subscribe(() => {
51             this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
52         });
53     }
54
55     ngOnDestroy() {
56         this.basketSub.unsubscribe();
57     }
58
59     orgName(orgId: number): string {
60         return this.org.get(orgId).shortname();
61     }
62
63     iconFormatLabel(code: string): string {
64         return this.cat.iconFormatLabel(code);
65     }
66
67     placeHold(): void {
68         let holdType = 'T';
69         let holdTarget = this.summary.id;
70
71         const ts = this.searchContext.termSearch;
72         if (ts.isMetarecordSearch()) {
73             holdType = 'M';
74             holdTarget = this.summary.metabibId;
75         }
76
77         this.router.navigate([`/staff/catalog/hold/${holdType}`],
78             {queryParams: {target: holdTarget}});
79     }
80
81     addToList(): void {
82         alert('Adding to list for bib ' + this.summary.id);
83     }
84
85     searchAuthor(summary: any) {
86         this.searchContext.reset();
87         this.searchContext.termSearch.fieldClass = ['author'];
88         this.searchContext.termSearch.query = [summary.display.author];
89         this.staffCat.search();
90     }
91
92     /**
93      * Propagate the search params along when navigating to each record.
94      */
95     navigateToRecord(summary: BibRecordSummary) {
96         const params = this.catUrl.toUrlParams(this.searchContext);
97
98         // Jump to metarecord constituent records page when a
99         // MR has more than 1 constituents.
100         if (summary.metabibId && summary.metabibRecords.length > 1) {
101             this.searchContext.termSearch.fromMetarecord = summary.metabibId;
102             this.staffCat.search();
103             return;
104         }
105
106         this.router.navigate(
107             ['/staff/catalog/record/' + summary.id], {queryParams: params});
108     }
109
110     toggleBasketEntry() {
111         if (this.isRecordSelected) {
112             return this.basket.addRecordIds([this.summary.id]);
113         } else {
114             return this.basket.removeRecordIds([this.summary.id]);
115         }
116     }
117 }
118
119