all files / component/message-box/ message-box.component.ts

97.37% Statements 37/38
93.75% Branches 15/16
100% Functions 10/10
97.06% Lines 33/34
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                                15×               46×     15×         30×         13× 13×     13×                                    
/*
 *  @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 { coerceBooleanProperty } from '@angular/cdk/coercion';
import {Component, ContentChild, EventEmitter, Input, OnInit, Output, ViewEncapsulation} from '@angular/core';
 
@Component({
    encapsulation: ViewEncapsulation.None,
    selector: 'deja-message-box',
    styles: [require('./message-box.component.scss')],
    template: require('./message-box.component.html'),
})
export class DejaMessageBoxComponent implements OnInit {
    @Input() public type: 'info' | 'primary' | 'success' | 'warn' | 'danger';
    @Input() public title: string;
    @Input() public icon: string;
    @Input() public actions: Array<{text?: string; type?: 'info' | 'primary' | 'success' | 'warn' | 'danger'; icon?: string; action(): any}>;
    /** Event Emmited when the close action is called */
    @Output() public close = new EventEmitter();
    @ContentChild('actionsTemplate') public actionsTemplate: any;
 
    private _horizontal: boolean;
 
    @Input()
    public set horizontal(value: boolean | string) {
        this._horizontal = coerceBooleanProperty(value);
    }
 
    public get horizontal() {
        return this._horizontal;
    }
 
    private _showCloseIcon = false;
    @Input()
    public set showCloseIcon(value: boolean | string) {
        this._showCloseIcon = coerceBooleanProperty(value);
    }
 
    public get showCloseIcon() {
        return this._showCloseIcon;
    }
 
    constructor() { }
 
    public ngOnInit() {
        if (!this.icon && this.type) {
            this.icon = this.getIconFromType(this.type);
        }
 
        if (this.actions) {
            this.actions.forEach((action) => {
                if (!action.icon && action.type) {
                    action.icon = this.getIconFromType(action.type);
                }
            });
        }
    }
 
    public onClose() {
        this.close.emit();
    }
 
    private getIconFromType(type: 'info' | 'primary' | 'success' | 'warn' | 'danger'): string {
        switch (type) {
            case 'info':
            case 'primary':
                type = 'primary';
                return 'info_outline';
            case 'success':
                return 'done';
            case 'warn':
                return 'warning_outline';
            case 'danger':
                return 'error_outline';
            default:
                return null;
        }
    }
}