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 import * as _ from "lodash";
23 import {ComponentInstanceFactory} from "app/utils";
24 import {ComponentInstance} from "app/models";
25 import {Requirement, Component, Capability} from "app/models";
27 interface IResourceInstanceViewModelScope extends ng.IScope {
29 componentInstanceModel:ComponentInstance;
30 validationPattern:RegExp;
32 isAlreadyPressed:boolean;
33 footerButtons:Array<any>;
35 modalInstanceName:ng.ui.bootstrap.IModalServiceInstance;
41 export class ResourceInstanceNameViewModel {
51 constructor(private $scope:IResourceInstanceViewModelScope,
52 private ValidationPattern:RegExp,
53 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
54 private component:Component) {
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;
67 this.$scope.isAlreadyPressed = false;
70 this.$scope.close = ():void => {
71 this.$uibModalInstance.dismiss();
74 this.$scope.save = ():void => {
76 let onFailed = () => {
77 this.$scope.isAlreadyPressed = true;
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;
93 _.forEach(this.component.selectedInstance.capabilities, (capabilitiesArray:Array<Capability>) => {
94 _.forEach(capabilitiesArray, (capability:Capability):void => {
95 capability.ownerName = componentInstance.name;
101 this.$scope.isAlreadyPressed = true;
102 if (this.$scope.oldName != this.$scope.componentInstanceModel.name) {
103 this.component.updateComponentInstance(this.$scope.componentInstanceModel).then(onSuccess, onFailed);
107 this.$scope.footerButtons = [
108 {'name': 'OK', 'css': 'blue', 'callback': this.$scope.save},
109 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
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;