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