bitwarden-estensione-browser/src/app/settings/options.component.ts

106 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-06-25 22:44:06 +02:00
import {
Component,
OnInit,
} from '@angular/core';
2018-06-26 15:04:12 +02:00
import { ToasterService } from 'angular2-toaster';
2018-06-25 22:44:06 +02:00
import { Angulartics2 } from 'angulartics2';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-28 04:52:27 +02:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-06-25 22:44:06 +02:00
import { StateService } from 'jslib/abstractions/state.service';
import { StorageService } from 'jslib/abstractions/storage.service';
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
2018-06-25 22:44:06 +02:00
import { ConstantsService } from 'jslib/services/constants.service';
2018-07-24 04:45:43 +02:00
import { Utils } from 'jslib/misc/utils';
2018-06-25 22:44:06 +02:00
@Component({
selector: 'app-options',
templateUrl: 'options.component.html',
})
export class OptionsComponent implements OnInit {
vaultTimeout: number = null;
vaultTimeoutAction: string = 'lock';
2018-06-25 22:44:06 +02:00
disableIcons: boolean;
2018-07-31 06:02:09 +02:00
enableGravatars: boolean;
enableFullWidth: boolean;
2018-06-25 22:44:06 +02:00
locale: string;
vaultTimeouts: any[];
2018-06-25 22:44:06 +02:00
localeOptions: any[];
2018-06-26 15:04:12 +02:00
private startingLocale: string;
2018-06-25 22:44:06 +02:00
constructor(private storageService: StorageService, private stateService: StateService,
2018-06-26 15:04:12 +02:00
private analytics: Angulartics2, private i18nService: I18nService,
private toasterService: ToasterService, private vaultTimeoutService: VaultTimeoutService,
2018-07-28 04:52:27 +02:00
private platformUtilsService: PlatformUtilsService) {
this.vaultTimeouts = [
{ name: i18nService.t('oneMinute'), value: 1 },
{ name: i18nService.t('fiveMinutes'), value: 5 },
{ name: i18nService.t('fifteenMinutes'), value: 15 },
{ name: i18nService.t('thirtyMinutes'), value: 30 },
{ name: i18nService.t('oneHour'), value: 60 },
{ name: i18nService.t('fourHours'), value: 240 },
{ name: i18nService.t('onRefresh'), value: -1 },
];
2018-07-28 04:52:27 +02:00
if (this.platformUtilsService.isDev()) {
this.vaultTimeouts.push({ name: i18nService.t('never'), value: null });
2018-07-28 04:52:27 +02:00
}
2018-07-24 04:45:43 +02:00
const localeOptions: any[] = [];
2018-06-25 22:44:06 +02:00
i18nService.supportedTranslationLocales.forEach((locale) => {
let name = locale;
if (i18nService.localeNames.has(locale)) {
name += (' - ' + i18nService.localeNames.get(locale));
}
localeOptions.push({ name: name, value: locale });
2018-06-25 22:44:06 +02:00
});
2018-07-24 04:45:43 +02:00
localeOptions.sort(Utils.getSortFunction(i18nService, 'name'));
localeOptions.splice(0, 0, { name: i18nService.t('default'), value: null });
this.localeOptions = localeOptions;
2018-06-25 22:44:06 +02:00
}
async ngOnInit() {
this.vaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
this.vaultTimeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
2018-06-25 22:44:06 +02:00
this.disableIcons = await this.storageService.get<boolean>(ConstantsService.disableFaviconKey);
2018-07-31 06:02:09 +02:00
this.enableGravatars = await this.storageService.get<boolean>('enableGravatars');
this.enableFullWidth = await this.storageService.get<boolean>('enableFullWidth');
2018-06-26 15:04:12 +02:00
this.locale = this.startingLocale = await this.storageService.get<string>(ConstantsService.localeKey);
2018-06-25 22:44:06 +02:00
}
2018-06-26 15:04:12 +02:00
async submit() {
await this.vaultTimeoutService.setVaultTimeoutOptions(this.vaultTimeout != null ? this.vaultTimeout : null,
this.vaultTimeoutAction);
2018-06-25 22:44:06 +02:00
await this.storageService.save(ConstantsService.disableFaviconKey, this.disableIcons);
await this.stateService.save(ConstantsService.disableFaviconKey, this.disableIcons);
2018-07-31 06:02:09 +02:00
await this.storageService.save('enableGravatars', this.enableGravatars);
await this.stateService.save('enableGravatars', this.enableGravatars);
await this.storageService.save('enableFullWidth', this.enableFullWidth);
await this.stateService.save('enableFullWidth', this.enableFullWidth);
2018-06-25 22:44:06 +02:00
await this.storageService.save(ConstantsService.localeKey, this.locale);
2018-06-26 15:04:12 +02:00
this.analytics.eventTrack.next({ action: 'Saved Options' });
if (this.locale !== this.startingLocale) {
window.location.reload();
} else {
this.toasterService.popAsync('success', null, this.i18nService.t('optionsUpdated'));
}
2018-06-25 22:44:06 +02:00
}
async vaultTimeoutActionChanged(newValue: string) {
if (newValue === 'logOut') {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('vaultTimeoutLogOutConfirmation'),
this.i18nService.t('vaultTimeoutLogOutConfirmationTitle'),
this.i18nService.t('yes'), this.i18nService.t('cancel'), 'warning');
if (!confirmed) {
this.vaultTimeoutAction = 'lock';
return;
}
}
this.vaultTimeoutAction = newValue;
}
2018-06-25 22:44:06 +02:00
}