LP1806087 Angular catalog Ang7 & lint repairs
[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 const ANGJS_TABS: any = {
14     marc_edit: true,
15     holds: true,
16     holdings: true,
17     conjoined: true
18 };
19
20 @Component({
21   selector: 'eg-catalog-record',
22   templateUrl: 'record.component.html'
23 })
24 export class RecordComponent implements OnInit {
25
26     recordId: number;
27     recordTab: string;
28     summary: BibRecordSummary;
29     searchContext: CatalogSearchContext;
30     @ViewChild('recordTabs') recordTabs: NgbTabset;
31     defaultTab: string; // eg.cat.default_record_tab
32
33     constructor(
34         private router: Router,
35         private route: ActivatedRoute,
36         private pcrud: PcrudService,
37         private bib: BibRecordService,
38         private cat: CatalogService,
39         private staffCat: StaffCatalogService,
40         private store: StoreService
41     ) {}
42
43     ngOnInit() {
44         this.searchContext = this.staffCat.searchContext;
45
46         this.defaultTab =
47             this.store.getLocalItem('eg.cat.default_record_tab')
48             || 'catalog';
49
50         // TODO: Implement default tab handling for tabs that require
51         // and AngJS redirect.
52
53         // Watch for URL record ID changes
54         // This includes the initial route.
55         // When applying the default configured tab, no navigation occurs
56         // to apply the tab name to the URL, it displays as the default.
57         // This is done so no intermediate redirect is required, which
58         // messes with browser back/forward navigation.
59         this.route.paramMap.subscribe((params: ParamMap) => {
60             this.recordTab = params.get('tab');
61             this.recordId = +params.get('id');
62             this.searchContext = this.staffCat.searchContext;
63
64             if (!this.recordTab) {
65                 this.recordTab = this.defaultTab || 'catalog';
66                 // On initial load, if the default tab is set to one of
67                 // the AngularJS tabs, redirect the user there.
68                 if (this.recordTab in ANGJS_TABS) {
69                     return this.routeToTab();
70                 }
71             }
72
73             this.loadRecord();
74         });
75     }
76
77     setDefaultTab() {
78         this.defaultTab = this.recordTab;
79         this.store.setLocalItem('eg.cat.default_record_tab', this.recordTab);
80     }
81
82     // Changing a tab in the UI means changing the route.
83     // Changing the route ultimately results in changing the tab.
84     onTabChange(evt: NgbTabChangeEvent) {
85         this.recordTab = evt.nextId;
86
87         // prevent tab changing until after route navigation
88         evt.preventDefault();
89
90         this.routeToTab();
91     }
92
93     routeToTab() {
94
95         // Route to the AngularJS catalog tab
96         if (this.recordTab in ANGJS_TABS) {
97             const angjsBase = '/eg/staff/cat/catalog/record';
98
99             window.location.href =
100                 `${angjsBase}/${this.recordId}/${this.recordTab}`;
101             return;
102         }
103
104         const url =
105             `/staff/catalog/record/${this.recordId}/${this.recordTab}`;
106
107         // Retain search parameters
108         this.router.navigate([url], {queryParamsHandling: 'merge'});
109     }
110
111     loadRecord(): void {
112
113         // Avoid re-fetching the same record summary during tab navigation.
114         if (this.staffCat.currentDetailRecordSummary &&
115             this.recordId === this.staffCat.currentDetailRecordSummary.id) {
116             this.summary = this.staffCat.currentDetailRecordSummary;
117             return;
118         }
119
120         this.summary = null;
121         this.bib.getBibSummary(
122             this.recordId,
123             this.searchContext.searchOrg.id(),
124             this.searchContext.searchOrg.ou_type().depth()).toPromise()
125         .then(summary => {
126             this.summary =
127                 this.staffCat.currentDetailRecordSummary = summary;
128             this.bib.fleshBibUsers([summary.record]);
129         });
130     }
131 }
132
133