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 {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";
26 interface IComponentInstancesMap {
27 [key:string]: ComponentInstance
30 export interface IHierarchyScope extends ng.IScope {
33 selectedModule:DisplayModule;
36 expandCollapseArtifactsList:ExpandCollapseListData;
37 expandCollapsePropertiesList:ExpandCollapseListData;
38 selectedInstanceId:string;
39 componentInstancesMap:IComponentInstancesMap;
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;
48 export class HierarchyViewModel {
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();
65 private initScopeMethods():void {
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";
74 this.$scope.onModuleSelected = (module:Module, selectedIndex:number, componentInstanceId?:string):void => {
76 let onSuccess = (module:DisplayModule) => {
77 console.log("Module Loaded: ", module);
78 this.$scope.selectedModule = module;
79 this.$scope.isLoading = false;
83 let onFailed = () => {
84 this.$scope.isLoading = false;
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);
94 this.$scope.component.getModuleForDisplay(module.uniqueId).then(onSuccess, onFailed);
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);
103 this.$scope.componentInstancesMap[compInstance.uniqueId] = compInstance;
109 this.$scope.updateHeatName = () => {
110 this.$scope.isLoading = true;
112 let originalName:string = this.$scope.selectedModule.name;
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;
120 let onFailed = () => {
121 this.$scope.isLoading = false;
122 this.$scope.selectedModule.name = originalName;
125 this.$scope.selectedModule.updateName();
126 this.$scope.component.updateGroupMetadata(new DisplayModule(this.$scope.selectedModule)).then(onSuccess, onFailed);
129 this.$scope.openEditPropertyModal = (property:PropertyModel, filteredProperties:Array<PropertyModel>):void => {
130 this.ModalsHandler.openEditModulePropertyModal(property, this.$scope.component, this.$scope.selectedModule, filteredProperties).then(() => {