LP1818288 Ang staff catalog record detail holds tab/actions
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / record.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
3 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
7 import {CatalogService} from '@eg/share/catalog/catalog.service';
8 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {StaffCatalogService} from '../catalog.service';
10 import {BibSummaryComponent} from '@eg/staff/share/bib-summary/bib-summary.component';
11 import {StoreService} from '@eg/core/store.service';
12
13 @Component({
14   selector: 'eg-catalog-record',
15   templateUrl: 'record.component.html'
16 })
17 export class RecordComponent implements OnInit {
18
19     recordId: number;
20     recordTab: string;
21     summary: BibRecordSummary;
22     searchContext: CatalogSearchContext;
23     @ViewChild('recordTabs') recordTabs: NgbTabset;
24     defaultTab: string; // eg.cat.default_record_tab
25
26     constructor(
27         private router: Router,
28         private route: ActivatedRoute,
29         private pcrud: PcrudService,
30         private bib: BibRecordService,
31         private cat: CatalogService,
32         private staffCat: StaffCatalogService,
33         private store: StoreService
34     ) {}
35
36     ngOnInit() {
37         this.searchContext = this.staffCat.searchContext;
38
39         this.defaultTab =
40             this.store.getLocalItem('eg.cat.default_record_tab')
41             || 'catalog';
42
43         // Watch for URL record ID changes
44         // This includes the initial route.
45         // When applying the default configured tab, no navigation occurs
46         // to apply the tab name to the URL, it displays as the default.
47         // This is done so no intermediate redirect is required, which
48         // messes with browser back/forward navigation.
49         this.route.paramMap.subscribe((params: ParamMap) => {
50             this.recordTab = params.get('tab');
51             this.recordId = +params.get('id');
52             this.searchContext = this.staffCat.searchContext;
53
54             if (!this.recordTab) {
55                 this.recordTab = this.defaultTab || 'catalog';
56             }
57
58             this.loadRecord();
59         });
60     }
61
62     setDefaultTab() {
63         this.defaultTab = this.recordTab;
64         this.store.setLocalItem('eg.cat.default_record_tab', this.recordTab);
65     }
66
67     // Changing a tab in the UI means changing the route.
68     // Changing the route ultimately results in changing the tab.
69     onTabChange(evt: NgbTabChangeEvent) {
70         this.recordTab = evt.nextId;
71
72         // prevent tab changing until after route navigation
73         evt.preventDefault();
74
75         this.routeToTab();
76     }
77
78     routeToTab() {
79         const url =
80             `/staff/catalog/record/${this.recordId}/${this.recordTab}`;
81
82         // Retain search parameters
83         this.router.navigate([url], {queryParamsHandling: 'merge'});
84     }
85
86     loadRecord(): void {
87
88         // Avoid re-fetching the same record summary during tab navigation.
89         if (this.staffCat.currentDetailRecordSummary &&
90             this.recordId === this.staffCat.currentDetailRecordSummary.id) {
91             this.summary = this.staffCat.currentDetailRecordSummary;
92             return;
93         }
94
95         this.summary = null;
96         this.bib.getBibSummary(
97             this.recordId,
98             this.searchContext.searchOrg.id(),
99             this.searchContext.searchOrg.ou_type().depth()).toPromise()
100         .then(summary => {
101             this.summary =
102                 this.staffCat.currentDetailRecordSummary = summary;
103             this.bib.fleshBibUsers([summary.record]);
104         });
105     }
106
107     currentSearchOrg(): IdlObject {
108         if (this.staffCat && this.staffCat.searchContext) {
109             return this.staffCat.searchContext.searchOrg;
110         }
111         return null;
112     }
113 }
114
115