CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / details / details-view-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 {Component, ModalModel, ButtonModel} from "app/models";
24 import {GRAPH_EVENTS} from "app/utils";
25 import {LeftPaletteLoaderService, EventListenerService} from "app/services";
26 import {ICompositionViewModelScope} from "../../composition-view-model";
27 import {LeftPaletteComponent} from "../../../../../../models/components/displayComponent";
28 import {ComponentServiceFactoryNg2} from "app/ng2/services/component-services/component.service.factory";
29 import {ServiceServiceNg2} from 'app/ng2/services/component-services/service.service';
30 import {Service} from "app/models/components/service";
31 import {ModalService} from 'app/ng2/services/modal.service';
32
33 export interface IEditResourceVersion {
34     allVersions:any;
35     changeVersion:string;
36 }
37
38 interface IDetailsViewModelScope extends ICompositionViewModelScope {
39     isLoading:boolean;
40     $parent:ICompositionViewModelScope;
41     expandedSection:Array<string>;
42     editForm:ng.IFormController;
43     editResourceVersion:IEditResourceVersion;
44
45     onChangeResourceVersion():void;
46     alertBeforeChangeResourceVersion():void;
47     changeVersion():void;
48     cancelChangeResourceVersion():void;
49 }
50
51 export class DetailsViewModel {
52
53     static '$inject' = [
54         '$scope',
55         '$filter',
56         'LeftPaletteLoaderService',
57         'EventListenerService',
58         'ComponentServiceFactoryNg2',
59         'ServiceServiceNg2',
60         'ModalServiceNg2'
61     ];
62
63     constructor(private $scope:IDetailsViewModelScope,
64                 private $filter:ng.IFilterService,
65                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
66                 private eventListenerService:EventListenerService,
67                 private ComponentServiceFactoryNg2: ComponentServiceFactoryNg2,
68                 private serviceService: ServiceServiceNg2,
69                 private ModalServiceNg2: ModalService) {
70         this.initScope();
71     }
72
73     private clearSelectedVersion = ():void => {
74         this.$scope.editResourceVersion = {
75             allVersions: {},
76             changeVersion: null
77         };
78     };
79
80     private versioning:Function = (versionNumber:string):string => {
81         let version:Array<string> = versionNumber.split('.');
82         return '00000000'.slice(version[0].length) + version[0] + '.' + '00000000'.slice(version[1].length) + version[1];
83     };
84
85     private initEditResourceVersion = ():void => {
86         this.clearSelectedVersion();
87         this.$scope.editResourceVersion.allVersions[this.$scope.currentComponent.selectedInstance.componentVersion] = this.$scope.currentComponent.selectedInstance.componentUid;
88         _.merge(this.$scope.editResourceVersion.allVersions, angular.copy(this.$scope.selectedComponent.allVersions));
89         let sorted:any = _.sortBy(_.toPairs(this.$scope.editResourceVersion.allVersions), (item)=> {
90             return this.versioning(item[0]);
91         });
92         this.clearSelectedVersion();
93         _.forEach(sorted, (item)=> {
94             this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
95         });
96
97         let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
98
99         if (parseFloat(highestVersion) % 1) { //if highest is minor, make sure it is the latest checked in -
100             let latestVersionComponent:LeftPaletteComponent = _.maxBy(_.filter(this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(this.$scope.currentComponent), (component:LeftPaletteComponent) => { //latest checked in
101                 return (component.systemName === this.$scope.selectedComponent.systemName
102                 || component.uuid === this.$scope.selectedComponent.uuid);
103             }),(component)=>{return component.version});
104
105             let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
106
107             if (highestVersion != latestVersion) { //highest is checked out - remove from options
108                 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
109             }
110         }
111         this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
112     };
113
114     private initScope = ():void => {
115         this.$scope.isLoading = false;
116         this.$scope.$parent.isLoading = false;
117         this.$scope.expandedSection = ['general', 'tags'];
118         //this.clearSelectedVersion();
119
120         this.$scope.$watch('selectedComponent', (component:Component) => {
121             if (this.$scope.isComponentInstanceSelected()) {
122                 this.initEditResourceVersion();
123             }
124         });
125
126         this.$scope.onChangeResourceVersion = ():void => {
127             if(this.$scope.isComponentInstanceSelected() && this.$scope.currentComponent.selectedInstance.isServiceProxy()) {
128                 this.$scope.alertBeforeChangeResourceVersion();
129             }
130             else {
131                 this.$scope.changeVersion();
132             }
133         };
134
135         this.$scope.alertBeforeChangeResourceVersion = ():void => {
136             let modalApproveTxt:string = this.$filter('translate')("MODAL_APPROVE");
137             let modalCancelTxt:string = this.$filter('translate')("MODAL_CANCEL");
138             let changeVersionModalTitle:string = this.$filter('translate')("DETAILS_TAB_CHANGE_VERSION_MODAL_TITLE");
139             let changeVersionModalMsg:string = this.$filter('translate')("DETAILS_TAB_CHANGE_VERSION_MODAL_MSG");
140
141             let actionButton: ButtonModel = new ButtonModel(modalApproveTxt, 'blue', this.$scope.changeVersion);
142             let cancelButton: ButtonModel = new ButtonModel(modalCancelTxt, 'grey', this.$scope.cancelChangeResourceVersion);
143             let modalModel: ModalModel = new ModalModel('sm', changeVersionModalTitle, changeVersionModalMsg, [actionButton, cancelButton]);
144             let customModal = this.ModalServiceNg2.createCustomModal(modalModel);
145             customModal.instance.open();
146         };
147
148         this.$scope.cancelChangeResourceVersion = () => {
149             this.ModalServiceNg2.closeCurrentModal();
150             this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
151         };
152
153         this.$scope.changeVersion = ():void => {
154             this.ModalServiceNg2.closeCurrentModal();
155             this.$scope.isLoading = true;
156             this.$scope.$parent.isLoading = true;
157
158             let service = <Service>this.$scope.currentComponent;
159             let {changeVersion} = this.$scope.editResourceVersion;
160             let componentUid:string = this.$scope.editResourceVersion.allVersions[changeVersion];
161
162             let onCancel = (error:any) => {
163                 this.$scope.isLoading = false;
164                 this.$scope.$parent.isLoading = false;
165                 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
166
167                 if (error) {
168                     console.log(error);
169                 }
170             };
171
172             let onUpdate = () => {
173                 let onSuccess = (component:Component) => {
174                     this.$scope.isLoading = false;
175                     this.$scope.$parent.isLoading = false;
176                     this.$scope.onComponentInstanceVersionChange(component);
177                 };
178  
179                 this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onCancel);
180             };
181
182             if (this.$scope.currentComponent.isService()) {
183                 this.serviceService.checkComponentInstanceVersionChange(service, componentUid).subscribe((pathsToDelete:string[]) => {
184                     if (pathsToDelete && pathsToDelete.length) {
185                         this.$scope.isLoading = false;
186                         this.$scope.$parent.isLoading = false;
187                         this.$scope.$parent.openVersionChangeModal(pathsToDelete).then(() => {
188                             this.$scope.isLoading = true;
189                             this.$scope.$parent.isLoading = true;
190                             onUpdate();
191                         }, onCancel);
192                     } else {
193                         onUpdate();
194                     }
195                 }, onCancel);
196             } else {
197                 onUpdate();
198             }
199         };
200     }
201 }