re base code
[sdc.git] / catalog-ui / src / app / view-models / tabs / hierarchy / hierarchy-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 {ModalsHandler} from "app/utils";
23 import {PropertyModel, DisplayModule, Component, ComponentInstance, Tab, Module} from "app/models";
24 import {ExpandCollapseListData} from "app/directives/utils/expand-collapse-list-header/expand-collapse-list-header";
25
26 interface IComponentInstancesMap {
27     [key:string]: ComponentInstance
28 }
29
30 export interface IHierarchyScope extends ng.IScope {
31     component:Component;
32     selectedIndex:number;
33     selectedModule:DisplayModule;
34     singleTab:Tab;
35     isLoading:boolean;
36     expandCollapseArtifactsList:ExpandCollapseListData;
37     expandCollapsePropertiesList:ExpandCollapseListData;
38     selectedInstanceId:string;
39     componentInstancesMap:IComponentInstancesMap;
40
41     onModuleSelected(module:Module, selectedIndex:number, componentInstanceId?:string):void;
42     onModuleNameChanged(module:DisplayModule):void;
43     updateHeatName():void;
44     loadInstanceModules(instance:ComponentInstance):ng.IPromise<boolean>;
45     openEditPropertyModal(property:PropertyModel, filteredProperties:Array<PropertyModel>):void;
46 }
47
48 export class HierarchyViewModel {
49
50     static '$inject' = [
51         '$scope',
52         '$q',
53         'ModalsHandler'
54     ];
55
56     constructor(private $scope:IHierarchyScope, private $q:ng.IQService, private ModalsHandler:ModalsHandler) {
57         this.$scope.component = this.$scope.singleTab.data;
58         this.$scope.isLoading = false;
59         this.$scope.expandCollapseArtifactsList = new ExpandCollapseListData();
60         this.$scope.expandCollapsePropertiesList = new ExpandCollapseListData();
61         this.$scope.componentInstancesMap = <IComponentInstancesMap>{};
62         this.initScopeMethods();
63     }
64
65     private initScopeMethods():void {
66
67         let collapseModuleData = ():void => {
68             this.$scope.expandCollapseArtifactsList.expandCollapse = false;
69             this.$scope.expandCollapsePropertiesList.expandCollapse = false;
70             this.$scope.expandCollapseArtifactsList.orderByField = "artifactName";
71             this.$scope.expandCollapsePropertiesList.orderByField = "name";
72         };
73
74         this.$scope.onModuleSelected = (module:Module, selectedIndex:number, componentInstanceId?:string):void => {
75
76             let onSuccess = (module:DisplayModule) => {
77                 console.log("Module Loaded: ", module);
78                 this.$scope.selectedModule = module;
79                 this.$scope.isLoading = false;
80                 collapseModuleData();
81             };
82
83             let onFailed = () => {
84                 this.$scope.isLoading = false;
85             };
86
87             this.$scope.selectedIndex = selectedIndex;
88             if (!this.$scope.selectedModule || (this.$scope.selectedModule && this.$scope.selectedModule.uniqueId != module.uniqueId)) {
89                 this.$scope.isLoading = true;
90                 if (this.$scope.component.isService()) {
91                     this.$scope.selectedInstanceId = componentInstanceId;
92                     this.$scope.component.getModuleInstanceForDisplay(componentInstanceId, module.uniqueId).then(onSuccess, onFailed);
93                 } else {
94                     this.$scope.component.getModuleForDisplay(module.uniqueId).then(onSuccess, onFailed);
95                 }
96             }
97
98             const componentInstances: Array<ComponentInstance> = this.$scope.component.componentInstances || [];
99             (<string[]>_.values(module.members)).forEach((memberId) => {
100                 if (!(memberId in this.$scope.componentInstancesMap)) {
101                     const compInstance = componentInstances.find((c) => c.uniqueId === memberId);
102                     if (compInstance) {
103                         this.$scope.componentInstancesMap[compInstance.uniqueId] = compInstance;
104                     }
105                 }
106             });
107         };
108
109         this.$scope.updateHeatName = () => {
110             this.$scope.isLoading = true;
111
112             let originalName:string = this.$scope.selectedModule.name;
113
114             let onSuccess = (module:Module) => {
115                 console.log("Module name updated:", module.name);
116                 this.$scope.selectedModule.name = module.name;
117                 this.$scope.isLoading = false;
118             };
119
120             let onFailed = () => {
121                 this.$scope.isLoading = false;
122                 this.$scope.selectedModule.name = originalName;
123             };
124
125             this.$scope.selectedModule.updateName();
126             this.$scope.component.updateGroupMetadata(new DisplayModule(this.$scope.selectedModule)).then(onSuccess, onFailed);
127         };
128
129         this.$scope.openEditPropertyModal = (property:PropertyModel, filteredProperties:Array<PropertyModel>):void => {
130             this.ModalsHandler.openEditModulePropertyModal(property, this.$scope.component, this.$scope.selectedModule, filteredProperties).then(() => {
131             });
132         }
133     }
134 }