1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/src/app/settings/options.component.ts

131 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-12-17 15:57:11 +01:00
import { Component, OnInit } from "@angular/core";
import { FormControl } from "@angular/forms";
2018-06-25 22:44:06 +02:00
2021-12-17 15:57:11 +01:00
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { StateService } from "jslib-common/abstractions/state.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
2018-06-25 22:44:06 +02:00
2021-12-17 15:57:11 +01:00
import { ThemeType } from "jslib-common/enums/themeType";
import { Utils } from "jslib-common/misc/utils";
2018-07-24 04:45:43 +02:00
2018-06-25 22:44:06 +02:00
@Component({
2021-12-17 15:57:11 +01:00
selector: "app-options",
templateUrl: "options.component.html",
2018-06-25 22:44:06 +02:00
})
export class OptionsComponent implements OnInit {
2021-12-17 15:57:11 +01:00
vaultTimeoutAction: string = "lock";
disableIcons: boolean;
enableGravatars: boolean;
enableFullWidth: boolean;
theme: ThemeType;
2021-12-17 15:57:11 +01:00
locale: string;
vaultTimeouts: { name: string; value: number }[];
localeOptions: any[];
themeOptions: any[];
2018-06-25 22:44:06 +02:00
2021-12-17 15:57:11 +01:00
vaultTimeout: FormControl = new FormControl(null);
2021-09-10 15:27:00 +02:00
2021-12-17 15:57:11 +01:00
private startingLocale: string;
private startingTheme: ThemeType;
2018-06-26 15:04:12 +02:00
2021-12-17 15:57:11 +01:00
constructor(
private stateService: StateService,
private i18nService: I18nService,
private vaultTimeoutService: VaultTimeoutService,
private platformUtilsService: PlatformUtilsService,
private messagingService: MessagingService
) {
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 },
];
if (this.platformUtilsService.isDev()) {
this.vaultTimeouts.push({ name: i18nService.t("never"), value: null });
2018-06-25 22:44:06 +02:00
}
2021-12-17 15:57:11 +01:00
const localeOptions: any[] = [];
i18nService.supportedTranslationLocales.forEach((locale) => {
let name = locale;
if (i18nService.localeNames.has(locale)) {
name += " - " + i18nService.localeNames.get(locale);
}
localeOptions.push({ name: name, value: locale });
});
localeOptions.sort(Utils.getSortFunction(i18nService, "name"));
localeOptions.splice(0, 0, { name: i18nService.t("default"), value: null });
this.localeOptions = localeOptions;
this.themeOptions = [
{ name: i18nService.t("themeLight"), value: ThemeType.Light },
{ name: i18nService.t("themeDark"), value: ThemeType.Dark },
{ name: i18nService.t("themeSystem"), value: ThemeType.System },
];
}
2018-06-25 22:44:06 +02:00
2021-12-17 15:57:11 +01:00
async ngOnInit() {
this.vaultTimeout.setValue(await this.vaultTimeoutService.getVaultTimeout());
this.vaultTimeoutAction = await this.stateService.getVaultTimeoutAction();
this.disableIcons = await this.stateService.getDisableFavicon();
this.enableGravatars = await this.stateService.getEnableGravitars();
this.enableFullWidth = await this.stateService.getEnableFullWidth();
this.locale = (await this.stateService.getLocale()) ?? this.startingLocale;
this.theme = (await this.stateService.getTheme()) ?? this.startingTheme;
}
2021-09-10 15:27:00 +02:00
2021-12-17 15:57:11 +01:00
async submit() {
if (!this.vaultTimeout.valid) {
this.platformUtilsService.showToast("error", null, this.i18nService.t("vaultTimeoutToLarge"));
return;
}
await this.vaultTimeoutService.setVaultTimeoutOptions(
this.vaultTimeout.value,
this.vaultTimeoutAction
);
await this.stateService.setDisableFavicon(this.disableIcons);
await this.stateService.setEnableGravitars(this.enableGravatars);
await this.stateService.setEnableFullWidth(this.enableFullWidth);
this.messagingService.send("setFullWidth");
if (this.theme !== this.startingTheme) {
await this.stateService.setTheme(this.theme);
this.startingTheme = this.theme;
const effectiveTheme = await this.platformUtilsService.getEffectiveTheme();
const htmlEl = window.document.documentElement;
htmlEl.classList.remove("theme_" + ThemeType.Light, "theme_" + ThemeType.Dark);
htmlEl.classList.add("theme_" + effectiveTheme);
}
await this.stateService.setLocale(this.locale);
if (this.locale !== this.startingLocale) {
window.location.reload();
} else {
this.platformUtilsService.showToast("success", null, [
this.i18nService.t("optionsUpdated"),
this.i18nService.t("optionsUpdated"),
]);
2018-06-25 22:44:06 +02:00
}
2021-12-17 15:57:11 +01:00
}
2021-12-17 15:57:11 +01: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;
}
}
2021-12-17 15:57:11 +01:00
this.vaultTimeoutAction = newValue;
}
2018-06-25 22:44:06 +02:00
}