Sync Integ to Master
[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} 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
32 export interface IEditResourceVersion {
33     allVersions:any;
34     changeVersion:string;
35 }
36
37 interface IDetailsViewModelScope extends ICompositionViewModelScope {
38     isLoading:boolean;
39     $parent:ICompositionViewModelScope;
40     expandedSection:Array<string>;
41     editForm:ng.IFormController;
42     editResourceVersion:IEditResourceVersion;
43
44     changeResourceVersion():void;
45 }
46
47 export class DetailsViewModel {
48
49     static '$inject' = [
50         '$scope',
51         'LeftPaletteLoaderService',
52         'EventListenerService',
53         'ComponentServiceFactoryNg2',
54         'ServiceServiceNg2'
55     ];
56
57     constructor(private $scope:IDetailsViewModelScope,
58                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
59                 private eventListenerService:EventListenerService,
60                 private ComponentServiceFactoryNg2: ComponentServiceFactoryNg2,
61                 private serviceService: ServiceServiceNg2) {
62         this.initScope();
63     }
64
65     private clearSelectedVersion = ():void => {
66         this.$scope.editResourceVersion = {
67             allVersions: {},
68             changeVersion: null
69         };
70     };
71
72     private versioning:Function = (versionNumber:string):string => {
73         let version:Array<string> = versionNumber.split('.');
74         return '00000000'.slice(version[0].length) + version[0] + '.' + '00000000'.slice(version[1].length) + version[1];
75     };
76
77     private initEditResourceVersion = ():void => {
78         this.clearSelectedVersion();
79         this.$scope.editResourceVersion.allVersions[this.$scope.currentComponent.selectedInstance.componentVersion] = this.$scope.currentComponent.selectedInstance.componentUid;
80         _.merge(this.$scope.editResourceVersion.allVersions, angular.copy(this.$scope.selectedComponent.allVersions));
81         let sorted:any = _.sortBy(_.toPairs(this.$scope.editResourceVersion.allVersions), (item)=> {
82             return this.versioning(item[0]);
83         });
84         this.clearSelectedVersion();
85         _.forEach(sorted, (item)=> {
86             this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
87         });
88
89         let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
90
91         if (parseFloat(highestVersion) % 1) { //if highest is minor, make sure it is the latest checked in -
92             let latestVersionComponent:LeftPaletteComponent = _.maxBy(_.filter(this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(this.$scope.currentComponent), (component:LeftPaletteComponent) => { //latest checked in
93                 return (component.systemName === this.$scope.selectedComponent.systemName
94                 || component.uuid === this.$scope.selectedComponent.uuid);
95             }),(component)=>{return component.version});
96
97             let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
98
99             if (highestVersion != latestVersion) { //highest is checked out - remove from options
100                 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
101             }
102         }
103         this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
104     };
105
106     private initScope = ():void => {
107         this.$scope.isLoading = false;
108         this.$scope.$parent.isLoading = false;
109         this.$scope.expandedSection = ['general', 'tags'];
110         //this.clearSelectedVersion();
111
112         this.$scope.$watch('selectedComponent', (component:Component) => {
113             if (this.$scope.isComponentInstanceSelected()) {
114                 this.initEditResourceVersion();
115             }
116         });
117
118         this.$scope.changeResourceVersion = ():void => {
119             this.$scope.isLoading = true;
120             this.$scope.$parent.isLoading = true;
121
122             let service = <Service>this.$scope.currentComponent;
123             let {changeVersion} = this.$scope.editResourceVersion;
124             let componentUid:string = this.$scope.editResourceVersion.allVersions[changeVersion];
125
126             let onCancel = (error:any) => {
127                 this.$scope.isLoading = false;
128                 this.$scope.$parent.isLoading = false;
129                 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
130
131                 if (error) {
132                     console.log(error);
133                 }
134             };
135
136             let onUpdate = () => {
137                 let onSuccess = (component:Component) => {
138                     this.$scope.isLoading = false;
139                     this.$scope.$parent.isLoading = false;
140                     this.$scope.onComponentInstanceVersionChange(component);
141                 };
142  
143                 this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onCancel);
144             };
145
146             this.serviceService.checkComponentInstanceVersionChange(service, componentUid).subscribe((pathsToDelete:string[]) => {
147                 if (pathsToDelete && pathsToDelete.length) {
148                     this.$scope.isLoading = false;
149                     this.$scope.$parent.isLoading = false;
150                     this.$scope.$parent.openVersionChangeModal(pathsToDelete).then(() => {
151                         this.$scope.isLoading = true;
152                         this.$scope.$parent.isLoading = true;
153                         onUpdate();
154                     }, onCancel);
155                 } else {
156                     onUpdate();
157                 }
158             }, onCancel);
159         };
160     }
161 }