1 import { Component, Compiler, EventEmitter, ViewContainerRef, ViewChild, Input, Output, ElementRef, ComponentRef, ComponentFactory, ComponentFactoryResolver } from '@angular/core'
2 import { UiElementCheckBoxComponent } from './elements-ui/checkbox/ui-element-checkbox.component';
3 import { UiElementDropDownComponent, DropdownValue } from './elements-ui/dropdown/ui-element-dropdown.component';
4 import { UiElementInputComponent } from './elements-ui/input/ui-element-input.component';
5 import {UiElementPopoverInputComponent} from "./elements-ui/popover-input/ui-element-popover-input.component";
6 import {ValidationConfiguration} from "app/models";
7 import {UiElementIntegerInputComponent} from "./elements-ui/integer-input/ui-element-integer-input.component";
10 selector: 'dynamic-element',
11 template: `<div #target></div>`,
12 styleUrls: ['./dynamic-element.component.less'],
14 UiElementInputComponent,
15 UiElementDropDownComponent,
16 UiElementCheckBoxComponent,
17 UiElementPopoverInputComponent,
18 UiElementIntegerInputComponent
21 export class DynamicElementComponent {
23 @ViewChild('target', { read: ViewContainerRef }) target: any;
25 @Input() name: string;
26 @Input() readonly:boolean;
29 // Two way binding for value (need to write the "Change" word like this)
30 @Output('valueChange') emitter: EventEmitter<string> = new EventEmitter<any>();
31 @Input('value') set setValueValue(value) {
35 cmpRef: ComponentRef<any>;
36 private isViewInitialized: boolean = false;
37 validation = ValidationConfiguration.validation;
40 private componentFactoryResolver: ComponentFactoryResolver,
41 private compiler: Compiler,
42 private el: ElementRef) {
46 if (!this.isViewInitialized) {
50 this.cmpRef.destroy();
53 // Factory to create component based on type or peroperty name.
57 this.createComponent(UiElementIntegerInputComponent);
58 this.cmpRef.instance.pattern = this.validation.validationPatterns.integer;
61 if (this.name.toUpperCase().indexOf("SUBNETPOOLID") !== -1) {
62 this.createComponent(UiElementPopoverInputComponent);
65 this.createComponent(UiElementInputComponent);
69 //this.createComponent(UiElementCheckBoxComponent);
71 this.createComponent(UiElementDropDownComponent);
73 // Build drop down values
75 tmp.push(new DropdownValue('true','TRUE'));
76 tmp.push(new DropdownValue('false','FALSE'));
77 this.cmpRef.instance.values = tmp;
80 this.createComponent(UiElementInputComponent);
81 console.log("ERROR: No ui component to handle type: " + this.type);
84 // Additional attributes in base element class
86 this.cmpRef.instance.name = this.name;
87 this.cmpRef.instance.type = this.type;
88 this.cmpRef.instance.value = this.value;
89 this.cmpRef.instance.readonly = this.readonly;
92 // Subscribe to change event of of ui-element-basic and fire event to change the value
93 this.cmpRef.instance.baseEmitter.subscribe((value):void => {
94 this.emitter.emit(value)
99 createComponent(ComponentToCreate:any):void {
100 let factory = this.componentFactoryResolver.resolveComponentFactory(ComponentToCreate);
101 this.cmpRef = this.target.createComponent(factory);
105 this.updateComponent();
108 ngAfterContentInit() {
109 //console.log("DynamicElementComponent: ngAfterContentInit: type: " + this.type + " value: " + this.value);
110 this.isViewInitialized = true;
111 this.updateComponent();
116 this.cmpRef.destroy();