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 {ValidationUtils} from "app/utils";
27 import { DataTypesService } from "app/services";
28 import { DataTypePropertyModel } from "app/models/data-type-properties";
29 import {DataTypesMap, PropertyModel} from "app/models";
31 export interface ISelectDataTypeFieldsStructureScope 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 isParentAlreadyInput:boolean;
48 expandAndCollapse():void;
49 getValidationPattern(type:string):RegExp;
50 validateIntRange(value:string):boolean;
51 isAlreadyInput(property:PropertyModel):boolean;
52 setSelectedType(property:PropertyModel):void;
53 onValueChange(propertyName:string, type:string):void;
57 export class SelectDataTypeFieldsStructureDirective implements ng.IDirective {
59 constructor(private DataTypesService:DataTypesService,
60 private PropertyNameValidationPattern:RegExp,
61 private ValidationUtils:ValidationUtils) {
68 fieldsPrefixName: '=',
73 isParentAlreadyInput: '='
78 template = ():string => {
79 return require('./select-data-type-fields-structure.html');
81 // public types=Utils.Constants.PROPERTY_DATA.TYPES;
83 //get data type properties array and return object with the properties and their default value
84 //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
85 // return: {prop1: 1, prop2: "bla bla"}
86 private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
87 let defaultValue = {};
88 for (let i = 0; i < dataTypeProperties.length; i++) {
89 if (dataTypeProperties[i].type != 'string') {
90 if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
91 defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
94 defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
100 private initDataOnScope = (scope:ISelectDataTypeFieldsStructureScope, $attr:any):void => {
101 scope.dataTypesService = this.DataTypesService;
102 scope.dataTypeProperties = angular.copy(this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName));
103 if ($attr.defaultValue) {
104 scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
106 scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
109 if (!scope.valueObjRef) {
110 scope.valueObjRef = {};
113 _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
114 if (angular.isUndefined(scope.valueObjRef[key])) {
115 if (typeof scope.currentTypeDefaultValue[key] == 'object') {
116 angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
118 scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
124 private rerender = (scope:any):void => {
125 scope.expanded = false;
126 scope.expand = false;
127 if (scope.expandByDefault) {
128 scope.expandAndCollapse();
132 link = (scope:ISelectDataTypeFieldsStructureScope, element:any, $attr:any) => {
133 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
135 scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
136 this.rerender(scope);
140 scope.expandAndCollapse = ():void => {
141 if (!scope.expanded) {
142 this.initDataOnScope(scope, $attr);
143 scope.expanded = true;
145 scope.expand = !scope.expand;
148 scope.getValidationPattern = (type:string):RegExp => {
149 return this.ValidationUtils.getValidationPattern(type);
152 scope.validateIntRange = (value:string):boolean => {
153 return !value || this.ValidationUtils.validateIntRange(value);
157 check if property is alrady declered on the service by meatching the input name & the property name
160 scope.isAlreadyInput = (property:PropertyModel):boolean => {
162 if (scope.isParentAlreadyInput) {
165 let parentInputName = this.DataTypesService.selectedInstance.normalizedName + '_' + scope.path.replace('#', '_');// set the input parent as he need to declared as input
166 let inputName = parentInputName + '_' + property.name;// set the input name as he need to declared as input
167 let selectedProperty = _.find(this.DataTypesService.selectedComponentInputs, (componentInput)=> {
168 if (componentInput.name == parentInputName) { //check if the parent(all the complex) is already declared
169 scope.isParentAlreadyInput = true;
171 } else if (componentInput.name.substring(0, inputName.length) == inputName) { //check if specific property inside the complex
174 //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
176 if (selectedProperty) {
183 scope.setSelectedType = (property:PropertyModel):void=> {
184 scope.dataTypesService.selectedInput = property;
185 scope.dataTypesService.selectedPropertiesName = scope.path + '#' + property.name;
188 scope.onValueChange = (propertyName:string, type:string):void => {
189 scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
190 if (scope.valueObjRef[propertyName] && type != 'string') {
191 scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
198 public static factory = (DataTypesService:DataTypesService,
199 PropertyNameValidationPattern:RegExp,
200 ValidationUtils:ValidationUtils)=> {
201 return new SelectDataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
205 SelectDataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];