-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathapp.component.ts
More file actions
198 lines (175 loc) · 5.5 KB
/
app.component.ts
File metadata and controls
198 lines (175 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
AsyncPipe,
isPlatformBrowser,
} from '@angular/common';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
DOCUMENT,
HostListener,
Inject,
OnInit,
PLATFORM_ID,
} from '@angular/core';
import {
NavigationCancel,
NavigationEnd,
NavigationStart,
Router,
} from '@angular/router';
import { AuthService } from '@dspace/core/auth/auth.service';
import { isAuthenticationBlocking } from '@dspace/core/auth/selectors';
import {
NativeWindowRef,
NativeWindowService,
} from '@dspace/core/services/window.service';
import { distinctNext } from '@dspace/core/shared/distinct-next';
import {
NgbModal,
NgbModalConfig,
} from '@ng-bootstrap/ng-bootstrap';
import {
select,
Store,
} from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
BehaviorSubject,
Observable,
} from 'rxjs';
import {
delay,
distinctUntilChanged,
take,
withLatestFrom,
} from 'rxjs/operators';
import { environment } from '../environments/environment';
import { ThemedRootComponent } from './root/themed-root.component';
import { HostWindowResizeAction } from './shared/host-window.actions';
import { IdleModalComponent } from './shared/idle-modal/idle-modal.component';
import { CSSVariableService } from './shared/sass-helper/css-variable.service';
import { HostWindowState } from './shared/search/host-window.reducer';
import { ThemeService } from './shared/theme-support/theme.service';
import { SocialComponent } from './social/social.component';
import { SocialService } from './social/social.service';
@Component({
selector: 'ds-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
AsyncPipe,
SocialComponent,
ThemedRootComponent,
],
})
export class AppComponent implements OnInit, AfterViewInit {
notificationOptions;
models;
/**
* Whether or not the authentication is currently blocking the UI
*/
isAuthBlocking$: Observable<boolean>;
/**
* Whether or not the app is in the process of rerouting
*/
isRouteLoading$: BehaviorSubject<boolean> = new BehaviorSubject(false);
/**
* Whether or not the theme is in the process of being swapped
*/
isThemeLoading$: Observable<boolean>;
/**
* Whether or not the idle modal is is currently open
*/
idleModalOpen: boolean;
/**
* In order to show sharing component only in csr
*/
browserPlatform = false;
constructor(
@Inject(NativeWindowService) private _window: NativeWindowRef,
@Inject(DOCUMENT) private document: any,
@Inject(PLATFORM_ID) private platformId: any,
private themeService: ThemeService,
private translate: TranslateService,
private store: Store<HostWindowState>,
private authService: AuthService,
private router: Router,
private cssService: CSSVariableService,
private modalService: NgbModal,
private modalConfig: NgbModalConfig,
private socialService: SocialService,
) {
this.notificationOptions = environment.notifications;
this.browserPlatform = isPlatformBrowser(this.platformId);
if (this.browserPlatform) {
this.trackIdleModal();
}
this.isThemeLoading$ = this.themeService.isThemeLoading$;
this.storeCSSVariables();
this.socialService.initialize();
}
ngOnInit() {
/** Implement behavior for interface {@link ModalBeforeDismiss} */
this.modalConfig.beforeDismiss = async function () {
if (typeof this?.componentInstance?.beforeDismiss === 'function') {
return this.componentInstance.beforeDismiss();
}
// fall back to default behavior
return true;
};
this.modalConfig.ariaLabelledBy = 'modal-title';
this.isAuthBlocking$ = this.store.pipe(
select(isAuthenticationBlocking),
distinctUntilChanged(),
);
this.dispatchWindowSize(this._window.nativeWindow.innerWidth, this._window.nativeWindow.innerHeight);
}
private storeCSSVariables() {
this.cssService.clearCSSVariables();
this.cssService.addCSSVariables(this.cssService.getCSSVariablesFromStylesheets(this.document));
}
ngAfterViewInit() {
this.router.events.pipe(
// delay(0) to prevent "Expression has changed after it was checked" errors
delay(0),
).subscribe((event) => {
if (event instanceof NavigationStart) {
distinctNext(this.isRouteLoading$, true);
} else if (
event instanceof NavigationEnd ||
event instanceof NavigationCancel
) {
distinctNext(this.isRouteLoading$, false);
}
});
}
@HostListener('window:resize', ['$event'])
public onResize(event): void {
this.dispatchWindowSize(event.target.innerWidth, event.target.innerHeight);
}
private dispatchWindowSize(width, height): void {
this.store.dispatch(
new HostWindowResizeAction(width, height),
);
}
private trackIdleModal() {
const isIdle$ = this.authService.isUserIdle();
const isAuthenticated$ = this.authService.isAuthenticated();
isIdle$.pipe(withLatestFrom(isAuthenticated$))
.subscribe(([userIdle, authenticated]) => {
if (userIdle && authenticated) {
if (!this.idleModalOpen) {
const modalRef = this.modalService.open(IdleModalComponent, { ariaLabelledBy: 'idle-modal.header' });
this.idleModalOpen = true;
modalRef.componentInstance.response.pipe(take(1)).subscribe((closed: boolean) => {
if (closed) {
this.idleModalOpen = false;
}
});
}
}
});
}
}