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;
40 stringSchema: SchemaProperty;
44 getValidationPattern(type:string):RegExp;
45 validateIntRange(value:string):boolean;
47 deleteListItem(listItemIndex:number):void;
48 getStringSchemaProperty():SchemaProperty;
52 export class TypeListDirective implements ng.IDirective {
54 private readonly stringSchema: SchemaProperty;
56 constructor(private DataTypesService:DataTypesService,
57 private PropertyNameValidationPattern:RegExp,
58 private ValidationUtils:ValidationUtils) {
59 this.stringSchema = new SchemaProperty();
60 this.stringSchema.type = PROPERTY_TYPES.STRING;
61 this.stringSchema.isSimpleType = true;
62 this.stringSchema.isDataType = false;
66 valueObjRef: '=',//ref to list object in the parent value object
67 schemaProperty: '=',//get the schema.property object
68 parentFormObj: '=',//ref to parent form (get angular form object)
69 fieldsPrefixName: '=',//prefix for form fields names
70 readOnly: '=',//is form read only
71 defaultValue: '@',//this list default value
78 template = ():string => {
79 return require('./type-list-directive.html');
82 link = (scope:ITypeListScope, element:any, $attr:any) => {
83 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
84 scope.stringSchema = this.stringSchema;
86 //reset valueObjRef when schema type is changed
87 scope.$watchCollection('schemaProperty.type', (newData:any):void => {
88 scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
91 //when user brows between properties in "edit property form"
92 scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
93 scope.listNewItem = {value: ''};
95 if ($attr.defaultValue) {
96 scope.listDefaultValue = JSON.parse($attr.defaultValue);
100 scope.getValidationPattern = (type:string):RegExp => {
101 return this.ValidationUtils.getValidationPattern(type);
104 scope.validateIntRange = (value:string):boolean => {
105 return !value || this.ValidationUtils.validateIntRange(value);
108 scope.addListItem = ():void => {
109 scope.valueObjRef = scope.valueObjRef || [];
111 if (scope.schemaProperty.type === PROPERTY_TYPES.MAP) {
113 } else if ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING) {
114 newVal = scope.listNewItem.value;
116 newVal = JSON.parse(scope.listNewItem.value);
118 scope.valueObjRef.push(newVal);
119 scope.listNewItem.value = "";
122 scope.deleteListItem = (listItemIndex: number): void => {
123 scope.valueObjRef.splice(listItemIndex, 1);
124 if (!scope.valueObjRef.length && scope.listDefaultValue) {
125 angular.copy(scope.listDefaultValue, scope.valueObjRef);
130 public static factory = (DataTypesService:DataTypesService,
131 PropertyNameValidationPattern:RegExp,
132 ValidationUtils:ValidationUtils)=> {
133 return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
137 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];