Sync Integ to Master
[sdc.git] / catalog-ui / src / app / directives / property-types / data-type-fields-structure / data-type-fields-structure.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /**
22  * Created by obarda on 1/27/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {DataTypesService} from "app/services";
27 import { ValidationUtils } from "app/utils";
28 import { DataTypePropertyModel } from "app/models/data-type-properties";
29 import { DataTypesMap} from "app/models";
30
31 export interface IDataTypeFieldsStructureScope extends ng.IScope {
32     parentFormObj:ng.IFormController;
33     dataTypeProperties:Array<DataTypePropertyModel>;
34     typeName:string;
35     valueObjRef:any;
36     propertyNameValidationPattern:RegExp;
37     fieldsPrefixName:string;
38     readOnly:boolean;
39     currentTypeDefaultValue:any;
40     types:DataTypesMap;
41     expandByDefault:boolean;
42     expand:boolean;
43     expanded:boolean;
44     dataTypesService:DataTypesService;
45
46     expandAndCollapse():void;
47     getValidationPattern(type:string):RegExp;
48     validateIntRange(value:string):boolean;
49     onValueChange(propertyName:string, type:string):void
50     inputOnValueChange(property:any):void;
51 }
52
53
54 export class DataTypeFieldsStructureDirective implements ng.IDirective {
55
56     constructor(private DataTypesService:DataTypesService,
57                 private PropertyNameValidationPattern:RegExp,
58                 private ValidationUtils:ValidationUtils) {
59     }
60
61     scope = {
62         valueObjRef: '=',
63         typeName: '=',
64         parentFormObj: '=',
65         fieldsPrefixName: '=',
66         readOnly: '=',
67         defaultValue: '@',
68         //     types: '=',
69         expandByDefault: '='
70     };
71
72     restrict = 'E';
73     replace = true;
74     template = ():string => {
75         return require('./data-type-fields-structure.html');
76     };
77     //public types=Utils.Constants.PROPERTY_DATA.TYPES;
78
79     //get data type properties array and return object with the properties and their default value
80     //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
81     //              return: {prop1: 1, prop2: "bla bla"}
82     private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
83         let defaultValue = {};
84         for (let i = 0; i < dataTypeProperties.length; i++) {
85             if (dataTypeProperties[i].type != 'string') {
86                 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
87                     defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
88                 }
89             } else {
90                 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
91             }
92         }
93         return defaultValue;
94     };
95
96     private initDataOnScope = (scope:any, $attr:any):void => {
97         scope.dataTypesService = this.DataTypesService;
98         scope.dataTypeProperties = this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName);
99         if ($attr.defaultValue) {
100             scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
101         } else {
102             scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
103         }
104
105         if (!scope.valueObjRef) {
106             scope.valueObjRef = {};
107         }
108
109         _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
110             if (angular.isUndefined(scope.valueObjRef[key])) {
111                 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
112                     angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
113                 } else {
114                     scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
115                 }
116             }
117         });
118     };
119
120     private rerender = (scope:any):void => {
121         scope.expanded = false;
122         scope.expand = false;
123         if (scope.expandByDefault) {
124             scope.expandAndCollapse();
125         }
126     };
127
128     link = (scope:IDataTypeFieldsStructureScope, element:any, $attr:any) => {
129         scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
130
131         scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
132             this.rerender(scope);
133         });
134
135
136         scope.expandAndCollapse = ():void => {
137             if (!scope.expanded) {
138                 this.initDataOnScope(scope, $attr);
139                 scope.expanded = true;
140             }
141             scope.expand = !scope.expand;
142         };
143
144         scope.getValidationPattern = (type:string):RegExp => {
145             return this.ValidationUtils.getValidationPattern(type);
146         };
147
148         scope.validateIntRange = (value:string):boolean => {
149             return !value || this.ValidationUtils.validateIntRange(value);
150         };
151
152         scope.onValueChange = (propertyName:string, type:string):void => {
153             scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
154             if (scope.valueObjRef[propertyName] && type != 'string') {
155                 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
156             }
157         };
158
159         scope.inputOnValueChange = (property:any) => {
160
161             let value = !scope.parentFormObj[scope.fieldsPrefixName + property.name].$error.pattern
162                 && ('integer' == property.type && scope.parentFormObj[scope.fieldsPrefixName + property.name].$setValidity('pattern', scope.validateIntRange(scope.valueObjRef[property.name]))
163                 || scope.onValueChange(property.name, (property.simpleType || property.type)));
164             return value;
165         }
166     };
167
168     public static factory = (DataTypesService:DataTypesService,
169                              PropertyNameValidationPattern:RegExp,
170                              ValidationUtils:ValidationUtils)=> {
171         return new DataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
172     };
173 }
174
175 DataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];