2 * Created by obarda on 1/18/2017.
5 import {PropertyModel, DisplayModule, Component, Resource, Service, ComponentInstance} from "app/models";
6 import {UNIQUE_GROUP_PROPERTIES_NAME} from "app/utils";
7 import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
8 import {DataTypesService} from "app/services/data-types-service";
10 export interface IModulePropertyViewScope extends IPropertyFormBaseViewScope {
14 export class ModulePropertyView extends PropertyFormBaseView {
24 'Sdc.Services.DataTypesService',
28 constructor(protected $scope:IModulePropertyViewScope,
29 protected $templateCache:ng.ITemplateCacheService,
30 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
31 protected $injector:ng.auto.IInjectorService,
32 protected originalProperty:PropertyModel,
33 protected component:Component,
34 private selectedModule:DisplayModule,
35 protected DataTypesService:DataTypesService,
36 private $q:ng.IQService) {
37 super($scope, $uibModalInstance, $injector, originalProperty, component, selectedModule.properties, DataTypesService);
39 this.$templateCache.put("module-property-view.html", require('app/view-models/forms/property-forms/module-property-modal/module-property-view.html'));
40 this.$scope.innerViewSrcUrl = "module-property-view.html";
41 this.initChildScope();
44 private findPropertyByName = (propertyName:string):PropertyModel => {
45 let property:PropertyModel = _.find(this.filteredProperties, (property:PropertyModel) => {
46 return property.name === propertyName;
51 save(isNeedToCloseModal):ng.IPromise<boolean> {
53 let deferred = this.$q.defer();
55 let onSuccess = (properties:Array<PropertyModel>):void => {
56 deferred.resolve(true);
57 if (isNeedToCloseModal === true) {
62 let onFailed = ():void => {
63 deferred.resolve(false);
66 let property = _.find(this.selectedModule.properties, (property) => {
67 return property.uniqueId === this.$scope.property.uniqueId;
69 if (property.value !== this.$scope.property.value) {
70 if (this.component.isResource()) {
71 (<Resource>this.component).updateResourceGroupProperties(this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
73 if (this.component.isService()) {
74 // Find the component instance of the group instance
75 let componentInstance:ComponentInstance = _.find(this.component.componentInstances, (componentInstance:ComponentInstance) => {
76 let groupInstance = _.find(componentInstance.groupInstances, {uniqueId: this.selectedModule.groupInstanceUniqueId});
77 return groupInstance !== undefined;
80 (<Service>this.component).updateGroupInstanceProperties(componentInstance.uniqueId, this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
83 deferred.resolve(true);
85 return deferred.promise;
88 onPropertyChange():void {
89 this.initValidation();
92 protected initValidation = ():void => {
94 this.$scope.isDeleteDisable = true;
95 this.$scope.isNameDisable = true;
96 this.$scope.isTypeSelectorDisable = true;
97 this.$scope.isDescriptionDisable = true;
99 switch (this.$scope.property.name) {
100 case UNIQUE_GROUP_PROPERTIES_NAME.IS_BASE:
101 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_TYPE:
102 case UNIQUE_GROUP_PROPERTIES_NAME.VOLUME_GROUP:
103 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_LABEL:
104 this.$scope.property.readonly = true;
106 case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_DESCRIPTION:
107 if (this.component.isService()) {
108 this.$scope.property.readonly = true;
110 this.$scope.property.readonly = false;
116 private isUniqueProperty = ():boolean => {
117 return this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES ||
118 this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES ||
119 this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT;
123 private initChildScope = ():void => {
125 this.initValidation();
127 // put default value when instance value is empty
128 this.$scope.onValueChange = ():void => {
130 if (!this.$scope.property.value) { // Resetting to default value
131 if (this.isPropertyValueOwner()) {
132 if (this.component.isService()) {
133 this.$scope.property.value = this.$scope.property.parentValue;
135 this.$scope.property.value = this.$scope.property.defaultValue;
140 if (this.isUniqueProperty()) {
143 let maxProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES);
144 let minProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES);
145 let initialCountProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT);
147 let maxPropertyValue = parseInt(maxProperty.value);
148 let minPropertyValue = parseInt(minProperty.value);
149 let initialCountPropertyValue = parseInt(initialCountProperty.value);
150 let propertyValue = parseInt(this.$scope.property.value);
151 let parentPropertyValue = parseInt(this.$scope.property.parentValue);
153 switch (this.$scope.property.name) {
155 case UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES:
156 if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
157 isValid = propertyValue <= initialCountPropertyValue;
160 isValid = propertyValue && (propertyValue <= maxPropertyValue && propertyValue <= initialCountPropertyValue);
162 this.$scope.forms.editForm["value"].$setValidity('maxValidation', isValid);
163 if (this.component.isService()) {
164 if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
167 isValid = propertyValue >= parentPropertyValue;
168 this.$scope.forms.editForm["value"].$setValidity('minValidationVfLevel', isValid);
172 case UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES:
173 if (isNaN(minPropertyValue) || minPropertyValue == null) {
174 isValid = propertyValue >= initialCountPropertyValue;
176 isValid = !propertyValue || (propertyValue >= minPropertyValue && propertyValue >= initialCountPropertyValue);
178 this.$scope.forms.editForm["value"].$setValidity('minValidation', isValid);
179 if (this.component.isService()) {
180 if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
184 isValid = propertyValue <= parentPropertyValue;
185 this.$scope.forms.editForm["value"].$setValidity('maxValidationVfLevel', isValid);
189 case UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT:
190 if ((isNaN(minPropertyValue) || minPropertyValue == null) && (isNaN(maxPropertyValue) || maxPropertyValue == null)) {
192 } else if (isNaN(minPropertyValue) || minPropertyValue == null) {
193 isValid = propertyValue <= maxPropertyValue;
194 } else if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
195 isValid = propertyValue >= minPropertyValue;
197 isValid = minPropertyValue <= propertyValue && propertyValue <= maxPropertyValue;
199 this.$scope.forms.editForm["value"].$setValidity('minOrMaxValidation', isValid);