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/19/2017.
25 import * as _ from "lodash";
26 import {DataTypesService} from "app/services/data-types-service";
27 import {PropertyModel, DataTypesMap, Component} from "app/models";
28 import {ValidationUtils, PROPERTY_DATA} from "app/utils";
30 export interface IPropertyFormBaseViewScope extends ng.IScope {
33 editForm:ng.IFormController;
35 property:PropertyModel;
37 nonPrimitiveTypes:Array<string>;
38 simpleTypes:Array<string>;
40 footerButtons:Array<any>;
41 modalPropertyFormBase:ng.ui.bootstrap.IModalServiceInstance;
42 currentPropertyIndex:number;
43 isLastProperty:boolean;
44 innerViewSrcUrl:string;
46 //Disabling filed - each child controller can change this when needed
48 isTypeSelectorDisable:boolean;
49 isDeleteDisable:boolean;
50 isNameDisable:boolean;
51 isDescriptionDisable:boolean;
52 isPropertyValueDisable:boolean;
53 isArrowsDisabled:boolean;
56 validationPattern:RegExp;
57 propertyNameValidationPattern:RegExp;
58 commentValidationPattern:RegExp;
59 numberValidationPattern:RegExp;
61 dataTypes:DataTypesMap;
69 getValidationPattern(type:string):RegExp;
72 export abstract class PropertyFormBaseView {
75 constructor(protected $scope:IPropertyFormBaseViewScope,
76 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
77 protected $injector:ng.auto.IInjectorService,
78 protected originalProperty:PropertyModel,
79 protected component:Component,
80 protected filteredProperties:Array<PropertyModel>,
81 protected DataTypesService:DataTypesService) {
87 protected validationUtils:ValidationUtils;
89 protected isPropertyValueOwner = ():boolean => {
90 return this.component.isService() || !!this.component.selectedInstance;
93 private isDisable = ():boolean => {
94 return this.isPropertyValueOwner() || this.$scope.property.readonly;
98 //This is the difault state, Childs screens can change if needed
99 protected initButtonsState = ():void => {
100 let isDisable = this.isDisable();
102 this.$scope.isArrowsDisabled = false;
103 this.$scope.isDeleteDisable = isDisable;
104 this.$scope.isDescriptionDisable = isDisable;
105 this.$scope.isNameDisable = isDisable;
106 this.$scope.isTypeSelectorDisable = isDisable;
107 this.$scope.isPropertyValueDisable = this.$scope.property.readonly && !this.isPropertyValueOwner();
110 protected initValidations = ():void => {
112 this.$scope.validationPattern = this.$injector.get('ValidationPattern');
113 this.$scope.propertyNameValidationPattern = this.$injector.get('PropertyNameValidationPattern');
114 this.$scope.commentValidationPattern = this.$injector.get('CommentValidationPattern');
115 this.$scope.numberValidationPattern = this.$injector.get('NumberValidationPattern');
116 this.validationUtils = this.$injector.get('ValidationUtils');
119 //Functions implemented on child's scope if needed
120 abstract save(doNotCloseModal?:boolean):ng.IPromise<boolean>;
122 protected onPropertyChange():void {
125 private updatePropertyByIndex = (index:number):void => {
126 this.$scope.property = new PropertyModel(this.filteredProperties[index]);
127 this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
128 this.onPropertyChange();
131 private initScope = ():void => {
133 this.$scope.forms = {};
134 this.$scope.isLoading = false;
135 this.$scope.property = new PropertyModel(this.originalProperty); //we create a new Object so if user press cance we won't update the property
136 this.$scope.types = PROPERTY_DATA.TYPES; //All types - simple type + map + list
137 this.$scope.simpleTypes = PROPERTY_DATA.SIMPLE_TYPES; //All simple types
138 this.$scope.dataTypes = this.DataTypesService.getAllDataTypesFromModel(this.component.model); //Get all data types in service
139 this.$scope.modalPropertyFormBase = this.$uibModalInstance;
140 this.$scope.isNew = !angular.isDefined(this.$scope.property.name);
142 this.initValidations();
143 this.initButtonsState();
144 this.filteredProperties = _.sortBy(this.filteredProperties, 'name');
145 this.$scope.currentPropertyIndex = _.findIndex(this.filteredProperties, propety => propety.uniqueId == this.$scope.property.uniqueId);
146 this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
148 this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
149 return this.$scope.types.indexOf(type) == -1;
152 this.$scope.close = ():void => {
153 this.$uibModalInstance.close();
156 this.$scope.save = ():void => {
158 let onSuccess = ():void => {
159 this.$scope.isLoading = false;
161 let onFailed = ():void => {
162 this.$scope.isLoading = false;
165 this.$scope.isLoading = true;
166 this.save(true).then(onSuccess, onFailed); // Child controller implement save logic
169 // Add the done button at the footer.
170 this.$scope.footerButtons = [
171 {'name': 'Save', 'css': 'blue', 'callback': this.$scope.save},
172 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
176 this.$scope.getPrev = ():void=> {
178 let onSuccess = ():void => {
179 this.$scope.isLoading = false;
180 this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
182 let onFailed = ():void => {
183 this.$scope.isLoading = false;
186 if (!this.$scope.property.readonly) {
187 this.$scope.isLoading = true;
188 this.save(false).then(onSuccess, onFailed);
191 this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
196 this.$scope.getNext = ():void=> {
198 let onSuccess = ():void => {
199 this.$scope.isLoading = false;
200 this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
202 let onFailed = ():void => {
203 this.$scope.isLoading = false;
206 if (!this.$scope.property.readonly) {
207 this.$scope.isLoading = true;
208 this.save(false).then(onSuccess, onFailed);
210 this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
216 this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
217 this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
221 this.$scope.getValidationPattern = (type:string):RegExp => {
222 return this.validationUtils.getValidationPattern(type);