1 import { Action, Selector, State, StateContext} from '@ngxs/store';
3 OnSidebarOpenOrCloseAction,
5 SetSelectedComponentAction,
6 TogglePanelLoadingAction
7 } from "./graph.actions";
8 import { PolicyInstance, GroupInstance, Component as TopologyTemplate, ComponentInstance, LeftPaletteComponent, FullComponentInstance} from "app/models";
9 import { TopologyTemplateService } from "app/ng2/services/component-services/topology-template.service";
10 import { tap } from "rxjs/operators";
11 import { CompositionService } from "app/ng2/pages/composition/composition.service";
12 import {GroupsService} from "../../../../services/groups.service";
13 import {PoliciesService} from "../../../../services/policies.service";
14 import {WorkspaceService} from "../../../workspace/workspace.service";
16 export class CompositionStateModel {
19 panelLoading?: boolean;
20 selectedComponentType?: SelectedComponentType;
21 selectedComponent?: PolicyInstance | GroupInstance | TopologyTemplate | ComponentInstance;
22 withSidebar?: boolean;
25 @State<CompositionStateModel>({
31 export class GraphState {
33 constructor(private topologyTemplateService: TopologyTemplateService,
34 private compositionService: CompositionService,
35 private policiesService:PoliciesService, private groupsService:GroupsService,
36 private workspaceService: WorkspaceService) {}
38 @Action(SetSelectedComponentAction)
39 setSelectedComponent({dispatch, getState, patchState}:StateContext<CompositionStateModel>, action: SetSelectedComponentAction) {
41 const state:CompositionStateModel = getState();
43 patchState({ panelLoading: true });
45 if(action.payload.component instanceof ComponentInstance){
46 let originComponent = this.compositionService.getOriginComponentById(action.payload.component.getComponentUid());
47 if(!originComponent) {
48 return this.topologyTemplateService.getFullComponent(action.payload.component.originType, action.payload.component.getComponentUid())
50 this.compositionService.addOriginComponent(resp);
51 this.compositionService.setSelectedComponentType(SelectedComponentType.COMPONENT_INSTANCE);
53 selectedComponent: new FullComponentInstance(action.payload.component, resp),
54 selectedComponentType: action.payload.type,
65 selectedComponent: new FullComponentInstance(action.payload.component, originComponent),
66 selectedComponentType: action.payload.type,
70 } else if (action.payload.component instanceof PolicyInstance) {
71 let topologyTemplate = this.workspaceService.metadata;
72 return this.policiesService.getSpecificPolicy(topologyTemplate.componentType, topologyTemplate.uniqueId, action.payload.component.uniqueId).pipe(tap(resp =>
74 this.compositionService.updatePolicy(resp);
76 selectedComponent: resp,
77 selectedComponentType: action.payload.type,
87 } else if (action.payload.component instanceof GroupInstance) {
88 let topologyTemplate = this.workspaceService.metadata;
89 return this.groupsService.getSpecificGroup(topologyTemplate.componentType, topologyTemplate.uniqueId, action.payload.component.uniqueId).pipe(tap(resp => {
90 this.compositionService.updateGroup(resp);
92 selectedComponent: resp,
93 selectedComponentType: action.payload.type,
102 } else { //TopologyTemplate
104 selectedComponent: action.payload.component,
105 selectedComponentType: action.payload.type,
112 // @Action(UpdateSelectedComponentNameAction)
113 // UpdateSelectedComponentNameAction({patchState}:StateContext<CompositionStateModel>, action: UpdateSelectedComponentNameAction) {
115 // switch(action.payload.type){
116 // case SelectedComponentType.COMPONENT_INSTANCE:
117 // this.store.dispatch(new UpdateComponentInstancesAction([action.payload.component]));
119 // case SelectedComponentType.POLICY:
120 // this.store.dispatch(new UpdatePolicyNameAction(action.payload.uniqueId, action.payload.newName));
122 // case SelectedComponentType.GROUP:
123 // this.store.dispatch(new UpdateGroupInstancesAction)
126 // if(action.payload.type === SelectedComponentType.COMPONENT_INSTANCE){
133 static getSelectedComponent(state:CompositionStateModel) {
134 return state.selectedComponent;
138 static getSelectedComponentId(state:CompositionStateModel) {
139 return state.selectedComponent.uniqueId;
143 static getSelectedComponentType(state:CompositionStateModel) {
144 return state.selectedComponentType;
148 @Action(OnSidebarOpenOrCloseAction)
149 onSidebarOpenOrCloseAction({getState, setState}:StateContext<CompositionStateModel>) {
150 const state:CompositionStateModel = getState();
154 withSidebar: !state.withSidebar
158 @Action(TogglePanelLoadingAction)
159 TogglePanelLoading({patchState}:StateContext<CompositionStateModel>, action: TogglePanelLoadingAction) {
162 panelLoading: action.payload.isLoading
166 @Selector() static withSidebar(state):boolean {
167 return state.withSidebar;