a8bcf3f1550c0037beb06605fb625c65b1fee620
[sdc.git] /
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 {ValidationUtils} from "app/utils";
26 import { DataTypesService } from "app/services";
27 import { DataTypePropertyModel } from "app/models/data-type-properties";
28 import {DataTypesMap, PropertyModel} from "app/models";
29
30 export interface ISelectDataTypeFieldsStructureScope 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     path:string;
45     isParentAlreadyInput:boolean;
46
47     expandAndCollapse():void;
48     getValidationPattern(type:string):RegExp;
49     validateIntRange(value:string):boolean;
50     isAlreadyInput(property:PropertyModel):boolean;
51     setSelectedType(property:PropertyModel):void;
52     onValueChange(propertyName:string, type:string):void;
53 }
54
55
56 export class SelectDataTypeFieldsStructureDirective implements ng.IDirective {
57
58     constructor(private DataTypesService:DataTypesService,
59                 private PropertyNameValidationPattern:RegExp,
60                 private ValidationUtils:ValidationUtils) {
61     }
62
63     scope = {
64         valueObjRef: '=',
65         typeName: '=',
66         parentFormObj: '=',
67         fieldsPrefixName: '=',
68         readOnly: '=',
69         defaultValue: '@',
70         expandByDefault: '=',
71         path: '@',
72         isParentAlreadyInput: '='
73     };
74
75     restrict = 'E';
76     replace = true;
77     template = ():string => {
78         return require('./select-data-type-fields-structure.html');
79     };
80     // public types=Utils.Constants.PROPERTY_DATA.TYPES;
81
82     //get data type properties array and return object with the properties and their default value
83     //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
84     //              return: {prop1: 1, prop2: "bla bla"}
85     private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
86         let defaultValue = {};
87         for (let i = 0; i < dataTypeProperties.length; i++) {
88             if (dataTypeProperties[i].type != 'string') {
89                 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
90                     defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
91                 }
92             } else {
93                 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
94             }
95         }
96         return defaultValue;
97     };
98
99     private initDataOnScope = (scope:ISelectDataTypeFieldsStructureScope, $attr:any):void => {
100         scope.dataTypesService = this.DataTypesService;
101         scope.dataTypeProperties = angular.copy(this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName));
102         if ($attr.defaultValue) {
103             scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
104         } else {
105             scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
106         }
107
108         if (!scope.valueObjRef) {
109             scope.valueObjRef = {};
110         }
111
112         _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
113             if (angular.isUndefined(scope.valueObjRef[key])) {
114                 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
115                     angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
116                 } else {
117                     scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
118                 }
119             }
120         });
121     };
122
123     private rerender = (scope:any):void => {
124         scope.expanded = false;
125         scope.expand = false;
126         if (scope.expandByDefault) {
127             scope.expandAndCollapse();
128         }
129     };
130
131     link = (scope:ISelectDataTypeFieldsStructureScope, element:any, $attr:any) => {
132         scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
133
134         scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
135             this.rerender(scope);
136         });
137
138
139         scope.expandAndCollapse = ():void => {
140             if (!scope.expanded) {
141                 this.initDataOnScope(scope, $attr);
142                 scope.expanded = true;
143             }
144             scope.expand = !scope.expand;
145         };
146
147         scope.getValidationPattern = (type:string):RegExp => {
148             return this.ValidationUtils.getValidationPattern(type);
149         };
150
151         scope.validateIntRange = (value:string):boolean => {
152             return !value || this.ValidationUtils.validateIntRange(value);
153         };
154
155         /*
156          check if property is alrady declered on the service by meatching the input name & the property name
157
158          */
159         scope.isAlreadyInput = (property:PropertyModel):boolean => {
160             if (scope.path) {
161                 if (scope.isParentAlreadyInput) {
162                     return true;
163                 }
164                 let parentInputName = this.DataTypesService.selectedInstance.normalizedName + '_' + scope.path.replace('#', '_');// set the input parent  as he need to declared as input
165                 let inputName = parentInputName + '_' + property.name;// set the input name as he need to declared as input
166                 let selectedProperty = _.find(this.DataTypesService.selectedComponentInputs, (componentInput)=> {
167                     if (componentInput.name == parentInputName) { //check if the parent(all the complex) is already declared
168                         scope.isParentAlreadyInput = true;
169                         return true;
170                     } else if (componentInput.name.substring(0, inputName.length) == inputName) { //check if specific property inside the complex
171                         return true;
172                     }
173                     //return componentInput.name == parentInputName || componentInput.name.substring(0,inputName.length) == inputName;//check if the parent(all the complex) is already declared or specific property inside the complex
174                 });
175                 if (selectedProperty) {
176                     return true;
177                 }
178             }
179             return false;
180         };
181
182         scope.setSelectedType = (property:PropertyModel):void=> {
183             scope.dataTypesService.selectedInput = property;
184             scope.dataTypesService.selectedPropertiesName = scope.path + '#' + property.name;
185         };
186
187         scope.onValueChange = (propertyName:string, type:string):void => {
188             scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
189             if (scope.valueObjRef[propertyName] && type != 'string') {
190                 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
191             }
192         };
193
194
195     };
196
197     public static factory = (DataTypesService:DataTypesService,
198                              PropertyNameValidationPattern:RegExp,
199                              ValidationUtils:ValidationUtils)=> {
200         return new SelectDataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
201     };
202 }
203
204 SelectDataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];