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 import * as _ from "lodash";
23 import {AttributeModel, Component} from "app/models";
24 import {IMapRegex, ValidationUtils, FormState, PROPERTY_TYPES} from "app/utils";
26 export interface IEditAttributeModel {
27 attribute:AttributeModel;
29 simpleTypes:Array<string>;
32 export class attributeValue {//in order to solve DE226783, we update the value on another obj
36 interface IAttributeFormViewModelScope extends ng.IScope {
39 editForm:ng.IFormController;
40 footerButtons:Array<any>;
42 editAttributeModel:IEditAttributeModel;
43 modalInstanceAttribute:ng.ui.bootstrap.IModalServiceInstance;
47 propertyNameValidationPattern:RegExp;
48 commentValidationPattern:RegExp;
50 validationPattern:RegExp;
51 attributeValue:attributeValue;
57 isAttributeValueOwner():boolean;
58 validateIntRange(value:string):boolean;
59 validateUniqueKeys(viewValue:string):boolean;
60 getValidationTranslate():string;
62 isSchemaEditable():boolean;
66 export class AttributeFormViewModel {
73 'CommentValidationPattern',
74 'PropertyNameValidationPattern',
78 private formState:FormState;
81 constructor(private $scope:IAttributeFormViewModelScope,
82 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
83 private attribute:AttributeModel,
84 private ValidationUtils:ValidationUtils,
85 private CommentValidationPattern:RegExp,
86 private PropertyNameValidationPattern:RegExp,
87 private component:Component) {
88 this.formState = angular.isDefined(attribute.name) ? FormState.UPDATE : FormState.CREATE;
92 private initResource = ():void => {
93 this.$scope.editAttributeModel.attribute = new AttributeModel(this.attribute);
94 if (this.$scope.editAttributeModel.types.indexOf(this.attribute.type) === -1) {//attribute defaulte type is string too?
95 this.attribute.type = "string";
99 private initEditAttributeModel = ():void => {
100 this.$scope.editAttributeModel = {
102 types: ['integer', 'string', 'float', 'boolean', 'list', 'map'],
103 simpleTypes: ['integer', 'string', 'float', 'boolean']
109 private initScope = ():void => {
112 this.$scope.forms = {};
113 this.$scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
114 this.$scope.commentValidationPattern = this.CommentValidationPattern;
116 this.$scope.modalInstanceAttribute = this.$uibModalInstance;
117 this.$scope.listRegex = this.ValidationUtils.getPropertyListPatterns();
118 this.$scope.mapRegex = this.ValidationUtils.getPropertyMapPatterns();
120 this.$scope.isNew = (this.formState === FormState.CREATE);
121 this.$scope.isLoading = false;
122 this.$scope.attributeValue = new attributeValue();
124 this.initEditAttributeModel();
125 this.setValidationPattern();
128 this.$scope.save = ():void => {
129 if (!this.$scope.forms.editForm.$invalid) {
130 let attribute:AttributeModel = this.$scope.editAttributeModel.attribute;
131 this.$scope.editAttributeModel.attribute.description = this.ValidationUtils.stripAndSanitize(this.$scope.editAttributeModel.attribute.description);
132 ////if read only - just closes the modal
133 if (this.$scope.editAttributeModel.attribute.readonly && !this.$scope.isAttributeValueOwner()) {
134 this.$uibModalInstance.close();
137 this.$scope.isLoading = true;
138 let onAttributeFaild = (response):void => {
139 console.info('onFaild', response);
140 this.$scope.isLoading = false;
143 let onAttributeSuccess = (attributeFromBE:AttributeModel):void => {
144 console.info('onAttributeResourceSuccess : ', attributeFromBE);
145 this.$scope.isLoading = false;
146 this.$uibModalInstance.close();
149 //in case we have uniqueId we call update method
150 if (this.$scope.isAttributeValueOwner()) {
151 attribute.value = this.$scope.attributeValue.value;
152 this.component.updateInstanceAttribute(attribute).then(onAttributeSuccess, onAttributeFaild);
154 attribute.defaultValue = this.$scope.attributeValue.value;
155 this.component.addOrUpdateAttribute(attribute).then(onAttributeSuccess, onAttributeFaild);
160 this.$scope.close = ():void => {
161 this.$uibModalInstance.close();
164 this.$scope.validateName = ():void => {
165 let existsAttr:AttributeModel = _.find(this.component.attributes, (attribute:AttributeModel) => {
166 return attribute.name === this.$scope.editAttributeModel.attribute.name;
169 this.$scope.forms.editForm["attributeName"].$setValidity('nameExist', false);
171 this.$scope.forms.editForm["attributeName"].$setValidity('nameExist', true);
176 this.$scope.onTypeChange = ():void => {
177 this.$scope.editAttributeModel.attribute.value = '';
178 this.$scope.editAttributeModel.attribute.defaultValue = '';
179 this.setValidationPattern();
182 this.$scope.isAttributeValueOwner = ():boolean=> {
183 return this.component.isService() || !!this.component.selectedInstance;
186 this.$scope.onValueChange = ():void => {
187 if (!this.$scope.editAttributeModel.attribute.value) {
188 if (this.$scope.isAttributeValueOwner()) {
189 this.$scope.editAttributeModel.attribute.value = this.$scope.editAttributeModel.attribute.defaultValue;
195 this.$scope.validateUniqueKeys = (viewValue:string):boolean => {
196 if (this.$scope.editAttributeModel.attribute.type === 'map') {
197 return this.ValidationUtils.validateUniqueKeys(viewValue);
200 return true; //always valid if not a map
204 this.$scope.validateIntRange = (value:string):boolean => {
205 return !value || this.ValidationUtils.validateIntRange(value);
208 this.$scope.isSchemaEditable = ():boolean => {
209 let schemaType = this.$scope.editAttributeModel.attribute.schema.property.type;
210 return this.$scope.editAttributeModel.simpleTypes.indexOf(schemaType) > -1 || !schemaType;
213 this.$scope.showSchema = ():boolean => {
214 return ['list', 'map'].indexOf(this.$scope.editAttributeModel.attribute.type) > -1;
217 this.$scope.getValidationTranslate = ():string => {
218 let result = "ATTRIBUTE_EDIT_PATTERN";
219 if (this.$scope.showSchema()) {
221 result = "ATTRIBUTE_EDIT_" + this.$scope.editAttributeModel.attribute.type.toUpperCase();
223 if (this.$scope.editAttributeModel.attribute.schema.property.type === PROPERTY_TYPES.STRING) {
225 } else if (this.$scope.editAttributeModel.attribute.schema.property.type === PROPERTY_TYPES.BOOLEAN) {
226 result += "_BOOLEAN";
228 result += "_GENERIC";
235 // Add the done button at the footer.
236 this.$scope.footerButtons = [
237 {'name': 'Done', 'css': 'blue', 'callback': this.$scope.save},
238 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
241 this.$scope.$watchCollection("forms.editForm.$invalid", (newVal, oldVal) => {
242 this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
245 this.$scope.attributeValue.value = this.$scope.isAttributeValueOwner() ? this.$scope.editAttributeModel.attribute.value : this.$scope.editAttributeModel.attribute.defaultValue;
249 private setValidationPattern = ():void => {
251 if (this.$scope.editAttributeModel.attribute.type === 'list') {
252 this.$scope.validationPattern = this.$scope.listRegex[this.$scope.editAttributeModel.attribute.schema.property.type];
254 else if (this.$scope.editAttributeModel.attribute.type === 'map') {
255 this.$scope.validationPattern = this.$scope.mapRegex[this.$scope.editAttributeModel.attribute.schema.property.type];
258 this.$scope.validationPattern = this.ValidationUtils.getValidationPattern(this.$scope.editAttributeModel.attribute.type);