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 * as _ from "lodash";
26 import {DataTypesService} from "app/services";
27 import { ValidationUtils } from "app/utils";
28 import { DataTypePropertyModel } from "app/models/data-type-properties";
29 import { DataTypesMap} from "app/models";
31 export interface IDataTypeFieldsStructureScope extends ng.IScope {
32 parentFormObj:ng.IFormController;
33 dataTypeProperties:Array<DataTypePropertyModel>;
36 propertyNameValidationPattern:RegExp;
37 fieldsPrefixName:string;
39 currentTypeDefaultValue:any;
41 expandByDefault:boolean;
44 dataTypesService:DataTypesService;
46 expandAndCollapse():void;
47 getValidationPattern(type:string):RegExp;
48 validateIntRange(value:string):boolean;
49 onValueChange(propertyName:string, type:string):void
50 inputOnValueChange(property:any):void;
54 export class DataTypeFieldsStructureDirective implements ng.IDirective {
56 constructor(private DataTypesService:DataTypesService,
57 private PropertyNameValidationPattern:RegExp,
58 private ValidationUtils:ValidationUtils) {
65 fieldsPrefixName: '=',
74 template = ():string => {
75 return require('./data-type-fields-structure.html');
77 //public types=Utils.Constants.PROPERTY_DATA.TYPES;
79 //get data type properties array and return object with the properties and their default value
80 //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
81 // return: {prop1: 1, prop2: "bla bla"}
82 private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
83 let defaultValue = {};
84 for (let i = 0; i < dataTypeProperties.length; i++) {
85 if (dataTypeProperties[i].type != 'string') {
86 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
87 defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
90 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
96 private initDataOnScope = (scope:any, $attr:any):void => {
97 scope.dataTypesService = this.DataTypesService;
98 scope.dataTypeProperties = this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName);
99 if ($attr.defaultValue) {
100 scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
102 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
105 if (!scope.valueObjRef) {
106 scope.valueObjRef = {};
109 _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
110 if (angular.isUndefined(scope.valueObjRef[key])) {
111 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
112 angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
114 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
120 private rerender = (scope:any):void => {
121 scope.expanded = false;
122 scope.expand = false;
123 if (scope.expandByDefault) {
124 scope.expandAndCollapse();
128 link = (scope:IDataTypeFieldsStructureScope, element:any, $attr:any) => {
129 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
131 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
132 this.rerender(scope);
136 scope.expandAndCollapse = ():void => {
137 if (!scope.expanded) {
138 this.initDataOnScope(scope, $attr);
139 scope.expanded = true;
141 scope.expand = !scope.expand;
144 scope.getValidationPattern = (type:string):RegExp => {
145 return this.ValidationUtils.getValidationPattern(type);
148 scope.validateIntRange = (value:string):boolean => {
149 return !value || this.ValidationUtils.validateIntRange(value);
152 scope.onValueChange = (propertyName:string, type:string):void => {
153 scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
154 if (scope.valueObjRef[propertyName] && type != 'string') {
155 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
159 scope.inputOnValueChange = (property:any) => {
161 let value = !scope.parentFormObj[scope.fieldsPrefixName + property.name].$error.pattern
162 && ('integer' == property.type && scope.parentFormObj[scope.fieldsPrefixName + property.name].$setValidity('pattern', scope.validateIntRange(scope.valueObjRef[property.name]))
163 || scope.onValueChange(property.name, (property.simpleType || property.type)));
168 public static factory = (DataTypesService:DataTypesService,
169 PropertyNameValidationPattern:RegExp,
170 ValidationUtils:ValidationUtils)=> {
171 return new DataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
175 DataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];