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;
27 @Input() path:string;//optional param. used only for for subnetpoolid type
30 // Two way binding for value (need to write the "Change" word like this)
31 @Output('valueChange') emitter: EventEmitter<string> = new EventEmitter<any>();
32 @Input('value') set setValueValue(value) {
36 cmpRef: ComponentRef<any>;
37 private isViewInitialized: boolean = false;
38 validation = ValidationConfiguration.validation;
41 private componentFactoryResolver: ComponentFactoryResolver,
42 private compiler: Compiler,
43 private el: ElementRef) {
47 if (!this.isViewInitialized) {
51 this.cmpRef.destroy();
54 // Factory to create component based on type or peroperty name.
58 this.createComponent(UiElementIntegerInputComponent);
59 this.cmpRef.instance.pattern = this.validation.validationPatterns.integer;
62 if (this.path && this.path.toUpperCase().indexOf("SUBNETPOOLID") !== -1) {
63 if(this.name.toUpperCase().indexOf("SUBNETPOOLID") == -1){//if it's an item of subnetpoolid list get the parent name
64 let pathArray = this.path.split("#");
65 this.name = pathArray[pathArray.length - 2];
67 this.createComponent(UiElementPopoverInputComponent);
70 this.createComponent(UiElementInputComponent);
74 //this.createComponent(UiElementCheckBoxComponent);
76 this.createComponent(UiElementDropDownComponent);
78 // Build drop down values
80 tmp.push(new DropdownValue('true','TRUE'));
81 tmp.push(new DropdownValue('false','FALSE'));
82 this.cmpRef.instance.values = tmp;
85 this.createComponent(UiElementInputComponent);
86 console.log("ERROR: No ui component to handle type: " + this.type);
89 // Additional attributes in base element class
91 this.cmpRef.instance.name = this.name;
92 this.cmpRef.instance.type = this.type;
93 this.cmpRef.instance.value = this.value;
94 this.cmpRef.instance.readonly = this.readonly;
97 // Subscribe to change event of of ui-element-basic and fire event to change the value
98 this.cmpRef.instance.baseEmitter.subscribe((value):void => {
99 this.emitter.emit(value)
104 createComponent(ComponentToCreate:any):void {
105 let factory = this.componentFactoryResolver.resolveComponentFactory(ComponentToCreate);
106 this.cmpRef = this.target.createComponent(factory);
110 this.updateComponent();
113 ngAfterContentInit() {
114 //console.log("DynamicElementComponent: ngAfterContentInit: type: " + this.type + " value: " + this.value);
115 this.isViewInitialized = true;
116 this.updateComponent();
121 this.cmpRef.destroy();