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/18/2017.
25 import * as _ from "lodash";
26 import {PropertyModel, DisplayModule, Component, Resource, Service, ComponentInstance} from "app/models";
27 import {UNIQUE_GROUP_PROPERTIES_NAME} from "app/utils";
28 import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
29 import {DataTypesService} from "app/services/data-types-service";
31 export interface IModulePropertyViewScope extends IPropertyFormBaseViewScope {
35 export class ModulePropertyView extends PropertyFormBaseView {
45 'Sdc.Services.DataTypesService',
49 constructor(protected $scope:IModulePropertyViewScope,
50 protected $templateCache:ng.ITemplateCacheService,
51 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
52 protected $injector:ng.auto.IInjectorService,
53 protected originalProperty:PropertyModel,
54 protected component:Component,
55 private selectedModule:DisplayModule,
56 protected DataTypesService:DataTypesService,
57 private $q:ng.IQService) {
58 super($scope, $uibModalInstance, $injector, originalProperty, component, selectedModule.properties, DataTypesService);
60 this.$templateCache.put("module-property-view.html", require('app/view-models/forms/property-forms/module-property-modal/module-property-view.html'));
61 this.$scope.innerViewSrcUrl = "module-property-view.html";
62 this.initChildScope();
65 private findPropertyByName = (propertyName:string):PropertyModel => {
66 let property:PropertyModel = _.find(this.filteredProperties, (property:PropertyModel) => {
67 return property.name === propertyName;
72 save(isNeedToCloseModal):ng.IPromise<boolean> {
74 let deferred = this.$q.defer();
76 let onSuccess = (properties:Array<PropertyModel>):void => {
77 deferred.resolve(true);
78 if (isNeedToCloseModal === true) {
83 let onFailed = ():void => {
84 deferred.resolve(false);
87 let property = _.find(this.selectedModule.properties, (property) => {
88 return property.uniqueId === this.$scope.property.uniqueId;
91 if (!property.readonly && property.value !== this.$scope.property.value) {
92 if (this.component.isResource()) {
93 (<Resource>this.component).updateResourceGroupProperties(this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
95 if (this.component.isService()) {
96 // Find the component instance of the group instance
97 let componentInstance:ComponentInstance = _.find(this.component.componentInstances, (componentInstance:ComponentInstance) => {
98 let groupInstance = _.find(componentInstance.groupInstances, {uniqueId: this.selectedModule.groupInstanceUniqueId});
99 return groupInstance !== undefined;
102 (<Service>this.component).updateGroupInstanceProperties(componentInstance.uniqueId, this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
105 deferred.resolve(true);
106 if (isNeedToCloseModal === true) {
107 this.$uibModalInstance.close();
111 return deferred.promise;
114 onPropertyChange():void {
115 this.initValidation();
118 protected initValidation = ():void => {
120 this.$scope.isDeleteDisable = true;
121 this.$scope.isNameDisable = true;
122 this.$scope.isTypeSelectorDisable = true;
123 this.$scope.isDescriptionDisable = true;
125 switch (this.$scope.property.name) {
126 case UNIQUE_GROUP_PROPERTIES_NAME.IS_BASE:
127 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_TYPE:
128 case UNIQUE_GROUP_PROPERTIES_NAME.VOLUME_GROUP:
129 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_LABEL:
130 this.$scope.property.readonly = true;
132 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_DESCRIPTION:
133 if (this.component.isService()) {
134 this.$scope.property.readonly = true;
136 this.$scope.property.readonly = false;
142 private isUniqueProperty = ():boolean => {
143 return this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES ||
144 this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES ||
145 this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT;
149 private initChildScope = ():void => {
151 this.initValidation();
153 this.$scope.clearValidationError = ():void => {
154 this.$scope.forms.editForm.$valid = true;
155 this.$scope.forms.editForm.$invalid = false;
156 this.$scope.forms.editForm['value'].$error = {};
157 this.$scope.forms.editForm.$error = {};
160 // put default value when instance value is empty
161 this.$scope.onValueChange = ():void => {
163 if (!this.$scope.property.value) { // Resetting to default value
164 if (this.isPropertyValueOwner()) {
165 if (this.component.isService()) {
166 this.$scope.property.value = this.$scope.property.parentValue;
168 this.$scope.property.value = this.$scope.property.defaultValue;
173 if (this.isUniqueProperty()) {
176 let maxProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES);
177 let minProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES);
178 let initialCountProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT);
180 let maxPropertyValue = parseInt(maxProperty.value);
181 let minPropertyValue = parseInt(minProperty.value);
182 let initialCountPropertyValue = parseInt(initialCountProperty.value);
183 let propertyValue = parseInt(this.$scope.property.value);
184 let parentPropertyValue = parseInt(this.$scope.property.parentValue);
186 switch (this.$scope.property.name) {
188 case UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES:
189 if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
190 isValid = propertyValue <= initialCountPropertyValue;
193 isValid = propertyValue && (propertyValue <= maxPropertyValue && propertyValue <= initialCountPropertyValue);
195 this.$scope.forms.editForm["value"].$setValidity('maxValidation', isValid);
196 if (this.component.isService()) {
197 if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
200 isValid = propertyValue >= parentPropertyValue;
201 this.$scope.forms.editForm["value"].$setValidity('minValidationVfLevel', isValid);
205 case UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES:
206 if (isNaN(minPropertyValue) || minPropertyValue == null) {
207 isValid = propertyValue >= initialCountPropertyValue;
209 isValid = isNaN(propertyValue) || (propertyValue >= minPropertyValue && propertyValue >= initialCountPropertyValue);
211 this.$scope.forms.editForm["value"].$setValidity('minValidation', isValid);
212 if (this.component.isService()) {
213 if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
217 isValid = propertyValue <= parentPropertyValue;
218 this.$scope.forms.editForm["value"].$setValidity('maxValidationVfLevel', isValid);
222 case UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT:
223 if ((isNaN(minPropertyValue) || minPropertyValue == null) && (isNaN(maxPropertyValue) || maxPropertyValue == null)) {
225 } else if (isNaN(minPropertyValue) || minPropertyValue == null) {
226 isValid = propertyValue <= maxPropertyValue;
227 } else if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
228 isValid = propertyValue >= minPropertyValue;
230 isValid = minPropertyValue <= propertyValue && propertyValue <= maxPropertyValue;
232 this.$scope.forms.editForm["value"].$setValidity('minOrMaxValidation', isValid);