All files / src/input input.ts

94.23% Statements 49/52
64.71% Branches 11/17
100% Functions 5/5
92.11% Lines 35/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 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234                                          1x                   1x 1x 1x 1x 1x                                                             10x     1x             10x           10x             10x           10x           10x                         10x                         10x           1x           10x           10x         10x             10x         10x         1x         1x         1x         1x           10x                     10x             7x 7x       7x     7x     7x                   2x 2x 2x        
/*
 MIT License
 
 Copyright (c) 2017 Temainfo Sistemas
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */
import {
    Component,
    Input,
    ViewChild,
    forwardRef,
    AfterViewInit,
    ChangeDetectionStrategy,
    Output,
    EventEmitter, ViewEncapsulation, ChangeDetectorRef
} from '@angular/core';
import { ComponentHasModelBase } from '../core/base/component-has-model.base';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { TabIndexService } from '../form/tabIndex.service';
import { IdGeneratorService } from '../core/helper/idgenerator.service';
import { NameGeneratorService } from '../core/helper/namegenerator.service';
 
/**
 * Input Component personalized with few features.
 *
 * import { InputModule } from 'truly-ui';
 *
 * @Component({
 *  selector: 'my-app-comp',
 *  template: '
 *  <tl-input
 *   [(ngModel)]="title"
 *   [iconBefore]="'ion-email'"
 *   [iconAfter]="'ion-email'"
 *   [textBefore]="'R$'"
 *   [textAfter]="'00,00'"
 *   [placeholder]="'Meu Input'"
 *   [clearButton]="true"
 *   [readonly]="true"
 *   [disabled]="true"
 *   [toUpperCase]="true">
 *  </tl-input>
 *  '
 * })
 */
@Component( {
    selector: 'tl-input',
    template: require('./input.html'),
    styles: [],
    encapsulation: ViewEncapsulation.None,
    providers: [
        { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef( () => TlInput ), multi: true }
    ]
} )
export class TlInput extends ComponentHasModelBase implements AfterViewInit {
 
 
    /**
     * Text placed before Input.
     * @type {string}
     */
    @Input() textBefore = '';
 
    /**
     * Text placed before Input.
     * @type {string}
     */
    @Input() textAfter = '';
 
    /**
     * Property to label placement [default]='left'
     * 'left' | 'top'
     * @type {string}
     */
    @Input() labelPlacement = 'left';
 
    /**
     * Property to labelSize
     * @type {string}
     */
    @Input() labelSize = '';
 
    /**
     * Label of Input
     * @type {string}
     */
    @Input() label = '';
 
    /**
     * Class of an Icon placed Before Input. Example usage:
     *
     * ```html
     * <tl-input [iconBefore]="'ion-email'"></tl-input>
     * ```
     *
     * ```html
     * <tl-input [iconBefore]="'fa fa-user'"></tl-input>
     * ```
     */
    @Input() iconBefore = '';
 
    /**
     * Class of an Icon placed After Input.
     *
     * ```html
     * <tl-input [iconAfter]="'ion-email'"></tl-input>
     * ```
     *
     * ```html
     * <tl-input [iconAfter]="'fa fa-address-card'"></tl-input>
     * ```
     */
    @Input() iconAfter = '';
 
    /**
     * Controller to display clear button after type something [default] = false.
     * @type {boolean}
     */
    @Input() clearButton: boolean;
 
    /**
     * Controller to make an input readonly.
     * @type {boolean}
     */
    @Input() readonly: boolean = null;
 
    /**
     * Controller to make an input disabled.
     * @type {boolean}
     */
    @Input() disabled: boolean = null;
 
    /**
     * Property to control autocomplete input
     */
    @Input() autocomplete: boolean = null;
 
 
    /**
     * Property required.
     * @type {boolean}
     */
    @Input() required = false;
 
    /**
     * TabIndex of Element;
     */
    @Input() tabindex = 0;
 
    /**
     * Define the alignment of the text inside of the input.
     */
    @Input() textAlign;
 
    /**
     * The element itself to be manipulated
     */
    @ViewChild( 'input' ) public input;
 
    /**
     * View for textAfter
     */
    @ViewChild('afterText') public textClearButton;
 
    /**
     * View for textAfter
     */
    @ViewChild('afterIcon') public iconClearButton;
 
    /**
     * Output event for clear button
     * @type {EventEmitter}
     */
    @Output() clear: EventEmitter<any> = new EventEmitter();
 
    /**
     * Control the position of the clearButton.
     */
    private clearButtonPosition;
 
    /**
     * Constructor
     */
    constructor(tabIndexService: TabIndexService, idService: IdGeneratorService, nameService: NameGeneratorService) {
        super(tabIndexService, idService, nameService);
    }
 
    /**
     * LifeHook Angular
     */
    ngAfterViewInit() {
        this.setElement( this.input, 'input' );
        this.validateClearButtonPosition();
    }
 
    validateClearButtonPosition() {
        Iif (this.textClearButton) {
            this.clearButtonPosition = this.textClearButton.nativeElement.offsetWidth + 5;
        }
        Iif (this.iconClearButton) {
            this.clearButtonPosition = this.iconClearButton.nativeElement.offsetWidth + 5;
        }
        Iif (this.textClearButton && this.iconClearButton) {
            this.clearButtonPosition =
                (this.textClearButton.nativeElement.offsetWidth + this.iconClearButton.nativeElement.offsetWidth) + 5;
        }
    }
 
    /**
     * Function to clear input value.
     */
    clearInput() {
        this.writeValue('');
        this.input.nativeElement.focus();
        this.clear.emit();
    }
 
}