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=========================================================
22 * Created by obarda on 1/27/2016.
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";
30 export interface IDataTypeFieldsStructureScope extends ng.IScope {
31 parentFormObj:ng.IFormController;
32 dataTypeProperties:Array<DataTypePropertyModel>;
35 propertyNameValidationPattern:RegExp;
36 fieldsPrefixName:string;
38 currentTypeDefaultValue:any;
40 expandByDefault:boolean;
43 dataTypesService:DataTypesService;
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;
53 export class DataTypeFieldsStructureDirective implements ng.IDirective {
55 constructor(private DataTypesService:DataTypesService,
56 private PropertyNameValidationPattern:RegExp,
57 private ValidationUtils:ValidationUtils) {
64 fieldsPrefixName: '=',
73 template = ():string => {
74 return require('./data-type-fields-structure.html');
76 //public types=Utils.Constants.PROPERTY_DATA.TYPES;
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);
89 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
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);
101 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
104 if (!scope.valueObjRef) {
105 scope.valueObjRef = {};
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]);
113 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
119 private rerender = (scope:any):void => {
120 scope.expanded = false;
121 scope.expand = false;
122 if (scope.expandByDefault) {
123 scope.expandAndCollapse();
127 link = (scope:IDataTypeFieldsStructureScope, element:any, $attr:any) => {
128 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
130 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
131 this.rerender(scope);
135 scope.expandAndCollapse = ():void => {
136 if (!scope.expanded) {
137 this.initDataOnScope(scope, $attr);
138 scope.expanded = true;
140 scope.expand = !scope.expand;
143 scope.getValidationPattern = (type:string):RegExp => {
144 return this.ValidationUtils.getValidationPattern(type);
147 scope.validateIntRange = (value:string):boolean => {
148 return !value || this.ValidationUtils.validateIntRange(value);
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]);
158 scope.inputOnValueChange = (property:any) => {
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)));
167 public static factory = (DataTypesService:DataTypesService,
168 PropertyNameValidationPattern:RegExp,
169 ValidationUtils:ValidationUtils)=> {
170 return new DataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
174 DataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];