lp1959010 have the staff display component check search context for the active search OU
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / share / bib-staff-view / bib-staff-view.component.ts
1 import {Component, OnInit, Input} from '@angular/core';
2 import {OrgService} from '@eg/core/org.service';
3 import {BibRecordService, BibRecordSummary
4     } from '@eg/share/catalog/bib-record.service';
5 import {ServerStoreService} from '@eg/core/server-store.service';
6 import {CatalogService} from '@eg/share/catalog/catalog.service';
7 import {StaffCatalogService} from '@eg/staff/catalog/catalog.service';
8
9 @Component({
10   selector: 'eg-bib-staff-view',
11   templateUrl: 'bib-staff-view.component.html',
12   styleUrls: ['bib-staff-view.component.css']
13 })
14 export class BibStaffViewComponent implements OnInit {
15
16     recId: number;
17     initDone = false;
18
19     // True / false if the display is vertically expanded
20     private _exp: boolean;
21     set expand(e: boolean) {
22         this._exp = e;
23         if (this.initDone) {
24             this.saveExpandState();
25         }
26     }
27     get expand(): boolean { return this._exp; }
28
29     @Input() set recordId(id: number) {
30         this.recId = id;
31         // Only force new data collection when recordId()
32         // is invoked after ngInit() has already run.
33         if (this.initDone) {
34             this.loadSummary();
35         }
36     }
37
38     // Otherwise, we'll use the provided bib summary object.
39     summary: BibRecordSummary;
40     @Input() set bibSummary(s: any) {
41         this.summary = s;
42         if (this.initDone && this.summary) {
43             this.summary.getBibCallNumber();
44         }
45     }
46
47     constructor(
48         private bib: BibRecordService,
49         private org: OrgService,
50         private store: ServerStoreService,
51         private cat: CatalogService,
52         private staffCat: StaffCatalogService
53     ) {}
54
55     ngOnInit() {
56
57         this.store.getItem('eg.cat.record.staff-view.collapse')
58         .then(value => this.expand = !value)
59         .then(_ => this.cat.fetchCcvms())
60         .then(_ => {
61             if (this.recId) {
62                 // ignore any existing this.summary, always refetch
63                 return this.loadSummary();
64             }
65         }).then(_ => this.initDone = true);
66     }
67
68     saveExpandState() {
69         this.store.setItem('eg.cat.record.staff-view.collapse', !this.expand);
70     }
71
72     loadSummary(): Promise<any> {
73         return this.bib.getBibSummary(
74             this.recId,
75             this.staffCat.searchContext.searchOrg.id(),
76             true // isStaff
77         ).toPromise()
78         .then(summary => {
79             this.summary = summary;
80             return summary.getBibCallNumber();
81         });
82     }
83
84     orgName(orgId: number): string {
85         if (orgId) {
86             return this.org.get(orgId).shortname();
87         }
88     }
89
90     iconFormatLabel(code: string): string {
91         return this.cat.iconFormatLabel(code);
92     }
93 }
94
95