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