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 {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";
32 export interface IEditResourceVersion {
37 interface IDetailsViewModelScope extends ICompositionViewModelScope {
39 $parent:ICompositionViewModelScope;
40 expandedSection:Array<string>;
41 editForm:ng.IFormController;
42 editResourceVersion:IEditResourceVersion;
44 changeResourceVersion():void;
47 export class DetailsViewModel {
51 'LeftPaletteLoaderService',
52 'EventListenerService',
53 'ComponentServiceFactoryNg2',
57 constructor(private $scope:IDetailsViewModelScope,
58 private LeftPaletteLoaderService:LeftPaletteLoaderService,
59 private eventListenerService:EventListenerService,
60 private ComponentServiceFactoryNg2: ComponentServiceFactoryNg2,
61 private serviceService: ServiceServiceNg2) {
65 private clearSelectedVersion = ():void => {
66 this.$scope.editResourceVersion = {
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];
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]);
84 this.clearSelectedVersion();
85 _.forEach(sorted, (item)=> {
86 this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
89 let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
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});
97 let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
99 if (highestVersion != latestVersion) { //highest is checked out - remove from options
100 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
103 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
106 private initScope = ():void => {
107 this.$scope.isLoading = false;
108 this.$scope.$parent.isLoading = false;
109 this.$scope.expandedSection = ['general', 'tags'];
110 //this.clearSelectedVersion();
112 this.$scope.$watch('selectedComponent', (component:Component) => {
113 if (this.$scope.isComponentInstanceSelected()) {
114 this.initEditResourceVersion();
118 this.$scope.changeResourceVersion = ():void => {
119 this.$scope.isLoading = true;
120 this.$scope.$parent.isLoading = true;
122 let service = <Service>this.$scope.currentComponent;
123 let {changeVersion} = this.$scope.editResourceVersion;
124 let componentUid:string = this.$scope.editResourceVersion.allVersions[changeVersion];
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;
136 let onUpdate = () => {
137 let onSuccess = (component:Component) => {
138 this.$scope.isLoading = false;
139 this.$scope.$parent.isLoading = false;
140 this.$scope.onComponentInstanceVersionChange(component);
143 this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onCancel);
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;