1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-26 21:23:34 +00:00

Apply Prettier (#1347)

This commit is contained in:
Oscar Hinton
2021-12-17 15:57:11 +01:00
committed by GitHub
parent 2b0a9d995e
commit 56477eb39c
414 changed files with 33390 additions and 26857 deletions

View File

@@ -1,14 +1,14 @@
import { Injectable } from '@angular/core';
import { Injectable } from "@angular/core";
import { BroadcasterService } from 'jslib-common/abstractions/broadcaster.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { BroadcasterService } from "jslib-common/abstractions/broadcaster.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
@Injectable()
export class BroadcasterMessagingService implements MessagingService {
constructor(private broadcasterService: BroadcasterService) { }
constructor(private broadcasterService: BroadcasterService) {}
send(subscriber: string, arg: any = {}) {
const message = Object.assign({}, { command: subscriber }, arg);
this.broadcasterService.send(message);
}
send(subscriber: string, arg: any = {}) {
const message = Object.assign({}, { command: subscriber }, arg);
this.broadcasterService.send(message);
}
}

View File

@@ -1,83 +1,84 @@
import { Injectable } from '@angular/core';
import { Injectable } from "@angular/core";
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { StorageService } from "jslib-common/abstractions/storage.service";
import { HtmlStorageLocation } from 'jslib-common/enums/htmlStorageLocation';
import { HtmlStorageLocation } from "jslib-common/enums/htmlStorageLocation";
import { GlobalState } from 'jslib-common/models/domain/globalState';
import { State } from 'jslib-common/models/domain/state';
import { StorageOptions } from 'jslib-common/models/domain/storageOptions';
import { GlobalState } from "jslib-common/models/domain/globalState";
import { State } from "jslib-common/models/domain/state";
import { StorageOptions } from "jslib-common/models/domain/storageOptions";
@Injectable()
export class HtmlStorageService implements StorageService {
get defaultOptions(): StorageOptions {
return { htmlStorageLocation: HtmlStorageLocation.Session };
}
get defaultOptions(): StorageOptions {
return { htmlStorageLocation: HtmlStorageLocation.Session };
async init() {
const state =
(await this.get<State>("state", { htmlStorageLocation: HtmlStorageLocation.Local })) ??
new State();
state.globals = state.globals ?? new GlobalState();
state.globals.vaultTimeout = state.globals.vaultTimeout ?? 15;
state.globals.vaultTimeoutAction = state.globals.vaultTimeoutAction ?? "lock";
await this.save("state", state, { htmlStorageLocation: HtmlStorageLocation.Local });
}
get<T>(key: string, options: StorageOptions = this.defaultOptions): Promise<T> {
let json: string = null;
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
json = window.localStorage.getItem(key);
break;
case HtmlStorageLocation.Session:
default:
json = window.sessionStorage.getItem(key);
break;
}
async init() {
const state = await this.get<State>('state', { htmlStorageLocation: HtmlStorageLocation.Local }) ?? new State();
state.globals = state.globals ?? new GlobalState();
state.globals.vaultTimeout = state.globals.vaultTimeout ?? 15;
state.globals.vaultTimeoutAction = state.globals.vaultTimeoutAction ?? 'lock';
await this.save('state', state, { htmlStorageLocation: HtmlStorageLocation.Local });
if (json != null) {
const obj = JSON.parse(json);
return Promise.resolve(obj as T);
}
return Promise.resolve(null);
}
async has(key: string, options: StorageOptions = this.defaultOptions): Promise<boolean> {
return (await this.get(key, options)) != null;
}
save(key: string, obj: any, options: StorageOptions = this.defaultOptions): Promise<any> {
if (obj == null) {
return this.remove(key, options);
}
get<T>(key: string, options: StorageOptions = this.defaultOptions): Promise<T> {
let json: string = null;
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
json = window.localStorage.getItem(key);
break;
case HtmlStorageLocation.Session:
default:
json = window.sessionStorage.getItem(key);
break;
}
if (json != null) {
const obj = JSON.parse(json);
return Promise.resolve(obj as T);
}
return Promise.resolve(null);
if (obj instanceof Set) {
obj = Array.from(obj);
}
async has(key: string, options: StorageOptions = this.defaultOptions): Promise<boolean> {
return await this.get(key, options) != null;
const json = JSON.stringify(obj);
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
window.localStorage.setItem(key, json);
break;
case HtmlStorageLocation.Session:
default:
window.sessionStorage.setItem(key, json);
break;
}
return Promise.resolve();
}
save(key: string, obj: any, options: StorageOptions = this.defaultOptions): Promise<any> {
if (obj == null) {
return this.remove(key, options);
}
if (obj instanceof Set) {
obj = Array.from(obj);
}
const json = JSON.stringify(obj);
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
window.localStorage.setItem(key, json);
break;
case HtmlStorageLocation.Session:
default:
window.sessionStorage.setItem(key, json);
break;
}
return Promise.resolve();
}
remove(key: string, options: StorageOptions = this.defaultOptions): Promise<any> {
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
window.localStorage.removeItem(key);
break;
case HtmlStorageLocation.Session:
default:
window.sessionStorage.removeItem(key);
break;
}
return Promise.resolve();
remove(key: string, options: StorageOptions = this.defaultOptions): Promise<any> {
switch (options.htmlStorageLocation) {
case HtmlStorageLocation.Local:
window.localStorage.removeItem(key);
break;
case HtmlStorageLocation.Session:
default:
window.sessionStorage.removeItem(key);
break;
}
return Promise.resolve();
}
}

View File

@@ -1,19 +1,60 @@
import { I18nService as BaseI18nService } from 'jslib-common/services/i18n.service';
import { I18nService as BaseI18nService } from "jslib-common/services/i18n.service";
export class I18nService extends BaseI18nService {
constructor(systemLanguage: string, localesDirectory: string) {
super(systemLanguage || 'en-US', localesDirectory, async (formattedLocale: string) => {
const filePath = this.localesDirectory + '/' + formattedLocale + '/messages.json?cache=' +
process.env.CACHE_TAG;
const localesResult = await fetch(filePath);
const locales = await localesResult.json();
return locales;
});
constructor(systemLanguage: string, localesDirectory: string) {
super(systemLanguage || "en-US", localesDirectory, async (formattedLocale: string) => {
const filePath =
this.localesDirectory +
"/" +
formattedLocale +
"/messages.json?cache=" +
process.env.CACHE_TAG;
const localesResult = await fetch(filePath);
const locales = await localesResult.json();
return locales;
});
// Please leave 'en' where it is, as it's our fallback language in case no translation can be found
this.supportedTranslationLocales = [
'en', 'az', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'eo', 'en-GB', 'en-IN', 'es', 'et', 'fi', 'fr', 'he', 'hr', 'hu', 'id', 'it', 'ja', 'kn', 'ko', 'lv', 'ml', 'nb',
'nl', 'pl', 'pt-PT', 'pt-BR', 'ro', 'ru', 'sk', 'sr', 'sv', 'tr', 'uk', 'zh-CN', 'zh-TW',
];
}
// Please leave 'en' where it is, as it's our fallback language in case no translation can be found
this.supportedTranslationLocales = [
"en",
"az",
"bg",
"ca",
"cs",
"da",
"de",
"el",
"eo",
"en-GB",
"en-IN",
"es",
"et",
"fi",
"fr",
"he",
"hr",
"hu",
"id",
"it",
"ja",
"kn",
"ko",
"lv",
"ml",
"nb",
"nl",
"pl",
"pt-PT",
"pt-BR",
"ro",
"ru",
"sk",
"sr",
"sv",
"tr",
"uk",
"zh-CN",
"zh-TW",
];
}
}

View File

@@ -1,30 +1,30 @@
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { StorageService } from "jslib-common/abstractions/storage.service";
export class MemoryStorageService implements StorageService {
private store = new Map<string, any>();
private store = new Map<string, any>();
get<T>(key: string): Promise<T> {
if (this.store.has(key)) {
const obj = this.store.get(key);
return Promise.resolve(obj as T);
}
return Promise.resolve(null);
get<T>(key: string): Promise<T> {
if (this.store.has(key)) {
const obj = this.store.get(key);
return Promise.resolve(obj as T);
}
return Promise.resolve(null);
}
async has(key: string): Promise<boolean> {
return this.get(key) != null;
}
async has(key: string): Promise<boolean> {
return this.get(key) != null;
}
save(key: string, obj: any): Promise<any> {
if (obj == null) {
return this.remove(key);
}
this.store.set(key, obj);
return Promise.resolve();
save(key: string, obj: any): Promise<any> {
if (obj == null) {
return this.remove(key);
}
this.store.set(key, obj);
return Promise.resolve();
}
remove(key: string): Promise<any> {
this.store.delete(key);
return Promise.resolve();
}
remove(key: string): Promise<any> {
this.store.delete(key);
return Promise.resolve();
}
}

View File

@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { Injectable } from "@angular/core";
import { PasswordRepromptService as BasePasswordRepromptService } from 'jslib-angular/services/passwordReprompt.service';
import { PasswordRepromptComponent } from '../app/components/password-reprompt.component';
import { PasswordRepromptService as BasePasswordRepromptService } from "jslib-angular/services/passwordReprompt.service";
import { PasswordRepromptComponent } from "../app/components/password-reprompt.component";
@Injectable()
export class PasswordRepromptService extends BasePasswordRepromptService {
component = PasswordRepromptComponent;
component = PasswordRepromptComponent;
}

View File

@@ -1,316 +1,337 @@
import Swal, { SweetAlertIcon } from 'sweetalert2';
import Swal, { SweetAlertIcon } from "sweetalert2";
import { DeviceType } from 'jslib-common/enums/deviceType';
import { ThemeType } from 'jslib-common/enums/themeType';
import { DeviceType } from "jslib-common/enums/deviceType";
import { ThemeType } from "jslib-common/enums/themeType";
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.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 { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService } from "jslib-common/abstractions/log.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";
export class WebPlatformUtilsService implements PlatformUtilsService {
identityClientId: string = 'web';
identityClientId: string = "web";
private browserCache: DeviceType = null;
private prefersColorSchemeDark = window.matchMedia('(prefers-color-scheme: dark)');
private browserCache: DeviceType = null;
private prefersColorSchemeDark = window.matchMedia("(prefers-color-scheme: dark)");
constructor(private i18nService: I18nService, private messagingService: MessagingService,
private logService: LogService, private stateService: StateService) { }
constructor(
private i18nService: I18nService,
private messagingService: MessagingService,
private logService: LogService,
private stateService: StateService
) {}
getDevice(): DeviceType {
if (this.browserCache != null) {
return this.browserCache;
getDevice(): DeviceType {
if (this.browserCache != null) {
return this.browserCache;
}
if (
navigator.userAgent.indexOf(" Firefox/") !== -1 ||
navigator.userAgent.indexOf(" Gecko/") !== -1
) {
this.browserCache = DeviceType.FirefoxBrowser;
} else if (navigator.userAgent.indexOf(" OPR/") >= 0) {
this.browserCache = DeviceType.OperaBrowser;
} else if (navigator.userAgent.indexOf(" Edg/") !== -1) {
this.browserCache = DeviceType.EdgeBrowser;
} else if (navigator.userAgent.indexOf(" Vivaldi/") !== -1) {
this.browserCache = DeviceType.VivaldiBrowser;
} else if (
navigator.userAgent.indexOf(" Safari/") !== -1 &&
navigator.userAgent.indexOf("Chrome") === -1
) {
this.browserCache = DeviceType.SafariBrowser;
} else if ((window as any).chrome && navigator.userAgent.indexOf(" Chrome/") !== -1) {
this.browserCache = DeviceType.ChromeBrowser;
} else if (navigator.userAgent.indexOf(" Trident/") !== -1) {
this.browserCache = DeviceType.IEBrowser;
} else {
this.browserCache = DeviceType.UnknownBrowser;
}
return this.browserCache;
}
getDeviceString(): string {
const device = DeviceType[this.getDevice()].toLowerCase();
return device.replace("browser", "");
}
isFirefox(): boolean {
return this.getDevice() === DeviceType.FirefoxBrowser;
}
isChrome(): boolean {
return this.getDevice() === DeviceType.ChromeBrowser;
}
isEdge(): boolean {
return this.getDevice() === DeviceType.EdgeBrowser;
}
isOpera(): boolean {
return this.getDevice() === DeviceType.OperaBrowser;
}
isVivaldi(): boolean {
return this.getDevice() === DeviceType.VivaldiBrowser;
}
isSafari(): boolean {
return this.getDevice() === DeviceType.SafariBrowser;
}
isIE(): boolean {
return this.getDevice() === DeviceType.IEBrowser;
}
isMacAppStore(): boolean {
return false;
}
isViewOpen(): Promise<boolean> {
return Promise.resolve(false);
}
launchUri(uri: string, options?: any): void {
const a = document.createElement("a");
a.href = uri;
if (options == null || !options.sameWindow) {
a.target = "_blank";
a.rel = "noreferrer noopener";
}
a.classList.add("d-none");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
let blob: Blob = null;
let type: string = null;
const fileNameLower = fileName.toLowerCase();
let doDownload = true;
if (fileNameLower.endsWith(".pdf")) {
type = "application/pdf";
doDownload = false;
} else if (fileNameLower.endsWith(".xlsx")) {
type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
} else if (fileNameLower.endsWith(".docx")) {
type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
} else if (fileNameLower.endsWith(".pptx")) {
type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
} else if (fileNameLower.endsWith(".csv")) {
type = "text/csv";
} else if (fileNameLower.endsWith(".png")) {
type = "image/png";
} else if (fileNameLower.endsWith(".jpg") || fileNameLower.endsWith(".jpeg")) {
type = "image/jpeg";
} else if (fileNameLower.endsWith(".gif")) {
type = "image/gif";
}
if (type != null) {
blobOptions = blobOptions || {};
if (blobOptions.type == null) {
blobOptions.type = type;
}
}
if (blobOptions != null && !this.isIE()) {
blob = new Blob([blobData], blobOptions);
} else {
blob = new Blob([blobData]);
}
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement("a");
if (doDownload) {
a.download = fileName;
} else if (!this.isSafari()) {
a.target = "_blank";
}
a.href = URL.createObjectURL(blob);
a.style.position = "fixed";
win.document.body.appendChild(a);
a.click();
win.document.body.removeChild(a);
}
}
getApplicationVersion(): Promise<string> {
return Promise.resolve(process.env.APPLICATION_VERSION || "-");
}
supportsWebAuthn(win: Window): boolean {
return typeof PublicKeyCredential !== "undefined";
}
supportsDuo(): boolean {
return true;
}
showToast(
type: "error" | "success" | "warning" | "info",
title: string,
text: string | string[],
options?: any
): void {
this.messagingService.send("showToast", {
text: text,
title: title,
type: type,
options: options,
});
}
async showDialog(
body: string,
title?: string,
confirmText?: string,
cancelText?: string,
type?: string,
bodyIsHtml: boolean = false
) {
let iconClasses: string = null;
if (type != null) {
// If you add custom types to this part, the type to SweetAlertIcon cast below needs to be changed.
switch (type) {
case "success":
iconClasses = "fa-check text-success";
break;
case "warning":
iconClasses = "fa-warning text-warning";
break;
case "error":
iconClasses = "fa-bolt text-danger";
break;
case "info":
iconClasses = "fa-info-circle text-info";
break;
default:
break;
}
}
const bootstrapModal = document.querySelector("div.modal");
if (bootstrapModal != null) {
bootstrapModal.removeAttribute("tabindex");
}
const iconHtmlStr =
iconClasses != null ? `<i class="swal-custom-icon fa ${iconClasses}"></i>` : undefined;
const confirmed = await Swal.fire({
heightAuto: false,
buttonsStyling: false,
icon: type as SweetAlertIcon, // required to be any of the SweetAlertIcons to output the iconHtml.
iconHtml: iconHtmlStr,
text: bodyIsHtml ? null : body,
html: bodyIsHtml ? body : null,
titleText: title,
showCancelButton: cancelText != null,
cancelButtonText: cancelText,
showConfirmButton: true,
confirmButtonText: confirmText == null ? this.i18nService.t("ok") : confirmText,
});
if (bootstrapModal != null) {
bootstrapModal.setAttribute("tabindex", "-1");
}
return confirmed.value;
}
isDev(): boolean {
return process.env.NODE_ENV === "development";
}
isSelfHost(): boolean {
return process.env.ENV.toString() === "selfhosted";
}
copyToClipboard(text: string, options?: any): void | boolean {
let win = window;
let doc = window.document;
if (options && (options.window || options.win)) {
win = options.window || options.win;
doc = win.document;
} else if (options && options.doc) {
doc = options.doc;
}
if ((win as any).clipboardData && (win as any).clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
(win as any).clipboardData.setData("Text", text);
} else if (doc.queryCommandSupported && doc.queryCommandSupported("copy")) {
const textarea = doc.createElement("textarea");
textarea.textContent = text;
// Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = "fixed";
let copyEl = doc.body;
// For some reason copy command won't work when modal is open if appending to body
if (doc.body.classList.contains("modal-open")) {
copyEl = doc.body.querySelector<HTMLElement>(".modal");
}
copyEl.appendChild(textarea);
textarea.select();
let success = false;
try {
// Security exception may be thrown by some browsers.
success = doc.execCommand("copy");
if (!success) {
this.logService.debug("Copy command unsupported or disabled.");
}
if (navigator.userAgent.indexOf(' Firefox/') !== -1 || navigator.userAgent.indexOf(' Gecko/') !== -1) {
this.browserCache = DeviceType.FirefoxBrowser;
} else if (navigator.userAgent.indexOf(' OPR/') >= 0) {
this.browserCache = DeviceType.OperaBrowser;
} else if (navigator.userAgent.indexOf(' Edg/') !== -1) {
this.browserCache = DeviceType.EdgeBrowser;
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
this.browserCache = DeviceType.VivaldiBrowser;
} else if (navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
this.browserCache = DeviceType.SafariBrowser;
} else if ((window as any).chrome && navigator.userAgent.indexOf(' Chrome/') !== -1) {
this.browserCache = DeviceType.ChromeBrowser;
} else if (navigator.userAgent.indexOf(' Trident/') !== -1) {
this.browserCache = DeviceType.IEBrowser;
} else {
this.browserCache = DeviceType.UnknownBrowser;
}
return this.browserCache;
} catch (e) {
// tslint:disable-next-line
console.warn("Copy to clipboard failed.", e);
} finally {
copyEl.removeChild(textarea);
}
return success;
}
}
getDeviceString(): string {
const device = DeviceType[this.getDevice()].toLowerCase();
return device.replace('browser', '');
readFromClipboard(options?: any): Promise<string> {
throw new Error("Cannot read from clipboard on web.");
}
supportsBiometric() {
return Promise.resolve(false);
}
authenticateBiometric() {
return Promise.resolve(false);
}
supportsSecureStorage() {
return false;
}
getDefaultSystemTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
return Promise.resolve(this.prefersColorSchemeDark.matches ? ThemeType.Dark : ThemeType.Light);
}
async getEffectiveTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
const theme = await this.stateService.getTheme();
if (theme === ThemeType.Dark) {
return ThemeType.Dark;
} else if (theme === ThemeType.System) {
return this.getDefaultSystemTheme();
} else {
return ThemeType.Light;
}
}
isFirefox(): boolean {
return this.getDevice() === DeviceType.FirefoxBrowser;
}
isChrome(): boolean {
return this.getDevice() === DeviceType.ChromeBrowser;
}
isEdge(): boolean {
return this.getDevice() === DeviceType.EdgeBrowser;
}
isOpera(): boolean {
return this.getDevice() === DeviceType.OperaBrowser;
}
isVivaldi(): boolean {
return this.getDevice() === DeviceType.VivaldiBrowser;
}
isSafari(): boolean {
return this.getDevice() === DeviceType.SafariBrowser;
}
isIE(): boolean {
return this.getDevice() === DeviceType.IEBrowser;
}
isMacAppStore(): boolean {
return false;
}
isViewOpen(): Promise<boolean> {
return Promise.resolve(false);
}
launchUri(uri: string, options?: any): void {
const a = document.createElement('a');
a.href = uri;
if (options == null || !options.sameWindow) {
a.target = '_blank';
a.rel = 'noreferrer noopener';
}
a.classList.add('d-none');
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
let blob: Blob = null;
let type: string = null;
const fileNameLower = fileName.toLowerCase();
let doDownload = true;
if (fileNameLower.endsWith('.pdf')) {
type = 'application/pdf';
doDownload = false;
} else if (fileNameLower.endsWith('.xlsx')) {
type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
} else if (fileNameLower.endsWith('.docx')) {
type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
} else if (fileNameLower.endsWith('.pptx')) {
type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
} else if (fileNameLower.endsWith('.csv')) {
type = 'text/csv';
} else if (fileNameLower.endsWith('.png')) {
type = 'image/png';
} else if (fileNameLower.endsWith('.jpg') || fileNameLower.endsWith('.jpeg')) {
type = 'image/jpeg';
} else if (fileNameLower.endsWith('.gif')) {
type = 'image/gif';
}
if (type != null) {
blobOptions = blobOptions || {};
if (blobOptions.type == null) {
blobOptions.type = type;
}
}
if (blobOptions != null && !this.isIE()) {
blob = new Blob([blobData], blobOptions);
} else {
blob = new Blob([blobData]);
}
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement('a');
if (doDownload) {
a.download = fileName;
} else if (!this.isSafari()) {
a.target = '_blank';
}
a.href = URL.createObjectURL(blob);
a.style.position = 'fixed';
win.document.body.appendChild(a);
a.click();
win.document.body.removeChild(a);
}
}
getApplicationVersion(): Promise<string> {
return Promise.resolve(process.env.APPLICATION_VERSION || '-');
}
supportsWebAuthn(win: Window): boolean {
return (typeof (PublicKeyCredential) !== 'undefined');
}
supportsDuo(): boolean {
return true;
}
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string | string[],
options?: any): void {
this.messagingService.send('showToast', {
text: text,
title: title,
type: type,
options: options,
});
}
async showDialog(body: string, title?: string, confirmText?: string, cancelText?: string, type?: string,
bodyIsHtml: boolean = false) {
let iconClasses: string = null;
if (type != null) {
// If you add custom types to this part, the type to SweetAlertIcon cast below needs to be changed.
switch (type) {
case 'success':
iconClasses = 'fa-check text-success';
break;
case 'warning':
iconClasses = 'fa-warning text-warning';
break;
case 'error':
iconClasses = 'fa-bolt text-danger';
break;
case 'info':
iconClasses = 'fa-info-circle text-info';
break;
default:
break;
}
}
const bootstrapModal = document.querySelector('div.modal');
if (bootstrapModal != null) {
bootstrapModal.removeAttribute('tabindex');
}
const iconHtmlStr = iconClasses != null ? `<i class="swal-custom-icon fa ${iconClasses}"></i>` : undefined;
const confirmed = await Swal.fire({
heightAuto: false,
buttonsStyling: false,
icon: type as SweetAlertIcon, // required to be any of the SweetAlertIcons to output the iconHtml.
iconHtml: iconHtmlStr,
text: bodyIsHtml ? null : body,
html: bodyIsHtml ? body : null,
titleText: title,
showCancelButton: (cancelText != null),
cancelButtonText: cancelText,
showConfirmButton: true,
confirmButtonText: confirmText == null ? this.i18nService.t('ok') : confirmText,
});
if (bootstrapModal != null) {
bootstrapModal.setAttribute('tabindex', '-1');
}
return confirmed.value;
}
isDev(): boolean {
return process.env.NODE_ENV === 'development';
}
isSelfHost(): boolean {
return process.env.ENV.toString() === 'selfhosted';
}
copyToClipboard(text: string, options?: any): void | boolean {
let win = window;
let doc = window.document;
if (options && (options.window || options.win)) {
win = options.window || options.win;
doc = win.document;
} else if (options && options.doc) {
doc = options.doc;
}
if ((win as any).clipboardData && (win as any).clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
(win as any).clipboardData.setData('Text', text);
} else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
const textarea = doc.createElement('textarea');
textarea.textContent = text;
// Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = 'fixed';
let copyEl = doc.body;
// For some reason copy command won't work when modal is open if appending to body
if (doc.body.classList.contains('modal-open')) {
copyEl = doc.body.querySelector<HTMLElement>('.modal');
}
copyEl.appendChild(textarea);
textarea.select();
let success = false;
try {
// Security exception may be thrown by some browsers.
success = doc.execCommand('copy');
if (!success) {
this.logService.debug('Copy command unsupported or disabled.');
}
} catch (e) {
// tslint:disable-next-line
console.warn('Copy to clipboard failed.', e);
} finally {
copyEl.removeChild(textarea);
}
return success;
}
}
readFromClipboard(options?: any): Promise<string> {
throw new Error('Cannot read from clipboard on web.');
}
supportsBiometric() {
return Promise.resolve(false);
}
authenticateBiometric() {
return Promise.resolve(false);
}
supportsSecureStorage() {
return false;
}
getDefaultSystemTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
return Promise.resolve(this.prefersColorSchemeDark.matches ? ThemeType.Dark : ThemeType.Light);
}
async getEffectiveTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
const theme = await this.stateService.getTheme();
if (theme === ThemeType.Dark) {
return ThemeType.Dark;
} else if (theme === ThemeType.System) {
return this.getDefaultSystemTheme();
} else {
return ThemeType.Light;
}
}
onDefaultSystemThemeChange(callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) {
try {
this.prefersColorSchemeDark.addEventListener('change', ({ matches }) => {
callback(matches ? ThemeType.Dark : ThemeType.Light);
});
} catch (e) {
// Safari older than v14
this.prefersColorSchemeDark.addListener(ev => {
callback(ev.matches ? ThemeType.Dark : ThemeType.Light);
});
}
onDefaultSystemThemeChange(callback: (theme: ThemeType.Light | ThemeType.Dark) => unknown) {
try {
this.prefersColorSchemeDark.addEventListener("change", ({ matches }) => {
callback(matches ? ThemeType.Dark : ThemeType.Light);
});
} catch (e) {
// Safari older than v14
this.prefersColorSchemeDark.addListener((ev) => {
callback(ev.matches ? ThemeType.Dark : ThemeType.Light);
});
}
}
}