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 {ValidationUtils} from "app/utils";
26 import { DataTypesService } from "app/services";
27 import { DataTypePropertyModel } from "app/models/data-type-properties";
28 import {DataTypesMap, PropertyModel} from "app/models";
30 export interface ISelectDataTypeFieldsStructureScope 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 isParentAlreadyInput:boolean;
47 expandAndCollapse():void;
48 getValidationPattern(type:string):RegExp;
49 validateIntRange(value:string):boolean;
50 isAlreadyInput(property:PropertyModel):boolean;
51 setSelectedType(property:PropertyModel):void;
52 onValueChange(propertyName:string, type:string):void;
56 export class SelectDataTypeFieldsStructureDirective implements ng.IDirective {
58 constructor(private DataTypesService:DataTypesService,
59 private PropertyNameValidationPattern:RegExp,
60 private ValidationUtils:ValidationUtils) {
67 fieldsPrefixName: '=',
72 isParentAlreadyInput: '='
77 template = ():string => {
78 return require('./select-data-type-fields-structure.html');
80 // public types=Utils.Constants.PROPERTY_DATA.TYPES;
82 //get data type properties array and return object with the properties and their default value
83 //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
84 // return: {prop1: 1, prop2: "bla bla"}
85 private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
86 let defaultValue = {};
87 for (let i = 0; i < dataTypeProperties.length; i++) {
88 if (dataTypeProperties[i].type != 'string') {
89 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
90 defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
93 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
99 private initDataOnScope = (scope:ISelectDataTypeFieldsStructureScope, $attr:any):void => {
100 scope.dataTypesService = this.DataTypesService;
101 scope.dataTypeProperties = angular.copy(this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName));
102 if ($attr.defaultValue) {
103 scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
105 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
108 if (!scope.valueObjRef) {
109 scope.valueObjRef = {};
112 _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
113 if (angular.isUndefined(scope.valueObjRef[key])) {
114 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
115 angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
117 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
123 private rerender = (scope:any):void => {
124 scope.expanded = false;
125 scope.expand = false;
126 if (scope.expandByDefault) {
127 scope.expandAndCollapse();
131 link = (scope:ISelectDataTypeFieldsStructureScope, element:any, $attr:any) => {
132 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
134 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
135 this.rerender(scope);
139 scope.expandAndCollapse = ():void => {
140 if (!scope.expanded) {
141 this.initDataOnScope(scope, $attr);
142 scope.expanded = true;
144 scope.expand = !scope.expand;
147 scope.getValidationPattern = (type:string):RegExp => {
148 return this.ValidationUtils.getValidationPattern(type);
151 scope.validateIntRange = (value:string):boolean => {
152 return !value || this.ValidationUtils.validateIntRange(value);
156 check if property is alrady declered on the service by meatching the input name & the property name
159 scope.isAlreadyInput = (property:PropertyModel):boolean => {
161 if (scope.isParentAlreadyInput) {
164 let parentInputName = this.DataTypesService.selectedInstance.normalizedName + '_' + scope.path.replace('#', '_');// set the input parent as he need to declared as input
165 let inputName = parentInputName + '_' + property.name;// set the input name as he need to declared as input
166 let selectedProperty = _.find(this.DataTypesService.selectedComponentInputs, (componentInput)=> {
167 if (componentInput.name == parentInputName) { //check if the parent(all the complex) is already declared
168 scope.isParentAlreadyInput = true;
170 } else if (componentInput.name.substring(0, inputName.length) == inputName) { //check if specific property inside the complex
173 //return componentInput.name == parentInputName || componentInput.name.substring(0,inputName.length) == inputName;//check if the parent(all the complex) is already declared or specific property inside the complex
175 if (selectedProperty) {
182 scope.setSelectedType = (property:PropertyModel):void=> {
183 scope.dataTypesService.selectedInput = property;
184 scope.dataTypesService.selectedPropertiesName = scope.path + '#' + property.name;
187 scope.onValueChange = (propertyName:string, type:string):void => {
188 scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
189 if (scope.valueObjRef[propertyName] && type != 'string') {
190 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
197 public static factory = (DataTypesService:DataTypesService,
198 PropertyNameValidationPattern:RegExp,
199 ValidationUtils:ValidationUtils)=> {
200 return new SelectDataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
204 SelectDataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];