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=========================================================
21 * Created by obarda on 1/27/2016.
23 /// <reference path="../../../references"/>
24 module Sdc.Directives {
27 export interface IDataTypeFieldsStructureScope extends ng.IScope {
28 parentFormObj:ng.IFormController;
29 dataTypeProperties:Array<Models.DataTypePropertyModel>;
32 propertyNameValidationPattern: RegExp;
33 fieldsPrefixName:string;
35 currentTypeDefaultValue:any;
36 types:Models.DataTypesMap;
37 expandByDefault:boolean;
40 dataTypesService:Sdc.Services.DataTypesService;
42 expandAndCollapse():void;
43 getValidationPattern(type:string):RegExp;
44 validateIntRange(value:string):boolean;
45 onValueChange(propertyName:string, type:string):void
49 export class DataTypeFieldsStructureDirective implements ng.IDirective {
51 constructor(private $templateCache:ng.ITemplateCacheService,
52 private DataTypesService:Sdc.Services.DataTypesService,
53 private PropertyNameValidationPattern: RegExp,
54 private ValidationUtils:Sdc.Utils.ValidationUtils) {
61 fieldsPrefixName: '=',
70 template = ():string => {
71 return this.$templateCache.get('/app/scripts/directives/property-types/data-type-fields-structure/data-type-fields-structure.html');
73 public types=Utils.Constants.PROPERTY_DATA.TYPES;
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);
86 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
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);
98 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
101 if(!scope.valueObjRef) {
102 scope.valueObjRef = {};
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]);
110 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
116 private rerender = (scope:any):void =>{
117 scope.expanded = false;
118 scope.expand = false;
119 if(scope.expandByDefault){
120 scope.expandAndCollapse();
124 link = (scope:IDataTypeFieldsStructureScope, element:any, $attr:any) => {
125 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
127 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
128 this.rerender(scope);
132 scope.expandAndCollapse = ():void => {
134 this.initDataOnScope(scope,$attr);
137 scope.expand=!scope.expand;
140 scope.getValidationPattern = (type:string):RegExp => {
141 return this.ValidationUtils.getValidationPattern(type);
144 scope.validateIntRange = (value:string):boolean => {
145 return !value || this.ValidationUtils.validateIntRange(value);
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]);
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);
164 DataTypeFieldsStructureDirective.factory.$inject = ['$templateCache','Sdc.Services.DataTypesService','PropertyNameValidationPattern','ValidationUtils'];