b69bf4a2a630384cf10cf5b858990dd88aeea5e6
[sdc.git] /
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 /// <reference path="../../../references"/>
21
22 module Sdc.ViewModels {
23     'use strict';
24
25     interface IResourceInstanceViewModelScope extends ng.IScope {
26
27         componentInstanceModel: Sdc.Models.ComponentsInstances.ComponentInstance;
28         validationPattern: RegExp;
29         oldName:string;
30         isAlreadyPressed:boolean;
31         footerButtons: Array<any>;
32         forms:any;
33         modalInstanceName:ng.ui.bootstrap.IModalServiceInstance;
34
35         save(): void;
36         close(): void;
37     }
38
39     export class ResourceInstanceNameViewModel {
40
41         static '$inject' = [
42             '$scope',
43             'ValidationPattern',
44             '$modalInstance',
45             'ComponentInstanceFactory',
46             'component'
47         ];
48
49
50         constructor(private $scope:IResourceInstanceViewModelScope,
51                     private ValidationPattern:RegExp,
52                     private $modalInstance:ng.ui.bootstrap.IModalServiceInstance,
53                     private ComponentInstanceFactory:Utils.ComponentInstanceFactory,
54                     private component:Models.Components.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 = Utils.ComponentInstanceFactory.createComponentInstance(this.component.selectedInstance);
64             this.$scope.oldName = this.component.selectedInstance.name;
65             this.$scope.modalInstanceName = this.$modalInstance;
66
67             this.$scope.isAlreadyPressed = false;
68
69
70             this.$scope.close = ():void => {
71                 this.$modalInstance.dismiss();
72             };
73
74             this.$scope.save = ():void => {
75
76                 let onFailed = () => {
77                     this.$scope.isAlreadyPressed = true;
78                 };
79
80                 let onSuccess = (componentInstance:Models.ComponentsInstances.ComponentInstance) => {
81                     this.$modalInstance.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
87                 };
88
89                 this.$scope.isAlreadyPressed = true;
90                 if (this.$scope.oldName != this.$scope.componentInstanceModel.name) {
91                     this.component.updateComponentInstance(this.$scope.componentInstanceModel).then(onSuccess, onFailed);
92                 }
93             };
94
95             this.$scope.footerButtons = [
96                 {'name': 'OK', 'css': 'blue', 'callback': this.$scope.save, 'disabled': (!this.$scope.componentInstanceModel.name || this.$scope.componentInstanceModel.name === this.$scope.oldName) || this.$scope.isAlreadyPressed},
97                 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
98             ];
99
100             this.$scope.$watch("forms.editNameForm.$invalid", (newVal, oldVal) => {
101                 this.$scope.footerButtons[0].disabled = this.$scope.forms.editNameForm.$invalid;
102             });
103         }
104     }
105 }