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

81.4% Statements 35/43
37.5% Branches 6/16
100% Functions 10/10
80.49% Lines 33/41
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 112 113 114                                                                                                                    24×                                              
/*
 *  @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 { coerceNumberProperty } from '@angular/cdk/coercion';
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
 
/**
 * Separator for the spltter component
 */
@Directive({
    selector: 'split-gutter',
})
export class SplitGutterDirective {
 
    /**
     * Order of the seperator
     */
    @Input()
    public set order(v: number | string) {
        this.setStyle('order', coerceNumberProperty(v));
    }
 
    private _direction: string;
    /**
     * Direction of the separator
     * Can be `horizontal` or `vertical`
     */
    @Input()
    public set direction(v: string) {
        this._direction = v;
        this.refreshStyle();
    }
 
    /**
     * Separator size in pixel
     */
    @Input()
    public set size(v: number | string) {
        Iif (!v) {
            this.setStyle('flex-basis', '');
        } else Iif (typeof v === 'string') {
            this.setStyle('flex-basis', `${coerceNumberProperty(v)}px`);
        } else {
            this.setStyle('flex-basis', `${v}px`);
        }
    }
 
    private _disabled = false;
    /**
     * Disable the separator
     * By default `false`
     */
    @Input()
    public set disabled(v: boolean) {
        this._disabled = v;
        this.refreshStyle();
    }
 
    /**
     * Constructor
     */
    constructor(private elementRef: ElementRef,
        private renderer: Renderer) {
    }
 
    private refreshStyle() {
        const state = this._disabled === true ? 'disabled' : this._direction;
 
        this.setStyle('cursor', this.getCursor(state));
        this.setStyle('background-image', `url("${this.getImage(state)}")`);
 
        // Add a content in css, to allow the gutter to take the full wize
        Eif (state === 'horizontal') {
            this.setStyle('content', ` `);
        }
    }
 
    private setStyle(key: string, value: any) {
        this.renderer.setElementStyle(this.elementRef.nativeElement, key, value);
    }
 
    private getCursor(state: string) {
        switch (state) {
            case 'disabled':
                return 'default';
            case 'vertical':
                return 'row-resize';
            case 'horizontal':
                return 'col-resize';
            default:
                return null;
        }
    }
 
    private getImage(state: string) {
        switch (state) {
            case 'disabled':
                return '';
            case 'vertical':
                return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFCAMAAABl/6zIAAAABlBMVEUAAADMzMzIT8AyAAAAAXRSTlMAQObYZgAAABRJREFUeAFjYGRkwIMJSeMHlBkOABP7AEGzSuPKAAAAAElFTkSuQmCC';
            case 'horizontal':
                return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==';
            default:
                return null;
        }
    }
}