-
-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathapp.component.ts
More file actions
756 lines (689 loc) · 26.8 KB
/
Copy pathapp.component.ts
File metadata and controls
756 lines (689 loc) · 26.8 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import { ActivationEnd, ActivationStart, Router, RouterOutlet } from '@angular/router';
import { DomSanitizer } from '@angular/platform-browser';
import { MatIconRegistry } from '@angular/material/icon';
import * as moment from 'moment';
import { AfterViewInit, Component, HostListener, NgZone, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { setTheme as setBootstrapTheme } from 'ngx-bootstrap/utils';
import { combineLatest, take } from 'rxjs';
import { DBSyncService, SyncStatus } from '@mm-services/db-sync.service';
import { Selectors } from '@mm-selectors/index';
import { GlobalActions } from '@mm-actions/global';
import { SessionService } from '@mm-services/session.service';
import { AuthService } from '@mm-services/auth.service';
import { CustomResourceService } from '@mm-services/custom-resource.service';
import { ChangesService } from '@mm-services/changes.service';
import { UpdateServiceWorkerService } from '@mm-services/update-service-worker.service';
import { LocationService } from '@mm-services/location.service';
import { ModalService } from '@mm-services/modal.service';
import { ReloadingComponent } from '@mm-modals/reloading/reloading.component';
import { FeedbackService } from '@mm-services/feedback.service';
import { FormatDateService } from '@mm-services/format-date.service';
import { XmlFormsService } from '@mm-services/xml-forms.service';
import { JsonFormsService } from '@mm-services/json-forms.service';
import { TranslateFromService } from '@mm-services/translate-from.service';
import { CountMessageService } from '@mm-services/count-message.service';
import { PrivacyPoliciesService } from '@mm-services/privacy-policies.service';
import { LanguageService, SetLanguageService } from '@mm-services/language.service';
import { UnreadRecordsService } from '@mm-services/unread-records.service';
import { RulesEngineService } from '@mm-services/rules-engine.service';
import { RecurringProcessManagerService } from '@mm-services/recurring-process-manager.service';
import { RouteSnapshotService } from '@mm-services/route-snapshot.service';
import { CheckDateService } from '@mm-services/check-date.service';
import { SessionExpiredComponent } from '@mm-modals/session-expired/session-expired.component';
import { WealthQuintilesWatcherService } from '@mm-services/wealth-quintiles-watcher.service';
import { DatabaseConnectionMonitorService } from '@mm-services/database-connection-monitor.service';
import { DatabaseClosedComponent } from '@mm-modals/database-closed/database-closed.component';
import { TranslationDocsMatcherProvider } from '@mm-providers/translation-docs-matcher.provider';
import { TranslateLocaleService } from '@mm-services/translate-locale.service';
import { TelemetryService } from '@mm-services/telemetry.service';
import { InteractionTrackingService } from '@mm-services/interaction-tracking.service';
import { TransitionsService } from '@mm-services/transitions.service';
import { CHTDatasourceService } from '@mm-services/cht-datasource.service';
import { TranslateService } from '@mm-services/translate.service';
import { AnalyticsModulesService } from '@mm-services/analytics-modules.service';
import { AnalyticsActions } from '@mm-actions/analytics';
import { TrainingCardsService } from '@mm-services/training-cards.service';
import { FormService } from '@mm-services/form.service';
import { BrowserDetectorService } from '@mm-services/browser-detector.service';
import { BrowserCompatibilityComponent } from '@mm-modals/browser-compatibility/browser-compatibility.component';
import { PerformanceService } from '@mm-services/performance.service';
import { UserSettings, UserSettingsService } from '@mm-services/user-settings.service';
import { HeaderComponent, OLD_NAV_PERMISSION } from '@mm-components/header/header.component';
import { NgIf } from '@angular/common';
import { PrivacyPolicyComponent } from '@mm-modules/privacy-policy/privacy-policy.component';
import { SidebarMenuComponent } from '@mm-components/sidebar-menu/sidebar-menu.component';
import { SnackbarComponent } from '@mm-components/snackbar/snackbar.component';
import { TasksNotificationService } from '@mm-services/task-notifications.service';
import { HTTP_HEADERS, DOC_IDS, DOC_TYPES, PREFIXES } from '@medic/constants';
const SYNC_STATUS = {
inProgress: {
icon: 'fa-refresh',
key: 'sync.status.in_progress',
disableSyncButton: true
},
success: {
icon: 'fa-check',
key: 'sync.status.not_required',
className: 'success'
},
required: {
icon: 'fa-exclamation-triangle',
key: 'sync.status.required',
className: 'required'
},
unknown: {
icon: 'fa-info-circle',
key: 'sync.status.unknown'
}
};
const DOC_IDS_TRIGGER_UPDATE = new Set([
'_design/medic',
'_design/medic-client',
DOC_IDS.SERVICE_WORKER_META,
DOC_IDS.SETTINGS,
DOC_IDS.EXTENSION_LIBS
]);
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
imports: [
NgIf,
PrivacyPolicyComponent,
SidebarMenuComponent,
HeaderComponent,
RouterOutlet,
SnackbarComponent,
],
})
export class AppComponent implements OnInit, AfterViewInit {
private readonly globalActions: GlobalActions;
private readonly analyticsActions: AnalyticsActions;
setupPromise;
translationsLoaded;
currentTab = '';
privacyPolicyAccepted;
isSidebarFilterOpen = false;
openSearch = false;
showPrivacyPolicy;
selectMode;
adminUrl;
canLogOut;
replicationStatus;
androidAppVersion;
hasOldNav = false;
initialisationComplete = false;
direction;
private readonly SVG_ICONS = new Map([
['icon-close', './img/icon-close.svg'],
['icon-filter', './img/icon-filter.svg'],
['icon-back', './img/icon-back.svg'],
['icon-check', './img/icon-check.svg'],
]);
constructor (
private readonly dbSyncService:DBSyncService,
private readonly store:Store,
private readonly translateService:TranslateService,
private readonly languageService:LanguageService,
private readonly setLanguageService:SetLanguageService,
private readonly sessionService:SessionService,
private readonly authService:AuthService,
private readonly customResourceService:CustomResourceService,
private readonly changesService:ChangesService,
private readonly updateServiceWorker:UpdateServiceWorkerService,
private readonly locationService:LocationService,
private readonly modalService:ModalService,
private readonly router:Router,
private readonly domSanitizer: DomSanitizer,
private readonly feedbackService:FeedbackService,
private readonly formatDateService:FormatDateService,
private readonly xmlFormsService:XmlFormsService,
private readonly jsonFormsService:JsonFormsService,
private readonly translateFromService:TranslateFromService,
private readonly countMessageService:CountMessageService,
private readonly privacyPoliciesService:PrivacyPoliciesService,
private readonly routeSnapshotService:RouteSnapshotService,
private readonly checkDateService:CheckDateService,
private readonly unreadRecordsService:UnreadRecordsService,
private readonly rulesEngineService:RulesEngineService,
private readonly recurringProcessManagerService:RecurringProcessManagerService,
private readonly wealthQuintilesWatcherService: WealthQuintilesWatcherService,
private readonly databaseConnectionMonitorService: DatabaseConnectionMonitorService,
private readonly translateLocaleService:TranslateLocaleService,
private readonly telemetryService:TelemetryService,
private readonly performanceService:PerformanceService,
private readonly transitionsService:TransitionsService,
private readonly ngZone:NgZone,
private readonly chtDatasourceService: CHTDatasourceService,
private readonly analyticsModulesService: AnalyticsModulesService,
private readonly trainingCardsService: TrainingCardsService,
private readonly matIconRegistry: MatIconRegistry,
private readonly browserDetectorService: BrowserDetectorService,
private readonly userSettingsService: UserSettingsService,
private readonly formService: FormService,
private readonly taskNotificationService: TasksNotificationService,
private readonly interactionTrackingService: InteractionTrackingService,
) {
this.globalActions = new GlobalActions(store);
this.analyticsActions = new AnalyticsActions(store);
this.registerMaterialIcons();
moment.locale(['en']);
this.formatDateService.init();
this.adminUrl = this.locationService.adminPath;
setBootstrapTheme('bs4');
}
private loadTranslations() {
this.translationsLoaded = this.languageService
.get()
.then((language) => this.setLanguageService.set(language, false))
.then(() => this.globalActions.setTranslationsLoaded())
.catch(err => {
console.error('Error loading language', err);
});
}
private registerMaterialIcons() {
this.matIconRegistry.registerFontClassAlias('fontawesome', 'fa');
this.matIconRegistry.setDefaultFontSetClass('fa');
this.SVG_ICONS.forEach((iconPath, iconName) => {
// Disabling Sonar because we trust the SVG_ICONS defined as readonly above
const iconUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(iconPath); //NoSONAR
this.matIconRegistry.addSvgIcon(iconName, iconUrl);
});
}
private setupRouter() {
const getTab = (snapshot) => {
let tab;
do {
tab = snapshot.data.tab;
snapshot = snapshot.parent;
} while (!tab && snapshot?.parent);
return tab;
};
this.router.events.subscribe((event:ActivationStart|ActivationEnd) => {
// close all select2 menus on navigation
// https://github.com/medic/cht-core/issues/2927
if (event instanceof ActivationStart) {
this.closeDropdowns();
}
if (event instanceof ActivationEnd) {
const tab = getTab(event.snapshot);
if (tab !== this.currentTab) {
this.globalActions.setCurrentTab(tab);
}
const data = this.routeSnapshotService.get()?.data;
this.globalActions.setSnapshotData(data);
}
});
}
private setupDb() {
this.globalActions.updateReplicationStatus({
disabled: false,
lastTrigger: undefined,
lastSuccessTo: parseInt(window.localStorage.getItem('medic-last-replicated-date')!),
});
// Set this first because if there are any bugs in configuration
// we want to ensure dbsync still happens so they can be fixed
// automatically.
if (this.dbSyncService.isEnabled()) {
// Delay it by 10 seconds so it doesn't slow down initial load.
setTimeout(() => this.dbSyncService.sync(), 10 * 1000);
} else {
console.debug('You have administrative privileges; not replicating');
this.globalActions.updateReplicationStatus({ disabled: true });
}
const dbFetch = window.PouchDB.fetch;
window.PouchDB.fetch = (...args) => {
return dbFetch
.apply(dbFetch, args)
.then((response) => {
// ignore 401 that could come through other channels than CHT API
if (response.status === 401 && response.headers?.get(HTTP_HEADERS.LOGOUT_AUTHORIZATION) === 'CHT-Core API') {
this.showSessionExpired();
setTimeout(() => {
console.info('Redirect to login after 1 minute of inactivity');
this.sessionService.navigateToLogin();
}, 60000);
}
return response;
});
};
this.dbSyncService.subscribe(({ state, to, from }) => {
if (state === SyncStatus.Disabled) {
this.globalActions.updateReplicationStatus({ disabled: true });
return;
}
if (state === SyncStatus.Unknown) {
this.globalActions.updateReplicationStatus({ current: SYNC_STATUS.unknown });
return;
}
const now = Date.now();
const lastTrigger = this.replicationStatus.lastTrigger;
const delay = lastTrigger ? Math.round((now - lastTrigger) / 1000) : 'unknown';
if (state === SyncStatus.InProgress) {
this.globalActions.updateReplicationStatus({
current: SYNC_STATUS.inProgress,
lastTrigger: now
});
console.info(`Replication started after ${delay} seconds since previous attempt`);
return;
}
const statusUpdates:any = {};
if (to === SyncStatus.Success) {
statusUpdates.lastSuccessTo = now;
}
if (from === SyncStatus.Success) {
statusUpdates.lastSuccessFrom = now;
}
if (to === SyncStatus.Success && from === SyncStatus.Success) {
console.info(`Replication succeeded after ${delay} seconds`);
statusUpdates.current = SYNC_STATUS.success;
} else {
console.info(`Replication failed after ${delay} seconds`);
statusUpdates.current = SYNC_STATUS.required;
}
this.globalActions.updateReplicationStatus(statusUpdates);
});
}
ngOnInit(): void {
this.recordStartupTelemetry();
this.subscribeToStore();
this.setupRouter();
this.loadTranslations();
this.setupDb();
this.countMessageService.init();
this.feedbackService.init();
this.sessionService.init();
this.warnOutdatedChrome();
// initialisation tasks that can occur after the UI has been rendered
this.setupPromise = Promise.resolve()
.then(() => this.chtDatasourceService.isInitialized())
.then(() => this.checkPrivacyPolicy())
.then(() => (this.initialisationComplete = true))
.then(() => this.initUser())
.then(() => this.interactionTrackingService.init())
.then(() => this.initRulesEngine())
.then(() => this.initTransitions())
.then(() => this.initForms())
.then(() => this.initBubbleCounter())
.then(() => this.checkDateService.check(true))
.then(() => this.startRecurringProcesses())
.catch(err => {
this.initialisationComplete = true;
console.error('Error during initialisation', err);
this.router.navigate(['/error', '503' ]);
});
this.watchBrandingChanges();
this.watchDDocChanges();
this.watchUserContextChanges();
this.watchTranslationsChanges();
this.watchDBSyncStatus();
this.watchDatabaseConnection();
this.setAppTitle();
this.setupAndroidVersion();
this.requestPersistentStorage();
this.startWealthQuintiles();
this.initAnalyticsModules();
this.initAndroidTaskNotifications();
}
private initAndroidTaskNotifications() {
const android = globalThis?.medicmobile_android;
if (
typeof android?.updateTaskNotificationStoreWithSettings === 'function' ||
typeof android?.updateTaskNotificationStore === 'function'
) {
this.taskNotificationService.initOnAndroid();
}
}
private async initUser() {
const userSettings:UserSettings = await this.userSettingsService.get();
this.globalActions.setUserContactId(userSettings.contact_id);
this.globalActions.setUserFacilityIds(userSettings.facility_id);
this.globalActions.setUserFacilities(await this.userSettingsService.getUserFacilities());
this.globalActions.setIsOnlineOnly(this.authService.online(true));
}
ngAfterViewInit() {
this.enableOldNav();
this.subscribeToSideFilterStore();
}
private initTransitions() {
if (!this.sessionService.isOnlineOnly()) {
return this.transitionsService.init();
}
}
private setupAndroidVersion() {
if (typeof window.medicmobile_android?.getAppVersion === 'function') {
this.globalActions.setAndroidAppVersion(window.medicmobile_android.getAppVersion());
}
if (this.androidAppVersion) {
this.authService
.has('can_log_out_on_android')
.then(canLogout => this.canLogOut = canLogout);
} else {
this.canLogOut = true;
}
}
private requestPersistentStorage() {
if (navigator.storage && navigator.storage.persist) {
navigator.storage
.persist()
.then(granted => {
if (granted) {
console.info('Persistent storage granted: storage will not be cleared except by explicit user action');
} else {
console.info('Persistent storage denied: storage may be cleared by the UA under storage pressure.');
}
});
}
}
private watchBrandingChanges() {
this.changesService.subscribe({
key: 'branding-icon',
filter: change => change.id === DOC_IDS.BRANDING,
callback: () => this.setAppTitle(),
});
}
private watchDDocChanges() {
this.updateServiceWorker.update(() => this.ngZone.run(() => this.showUpdateReady()));
this.changesService.subscribe({
key: 'ddoc',
filter: ({ id }) => DOC_IDS_TRIGGER_UPDATE.has(id) || id.startsWith(PREFIXES.UI_EXTENSION),
callback: (change) => {
if (change.id === DOC_IDS.SERVICE_WORKER_META) {
this.updateServiceWorker.update(() => this.ngZone.run(() => this.showUpdateReady()));
} else {
console.debug(`${change.id} changed`);
this.showUpdateReady();
}
},
});
}
private watchUserContextChanges() {
const userCtx = this.sessionService.userCtx();
this.changesService.subscribe({
key: 'user-context',
filter: (change) => {
return (
userCtx &&
userCtx.name &&
change.id === `${PREFIXES.COUCH_USER}${userCtx.name}`
);
},
callback: () => {
this.sessionService.init().then(refresh => refresh && this.showUpdateReady());
},
});
}
private watchTranslationsChanges() {
this.changesService.subscribe({
key: DOC_TYPES.TRANSLATIONS,
filter: change => TranslationDocsMatcherProvider.test(change.id),
callback: change => {
const locale = TranslationDocsMatcherProvider.getLocaleCode(change.id);
return this.languageService
.get()
.then(enabledLocale => {
const hotReload = enabledLocale === locale;
return this.translateLocaleService.reloadLang(locale, hotReload);
});
},
});
}
private watchDBSyncStatus() {
window.addEventListener('online', () => this.dbSyncService.setOnlineStatus(true), false);
window.addEventListener('offline', () => this.dbSyncService.setOnlineStatus(false), false);
this.changesService.subscribe({
key: 'sync-status',
callback: () => {
if (!this.dbSyncService.isSyncInProgress()) {
this.globalActions.updateReplicationStatus({ current: SYNC_STATUS.required });
this.dbSyncService.sync(false, true);
}
},
});
}
private watchDatabaseConnection() {
this.databaseConnectionMonitorService
.listenForDatabaseClosed()
.subscribe(() => {
this.modalService.show(DatabaseClosedComponent);
this.closeDropdowns();
});
}
private subscribeToStore() {
combineLatest([
this.store.select(Selectors.getReplicationStatus),
this.store.select(Selectors.getAndroidAppVersion),
this.store.select(Selectors.getCurrentTab),
this.store.select(Selectors.getSelectMode),
this.store.select(Selectors.getSearchBar),
this.store.select(Selectors.getDirection),
]).subscribe(([
replicationStatus,
androidAppVersion,
currentTab,
selectMode,
searchBar,
direction,
]) => {
this.replicationStatus = replicationStatus;
this.androidAppVersion = androidAppVersion;
this.currentTab = currentTab || '';
this.selectMode = selectMode;
this.openSearch = !!searchBar?.isOpen;
this.direction = direction;
});
combineLatest([
this.store.select(Selectors.getPrivacyPolicyAccepted),
this.store.select(Selectors.getShowPrivacyPolicy),
]).subscribe(([ privacyPolicyAccepted, showPrivacyPolicy ]) => {
this.showPrivacyPolicy = showPrivacyPolicy;
this.privacyPolicyAccepted = privacyPolicyAccepted;
});
combineLatest([
this.store.select(Selectors.getUserContactId),
this.store.select(Selectors.getUserFacilityIds),
]).subscribe(([ userContactId, userFacilityIds ]) => {
this.formService.setUserContext(userFacilityIds, userContactId);
});
}
private async subscribeToSideFilterStore() {
this.store
.select(Selectors.getSidebarFilter)
.subscribe(({ isOpen }) => this.isSidebarFilterOpen = !!isOpen);
}
private async enableOldNav() {
this.hasOldNav = !this.sessionService.isAdmin() && await this.authService.has(OLD_NAV_PERMISSION);
}
private initForms() {
/**
* Translates using the key if truthy using the old style label
* array as a fallback.
*/
const translateTitle = (key, label) => {
return key ? this.translateService.instant(key) : this.translateFromService.get(label);
};
return this.translationsLoaded
.then(() => this.jsonFormsService.get())
.then((jsonForms) => {
const jsonFormSummaries = jsonForms.map((jsonForm) => {
return {
id: jsonForm.code,
code: jsonForm.code,
title: translateTitle(jsonForm.translation_key, jsonForm.name),
icon: jsonForm.icon,
subjectKey: jsonForm.subject_key
};
});
this.xmlFormsService.subscribe(
'FormsFilter',
{ reportForms: true, ignoreContext: true },
(err, xForms) => {
if (err) {
return console.error('Error fetching form definitions', err);
}
const xFormSummaries = xForms.map(function(xForm) {
return {
id: xForm._id,
code: xForm.internalId,
title: translateTitle(xForm.translation_key, xForm.title),
icon: xForm.icon,
subjectKey: xForm.subject_key
};
});
const forms = xFormSummaries.concat(jsonFormSummaries);
this.globalActions.setForms(forms);
}
);
// Get forms for training cards and display the cards if necessary
this.trainingCardsService.initTrainingCards();
})
.catch(err => console.error('Failed to retrieve forms', err));
}
private setAppTitle() {
this.customResourceService
.getAppTitle()
.then(title => {
document.title = title;
$('.header-logo').attr('title', `${title}`);
});
}
private showSessionExpired() {
this.modalService.show(SessionExpiredComponent);
}
private showUpdateReady() {
const TWO_HOURS = 2 * 60 * 60 * 1000;
this.modalService
.show(ReloadingComponent)
.afterClosed()
.pipe(take(1))
.subscribe(reloaded => {
if (reloaded) {
return;
}
console.debug('Delaying update');
setTimeout(() => this.showUpdateReady(), TWO_HOURS);
});
this.closeDropdowns();
}
private checkPrivacyPolicy() {
return this.privacyPoliciesService
.hasAccepted()
.then(({ privacyPolicy, accepted }: any = {}) => {
this.globalActions.setPrivacyPolicyAccepted(accepted);
this.globalActions.setShowPrivacyPolicy(privacyPolicy);
})
.catch(err => console.error('Failed to load privacy policy', err));
}
private initBubbleCounter() {
this.unreadRecordsService.init((err, data) => {
if (err) {
console.error('Error fetching read status', err);
return;
}
this.globalActions.setBubbleCounter(data);
});
}
private initRulesEngine() {
return this.rulesEngineService
.isEnabled()
.then(isEnabled => console.info(`RulesEngine Status: ${isEnabled ? 'Enabled' : 'Disabled'}`))
.catch(err => {
console.error('RuleEngine failed to initialize', err);
});
}
private startRecurringProcesses() {
this.recurringProcessManagerService.startUpdateRelativeDate();
if (this.sessionService.isOnlineOnly()) {
this.recurringProcessManagerService.startUpdateReadDocsCount();
}
}
private startWealthQuintiles() {
this.authService
.has('can_write_wealth_quintiles')
.then(canWriteQuintiles => {
if (canWriteQuintiles) {
this.wealthQuintilesWatcherService.start();
}
});
}
// close select2 dropdowns in the background
private closeDropdowns() {
$('select.select2-hidden-accessible').each((idx, element) => {
// prevent errors being thrown if selectors have not been
// initialised yet
try {
$(element).select2('close');
} catch (_) {
// exception thrown on clicking 'close'
}
});
}
private async warnOutdatedChrome(): Promise<void> {
if (!this.browserDetectorService.isUsingOutdatedBrowser()) {
return;
}
await this.translationsLoaded;
this.modalService.show(BrowserCompatibilityComponent);
}
private recordStartupTelemetry() {
window.startupTimes.angularBootstrapped = performance.now();
this.performanceService.recordPerformance(
{ name: 'boot_time:1:to_first_code_execution' },
window.startupTimes.firstCodeExecution - window.startupTimes.start
);
if (window.startupTimes.replication) {
this.performanceService.recordPerformance(
{ name: 'boot_time:2_1:to_replication' },
window.startupTimes.replication,
);
}
if (window.startupTimes.purgingMetaFailed) {
console.error(`Error when purging meta on device startup: ${window.startupTimes.purgingMetaFailed}`);
this.telemetryService.record('boot_time:purging_meta_failed');
} else {
// When: 1- Purging ran and successfully completed. 2- Purging didn't run.
this.telemetryService.record(`boot_time:purging_meta:${!!window.startupTimes.purgingMeta}`);
}
if (window.startupTimes.purgeMeta) {
this.performanceService.recordPerformance(
{ name: 'boot_time:2_3:to_purge_meta' },
window.startupTimes.purgeMeta,
);
}
this.performanceService.recordPerformance(
{ name: 'boot_time:2:to_bootstrap' },
window.startupTimes.bootstrapped - window.startupTimes.firstCodeExecution,
);
this.performanceService.recordPerformance(
{ name: 'boot_time:3:to_angular_bootstrap' },
window.startupTimes.angularBootstrapped - window.startupTimes.bootstrapped,
);
this.performanceService.recordPerformance(
{ name: 'boot_time', recordApdex: true },
window.startupTimes.angularBootstrapped - window.startupTimes.start
);
}
@HostListener('window:beforeunload')
private stopWatchingChanges() {
// avoid Failed to fetch errors being logged when the browser window is reloaded
this.changesService.killWatchers();
}
@HostListener('window:visibilitychange')
private onVisibilityChange() {
this.interactionTrackingService.persistBuffer();
}
@HostListener('window:pageshow', ['$event'])
private pageshow(event) {
if (event.persisted) {
this.sessionService.check();
}
}
private async initAnalyticsModules() {
try {
const modules = await this.analyticsModulesService.get();
this.analyticsActions.setAnalyticsModules(modules);
} catch (error) {
console.error('Error while initializing analytics modules', error);
}
}
}