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;
47 expandAndCollapse():void;
48 getValidationPattern(type:string):RegExp;
49 validateIntRange(value:string):boolean;
50 onValueChange(propertyName:string, type:string):void
51 inputOnValueChange(property:any):void;
55 export class DataTypeFieldsStructureDirective implements ng.IDirective {
59 constructor(private DataTypesService:DataTypesService,
60 private PropertyNameValidationPattern:RegExp,
61 private ValidationUtils:ValidationUtils) {
64 constraints: string[];
70 fieldsPrefixName: '=',
80 template = ():string => {
81 return require('./data-type-fields-structure.html');
83 //public types=Utils.Constants.PROPERTY_DATA.TYPES;
85 //get data type properties array and return object with the properties and their default value
86 //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
87 // return: {prop1: 1, prop2: "bla bla"}
88 private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
89 let defaultValue = {};
90 for (let i = 0; i < dataTypeProperties.length; i++) {
91 if (dataTypeProperties[i].type != 'string') {
92 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
93 defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
96 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
103 private initDataOnScope = (scope:any, $attr:any):void => {
104 scope.dataTypesService = this.DataTypesService;
105 scope.dataTypeProperties = this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName);
106 if ($attr.defaultValue) {
107 scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
109 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
112 if (!scope.valueObjRef) {
113 scope.valueObjRef = {};
116 _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
117 if (angular.isUndefined(scope.valueObjRef[key])) {
118 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
119 angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
121 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
127 private rerender = (scope:any):void => {
128 scope.expanded = false;
129 scope.expand = false;
130 if (scope.expandByDefault) {
131 scope.expandAndCollapse();
135 link = (scope:IDataTypeFieldsStructureScope, element:any, $attr:any) => {
136 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
138 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
139 this.rerender(scope);
143 scope.expandAndCollapse = ():void => {
144 if (!scope.expanded) {
145 this.initDataOnScope(scope, $attr);
146 scope.expanded = true;
148 scope.expand = !scope.expand;
151 scope.getValidationPattern = (type:string):RegExp => {
152 return this.ValidationUtils.getValidationPattern(type);
155 scope.validateIntRange = (value:string):boolean => {
156 return !value || this.ValidationUtils.validateIntRange(value);
159 scope.onValueChange = (propertyName:string, type:string,):void => {
160 scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
161 if (scope.valueObjRef[propertyName] && type != 'string') {
162 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
166 scope.inputOnValueChange = (property:any) => {
167 if (property.constraints){
168 // this.constraints = property.constraints[0].validValues;
171 let value = !scope.parentFormObj[scope.fieldsPrefixName + property.name].$error.pattern
172 && ('integer' == property.type && scope.parentFormObj[scope.fieldsPrefixName + property.name].$setValidity('pattern', scope.validateIntRange(scope.valueObjRef[property.name]))
173 || scope.onValueChange(property.name, (property.simpleType || property.type)));
178 public static factory = (DataTypesService:DataTypesService,
179 PropertyNameValidationPattern:RegExp,
180 ValidationUtils:ValidationUtils)=> {
181 return new DataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
185 DataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];