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=========================================================
22 * Created by obarda on 1/27/2016.
25 import { DataTypesMap, PropertyModel } from 'app/models';
26 import { DataTypePropertyModel } from 'app/models/data-type-properties';
27 import { DataTypesService } from 'app/services';
28 import { ValidationUtils } from 'app/utils';
29 import {InstanceFeDetails} from "app/models/instance-fe-details";
30 import * as _ from 'lodash';
32 export interface IDataTypeFieldsStructureScope extends ng.IScope {
33 parentFormObj: ng.IFormController;
34 dataTypeProperties: DataTypePropertyModel[];
35 parentProperty:PropertyModel;
36 componentInstanceMap: Map<string, InstanceFeDetails>;
39 propertyNameValidationPattern: RegExp;
40 fieldsPrefixName: string;
42 currentTypeDefaultValue: any;
44 expandByDefault: boolean;
47 dataTypesService: DataTypesService;
48 constraints: string[];
51 expandAndCollapse(): void;
52 getValidationPattern(type: string): RegExp;
53 validateIntRange(value: string): boolean;
54 onValueChange(propertyName: string, type: string): void;
55 inputOnValueChange(property: any): void;
58 export class DataTypeFieldsStructureDirective implements ng.IDirective {
60 constraints: string[];
65 componentInstanceMap: '=',
68 fieldsPrefixName: '=',
79 constructor(private DataTypesService: DataTypesService,
80 private PropertyNameValidationPattern: RegExp,
81 private ValidationUtils: ValidationUtils) {
84 public static factory = (DataTypesService: DataTypesService,
85 PropertyNameValidationPattern: RegExp,
86 ValidationUtils: ValidationUtils) => {
87 return new DataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
89 template = (): string => {
90 return require('./data-type-fields-structure.html');
93 link = (scope: IDataTypeFieldsStructureScope, element: any, $attr: any) => {
94 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
96 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData: any): void => {
100 scope.expandAndCollapse = (): void => {
101 if (!scope.expanded) {
102 this.initDataOnScope(scope, $attr);
103 scope.expanded = true;
105 scope.expand = !scope.expand;
108 scope.getValidationPattern = (type: string): RegExp => {
109 return this.ValidationUtils.getValidationPattern(type);
112 scope.validateIntRange = (value: string): boolean => {
113 return !value || this.ValidationUtils.validateIntRange(value);
116 scope.onValueChange = (propertyName: string, type: string, ): void => {
117 scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
118 if (scope.valueObjRef[propertyName] && type != 'string') {
119 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
123 scope.inputOnValueChange = (property: any) => {
124 if (property.constraints) {
125 // this.constraints = property.constraints[0].validValues;
128 const value = !scope.parentFormObj[scope.fieldsPrefixName + property.name].$error.pattern
129 && ('integer' == property.type && scope.parentFormObj[scope.fieldsPrefixName + property.name].$setValidity('pattern', scope.validateIntRange(scope.valueObjRef[property.name]))
130 || scope.onValueChange(property.name, (property.simpleType || property.type)));
134 // public types=Utils.Constants.PROPERTY_DATA.TYPES;
136 // get data type properties array and return object with the properties and their default value
137 // (for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
138 // return: {prop1: 1, prop2: "bla bla"}
139 private getDefaultValue = (dataTypeProperties: DataTypePropertyModel[]): any => {
140 const defaultValue = {};
141 for (const element of dataTypeProperties) {
142 if (element.type != 'string') {
143 if (!angular.isUndefined(element.defaultValue)) {
144 defaultValue[element.name] = JSON.parse(element.defaultValue);
147 defaultValue[element.name] = element.defaultValue;
153 private initDataOnScope = (scope: any, $attr: any): void => {
154 scope.dataTypesService = this.DataTypesService;
155 scope.dataTypeProperties = this.getDataTypeProperties(scope.typeName, scope.types);
156 if ($attr.defaultValue) {
157 scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
159 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
162 if (!scope.valueObjRef) {
163 scope.valueObjRef = {};
166 _.forEach(scope.currentTypeDefaultValue, (value, key) => {
167 if (angular.isUndefined(scope.valueObjRef[key])) {
168 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
169 angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
171 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
177 private getDataTypeProperties = (dataTypeName: string, typesInModel: DataTypesMap): DataTypePropertyModel[] => {
178 let properties = typesInModel[dataTypeName].properties || [];
179 if (typesInModel[dataTypeName].derivedFromName != 'tosca.datatypes.Root') {
180 properties = this.getDataTypeProperties(typesInModel[dataTypeName].derivedFromName, typesInModel).concat(properties);
185 private rerender = (scope: any): void => {
186 scope.expanded = false;
187 scope.expand = false;
188 if (scope.expandByDefault) {
189 scope.expandAndCollapse();
194 DataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];