]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/staff.component.ts
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / staff.component.ts
1 import {Component, OnInit, NgZone, HostListener} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
4 import {AuthService, AuthWsState} from '@eg/core/auth.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AccessKeyService} from '@eg/share/accesskey/accesskey.service';
7 import {AccessKeyInfoComponent} from '@eg/share/accesskey/accesskey-info.component';
8
9 const LOGIN_PATH = '/staff/login';
10 const WS_BASE_PATH = '/staff/admin/workstation/workstations/';
11 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
12
13 @Component({
14   templateUrl: 'staff.component.html',
15   styleUrls: ['staff.component.css']
16 })
17
18 export class StaffComponent implements OnInit {
19
20     constructor(
21         private router: Router,
22         private route: ActivatedRoute,
23         private ngLocation: Location,
24         private zone: NgZone,
25         private net: NetService,
26         private auth: AuthService,
27         private keys: AccessKeyService
28     ) {}
29
30     ngOnInit() {
31
32         // Fires on all in-staff-app router navigation, but not initial
33         // page load.
34         this.router.events.subscribe(routeEvent => {
35             if (routeEvent instanceof NavigationEnd) {
36                 // console.debug(`StaffComponent routing to ${routeEvent.url}`);
37                 this.preventForbiddenNavigation(routeEvent.url);
38             }
39         });
40
41         // Redirect to the login page on any auth timeout events.
42         this.net.authExpired$.subscribe(expireEvent => {
43
44             // If the expired authtoken was identified locally (i.e.
45             // in this browser tab) notify all tabs of imminent logout.
46             if (!expireEvent.viaExternal) {
47                 this.auth.broadcastLogout();
48             }
49
50             console.debug('Auth session has expired. Redirecting to login');
51             const url = this.ngLocation.prepareExternalUrl(this.router.url);
52
53             // https://github.com/angular/angular/issues/18254
54             // When a tab redirects to a login page as a result of
55             // another tab broadcasting a logout, ngOnInit() fails to
56             // fire in the login component, until the user interacts
57             // with the page.  Fix it by wrapping it in zone.run().
58             // This is the only navigate() where I have seen this happen.
59             this.zone.run(() => {
60                 this.router.navigate([LOGIN_PATH], {queryParams: {routeTo: url}});
61             });
62         });
63
64         this.route.data.subscribe((data: {staffResolver: any}) => {
65             // Data fetched via StaffResolver is available here.
66         });
67     }
68
69     /**
70      * Prevent the user from leaving the login page when they don't have
71      * a valid authoken.
72      *
73      * Prevent the user from leaving the workstation admin page when
74      * they don't have a valid workstation.
75      *
76      * This does not verify auth tokens with the server on every route,
77      * because that would be overkill.  This is only here to keep
78      * people boxed in with their authenication state was already
79      * known to be less then 100%.
80      */
81     preventForbiddenNavigation(url: string): void {
82
83         // No auth checks needed for login page.
84         if (url.startsWith(LOGIN_PATH)) {
85             return;
86         }
87
88         // We lost our authtoken, go back to the login page.
89         if (!this.auth.token()) {
90             this.router.navigate([LOGIN_PATH]);
91         }
92
93         // No workstation checks needed for workstation admin page.
94         if (url.startsWith(WS_BASE_PATH)) {
95             return;
96         }
97
98         if (this.auth.workstationState !== AuthWsState.VALID) {
99             this.router.navigate([WS_MANAGE_PATH]);
100         }
101     }
102
103     /**
104      * Listen for keyboard events here -- the root directive --  and pass
105      * events down to the key service for processing.
106      */
107     @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
108         this.keys.fire(evt);
109     }
110
111     /**
112      * Make sure to fire the contextmenu Event on Shift+F10
113      */
114     fireContextMenuEvent(): void {
115         const event = new MouseEvent('contextmenu', {
116             bubbles: true,
117             cancelable: false,
118             view: window,
119             button: 2,
120             buttons: 0,
121         });
122         document.activeElement.dispatchEvent(event);
123     }
124
125     /*
126     @ViewChild('egAccessKeyInfo')
127     private keyComponent: AccessKeyInfoComponent;
128     */
129
130 }
131