|
| 1 | +import { Directionality } from '@angular/cdk/bidi'; |
| 2 | +import { Overlay, ScrollDispatcher } from '@angular/cdk/overlay'; |
| 3 | +import { |
| 4 | + ChangeDetectionStrategy, |
| 5 | + ChangeDetectorRef, |
| 6 | + Component, |
| 7 | + Directive, |
| 8 | + ElementRef, |
| 9 | + EventEmitter, |
| 10 | + Inject, |
| 11 | + InjectionToken, |
| 12 | + Input, |
| 13 | + NgZone, |
| 14 | + Optional, |
| 15 | + Output, |
| 16 | + ViewContainerRef, |
| 17 | + ViewEncapsulation |
| 18 | +} from '@angular/core'; |
| 19 | +import { ThemePalette } from '@ptsecurity/mosaic/core'; |
| 20 | +import { Subject } from 'rxjs'; |
| 21 | +import { takeUntil } from 'rxjs/operators'; |
| 22 | + |
| 23 | +import { mcPopoverAnimations } from './popover-animations'; |
| 24 | +import { MC_POPOVER_SCROLL_STRATEGY, McPopoverComponent, McPopoverTrigger } from './popover.component'; |
| 25 | + |
| 26 | + |
| 27 | +export const MC_POPOVER_CONFIRM_TEXT = new InjectionToken<string>(''); |
| 28 | +export const MC_POPOVER_CONFIRM_BUTTON_TEXT = new InjectionToken<string>(''); |
| 29 | + |
| 30 | +@Component({ |
| 31 | + selector: 'mc-popover-confirm-component', |
| 32 | + templateUrl: './popover-confirm.component.html', |
| 33 | + preserveWhitespaces: false, |
| 34 | + styleUrls: ['./popover.scss'], |
| 35 | + encapsulation: ViewEncapsulation.None, |
| 36 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 37 | + animations: [mcPopoverAnimations.popoverState] |
| 38 | +}) |
| 39 | +export class McPopoverConfirmComponent extends McPopoverComponent { |
| 40 | + themePalette = ThemePalette; |
| 41 | + |
| 42 | + onConfirm = new Subject<void>(); |
| 43 | + confirmButtonText: string; |
| 44 | + |
| 45 | + confirmText: string; |
| 46 | + |
| 47 | + constructor(changeDetectorRef: ChangeDetectorRef) { |
| 48 | + super(changeDetectorRef); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +@Directive({ |
| 54 | + selector: '[mcPopoverConfirm]', |
| 55 | + exportAs: 'mcPopoverConfirm', |
| 56 | + host: { |
| 57 | + '[class.mc-popover_open]': 'isOpen', |
| 58 | + '(keydown)': 'handleKeydown($event)', |
| 59 | + '(touchend)': 'handleTouchend()' |
| 60 | + } |
| 61 | +}) |
| 62 | +export class McPopoverConfirmTrigger extends McPopoverTrigger { |
| 63 | + @Output() confirm: EventEmitter<void> = new EventEmitter<void>(); |
| 64 | + |
| 65 | + @Input('mcPopoverConfirmText') |
| 66 | + get confirmText(): string { |
| 67 | + return this._confirmText; |
| 68 | + } |
| 69 | + |
| 70 | + set confirmText(value: string) { |
| 71 | + this._confirmText = value; |
| 72 | + |
| 73 | + this.updateData(); |
| 74 | + } |
| 75 | + |
| 76 | + private _confirmText: string; |
| 77 | + |
| 78 | + @Input('mcPopoverConfirmButtonText') |
| 79 | + get confirmButtonText(): string { |
| 80 | + return this._confirmButtonText; |
| 81 | + } |
| 82 | + |
| 83 | + set confirmButtonText(value: string) { |
| 84 | + this._confirmButtonText = value; |
| 85 | + |
| 86 | + this.updateData(); |
| 87 | + } |
| 88 | + |
| 89 | + private _confirmButtonText: string = 'Да'; |
| 90 | + |
| 91 | + constructor( |
| 92 | + overlay: Overlay, |
| 93 | + elementRef: ElementRef, |
| 94 | + ngZone: NgZone, |
| 95 | + scrollDispatcher: ScrollDispatcher, |
| 96 | + hostView: ViewContainerRef, |
| 97 | + @Inject(MC_POPOVER_SCROLL_STRATEGY) scrollStrategy, |
| 98 | + @Optional() direction: Directionality, |
| 99 | + @Optional() @Inject(MC_POPOVER_CONFIRM_TEXT) confirmText: string, |
| 100 | + @Optional() @Inject(MC_POPOVER_CONFIRM_BUTTON_TEXT) confirmButtonText: string |
| 101 | + ) { |
| 102 | + super(overlay, elementRef, ngZone, scrollDispatcher, hostView, scrollStrategy, direction); |
| 103 | + |
| 104 | + this.confirmText = confirmText || 'Вы уверены, что хотите продолжить?'; |
| 105 | + this.confirmButtonText = confirmButtonText || 'Да'; |
| 106 | + } |
| 107 | + |
| 108 | + updateData() { |
| 109 | + if (!this.instance) { return; } |
| 110 | + super.updateData(); |
| 111 | + this.setupButtonEvents(); |
| 112 | + this.instance.confirmButtonText = this.confirmButtonText; |
| 113 | + this.instance.confirmText = this.confirmText; |
| 114 | + } |
| 115 | + |
| 116 | + setupButtonEvents() { |
| 117 | + this.instance.onConfirm.pipe(takeUntil(this.destroyed)).subscribe(() => { |
| 118 | + this.confirm.emit(); |
| 119 | + this.hide(); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + getOverlayHandleComponentType() { |
| 124 | + return McPopoverConfirmComponent; |
| 125 | + } |
| 126 | +} |
0 commit comments