[SDC] rebase 1710 code
[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 {Component} from "app/models";
23 import {GRAPH_EVENTS} from "app/utils";
24 import {LeftPaletteLoaderService, EventListenerService} from "app/services";
25 import {ICompositionViewModelScope} from "../../composition-view-model";
26 import {LeftPaletteComponent} from "../../../../../../models/components/displayComponent";
27
28 export interface IEditResourceVersion {
29     allVersions:any;
30     changeVersion:string;
31 }
32
33 interface IDetailsViewModelScope extends ICompositionViewModelScope {
34     isLoading:boolean;
35     $parent:ICompositionViewModelScope;
36     expandedSection:Array<string>;
37     editForm:ng.IFormController;
38     editResourceVersion:IEditResourceVersion;
39
40     changeResourceVersion():void;
41 }
42
43 export class DetailsViewModel {
44
45     static '$inject' = [
46         '$scope',
47         'LeftPaletteLoaderService',
48         'EventListenerService'
49
50     ];
51
52     constructor(private $scope:IDetailsViewModelScope,
53                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
54                 private eventListenerService:EventListenerService) {
55         this.initScope();
56     }
57
58     private clearSelectedVersion = ():void => {
59         this.$scope.editResourceVersion = {
60             allVersions: {},
61             changeVersion: null
62         };
63     };
64
65     private versioning:Function = (versionNumber:string):string => {
66         let version:Array<string> = versionNumber.split('.');
67         return '00000000'.slice(version[0].length) + version[0] + '.' + '00000000'.slice(version[1].length) + version[1];
68     };
69
70     private initEditResourceVersion = ():void => {
71         this.clearSelectedVersion();
72         this.$scope.editResourceVersion.allVersions[this.$scope.currentComponent.selectedInstance.componentVersion] = this.$scope.currentComponent.selectedInstance.componentUid;
73         _.merge(this.$scope.editResourceVersion.allVersions, angular.copy(this.$scope.selectedComponent.allVersions));
74         let sorted:any = _.sortBy(_.toPairs(this.$scope.editResourceVersion.allVersions), (item)=> {
75             return this.versioning(item[0]);
76         });
77         this.clearSelectedVersion();
78         _.forEach(sorted, (item)=> {
79             this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
80         });
81
82         let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
83
84         if (parseFloat(highestVersion) % 1) { //if highest is minor, make sure it is the latest checked in -
85             let latestVersionComponent:LeftPaletteComponent = _.maxBy(_.filter(this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(this.$scope.currentComponent), (component:LeftPaletteComponent) => { //latest checked in
86                 return (component.systemName === this.$scope.selectedComponent.systemName
87                 || component.uuid === this.$scope.selectedComponent.uuid);
88             }),(component)=>{return component.version});
89
90             let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
91
92             if (highestVersion != latestVersion) { //highest is checked out - remove from options
93                 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
94             }
95         }
96         this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
97     };
98
99     private initScope = ():void => {
100         this.$scope.isLoading = false;
101         this.$scope.$parent.isLoading = false;
102         this.$scope.expandedSection = ['general', 'tags'];
103         //this.clearSelectedVersion();
104
105         this.$scope.$watch('selectedComponent', (component:Component) => {
106             if (this.$scope.isComponentInstanceSelected()) {
107                 this.initEditResourceVersion();
108             }
109         });
110
111         this.$scope.changeResourceVersion = ():void => {
112             this.$scope.isLoading = true;
113             this.$scope.$parent.isLoading = true;
114
115             let onSuccess = (component:Component)=> {
116                 this.$scope.isLoading = false;
117                 this.$scope.$parent.isLoading = false;
118                 this.$scope.onComponentInstanceVersionChange(component);
119
120                 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_VERSION_CHANGED, this.$scope.currentComponent);
121             };
122
123             let onFailed = (error:any)=> {
124                 this.$scope.isLoading = false;
125                 this.$scope.$parent.isLoading = false;
126                 console.log(error);
127             };
128
129             let componentUid:string = this.$scope.editResourceVersion.allVersions[this.$scope.editResourceVersion.changeVersion];
130             this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onFailed);
131         };
132     }
133 }