all files / component/dialog/ dialog.component.ts

86.11% Statements 31/36
27.78% Branches 5/18
71.43% Functions 5/7
84.38% Lines 27/32
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                                                                                                             
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
import { Component, ContentChild, ElementRef, EventEmitter, HostListener, OnDestroy, Output } from '@angular/core';
import { fromEvent as observableFromEvent } from 'rxjs';
import { filter, takeWhile } from 'rxjs/operators';
import { KeyCodes } from '../../common/core/keycodes.enum';
 
/**
 * Simple dialog for Angular
 */
@Component({
    selector: 'deja-dialog',
    styles: [require('./dialog.component.scss')],
    template: require('./dialog.component.html'),
})
export class DejaDialogComponent implements OnDestroy {
    /** Event emitted when dialog close action is called */
    @Output() public closed = new EventEmitter();
 
    @ContentChild('okaction') private okButton: any;
    @ContentChild('cancelaction') private cancelButton: any;
 
    private isAlive = true;
 
    /**
     * Constructor
     */
    constructor(elementRef: ElementRef) {
        const element = elementRef.nativeElement as HTMLElement;
 
        observableFromEvent(element.ownerDocument, 'keyup').pipe(
            takeWhile(() => this.isAlive),
            filter((event: KeyboardEvent) => !!(event.keyCode === KeyCodes.Enter && this.okButton && this.okButton._elementRef) || !!(event.keyCode === KeyCodes.Escape && this.cancelButton && this.cancelButton._elementRef)))
            .subscribe((event: KeyboardEvent) => {
                if (event.keyCode === KeyCodes.Enter) {
                    this.okButton._elementRef.nativeElement.click();
                }
                if (event.keyCode === KeyCodes.Escape) {
                    this.cancelButton._elementRef.nativeElement.click();
                }
            });
    }
 
    /** Unsubscribe to all observables when component is destroyed */
    public ngOnDestroy() {
        this.isAlive = false;
    }
 
    /**
     * Listen on click on dialogComponent.
     * If click is not inside the dialog, close action is called.
     *
     * @param event
     */
    @HostListener('click', ['$event'])
    public close(event: MouseEvent) {
 
        let close = true;
 
        let target = event.target as HTMLElement;
        const element = event.currentTarget as HTMLElement;
 
        while (target.parentElement && target !== element) {
            Eif (target.className === 'dialog') {
                close = false;
            }
            target = target.parentElement;
        }
 
        if (close) {
            this.closed.emit();
            event.preventDefault();
        }
    }
}