aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/common/common-graph-data.service.ts
blob: d4caa5e9ed05998c3223abfa5aeeb258c2a8ba3c (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
import {Injectable} from "@angular/core";
import 'rxjs/add/observable/forkJoin';
import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
import {SelectedComponentType} from "./store/graph.actions";
import {RelationshipModel} from "../../../../models/graph/relationship";

@Injectable()
export class CommonGraphDataService {

    public componentInstances: Array<ComponentInstance>;
    public componentInstancesRelations: RelationshipModel[];
    public selectedComponentType: SelectedComponentType;

    constructor() {
    }

    //------------------------ RELATIONS ---------------------------------//
    public setRelations = (componentInstancesRelations: RelationshipModel[]) => {
        this.componentInstancesRelations = this.componentInstancesRelations;
    }

    public getRelations = (): RelationshipModel[] => {
        return this.componentInstancesRelations;
    }

    public addRelation = (componentInstancesRelations: RelationshipModel) => {
        this.componentInstancesRelations.push(componentInstancesRelations);
    }

    public deleteRelation(relationToDelete: RelationshipModel) {
        this.componentInstancesRelations = _.filter(this.componentInstancesRelations, (relationship: RelationshipModel) => {
            return relationship.relationships[0].relation.id !== relationToDelete.relationships[0].relation.id;
        });
    }

    //---------------------------- COMPONENT INSTANCES ------------------------------------//
    public getComponentInstances = (): Array<ComponentInstance> => {
        return this.componentInstances;
    }

    public addComponentInstance = (instance: ComponentInstance) => {
        return this.componentInstances.push(instance);
    }

    public updateComponentInstances = (componentInstances: ComponentInstance[]) => {
        _.unionBy(this.componentInstances, componentInstances, 'uniqueId');
    }

    public updateInstance = (instance: ComponentInstance) => {
        this.componentInstances = this.componentInstances.map(componentInstance => instance.uniqueId === componentInstance.uniqueId? instance : componentInstance);
    }

    public deleteComponentInstance(instanceToDelete: string) {
        this.componentInstances = _.filter(this.componentInstances, (instance: ComponentInstance) => {
            return instance.uniqueId !== instanceToDelete;
        });
    }

    //----------------------------SELECTED COMPONENT -----------------------//

    public setSelectedComponentType = (selectedType: SelectedComponentType) => {
        this.selectedComponentType = selectedType;
    }
}