all files / component/splitter/ split-area.directive.ts

95.24% Statements 40/42
60% Branches 6/10
83.33% Functions 10/12
100% Lines 38/38
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                                                                                                                12× 12× 12×                   30×                
/*
 *  @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
 */
 
/**
 * Created by rtr on 22.12.2016.
 */
import { coerceNumberProperty } from '@angular/cdk/coercion';
import { Directive, ElementRef, Input, OnDestroy, OnInit, Renderer } from '@angular/core';
import { DejaSplitterComponent } from './splitter.component';
 
/**
 * Directive representing a panel in a Splitter Component
 */
@Directive({
    selector: 'split-area',
})
export class SplitAreaDirective implements OnInit, OnDestroy {
 
    /**
     * Order position of the current area
     */
    @Input()
    public set order(value: number | string) {
        const v = coerceNumberProperty(value);
        this._order = !isNaN(v) ? v : null;
        this.split.updateArea(this, this._order, this._size, this._minSizePixel);
    }
 
    /**
     * Size in percent of the current area
     */
    @Input()
    public set size(value: number | string) {
        const v = coerceNumberProperty(value);
        this._size = !isNaN(v) ? v : null;
        this.split.updateArea(this, this._order, this._size, this._minSizePixel);
    }
 
    /**
     * Min size in percent of the current area
     */
    @Input()
    public set minSizePixel(value: number | string) {
        const v = coerceNumberProperty(value);
        this._minSizePixel = (!isNaN(v) && v > 0) ? v : 0;
        this.split.updateArea(this, this._order, this._size, this._minSizePixel);
    }
 
    private _order: number | null = null;
    private _size: number | null = null;
    private _minSizePixel = 0;
    private eventsLockFct: Function[] = [];
 
    /**
     * Constructor
     */
    constructor(private elementRef: ElementRef,
                private renderer: Renderer,
                private split: DejaSplitterComponent) {
    }
 
    /**
     * Lifecycle hook that is called after data-bound properties of a directive are initialized.
     */
    public ngOnInit() {
        this.split.addArea(this, this._order, this._size, this._minSizePixel);
    }
 
    /**
     * Lock the events
     */
    public lockEvents() {
        this.eventsLockFct.push(this.renderer.listen(this.elementRef.nativeElement, 'selectstart', () => false));
        this.eventsLockFct.push(this.renderer.listen(this.elementRef.nativeElement, 'dragstart', () => false));
    }
 
    /**
     * Unlock the events
     */
    public unlockEvents() {
        while (this.eventsLockFct.length > 0) {
            const fct = this.eventsLockFct.pop();
            Eif (fct) {
                fct();
            }
        }
    }
 
    /**
     * Set a style for the current area
     * @param key style key
     * @param value style value
     */
    public setStyle(key: string, value: any) {
        this.renderer.setElementStyle(this.elementRef.nativeElement, key, value);
    }
 
    /**
     * Lifecycle hook that is called when a directive, pipe or service is destroyed.
     */
    public ngOnDestroy() {
        this.split.removeArea(this);
    }
}