Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | 13x 4x 1x 26x 26x 26x 26x 26x 26x 43x 43x 43x 43x 26x 26x 26x 41x 26x 26x 26x 26x 26x 26x 26x 26x 26x 55x 26x 41x 41x 26x 26x 26x 26x 26x 26x 26x 26x 26x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import {
Binding,
ComponentRef,
EnvironmentInjector,
inject,
Injectable,
Injector,
inputBinding,
StaticProvider,
TemplateRef,
Type
} from '@angular/core';
import { areAnimationsDisabled, isRTL } from '@siemens/element-ng/common';
import { SiNoTranslateService, SiTranslateService } from '@siemens/element-translate-ng/translate';
import { ModalRef } from './modalref';
import { SiModalComponent } from './si-modal.component';
export interface ModalDependencyInjectionOptions {
/** Additional providers for the modal. */
providers?: StaticProvider[];
/** Optional element Injector. */
injector?: Injector;
/** Optional environment Injector. */
environmentInjector?: EnvironmentInjector;
}
export interface ModalOptions<T = Record<string, any>> extends ModalDependencyInjectionOptions {
/**
* Angular bindings for component content, created with `inputBinding`, `outputBinding`, or
* `twoWayBinding`. Ignored for template content.
*/
bindings?: Binding[];
/**
* Use this to assign values to either `@Input()` or `input()` fields of the provided component.
* If a template is used, the values are available in the template context.
*/
inputValues?: Record<string, unknown>;
/** When set to true, clicking the backdrop has no effect. */
ignoreBackdropClick?: boolean;
/** When set to true, backdrop click or Esc don't close the modal. Instead a message will be sent. */
messageInsteadOfAutoHide?: boolean;
/** Handle Esc to close/message. */
keyboard?: boolean;
/** Whether to enable animation. */
animated?: boolean;
/** Additional CSS class. */
class?: string;
/** aria-labelled-by value. */
ariaLabelledBy?: string;
}
class ModalRefImpl<T = never, CT = void> extends ModalRef<T, CT> {
modalCompRef?: ComponentRef<SiModalComponent>;
overlayRef?: OverlayRef;
override get content(): T {
return this.modalCompRef?.instance.componentRef?.instance as T;
}
override setInput(input: string, value: unknown): void {
this.modalCompRef?.instance.componentRef?.setInput(input, value);
}
constructor(layer: number, data: ModalOptions, closeValue?: CT) {
super();
this.layer = layer;
this.data = data;
this.ignoreBackdropClick = data.ignoreBackdropClick !== false;
this.dialogClass = data?.class ?? '';
this.closeValue = closeValue;
}
}
@Injectable({ providedIn: 'root' })
export class SiModalService {
private modalsCount = 0;
private readonly overlay = inject(Overlay);
private injector = inject(Injector);
/**
* Shows a modal with the given content and configuration.
* @param content - Content to be displayed in the modal can be a template reference or a component.
* @param config - Configuration for the modal.
* @param closeValue - Default closing value which can be overridden by calling `modalRef.hide(value)`.
* @returns A reference to the modal.
*/
show<T, CT = any>(
content: TemplateRef<any> | (new (...args: any[]) => T),
config: ModalOptions<T>,
closeValue?: CT
): ModalRef<T, CT> {
const modalRef = new ModalRefImpl<T, CT>(++this.modalsCount, config, closeValue);
const siModalRef = this.attachComponent(content, modalRef);
modalRef.modalCompRef = siModalRef;
modalRef.detach = () => this.detach(modalRef);
modalRef.hide = (param?: CT) => modalRef.modalCompRef?.instance.hideDialog(param);
modalRef.isCurrent = () => modalRef.layer === this.modalsCount;
// Initialize the modal view so content is available before show() returns.
siModalRef.changeDetectorRef.detectChanges();
return modalRef;
}
private attachComponent<T, CT>(
content: TemplateRef<unknown> | Type<T>,
modalRef: ModalRefImpl<T, CT>
): ComponentRef<SiModalComponent> {
const providers: StaticProvider[] = [{ provide: ModalRef, useValue: modalRef }];
modalRef.data.providers?.forEach(p => providers.push(p));
const injector = Injector.create({
providers,
parent: modalRef.data.injector ?? this.buildInjector()
});
modalRef.overlayRef = this.overlay.create({
positionStrategy: this.overlay.position().global().centerHorizontally().centerVertically(),
direction: isRTL() ? 'rtl' : 'ltr',
hasBackdrop: modalRef.layer === 1,
disableAnimations: modalRef.data.animated === false || areAnimationsDisabled()
});
const compPortal = new ComponentPortal(SiModalComponent, null, injector, undefined, [
inputBinding('content', () => content)
]);
return modalRef.overlayRef.attach(compPortal);
}
protected detach(modalRef: ModalRef<unknown, any>): void {
const ref = modalRef as ModalRefImpl<unknown>;
if (ref.modalCompRef) {
ref.modalCompRef = undefined;
ref.overlayRef?.detach();
ref.overlayRef = undefined;
ref.shown.complete();
this.modalsCount--;
}
}
// TODO remove once translation must be defined at application start
// Notification service is provided in 'root'. If no translation is defined, SiNoTranslateService is not provided
protected buildInjector(): Injector {
let injector = this.injector;
Eif (!injector.get(SiTranslateService, null)) {
injector = Injector.create({
providers: [{ provide: SiTranslateService, useClass: SiNoTranslateService, deps: [] }],
parent: this.injector
});
}
return injector;
}
}
|