bitwarden-estensione-browser/src/popup2/app.component.ts

117 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-04-04 04:14:54 +02:00
import {
ToasterConfig,
ToasterContainerComponent,
} from 'angular2-toaster';
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import {
Component,
OnInit,
} from '@angular/core';
2018-04-09 15:51:22 +02:00
import {
2018-04-09 20:17:58 +02:00
NavigationEnd,
2018-04-09 15:51:22 +02:00
Router,
RouterOutlet,
} from '@angular/router';
2018-04-04 04:14:54 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
2018-04-06 21:33:20 +02:00
import { BrowserApi } from '../browser/browserApi';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { AuthService } from 'jslib/abstractions/auth.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-04-09 20:17:58 +02:00
import { StateService } from 'jslib/abstractions/state.service';
2018-04-06 21:33:20 +02:00
import { StorageService } from 'jslib/abstractions/storage.service';
import { ConstantsService } from 'jslib/services/constants.service';
2018-04-08 06:30:04 +02:00
import { routerTransition } from './app-routing.animations';
2018-04-04 04:14:54 +02:00
@Component({
selector: 'app-root',
styles: [],
2018-04-08 06:30:04 +02:00
animations: [routerTransition],
2018-04-04 04:14:54 +02:00
template: `
<toaster-container [toasterconfig]="toasterConfig"></toaster-container>
2018-04-08 06:30:04 +02:00
<main [@routerTransition]="getState(o)">
<router-outlet #o="outlet"></router-outlet>
</main>`,
2018-04-04 04:14:54 +02:00
})
2018-04-06 21:33:20 +02:00
export class AppComponent implements OnInit {
2018-04-04 04:14:54 +02:00
toasterConfig: ToasterConfig = new ToasterConfig({
2018-04-10 04:53:46 +02:00
showCloseButton: false,
2018-04-04 04:14:54 +02:00
mouseoverTimerStop: true,
animation: 'slideUp',
limit: 2,
positionClass: 'toast-bottom-full-width',
newestOnTop: false
2018-04-04 04:14:54 +02:00
});
2018-04-06 21:33:20 +02:00
private lastActivity: number = null;
2018-04-09 20:17:58 +02:00
private previousUrl: string = '';
2018-04-06 21:33:20 +02:00
2018-04-04 04:14:54 +02:00
constructor(private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics, private analytics: Angulartics2,
2018-04-06 21:33:20 +02:00
private toasterService: ToasterService, private storageService: StorageService,
private broadcasterService: BroadcasterService, private authService: AuthService,
2018-04-09 20:17:58 +02:00
private i18nService: I18nService, private router: Router,
private stateService: StateService) { }
2018-04-06 21:33:20 +02:00
ngOnInit() {
window.onmousemove = () => this.recordActivity();
window.onmousedown = () => this.recordActivity();
window.ontouchstart = () => this.recordActivity();
window.onclick = () => this.recordActivity();
window.onscroll = () => this.recordActivity();
window.onkeypress = () => this.recordActivity();
(window as any).bitwardenPopupMainMessageListener = (msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'doneLoggingOut') {
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.router.navigate(['home']);
});
} else {
msg.webExtSender = sender;
this.broadcasterService.send(msg);
}
};
BrowserApi.messageListener((window as any).bitwardenPopupMainMessageListener);
2018-04-09 20:17:58 +02:00
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
const url = event.urlAfterRedirects || event.url || '';
if (url.startsWith('/tabs/') && this.previousUrl.startsWith('/tabs/')) {
this.stateService.remove('GroupingsComponent');
this.stateService.remove('CiphersComponent');
}
2018-04-09 23:35:16 +02:00
if (url.startsWith('/tabs/')) {
this.stateService.remove('addEditCipher');
}
2018-04-09 20:17:58 +02:00
this.previousUrl = url;
}
});
2018-04-06 21:33:20 +02:00
}
2018-04-09 15:51:22 +02:00
getState(outlet: RouterOutlet) {
2018-04-08 06:30:04 +02:00
return outlet.activatedRouteData.state;
}
2018-04-06 21:33:20 +02:00
private async recordActivity() {
const now = (new Date()).getTime();
if (this.lastActivity != null && now - this.lastActivity < 250) {
return;
}
this.lastActivity = now;
this.storageService.save(ConstantsService.lastActiveKey, now);
}
2018-04-04 04:14:54 +02:00
}