aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/common/store/graph.state.ts
blob: 7c352a61dc98d1e20efb8c36bb4ec423cba55c1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Action, Selector, State, StateContext} from '@ngxs/store';
import {
    OnSidebarOpenOrCloseAction,
    SelectedComponentType,
    SetSelectedComponentAction,
    TogglePanelLoadingAction
} from "./graph.actions";
import { PolicyInstance, GroupInstance, Component as TopologyTemplate, ComponentInstance, LeftPaletteComponent, FullComponentInstance} from "app/models";
import { TopologyTemplateService } from "app/ng2/services/component-services/topology-template.service";
import { tap } from "rxjs/operators";
import { CompositionService } from "app/ng2/pages/composition/composition.service";
import {GroupsService} from "../../../../services/groups.service";
import {PoliciesService} from "../../../../services/policies.service";
import {WorkspaceService} from "../../../workspace/workspace.service";

export class CompositionStateModel {

    isViewOnly?: boolean;
    panelLoading?: boolean;
    selectedComponentType?: SelectedComponentType;
    selectedComponent?: PolicyInstance | GroupInstance | TopologyTemplate | ComponentInstance;
    withSidebar?: boolean;
}

@State<CompositionStateModel>({
    name: 'composition',
    defaults: {
         withSidebar: true
    }
})
export class GraphState {

    constructor(private topologyTemplateService: TopologyTemplateService,
                private compositionService: CompositionService,
         private policiesService:PoliciesService, private groupsService:GroupsService,
                 private workspaceService: WorkspaceService) {}

    @Action(SetSelectedComponentAction)
    setSelectedComponent({dispatch, getState, patchState}:StateContext<CompositionStateModel>, action: SetSelectedComponentAction) {
        
        const state:CompositionStateModel = getState();

        patchState({ panelLoading: true });

        if(action.payload.component instanceof ComponentInstance){
            let originComponent = this.compositionService.getOriginComponentById(action.payload.component.getComponentUid());
            if(!originComponent) {
                return this.topologyTemplateService.getFullComponent(action.payload.component.originType, action.payload.component.getComponentUid())
                .pipe(tap(resp => {
                    this.compositionService.addOriginComponent(resp);
                    this.compositionService.setSelectedComponentType(SelectedComponentType.COMPONENT_INSTANCE);
                    patchState({
                        selectedComponent: new FullComponentInstance(action.payload.component, resp), 
                        selectedComponentType: action.payload.type,
                        panelLoading: false
                    });
                    }, err => {
                        patchState({
                            panelLoading: false
                        })
                    }
                ));
            } else {
                patchState({
                    selectedComponent: new FullComponentInstance(action.payload.component, originComponent), 
                    selectedComponentType: action.payload.type,
                    panelLoading: false
                });
            }
        } else if (action.payload.component instanceof PolicyInstance) {
            let topologyTemplate = this.workspaceService.metadata;
            return this.policiesService.getSpecificPolicy(topologyTemplate.componentType, topologyTemplate.uniqueId, action.payload.component.uniqueId).pipe(tap(resp => 
                {
                    this.compositionService.updatePolicy(resp);
                    patchState({
                        selectedComponent: resp, 
                        selectedComponentType: action.payload.type,
                        panelLoading: false
                    })
                }, err => {
                    patchState({
                        panelLoading: false
                    })
                 }
            ));
        
        } else if (action.payload.component instanceof GroupInstance) {
            let topologyTemplate = this.workspaceService.metadata;
            return this.groupsService.getSpecificGroup(topologyTemplate.componentType, topologyTemplate.uniqueId, action.payload.component.uniqueId).pipe(tap(resp => {
                this.compositionService.updateGroup(resp);
                patchState({
                    selectedComponent: resp,
                    selectedComponentType: action.payload.type,
                    panelLoading: false
                });
            }, err => {
                patchState({
                    panelLoading: false
                })
             }
            ));
        } else { //TopologyTemplate
            patchState({
                selectedComponent: action.payload.component,
                selectedComponentType: action.payload.type,
                panelLoading: false
            })
        }
    }


    // @Action(UpdateSelectedComponentNameAction)
    // UpdateSelectedComponentNameAction({patchState}:StateContext<CompositionStateModel>, action: UpdateSelectedComponentNameAction) {

    //     switch(action.payload.type){
    //         case SelectedComponentType.COMPONENT_INSTANCE:
    //             this.store.dispatch(new UpdateComponentInstancesAction([action.payload.component]));
    //             break;
    //         case SelectedComponentType.POLICY:
    //             this.store.dispatch(new UpdatePolicyNameAction(action.payload.uniqueId, action.payload.newName));
    //             break;
    //         case SelectedComponentType.GROUP:
    //             this.store.dispatch(new UpdateGroupInstancesAction)

    //     }
    //     if(action.payload.type === SelectedComponentType.COMPONENT_INSTANCE){
            
    //     } 
    
    // }

    @Selector() 
    static getSelectedComponent(state:CompositionStateModel) {
        return state.selectedComponent;
    }

    @Selector() 
    static getSelectedComponentId(state:CompositionStateModel) {
        return state.selectedComponent.uniqueId;
    }

    @Selector() 
    static getSelectedComponentType(state:CompositionStateModel) {
        return state.selectedComponentType;
    }


    @Action(OnSidebarOpenOrCloseAction)
    onSidebarOpenOrCloseAction({getState, setState}:StateContext<CompositionStateModel>) {
        const state:CompositionStateModel = getState();

        setState({
            ...state,
            withSidebar: !state.withSidebar
        });
    }
    
    @Action(TogglePanelLoadingAction)
    TogglePanelLoading({patchState}:StateContext<CompositionStateModel>, action: TogglePanelLoadingAction) {

        patchState({
            panelLoading: action.payload.isLoading
        });
    }

    @Selector() static withSidebar(state):boolean {
        return state.withSidebar;
    }

}