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 rcohen on 9/15/2016.
25 import {SchemaProperty} from "app/models";
26 import {ValidationUtils, PROPERTY_TYPES} from "app/utils";
27 import {DataTypesService} from "app/services";
29 export interface ITypeListScope extends ng.IScope {
30 parentFormObj:ng.IFormController;
31 schemaProperty:SchemaProperty;
32 isSchemaTypeDataType:boolean;
34 propertyNameValidationPattern:RegExp;
35 fieldsPrefixName:string;
43 getValidationPattern(type:string):RegExp;
44 validateIntRange(value:string):boolean;
46 deleteListItem(listItemIndex:number):void
50 export class TypeListDirective implements ng.IDirective {
52 constructor(private DataTypesService:DataTypesService,
53 private PropertyNameValidationPattern:RegExp,
54 private ValidationUtils:ValidationUtils) {
58 valueObjRef: '=',//ref to list object in the parent value object
59 schemaProperty: '=',//get the schema.property object
60 parentFormObj: '=',//ref to parent form (get angular form object)
61 fieldsPrefixName: '=',//prefix for form fields names
62 readOnly: '=',//is form read only
63 defaultValue: '@',//this list default value
70 template = ():string => {
71 return require('./type-list-directive.html');
74 link = (scope:ITypeListScope, element:any, $attr:any) => {
75 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
77 //reset valueObjRef when schema type is changed
78 scope.$watchCollection('schemaProperty.type', (newData:any):void => {
79 scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
80 //insert 1 empty item dt by default
81 if (scope.isSchemaTypeDataType && (!scope.valueObjRef || !scope.valueObjRef.length)) {
82 scope.valueObjRef = scope.valueObjRef || [];
83 scope.valueObjRef.push({});
87 //when user brows between properties in "edit property form"
88 scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
89 scope.listNewItem = {value: ''};
91 if ($attr.defaultValue) {
92 scope.listDefaultValue = JSON.parse($attr.defaultValue);
96 scope.getValidationPattern = (type:string):RegExp => {
97 return this.ValidationUtils.getValidationPattern(type);
100 scope.validateIntRange = (value:string):boolean => {
101 return !value || this.ValidationUtils.validateIntRange(value);
104 scope.addListItem = ():void => {
105 scope.valueObjRef = scope.valueObjRef || [];
106 let newVal = ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING ? scope.listNewItem.value : JSON.parse(scope.listNewItem.value));
107 scope.valueObjRef.push(newVal);
108 scope.listNewItem.value = "";
111 scope.deleteListItem = (listItemIndex:number):void => {
112 scope.valueObjRef.splice(listItemIndex, 1);
113 if (!scope.valueObjRef.length) {
114 if (scope.listDefaultValue) {
115 angular.copy(scope.listDefaultValue, scope.valueObjRef);
121 public static factory = (DataTypesService:DataTypesService,
122 PropertyNameValidationPattern:RegExp,
123 ValidationUtils:ValidationUtils)=> {
124 return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
128 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];