2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import { Component, Compiler, EventEmitter, ViewContainerRef, ViewChild, Input, Output, ElementRef, ComponentRef, ComponentFactory, ComponentFactoryResolver } from '@angular/core'
22 import { UiElementCheckBoxComponent } from './elements-ui/checkbox/ui-element-checkbox.component';
23 import { UiElementDropDownComponent, DropdownValue } from './elements-ui/dropdown/ui-element-dropdown.component';
24 import { UiElementInputComponent } from './elements-ui/input/ui-element-input.component';
25 import {UiElementPopoverInputComponent} from "./elements-ui/popover-input/ui-element-popover-input.component";
26 import {ValidationConfiguration} from "app/models";
27 import {UiElementIntegerInputComponent} from "./elements-ui/integer-input/ui-element-integer-input.component";
30 selector: 'dynamic-element',
31 template: `<div #target></div>`,
32 styleUrls: ['./dynamic-element.component.less'],
34 UiElementInputComponent,
35 UiElementDropDownComponent,
36 UiElementCheckBoxComponent,
37 UiElementPopoverInputComponent,
38 UiElementIntegerInputComponent
41 export class DynamicElementComponent {
43 @ViewChild('target', { read: ViewContainerRef }) target: any;
45 @Input() name: string;
46 @Input() readonly:boolean;
47 @Input() path:string;//optional param. used only for for subnetpoolid type
50 // Two way binding for value (need to write the "Change" word like this)
51 @Output('valueChange') emitter: EventEmitter<string> = new EventEmitter<any>();
52 @Input('value') set setValueValue(value) {
56 cmpRef: ComponentRef<any>;
57 private isViewInitialized: boolean = false;
58 validation = ValidationConfiguration.validation;
61 private componentFactoryResolver: ComponentFactoryResolver,
62 private compiler: Compiler,
63 private el: ElementRef) {
67 if (!this.isViewInitialized) {
71 this.cmpRef.destroy();
74 // Factory to create component based on type or peroperty name.
78 this.createComponent(UiElementIntegerInputComponent);
79 this.cmpRef.instance.pattern = this.validation.validationPatterns.integer;
82 if (this.path && this.path.toUpperCase().indexOf("SUBNETPOOLID") !== -1) {
83 if(this.name.toUpperCase().indexOf("SUBNETPOOLID") == -1){//if it's an item of subnetpoolid list get the parent name
84 let pathArray = this.path.split("#");
85 this.name = pathArray[pathArray.length - 2];
87 this.createComponent(UiElementPopoverInputComponent);
90 this.createComponent(UiElementInputComponent);
94 //this.createComponent(UiElementCheckBoxComponent);
96 this.createComponent(UiElementDropDownComponent);
98 // Build drop down values
100 tmp.push(new DropdownValue('true','TRUE'));
101 tmp.push(new DropdownValue('false','FALSE'));
102 this.cmpRef.instance.values = tmp;
105 this.createComponent(UiElementInputComponent);
106 console.log("ERROR: No ui component to handle type: " + this.type);
109 // Additional attributes in base element class
111 this.cmpRef.instance.name = this.name;
112 this.cmpRef.instance.type = this.type;
113 this.cmpRef.instance.value = this.value;
114 this.cmpRef.instance.readonly = this.readonly;
117 // Subscribe to change event of of ui-element-basic and fire event to change the value
118 this.cmpRef.instance.baseEmitter.subscribe((value):void => {
119 this.emitter.emit(value)
124 createComponent(ComponentToCreate:any):void {
125 let factory = this.componentFactoryResolver.resolveComponentFactory(ComponentToCreate);
126 this.cmpRef = this.target.createComponent(factory);
130 this.updateComponent();
133 ngAfterContentInit() {
134 //console.log("DynamicElementComponent: ngAfterContentInit: type: " + this.type + " value: " + this.value);
135 this.isViewInitialized = true;
136 this.updateComponent();
141 this.cmpRef.destroy();