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, 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';
33 export interface IEditResourceVersion {
38 interface IDetailsViewModelScope extends ICompositionViewModelScope {
40 $parent:ICompositionViewModelScope;
41 expandedSection:Array<string>;
42 editForm:ng.IFormController;
43 editResourceVersion:IEditResourceVersion;
45 onChangeResourceVersion():void;
46 alertBeforeChangeResourceVersion():void;
48 cancelChangeResourceVersion():void;
51 export class DetailsViewModel {
56 'LeftPaletteLoaderService',
57 'EventListenerService',
58 'ComponentServiceFactoryNg2',
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) {
73 private clearSelectedVersion = ():void => {
74 this.$scope.editResourceVersion = {
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];
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]);
92 this.clearSelectedVersion();
93 _.forEach(sorted, (item)=> {
94 this.$scope.editResourceVersion.allVersions[item[0]] = item[1];
97 let highestVersion = _.last(Object.keys(this.$scope.selectedComponent.allVersions));
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});
105 let latestVersion:string = latestVersionComponent ? latestVersionComponent.version : highestVersion;
107 if (highestVersion != latestVersion) { //highest is checked out - remove from options
108 this.$scope.editResourceVersion.allVersions = _.omit(this.$scope.editResourceVersion.allVersions, highestVersion);
111 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
114 private initScope = ():void => {
115 this.$scope.isLoading = false;
116 this.$scope.$parent.isLoading = false;
117 this.$scope.expandedSection = ['general', 'tags'];
118 //this.clearSelectedVersion();
120 this.$scope.$watch('selectedComponent', (component:Component) => {
121 if (this.$scope.isComponentInstanceSelected()) {
122 this.initEditResourceVersion();
126 this.$scope.onChangeResourceVersion = ():void => {
127 if(this.$scope.isComponentInstanceSelected() && this.$scope.currentComponent.selectedInstance.isServiceProxy()) {
128 this.$scope.alertBeforeChangeResourceVersion();
131 this.$scope.changeVersion();
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");
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();
148 this.$scope.cancelChangeResourceVersion = () => {
149 this.ModalServiceNg2.closeCurrentModal();
150 this.$scope.editResourceVersion.changeVersion = this.$scope.currentComponent.selectedInstance.componentVersion;
153 this.$scope.changeVersion = ():void => {
154 this.ModalServiceNg2.closeCurrentModal();
155 this.$scope.isLoading = true;
156 this.$scope.$parent.isLoading = true;
158 let service = <Service>this.$scope.currentComponent;
159 let {changeVersion} = this.$scope.editResourceVersion;
160 let componentUid:string = this.$scope.editResourceVersion.allVersions[changeVersion];
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;
172 let onUpdate = () => {
173 let onSuccess = (component:Component) => {
174 this.$scope.isLoading = false;
175 this.$scope.$parent.isLoading = false;
176 this.$scope.onComponentInstanceVersionChange(component);
179 this.$scope.currentComponent.changeComponentInstanceVersion(componentUid).then(onSuccess, onCancel);
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;