mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
154 lines
4.8 KiB
TypeScript
154 lines
4.8 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import {
|
|
DEFAULT_DIALOG_CONFIG,
|
|
Dialog,
|
|
DialogConfig,
|
|
DialogRef,
|
|
DIALOG_SCROLL_STRATEGY,
|
|
} from "@angular/cdk/dialog";
|
|
import { ComponentType, Overlay, OverlayContainer, ScrollStrategy } from "@angular/cdk/overlay";
|
|
import {
|
|
Inject,
|
|
Injectable,
|
|
Injector,
|
|
OnDestroy,
|
|
Optional,
|
|
SkipSelf,
|
|
TemplateRef,
|
|
} from "@angular/core";
|
|
import { NavigationEnd, Router } from "@angular/router";
|
|
import { filter, firstValueFrom, Subject, switchMap, takeUntil } from "rxjs";
|
|
|
|
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
|
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
|
|
import { SimpleConfigurableDialogComponent } from "./simple-dialog/simple-configurable-dialog/simple-configurable-dialog.component";
|
|
import { SimpleDialogOptions, Translation } from "./simple-dialog/types";
|
|
|
|
/**
|
|
* The default `BlockScrollStrategy` does not work well with virtual scrolling.
|
|
*
|
|
* https://github.com/angular/components/issues/7390
|
|
*/
|
|
class CustomBlockScrollStrategy implements ScrollStrategy {
|
|
enable() {
|
|
document.body.classList.add("tw-overflow-hidden");
|
|
}
|
|
|
|
disable() {
|
|
document.body.classList.remove("tw-overflow-hidden");
|
|
}
|
|
|
|
/** Noop */
|
|
attach() {}
|
|
|
|
/** Noop */
|
|
detach() {}
|
|
}
|
|
|
|
@Injectable()
|
|
export class DialogService extends Dialog implements OnDestroy {
|
|
private _destroy$ = new Subject<void>();
|
|
|
|
private backDropClasses = ["tw-fixed", "tw-bg-black", "tw-bg-opacity-30", "tw-inset-0"];
|
|
|
|
private defaultScrollStrategy = new CustomBlockScrollStrategy();
|
|
|
|
constructor(
|
|
/** Parent class constructor */
|
|
_overlay: Overlay,
|
|
_injector: Injector,
|
|
@Optional() @Inject(DEFAULT_DIALOG_CONFIG) _defaultOptions: DialogConfig,
|
|
@Optional() @SkipSelf() _parentDialog: Dialog,
|
|
_overlayContainer: OverlayContainer,
|
|
@Inject(DIALOG_SCROLL_STRATEGY) scrollStrategy: any,
|
|
|
|
/** Not in parent class */
|
|
@Optional() router: Router,
|
|
@Optional() authService: AuthService,
|
|
|
|
protected i18nService: I18nService,
|
|
) {
|
|
super(_overlay, _injector, _defaultOptions, _parentDialog, _overlayContainer, scrollStrategy);
|
|
|
|
/** Close all open dialogs if the vault locks */
|
|
if (router && authService) {
|
|
router.events
|
|
.pipe(
|
|
filter((event) => event instanceof NavigationEnd),
|
|
switchMap(() => authService.getAuthStatus()),
|
|
filter((v) => v !== AuthenticationStatus.Unlocked),
|
|
takeUntil(this._destroy$),
|
|
)
|
|
.subscribe(() => this.closeAll());
|
|
}
|
|
}
|
|
|
|
override ngOnDestroy(): void {
|
|
this._destroy$.next();
|
|
this._destroy$.complete();
|
|
super.ngOnDestroy();
|
|
}
|
|
|
|
override open<R = unknown, D = unknown, C = unknown>(
|
|
componentOrTemplateRef: ComponentType<C> | TemplateRef<C>,
|
|
config?: DialogConfig<D, DialogRef<R, C>>,
|
|
): DialogRef<R, C> {
|
|
config = {
|
|
backdropClass: this.backDropClasses,
|
|
scrollStrategy: this.defaultScrollStrategy,
|
|
...config,
|
|
};
|
|
|
|
return super.open(componentOrTemplateRef, config);
|
|
}
|
|
|
|
/**
|
|
* Opens a simple dialog, returns true if the user accepted the dialog.
|
|
*
|
|
* @param {SimpleDialogOptions} simpleDialogOptions - An object containing options for the dialog.
|
|
* @returns `boolean` - True if the user accepted the dialog, false otherwise.
|
|
*/
|
|
async openSimpleDialog(simpleDialogOptions: SimpleDialogOptions): Promise<boolean> {
|
|
const dialogRef = this.openSimpleDialogRef(simpleDialogOptions);
|
|
|
|
return firstValueFrom(dialogRef.closed);
|
|
}
|
|
|
|
/**
|
|
* Opens a simple dialog.
|
|
*
|
|
* You should probably use `openSimpleDialog` instead, unless you need to programmatically close the dialog.
|
|
*
|
|
* @param {SimpleDialogOptions} simpleDialogOptions - An object containing options for the dialog.
|
|
* @returns `DialogRef` - The reference to the opened dialog.
|
|
* Contains a closed observable which can be subscribed to for determining which button
|
|
* a user pressed
|
|
*/
|
|
openSimpleDialogRef(simpleDialogOptions: SimpleDialogOptions): DialogRef<boolean> {
|
|
return this.open<boolean, SimpleDialogOptions>(SimpleConfigurableDialogComponent, {
|
|
data: simpleDialogOptions,
|
|
disableClose: simpleDialogOptions.disableClose,
|
|
});
|
|
}
|
|
|
|
protected translate(translation: string | Translation, defaultKey?: string): string {
|
|
if (translation == null && defaultKey == null) {
|
|
return null;
|
|
}
|
|
|
|
if (translation == null) {
|
|
return this.i18nService.t(defaultKey);
|
|
}
|
|
|
|
// Translation interface use implies we must localize.
|
|
if (typeof translation === "object") {
|
|
return this.i18nService.t(translation.key, ...(translation.placeholders ?? []));
|
|
}
|
|
|
|
return translation;
|
|
}
|
|
}
|