Sync Integ to Master
[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 export interface IHierarchyScope extends ng.IScope {
27     component:Component;
28     selectedIndex:number;
29     selectedModule:DisplayModule;
30     singleTab:Tab;
31     isLoading:boolean;
32     expandCollapseArtifactsList:ExpandCollapseListData;
33     expandCollapsePropertiesList:ExpandCollapseListData;
34     selectedInstanceId:string;
35
36     onModuleSelected(moduleId:string, selectedIndex:number):void;
37     onModuleNameChanged(module:DisplayModule):void;
38     updateHeatName():void;
39     loadInstanceModules(instance:ComponentInstance):ng.IPromise<boolean>;
40     openEditPropertyModal(property:PropertyModel, filteredProperties:Array<PropertyModel>):void;
41 }
42
43 export class HierarchyViewModel {
44
45     static '$inject' = [
46         '$scope',
47         '$q',
48         'ModalsHandler'
49     ];
50
51     constructor(private $scope:IHierarchyScope, private $q:ng.IQService, private ModalsHandler:ModalsHandler) {
52         this.$scope.component = this.$scope.singleTab.data;
53         this.$scope.isLoading = false;
54         this.$scope.expandCollapseArtifactsList = new ExpandCollapseListData();
55         this.$scope.expandCollapsePropertiesList = new ExpandCollapseListData();
56         this.initScopeMethods();
57     }
58
59     private initScopeMethods():void {
60
61         let collapseModuleData = ():void => {
62             this.$scope.expandCollapseArtifactsList.expandCollapse = false;
63             this.$scope.expandCollapsePropertiesList.expandCollapse = false;
64             this.$scope.expandCollapseArtifactsList.orderByField = "artifactName";
65             this.$scope.expandCollapsePropertiesList.orderByField = "name";
66         };
67
68         this.$scope.onModuleSelected = (moduleId:string, selectedIndex:number, componentInstanceId?:string):void => {
69
70             let onSuccess = (module:DisplayModule) => {
71                 console.log("Module Loaded: ", module);
72                 this.$scope.selectedModule = module;
73                 this.$scope.isLoading = false;
74                 collapseModuleData();
75             };
76
77             let onFailed = () => {
78                 this.$scope.isLoading = false;
79             };
80
81             this.$scope.selectedIndex = selectedIndex;
82             if (!this.$scope.selectedModule || (this.$scope.selectedModule && this.$scope.selectedModule.uniqueId != moduleId)) {
83                 this.$scope.isLoading = true;
84                 if (this.$scope.component.isService()) {
85                     this.$scope.selectedInstanceId = componentInstanceId;
86                     this.$scope.component.getModuleInstanceForDisplay(componentInstanceId, moduleId).then(onSuccess, onFailed);
87                 } else {
88                     this.$scope.component.getModuleForDisplay(moduleId).then(onSuccess, onFailed);
89                 }
90             }
91         };
92
93         this.$scope.updateHeatName = () => {
94             this.$scope.isLoading = true;
95
96             let originalName:string = this.$scope.selectedModule.name;
97
98             let onSuccess = (module:Module) => {
99                 console.log("Module name updated:", module.name);
100                 this.$scope.selectedModule.name = module.name;
101                 this.$scope.isLoading = false;
102             };
103
104             let onFailed = () => {
105                 this.$scope.isLoading = false;
106                 this.$scope.selectedModule.name = originalName;
107             };
108
109             this.$scope.selectedModule.updateName();
110             this.$scope.component.updateGroupMetadata(new DisplayModule(this.$scope.selectedModule)).then(onSuccess, onFailed);
111         };
112
113         this.$scope.openEditPropertyModal = (property:PropertyModel, filteredProperties:Array<PropertyModel>):void => {
114             this.ModalsHandler.openEditModulePropertyModal(property, this.$scope.component, this.$scope.selectedModule, filteredProperties).then(() => {
115             });
116         }
117     }
118 }