1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

platform utils for toasting

This commit is contained in:
Kyle Spearrin
2018-10-02 23:33:56 -04:00
parent adba3f8d67
commit 89a448b12f
21 changed files with 91 additions and 86 deletions

View File

@@ -1,8 +1,11 @@
import { BrowserApi } from '../browser/browserApi';
import {
BodyOutputType,
Toast,
ToasterConfig,
ToasterContainerComponent,
ToasterService,
} from 'angular2-toaster';
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import swal from 'sweetalert';
@@ -12,14 +15,15 @@ import {
Component,
NgZone,
OnInit,
SecurityContext,
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import {
NavigationEnd,
Router,
RouterOutlet,
} from '@angular/router';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
@@ -62,7 +66,8 @@ export class AppComponent implements OnInit {
private broadcasterService: BroadcasterService, private authService: AuthService,
private i18nService: I18nService, private router: Router,
private stateService: StateService, private messagingService: MessagingService,
private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone) { }
private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone,
private sanitizer: DomSanitizer) { }
ngOnInit() {
if (BrowserApi.getBackgroundPage() == null) {
@@ -84,8 +89,11 @@ export class AppComponent implements OnInit {
this.authService.logOut(() => {
this.analytics.eventTrack.next({ action: 'Logged Out' });
if (msg.expired) {
this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'),
this.i18nService.t('loginExpired'));
this.showToast({
type: 'warning',
title: this.i18nService.t('loggedOut'),
text: this.i18nService.t('loginExpired'),
});
}
this.router.navigate(['home']);
this.stateService.purge();
@@ -96,6 +104,11 @@ export class AppComponent implements OnInit {
this.stateService.purge();
} else if (msg.command === 'showDialog') {
await this.showDialog(msg);
} else if (msg.command === 'showToast') {
this.ngZone.run(async () => {
await this.showToast(msg);
this.changeDetectorRef.detectChanges();
});
} else {
msg.webExtSender = sender;
this.broadcasterService.send(msg);
@@ -133,6 +146,33 @@ export class AppComponent implements OnInit {
this.storageService.save(ConstantsService.lastActiveKey, now);
}
private async showToast(msg: any) {
const toast: Toast = {
type: msg.type,
title: msg.title,
};
if (typeof (msg.text) === 'string') {
toast.body = msg.text;
} else if (msg.text.length === 1) {
toast.body = msg.text[0];
} else {
let message = '';
msg.text.forEach((t: string) =>
message += ('<p>' + this.sanitizer.sanitize(SecurityContext.HTML, t) + '</p>'));
toast.body = message;
toast.bodyOutputType = BodyOutputType.TrustedHtml;
}
if (msg.options != null) {
if (msg.options.trustedHtml === true) {
toast.bodyOutputType = BodyOutputType.TrustedHtml;
}
if (msg.options.timeout != null && msg.options.timeout > 0) {
toast.timeout = msg.options.timeout;
}
}
this.toasterService.popAsync(toast);
}
private async showDialog(msg: any) {
const buttons = [msg.confirmText == null ? this.i18nService.t('ok') : msg.confirmText];
if (msg.cancelText != null) {