all files / component/editor/material-field-wrapper/ deja-editor-selector.directive.ts

28.72% Statements 27/94
0% Branches 0/28
4.76% Functions 1/21
26.97% Lines 24/89
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                                                                                                                                                                                                                                                                                                                               
/*
 *  @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 {
    Directive,
    DoCheck,
    ElementRef,
    Host,
    HostBinding,
    Input,
    OnDestroy,
    OnInit,
    Optional,
    Self
} from '@angular/core';
import { FormGroupDirective, NgControl, NgForm } from '@angular/forms';
import {
    _MatInputMixinBase,
    CanUpdateErrorState,
    ErrorStateMatcher,
    MatFormFieldControl
} from '@angular/material';
import { Subject } from 'rxjs';
import { DejaEditorComponent } from '../deja-editor.component';
 
let nextUniqueId = 0;
 
@Directive({
    selector: 'deja-editor',
    providers: [
        {
            provide: MatFormFieldControl,
            useExisting: DejaEditorSelectorDirective
        }
    ]
})
export class DejaEditorSelectorDirective extends _MatInputMixinBase
    implements
        MatFormFieldControl<any>,
        DoCheck,
        OnInit,
        OnDestroy,
        CanUpdateErrorState {
    public errorState: boolean;
    public autofilled?: boolean;
    protected _uid = `mat-input-${nextUniqueId++}`;
    private _placeholder: HTMLDivElement;
    public value: any;
    public stateChanges: Subject<void> = new Subject<void>();
    @Input()
    public get id(): string {
        return this._id;
    }
    public set id(value: string) {
        this._id = value || this._uid;
    }
    protected _id: string;
    @Input() public placeholder: string;
    public focused: boolean;
    @Input()
    public get required(): boolean {
        return this._required;
    }
    public set required(value: boolean) {
        this._required = coerceBooleanProperty(value);
    }
    protected _required = false;
    @Input()
    public get disabled(): boolean {
        if (this.ngControl && this.ngControl.disabled !== null) {
            return this.ngControl.disabled;
        }
        return this._disabled;
    }
    public set disabled(value: boolean) {
        this._disabled = coerceBooleanProperty(value);
 
        // Browsers may not fire the blur event if the input is disabled too quickly.
        // Reset from here to ensure that the element doesn't become stuck.
        if (this.focused) {
            this.focused = false;
            this.stateChanges.next();
        }
    }
    protected _disabled = false;
    @HostBinding('attr.aria-describedby') public describedBy = '';
    public controlType = 'app-editor';
    public onContainerClick(): void {
        this._editor.setFocus();
    }
 
    constructor(
        @Self() private _editor: DejaEditorComponent,
        @Optional()
        @Self()
        public ngControl: NgControl,
        @Optional() _parentForm: NgForm,
        @Optional() _parentFormGroup: FormGroupDirective,
        _defaultErrorStateMatcher: ErrorStateMatcher,
        @Host() private _hostElement: ElementRef
    ) {
        super(
            _defaultErrorStateMatcher,
            _parentForm,
            _parentFormGroup,
            ngControl
        );
    }
 
    public setDescribedByIds(ids: string[]): void {
        this.describedBy = ids.join(' ');
    }
 
    public ngOnInit() {
        this._editor.focus.subscribe(() => {
            this.focused = true;
            this.stateChanges.next();
        });
 
        this._editor.blur.subscribe(() => {
            this.focused = false;
            this.stateChanges.next();
        });
        this._editor.change.subscribe(() => {
            this.stateChanges.next();
        });
        this._generatePlaceholder();
    }
 
    public ngDoCheck() {
        if (this.ngControl) {
            this.updateErrorState();
        }
    }
 
    public ngOnDestroy(): void {
        this.stateChanges.complete();
    }
 
    public get empty(): boolean {
        return !this._editor.value;
    }
 
    public get shouldLabelFloat(): boolean {
        if (this.focused || !this.empty) {
            if (this.empty) {
                this._attachPlaceholder();
            } else {
                this._detachPlaceholder();
            }
            return true;
        } else {
            this._detachPlaceholder();
            return false;
        }
    }
 
    private _attachPlaceholder() {
        if (this._placeholder && !this._placeholder.parentElement) {
            this._hostElement.nativeElement.appendChild(this._placeholder);
        }
    }
 
    private _detachPlaceholder() {
        if (this._placeholder && this._placeholder.parentElement) {
            this._placeholder.remove();
        }
    }
 
    private _generatePlaceholder() {
        if (this.placeholder) {
            this._placeholder = document.createElement('div');
            this._placeholder.style.position = 'absolute';
            this._placeholder.style.position = 'absolute';
            this._placeholder.style.left = '0';
            this._placeholder.style.boxSizing = 'content-box';
            this._placeholder.style.width = '100%';
            this._placeholder.style.height = '100%';
            this._placeholder.style.overflow = 'hidden';
            this._placeholder.style.pointerEvents = 'none';
            this._placeholder.style.top = '-0.84375em';
            this._placeholder.style.paddingTop = '0.84375em';
            const placeholderChildren = document.createElement('div');
            placeholderChildren.style.color = 'rgba(0,0,0,0.54)';
            placeholderChildren.style.top = '1.28125em';
            placeholderChildren.style.position = 'absolute';
            const placeholderText = document.createTextNode(this.placeholder);
            placeholderChildren.appendChild(placeholderText);
            this._placeholder.appendChild(placeholderChildren);
        }
    }
}