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 {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";
28 export interface IEditResourceVersion {
33 interface IDetailsViewModelScope extends ICompositionViewModelScope {
35 $parent:ICompositionViewModelScope;
36 expandedSection:Array<string>;
37 editForm:ng.IFormController;
38 editResourceVersion:IEditResourceVersion;
40 changeResourceVersion():void;
43 export class DetailsViewModel {
47 'LeftPaletteLoaderService',
48 'EventListenerService'
52 constructor(private $scope:IDetailsViewModelScope,
53 private LeftPaletteLoaderService:LeftPaletteLoaderService,
54 private eventListenerService:EventListenerService) {
58 private clearSelectedVersion = ():void => {
59 this.$scope.editResourceVersion = {
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];
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]);
77 this.clearSelectedVersion();
78 _.forEach(sorted, (item)=> {
79 this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
82 let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
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});
90 let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
92 if (highestVersion != latestVersion) { //highest is checked out - remove from options
93 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
96 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
99 private initScope = ():void => {
100 this.$scope.isLoading = false;
101 this.$scope.$parent.isLoading = false;
102 this.$scope.expandedSection = ['general', 'tags'];
103 //this.clearSelectedVersion();
105 this.$scope.$watch('selectedComponent', (component:Component) => {
106 if (this.$scope.isComponentInstanceSelected()) {
107 this.initEditResourceVersion();
111 this.$scope.changeResourceVersion = ():void => {
112 this.$scope.isLoading = true;
113 this.$scope.$parent.isLoading = true;
115 let onSuccess = (component:Component)=> {
116 this.$scope.isLoading = false;
117 this.$scope.$parent.isLoading = false;
118 this.$scope.onComponentInstanceVersionChange(component);
120 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_VERSION_CHANGED, this.$scope.currentComponent);
123 let onFailed = (error:any)=> {
124 this.$scope.isLoading = false;
125 this.$scope.$parent.isLoading = false;
129 let componentUid:string = this.$scope.editResourceVersion.allVersions[this.$scope.editResourceVersion.changeVersion];
130 this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onFailed);