UNPKG

smart-webcomponents

Version:

Web Components & Custom Elements for Professional Web Applications

296 lines (256 loc) 9.56 kB
import { Form } from './../index'; import { ControlControlType, ControlLabelPosition, ControlViewMode, FormLabelPosition, Control, ElementRenderMode} from './../index'; import { Component, Directive, AfterViewInit, ElementRef, Input, OnInit, OnChanges, OnDestroy, SimpleChanges, Output, EventEmitter, QueryList, ContentChildren } from '@angular/core'; import { BaseElement, Smart } from './smart.element'; export { ControlControlType, ControlLabelPosition, ControlViewMode, FormLabelPosition, Control, ElementRenderMode} from './../index'; export { Smart } from './smart.element'; export { Form } from './../index'; import { FormControlComponent } from './smart.formcontrol'; import { FormGroupComponent } from './smart.formgroup'; @Directive({ exportAs: 'smart-form', selector: 'smart-form, [smart-form]' }) export class FormComponent extends BaseElement implements OnInit, AfterViewInit, OnDestroy, OnChanges { constructor(ref: ElementRef<Form>) { super(ref); this.nativeElement = ref.nativeElement as Form; } private eventHandlers: any[] = []; public declare nativeElement: Form; /** @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 = <Form>document.createElement('smart-form'); 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 or gets the form controls. */ @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(): FormLabelPosition | string { return this.nativeElement ? this.nativeElement.labelPosition : undefined; } set labelPosition(value: FormLabelPosition | 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 the Form's status. Each member in the status has { dirty, untouched, disabled } properties. */ @Input() get showColonAfterLabel(): boolean { return this.nativeElement ? this.nativeElement.showColonAfterLabel : undefined; } set showColonAfterLabel(value: boolean) { this.nativeElement ? this.nativeElement.showColonAfterLabel = value : undefined; } /** @description Gets or Sets the Form value. */ @Input() get showSummary(): boolean { return this.nativeElement ? this.nativeElement.showSummary : undefined; } set showSummary(value: boolean) { this.nativeElement ? this.nativeElement.showSummary = value : undefined; } /** @description Automatically validates the form when it is created. */ @Input() get status(): any { return this.nativeElement ? this.nativeElement.status : undefined; } set status(value: any) { this.nativeElement ? this.nativeElement.status = 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 undefined */ @Input() get validateOnLoad(): boolean { return this.nativeElement ? this.nativeElement.validateOnLoad : undefined; } set validateOnLoad(value: boolean) { this.nativeElement ? this.nativeElement.validateOnLoad = 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); }); } } /** @description Submits the form. * @param {any} submitOptions?. Sets the submit options object. The object may have the following properties: <em>async</em>, <em>action</em>, <em>target</em>, <em>method</em>. <em>async</em> determines whether the form will be submitted asynchronously. <em>action</em> determines the submit url, <em>method</em> sets whether the submit is through 'GET' or 'POST'. <em>target</em> determines the submit target. */ public submit(submitOptions?: any): void { if (this.nativeElement.isRendered) { this.nativeElement.submit(submitOptions); } else { this.nativeElement.whenRendered(() => { this.nativeElement.submit(submitOptions); }); } } /** @description Clears the form. */ public reset(): void { if (this.nativeElement.isRendered) { this.nativeElement.reset(); } else { this.nativeElement.whenRendered(() => { this.nativeElement.reset(); }); } } /** @description Validates the form. */ public validate(): void { if (this.nativeElement.isRendered) { this.nativeElement.validate(); } else { this.nativeElement.whenRendered(() => { this.nativeElement.validate(); }); } } get isRendered(): boolean { return this.nativeElement ? this.nativeElement.isRendered : false; } ngOnInit() { } ngAfterViewInit() { const that = this; that.onCreate.emit(that.nativeElement); if (Smart) Smart.Render(); 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; } }