Sync Integ to Master
[sdc.git] / catalog-ui / src / app / view-models / forms / resource-instance-name-form / resource-instance-name-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 'use strict';
22 import * as _ from "lodash";
23 import {ComponentInstanceFactory} from "app/utils";
24 import {ComponentInstance} from "app/models";
25 import {Requirement, Component, Capability} from "app/models";
26
27 interface IResourceInstanceViewModelScope extends ng.IScope {
28
29     componentInstanceModel:ComponentInstance;
30     validationPattern:RegExp;
31     oldName:string;
32     isAlreadyPressed:boolean;
33     footerButtons:Array<any>;
34     forms:any;
35     modalInstanceName:ng.ui.bootstrap.IModalServiceInstance;
36
37     save():void;
38     close():void;
39 }
40
41 export class ResourceInstanceNameViewModel {
42
43     static '$inject' = [
44         '$scope',
45         'ValidationPattern',
46         '$uibModalInstance',
47         'component'
48     ];
49
50
51     constructor(private $scope:IResourceInstanceViewModelScope,
52                 private ValidationPattern:RegExp,
53                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
54                 private component:Component) {
55
56         this.initScope();
57     }
58
59
60     private initScope = ():void => {
61         this.$scope.forms = {};
62         this.$scope.validationPattern = this.ValidationPattern;
63         this.$scope.componentInstanceModel = ComponentInstanceFactory.createComponentInstance(this.component.selectedInstance);
64         this.$scope.oldName = this.component.selectedInstance.name;
65         this.$scope.modalInstanceName = this.$uibModalInstance;
66
67         this.$scope.isAlreadyPressed = false;
68
69
70         this.$scope.close = ():void => {
71             this.$uibModalInstance.dismiss();
72         };
73
74         this.$scope.save = ():void => {
75
76             let onFailed = () => {
77                 this.$scope.isAlreadyPressed = true;
78             };
79
80             let onSuccess = (componentInstance:ComponentInstance) => {
81                 this.$uibModalInstance.close();
82                 this.$scope.isAlreadyPressed = false;
83                 this.$scope.componentInstanceModel = componentInstance;
84                 //this.component.name = componentInstance.name;//DE219124
85                 this.component.selectedInstance.name = componentInstance.name;
86                 //update requirements and capabilities owner name
87                 _.forEach(this.component.selectedInstance.requirements, (requirementsArray:Array<Requirement>) => {
88                     _.forEach(requirementsArray, (requirement:Requirement):void => {
89                         requirement.ownerName = componentInstance.name;
90                     });
91                 });
92
93                 _.forEach(this.component.selectedInstance.capabilities, (capabilitiesArray:Array<Capability>) => {
94                     _.forEach(capabilitiesArray, (capability:Capability):void => {
95                         capability.ownerName = componentInstance.name;
96                     });
97                 });
98
99             };
100
101             this.$scope.isAlreadyPressed = true;
102             if (this.$scope.oldName != this.$scope.componentInstanceModel.name) {
103                 this.component.updateComponentInstance(this.$scope.componentInstanceModel).then(onSuccess, onFailed);
104             }
105         };
106
107         this.$scope.footerButtons = [
108             {'name': 'OK', 'css': 'blue', 'callback': this.$scope.save},
109             {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
110         ];
111
112         this.$scope.$watch("[forms.editNameForm.$invalid,componentInstanceModel.name,isAlreadyPressed]", (newVal, oldVal) => {
113             //if the name is invalid or if user pressed ok and didn't try to change name again or the new name = source name
114             this.$scope.footerButtons[0].disabled = this.$scope.forms.editNameForm.$invalid || (this.$scope.isAlreadyPressed && newVal[1] === oldVal[1]) || this.$scope.componentInstanceModel.name === this.$scope.oldName;
115         });
116     }
117 }