UNPKG

smart-webcomponents

Version:

Web Components & Custom Elements for Professional Web Applications

246 lines (213 loc) 8.05 kB
import { FormGroup } from './../index'; import { ControlControlType, ControlLabelPosition, ControlViewMode, FormGroupLabelPosition, Control, ElementRenderMode} from './../index'; import { Component, Directive, AfterViewInit, ElementRef, Input, OnInit, OnChanges, OnDestroy, SimpleChanges, Output, EventEmitter } from '@angular/core'; import { BaseElement, Smart } from './smart.element'; export { ControlControlType, ControlLabelPosition, ControlViewMode, FormGroupLabelPosition, Control, ElementRenderMode} from './../index'; export { Smart } from './smart.element'; export { FormGroup } from './../index'; @Directive({ exportAs: 'smart-form-group', selector: 'smart-form-group, [smart-form-group]' }) export class FormGroupComponent extends BaseElement implements OnInit, AfterViewInit, OnDestroy, OnChanges { constructor(ref: ElementRef<FormGroup>) { super(ref); this.nativeElement = ref.nativeElement as FormGroup; } private eventHandlers: any[] = []; public declare nativeElement: FormGroup; /** @description Creates the component on demand. * @param properties An optional object of properties, which will be added to the template binded ones. */ public createComponent(properties = {}): any { this.nativeElement = <FormGroup>document.createElement('smart-form-group'); for (let propertyName in properties) { this.nativeElement[propertyName] = properties[propertyName]; } return this.nativeElement; } /** @description Sets or gets the form columns. */ @Input() get columns(): number { return this.nativeElement ? this.nativeElement.columns : undefined; } set columns(value: number) { this.nativeElement ? this.nativeElement.columns = value : undefined; } /** @description Sets the Form control data field. The control's inner input's name is set to the dataField value and in the FormGroup it is accessible through the dataField value. */ @Input() get dataField(): string { return this.nativeElement ? this.nativeElement.dataField : undefined; } set dataField(value: string) { this.nativeElement ? this.nativeElement.dataField = value : undefined; } /** @description Gets or Sets the Form control's label. */ @Input() get label(): string { return this.nativeElement ? this.nativeElement.label : undefined; } set label(value: string) { this.nativeElement ? this.nativeElement.label = value : undefined; } /** @description */ @Input() get controls(): Control[] { return this.nativeElement ? this.nativeElement.controls : undefined; } set controls(value: Control[]) { this.nativeElement ? this.nativeElement.controls = value : undefined; } /** @description Sets or Gets the labels position. */ @Input() get onStatusChanges(): {(value: string): void} { return this.nativeElement ? this.nativeElement.onStatusChanges : undefined; } set onStatusChanges(value: {(value: string): void}) { this.nativeElement ? this.nativeElement.onStatusChanges = value : undefined; } /** @description Makes the form readonly. */ @Input() get onValueChanges(): {(value: any): void} { return this.nativeElement ? this.nativeElement.onValueChanges : undefined; } set onValueChanges(value: {(value: any): void}) { this.nativeElement ? this.nativeElement.onValueChanges = value : undefined; } /** @description Shows / hides the colon after the labels. */ @Input() get labelPosition(): FormGroupLabelPosition | string { return this.nativeElement ? this.nativeElement.labelPosition : undefined; } set labelPosition(value: FormGroupLabelPosition | string) { this.nativeElement ? this.nativeElement.labelPosition = value : undefined; } /** @description Shows / hides validation summary. */ @Input() get readonly(): boolean { return this.nativeElement ? this.nativeElement.readonly : undefined; } set readonly(value: boolean) { this.nativeElement ? this.nativeElement.readonly = value : undefined; } /** @description Gets or Sets the Form value. */ @Input() get showColonAfterLabel(): boolean { return this.nativeElement ? this.nativeElement.showColonAfterLabel : undefined; } set showColonAfterLabel(value: boolean) { this.nativeElement ? this.nativeElement.showColonAfterLabel = value : undefined; } /** @description undefined */ @Input() get showSummary(): boolean { return this.nativeElement ? this.nativeElement.showSummary : undefined; } set showSummary(value: boolean) { this.nativeElement ? this.nativeElement.showSummary = value : undefined; } /** @description undefined */ @Input() get value(): any { return this.nativeElement ? this.nativeElement.value : undefined; } set value(value: any) { this.nativeElement ? this.nativeElement.value = value : undefined; } /** @description Adds a control to the Form. * @param {any} controlOptions. Control options. The control options description is available in the <em>controls</em> property. */ public addControl(controlOptions: any): void { if (this.nativeElement.isRendered) { this.nativeElement.addControl(controlOptions); } else { this.nativeElement.whenRendered(() => { this.nativeElement.addControl(controlOptions); }); } } /** @description Gets a control by its name(dataField). * @param {string} dataField. dataField of a FormControl or FormGroup * @returns {Control} */ public async getControlAsync(dataField): Promise<any> { const getResultOnRender = () => { return new Promise(resolve => { this.nativeElement.whenRendered(() => { const result = this.nativeElement.getControl(dataField); resolve(result) }); }); }; const result = await getResultOnRender(); return result; } public getControl(dataField): Control { if (this.nativeElement.isRendered) { return this.nativeElement.getControl(dataField); } return null; } /** @description Inserts a control to the Form. * @param {number} index. Control insert index * @param {any} controlOptions. Control options. The control options description is available in the <em>controls</em> property. */ public insertControl(index: number, controlOptions: any): void { if (this.nativeElement.isRendered) { this.nativeElement.insertControl(index, controlOptions); } else { this.nativeElement.whenRendered(() => { this.nativeElement.insertControl(index, controlOptions); }); } } /** @description Remove a control from the Form. * @param {any} controlOptions. Control options. The control options description is available in the <em>controls</em> property. */ public removeControl(controlOptions: any): void { if (this.nativeElement.isRendered) { this.nativeElement.removeControl(controlOptions); } else { this.nativeElement.whenRendered(() => { this.nativeElement.removeControl(controlOptions); }); } } get isRendered(): boolean { return this.nativeElement ? this.nativeElement.isRendered : false; } ngOnInit() { } ngAfterViewInit() { const that = this; that.onCreate.emit(that.nativeElement); this.nativeElement.classList.add('smart-angular'); if (this.nativeElement.whenRendered) this.nativeElement.whenRendered(() => { that.onReady.emit(that.nativeElement); }); this.listen(); } ngOnDestroy() { this.unlisten(); } ngOnChanges(changes: SimpleChanges) { if (this.nativeElement && this.nativeElement.isRendered) { for (const propName in changes) { if (changes.hasOwnProperty(propName)) { this.nativeElement[propName] = changes[propName].currentValue; } } } } /** @description Add event listeners. */ private listen(): void { const that = this; } /** @description Remove event listeners. */ private unlisten(): void { const that = this; } }