21657057f5d86a3daa7e3f5fa768bbd3285b7a21
[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
8 @Component({
9   selector: 'eg-bib-staff-view',
10   templateUrl: 'bib-staff-view.component.html',
11   styleUrls: ['bib-staff-view.component.css']
12 })
13 export class BibStaffViewComponent implements OnInit {
14
15     initDone = false;
16
17     // True / false if the display is vertically expanded
18     private _exp: boolean;
19     set expand(e: boolean) {
20         this._exp = e;
21         if (this.initDone) {
22             this.saveExpandState();
23         }
24     }
25     get expand(): boolean { return this._exp; }
26
27     // If provided, the record will be fetched by the component.
28     @Input() recordId: number;
29
30     // Otherwise, we'll use the provided bib summary object.
31     summary: BibRecordSummary;
32     @Input() set bibSummary(s: any) {
33         this.summary = s;
34         if (this.initDone && this.summary) {
35             this.summary.getBibCallNumber();
36         }
37     }
38
39     constructor(
40         private bib: BibRecordService,
41         private org: OrgService,
42         private store: ServerStoreService,
43         private cat: CatalogService
44     ) {}
45
46     ngOnInit() {
47
48         this.store.getItem('eg.cat.record.staff-view.collapse')
49         .then(value => this.expand = !value)
50         .then(_ => this.cat.fetchCcvms())
51         .then(_ => {
52             if (this.summary) {
53                 return this.summary.getBibCallNumber();
54             } else {
55                 if (this.recordId) {
56                     return this.loadSummary();
57                 }
58             }
59         }).then(_ => this.initDone = true);
60     }
61
62     saveExpandState() {
63         this.store.setItem('eg.cat.record.staff-view.collapse', !this.expand);
64     }
65
66     loadSummary(): Promise<any> {
67         return this.bib.getBibSummary(
68             this.recordId,
69             this.org.root().id(),
70             true // isStaff
71         ).toPromise()
72         .then(summary => {
73             this.summary = summary;
74             return summary.getBibCallNumber();
75         });
76     }
77
78     orgName(orgId: number): string {
79         if (orgId) {
80             return this.org.get(orgId).shortname();
81         }
82     }
83
84     iconFormatLabel(code: string): string {
85         return this.cat.iconFormatLabel(code);
86     }
87 }
88
89