|
| 1 | +import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core'; |
| 2 | +import { animate, state, style, transition, trigger } from '@angular/animations'; |
| 3 | + |
| 4 | +import { Notification } from '../../models/notification'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'undoable-notifier', |
| 8 | + templateUrl: './undoable-notifier.component.html', |
| 9 | + styleUrls: ['./undoable-notifier.component.scss'], |
| 10 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 11 | + animations: [ |
| 12 | + trigger('slideInOut', [ |
| 13 | + state('in', style({ |
| 14 | + transform: 'translate3d(0, 0, 0)' |
| 15 | + })), |
| 16 | + state('out', style({ |
| 17 | + transform: 'translate3d(200%, 0, 0)' |
| 18 | + })), |
| 19 | + transition('in => out', animate('400ms ease-in-out')), |
| 20 | + transition('out => in', animate('400ms ease-in-out')) |
| 21 | + ]) |
| 22 | + ] |
| 23 | +}) |
| 24 | +export class UndoableNotifierComponent implements OnChanges { |
| 25 | + text: string; |
| 26 | + undoable: boolean; |
| 27 | + @Input() notification: Notification; |
| 28 | + @Output() close = new EventEmitter(); |
| 29 | + @Output() undo = new EventEmitter(); |
| 30 | + |
| 31 | + get slideInOut() { |
| 32 | + return this.notification ? 'in' : 'out'; |
| 33 | + } |
| 34 | + |
| 35 | + ngOnChanges(changes: SimpleChanges): void { |
| 36 | + if (changes['notification']) { |
| 37 | + const not: Notification = changes['notification'].currentValue; |
| 38 | + if (not) { |
| 39 | + this.text = not.message; |
| 40 | + this.undoable = !!not.extra; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + clickOnClose(): void { |
| 46 | + this.close.emit(); |
| 47 | + } |
| 48 | + |
| 49 | + clickOnUndo(): void { |
| 50 | + this.undo.emit(this.notification.extra); |
| 51 | + } |
| 52 | +} |
0 commit comments