]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/nav.component.ts
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / nav.component.ts
1 import {Component, OnInit, OnDestroy, QueryList, ViewChild, ViewChildren} from '@angular/core';
2 import {ActivatedRoute, Router} from '@angular/router';
3 import {Location} from '@angular/common';
4 import {Subscription} from 'rxjs';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {LocaleService} from '@eg/core/locale.service';
9 import {PrintService} from '@eg/share/print/print.service';
10 import {StoreService} from '@eg/core/store.service';
11 import {NetRequest, NetService} from '@eg/core/net.service';
12 import {OpChangeComponent} from '@eg/staff/share/op-change/op-change.component';
13 import {PermService} from '@eg/core/perm.service';
14 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
15 import {NgbCollapseModule, NgbDropdown} from '@ng-bootstrap/ng-bootstrap';
16
17 @Component({
18     selector: 'eg-staff-nav-bar',
19     styleUrls: ['nav.component.css'],
20     templateUrl: 'nav.component.html'
21 })
22
23 export class StaffNavComponent implements OnInit, OnDestroy {
24
25     // Locales that have Angular staff translations
26     locales: any[];
27     currentLocale: any;
28
29     // When active, show a link to the traditional staff catalog
30     showTraditionalCatalog = true;
31     showAngularAcq: boolean;
32     curbsideEnabled: boolean;
33     showAngularCirc = false;
34     maxRecentPatrons = 1;
35
36     // Menu toggle
37     isMenuCollapsed = true;
38
39     @ViewChild('navOpChange', {static: false}) opChange: OpChangeComponent;
40     @ViewChild('confirmLogout', { static: true }) confirmLogout: ConfirmDialogComponent;
41     @ViewChildren(NgbDropdown) dropdowns: QueryList<NgbDropdown>;
42     permFailedSub: Subscription;
43
44     constructor(
45         private router: Router,
46         private store: StoreService,
47         private net: NetService,
48         private org: OrgService,
49         private auth: AuthService,
50         private perm: PermService,
51         private pcrud: PcrudService,
52         private locale: LocaleService,
53         private printer: PrintService
54     ) {
55         this.locales = [];
56     }
57
58     ngOnInit() {
59
60         this.locale.supportedLocales().subscribe(
61             l => this.locales.push(l),
62             (err: unknown) => {},
63             () => {
64                 this.currentLocale = this.locales.filter(
65                     l => l.code() === this.locale.currentLocaleCode())[0];
66             }
67         );
68
69         // NOTE: this can eventually go away.
70         // Avoid attempts to fetch org settings if the user has not yet
71         // logged in (e.g. this is the login page).
72         if (this.user()) {
73             // Note these are all pre-cached by our resolver.
74             // Batching not required.
75             this.org.settings('ui.staff.traditional_catalog.enabled')
76                 .then(settings => this.showTraditionalCatalog =
77                 Boolean(settings['ui.staff.traditional_catalog.enabled']));
78
79             this.org.settings('circ.curbside')
80                 .then(settings => this.curbsideEnabled =
81                 Boolean(settings['circ.curbside']));
82
83             this.org.settings('ui.staff.max_recent_patrons')
84                 .then(settings => this.maxRecentPatrons =
85                 settings['ui.staff.max_recent_patrons'] ?? 1);
86
87             // Do we show the angular circ menu?
88             // TODO remove these once Angular Circ takes over.
89             const angSet = 'ui.staff.angular_circ.enabled';
90             const angPerm = 'ACCESS_ANGULAR_CIRC';
91
92             this.org.settings(angSet).then(s => {
93                 if (s[angSet]) {
94                     return this.perm.hasWorkPermHere([angPerm])
95                         .then(perms => perms[angPerm]);
96                 } else {
97                     return false;
98                 }
99             }).then(enable => this.showAngularCirc = enable);
100         }
101
102         // Wire up our op-change component as the general purpose
103         // permission failed handler.
104         this.net.permFailedHasHandler = true;
105         this.permFailedSub =
106             this.net.permFailed$.subscribe(
107                 (req: NetRequest) => this.opChange.escalateRequest(req));
108     }
109
110     ngOnDestroy() {
111         if (this.permFailedSub) {
112             this.permFailedSub.unsubscribe();
113         }
114     }
115
116     user() {
117         return this.auth.user() ? this.auth.user().usrname() : '';
118     }
119
120     user_id() {
121         return this.auth.user() ? this.auth.user().id() : '';
122     }
123
124     workstation() {
125         return this.auth.user() ? this.auth.workstation() : '';
126     }
127
128     ws_ou() {
129         return this.auth.user() ? this.auth.user().ws_ou() : '';
130     }
131
132     setLocale(locale: any) {
133         this.locale.setLocale(locale.code());
134     }
135
136     opChangeActive(): boolean {
137         return this.auth.opChangeIsActive();
138     }
139
140     maybeLogout() {
141         this.confirmLogout.open().subscribe(confirmed => {
142             if (!confirmed) { return; }
143
144             this.logout();
145         });
146     }
147
148     // Broadcast to all tabs that we're logging out.
149     // Redirect to the login page, which performs the remaining
150     // logout duties.
151     logout(): void {
152         this.auth.broadcastLogout();
153         this.router.navigate(['/staff/login']);
154     }
155
156     reprintLast() {
157         this.printer.reprintLast();
158     }
159
160     retrieveLastRecord() {
161         const recId = this.store.getLocalItem('eg.cat.last_record_retrieved');
162         if (recId) {
163             this.router.navigate(['/staff/catalog/record/' + recId]);
164         }
165     }
166
167     closeDropdowns() {
168         this.dropdowns?.forEach(x => x.close());
169     }
170 }
171
172