Update base types based on model
[sdc.git] / catalog-ui / src / app / view-models / forms / property-forms / base-property-form / property-form-base-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /**
22  * Created by obarda on 1/19/2017.
23  */
24 'use strict';
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";
29
30 export interface IPropertyFormBaseViewScope extends ng.IScope {
31
32     forms:any;
33     editForm:ng.IFormController;
34
35     property:PropertyModel;
36     types:Array<string>;
37     nonPrimitiveTypes:Array<string>;
38     simpleTypes:Array<string>;
39
40     footerButtons:Array<any>;
41     modalPropertyFormBase:ng.ui.bootstrap.IModalServiceInstance;
42     currentPropertyIndex:number;
43     isLastProperty:boolean;
44     innerViewSrcUrl:string;
45
46     //Disabling filed - each child controller can change this when needed
47     isNew:boolean;
48     isTypeSelectorDisable:boolean;
49     isDeleteDisable:boolean;
50     isNameDisable:boolean;
51     isDescriptionDisable:boolean;
52     isPropertyValueDisable:boolean;
53     isArrowsDisabled:boolean;
54
55     //Validation pattern
56     validationPattern:RegExp;
57     propertyNameValidationPattern:RegExp;
58     commentValidationPattern:RegExp;
59     numberValidationPattern:RegExp;
60
61     dataTypes:DataTypesMap;
62
63     isLoading:boolean;
64
65     save():void;
66     close():void;
67     getNext():void;
68     getPrev():void;
69     getValidationPattern(type:string):RegExp;
70 }
71
72 export abstract class PropertyFormBaseView {
73
74
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) {
82
83         this.initScope();
84
85     }
86
87     protected validationUtils:ValidationUtils;
88
89     protected isPropertyValueOwner = ():boolean => {
90         return this.component.isService() || !!this.component.selectedInstance;
91     };
92
93     private isDisable = ():boolean => {
94         return this.isPropertyValueOwner() || this.$scope.property.readonly;
95     };
96
97
98     //This is the difault state, Childs screens can change if needed
99     protected initButtonsState = ():void => {
100         let isDisable = this.isDisable();
101
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();
108     };
109
110     protected initValidations = ():void => {
111
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');
117     };
118
119     //Functions implemented on child's scope if needed
120     abstract save(doNotCloseModal?:boolean):ng.IPromise<boolean>;
121
122     protected onPropertyChange():void {
123     };
124
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();
129     };
130
131     private initScope = ():void => {
132
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);
141
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);
147
148         this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
149             return this.$scope.types.indexOf(type) == -1;
150         });
151
152         this.$scope.close = ():void => {
153             this.$uibModalInstance.close();
154         };
155
156         this.$scope.save = ():void => {
157
158             let onSuccess = ():void => {
159                 this.$scope.isLoading = false;
160             };
161             let onFailed = ():void => {
162                 this.$scope.isLoading = false;
163             };
164
165             this.$scope.isLoading = true;
166             this.save(true).then(onSuccess, onFailed); // Child controller implement save logic
167         };
168
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}
173         ];
174
175
176         this.$scope.getPrev = ():void=> {
177
178             let onSuccess = ():void => {
179                 this.$scope.isLoading = false;
180                 this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
181             };
182             let onFailed = ():void => {
183                 this.$scope.isLoading = false;
184             };
185
186             if (!this.$scope.property.readonly) {
187                 this.$scope.isLoading = true;
188                 this.save(false).then(onSuccess, onFailed);
189
190             } else {
191                 this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
192             }
193
194         };
195
196         this.$scope.getNext = ():void=> {
197
198             let onSuccess = ():void => {
199                 this.$scope.isLoading = false;
200                 this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
201             };
202             let onFailed = ():void => {
203                 this.$scope.isLoading = false;
204             };
205
206             if (!this.$scope.property.readonly) {
207                 this.$scope.isLoading = true;
208                 this.save(false).then(onSuccess, onFailed);
209             } else {
210                 this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
211             }
212
213         };
214
215
216         this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
217             this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
218         });
219
220
221         this.$scope.getValidationPattern = (type:string):RegExp => {
222             return this.validationUtils.getValidationPattern(type);
223         };
224     }
225 }