import { Component, OnInit, Input, Output, ElementRef, HostBinding, EventEmitter, forwardRef } from '@angular/core'; import { CfCoreComponent } from '../core/core.component'; import { IconModel } from '../../models/icon/icon.model'; import { InputModel } from '../../models/input/input.model'; import { InputStylingModel } from '../../models/input/input-styling.model'; import { MenuItemModel } from '../../models/menu/menu-item.model'; import { InputTemplates } from '../../templates/input.template'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS,FormControl } from '@angular/forms'; /** *

The input is created based on the inputClass (see /models/input).

*

Example of using component:

*
 * <cf-input [settings]="input"></cf-input>
 * 
*/ /* export function validateCfInput(c: FormControl) { let err = { rangeError: { given: c.value } }; return (c.value == null)? err: null; } */ @Component({ selector: 'cf-input', template: "
{{cfInput.hint.text}} {{cfInput.prefix}} {{cfInput.suffix}}
", styles: [":host { position: relative; z-index: 0; } :host.notification-clickable cf-notification:hover { cursor: pointer; } .mat-input-container { font-family: 'Avenir', Helvetica, sans-serif; } "], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CfInputComponent), multi: true } ] }) export class CfInputComponent extends CfCoreComponent implements OnInit,ControlValueAccessor { writeValue(obj: any): void { if (obj) { this.value = obj; } } registerOnChange(fn: any): void { this.onChange = (value) => { Promise.resolve(null).then(() => fn(value)) }; } registerOnTouched(fn: any): void { this.onTouched = (value) => { Promise.resolve(null).then(() => fn(value)) }; } setDisabledState(isDisabled: boolean): void { } onChange: any = () => { }; onTouched: any = () => { }; @HostBinding('class') notifacationClickable = ''; /** * It is Input object which will recive values provided by InputClass Class and after that md-input will be builded on page. */ cfInput: InputModel; private val: string; /** *

The properties objject of the input.

*/ @Input() public properties: InputModel; /** * It is function for creating Notification click events by using Core Component notificationAction event emmiter. * @param notification it is json notification object */ cfNotificationAction(notification) { this.notificationAction.emit(notification); } /** *

The styling object of the input.

*/ @Input() public styling: InputStylingModel; /** *

Disable the input.

*/ @Input() disable: boolean; /** *

Display or hide the input.

*/ @Input() display: boolean; /** *

Type of the input if text or password.

*/ @Input() type: string; /** *

The placeholder of the input.

*/ @Input() placeholder: string; /** *

The hind text under the input.

*/ @Input() hintText: string; /** *

The value inside the input (double bound).

*/ @Input() public get value() { return this.val; } /** *

The icon name of the input if exists.

*/ @Input() iconName: string; /** *

The event that will be called when the value is changed.

*/ @Output() valueChange = new EventEmitter(); public set value(value) { this.val = value; this.cfInput.value = value; this.valueChange.emit(this.val); this.onChange(value); this.onTouched(); } constructor(public elementRef: ElementRef) { super(elementRef); } ngOnInit() { super.ngOnInit(); if(this.notificationAction.observers.length > 0) this.notifacationClickable = 'notification-clickable'; this.activeTemplate = InputTemplates["defaultTemplate"]; if(this.compTemplate!=null && this.compTemplate!="") { if(InputTemplates[this.compTemplate]!=null) { this.activeTemplate = InputTemplates[this.compTemplate]; } else this.activeTemplate = this.compTemplate; } if(this.properties==null) { this.properties = new InputModel(this.activeTemplate["property"]); } else { var mainKeys = Object.keys(this.activeTemplate["property"]); for(let key of mainKeys) { if(this.properties[key]==null && key!="iconProperty") this.properties[key]=this.activeTemplate["property"][key]; } } if(this.styling==null) this.styling = new InputStylingModel (this.activeTemplate["style"]); if(this.disable!=null) this.properties.disable = this.disable; if(this.display!=null) this.properties.display = this.display; if(this.value!=null) this.properties.value = this.value; if(this.type!=null) this.properties.type = this.type; if(this.placeholder!=null) this.properties.placeholder = this.placeholder; if(this.hintText!=null) { if(this.properties.hint==null) this.properties.hint = { text: "", align:"end"}; this.properties.hint.text = this.hintText; } if(this.iconName!=null) { if(this.properties.iconProperty==null) this.properties.iconProperty = new IconModel(); this.properties.iconProperty.name = this.iconName; if(this.iconName.startsWith("fa-")) this.properties.iconProperty.type = "fa"; else this.properties.iconProperty.type = "mi"; } this.cfInput = this.properties; } /** *

Update the properties object with hte latest inputs on change to keep the component dynaic.

* @param changes */ ngOnChanges(changes: any) { if(this.properties!=null) { if(changes.disable!=null) this.properties.disable = changes.disable.currentValue; if(changes.display!=null) this.properties.display = changes.display.currentValue; if(changes.value!=null) this.properties.value = changes.value.currentValue; if(changes.type!=null) this.properties.type = changes.type.currentValue; if(changes.placeholder!=null) this.properties.placeholder = changes.placeholder.currentValue; if(changes.hintText!=null) { if(this.properties.hint==null) this.properties.hint = { text: "", align:"end"}; this.properties.hint.text = changes.hintText.currentValue; } if(changes.iconName!=null) { if(this.properties.iconProperty==null) this.properties.iconProperty = new IconModel(); this.properties.iconProperty.name = changes.iconName.currentValue; if(changes.iconName.currentValue.startsWith("fa-")) this.properties.iconProperty.type = "fa"; else this.properties.iconProperty.type = "mi"; } } } }