Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / common / common-graph-data.service.ts
1 import {Injectable} from "@angular/core";
2 import 'rxjs/add/observable/forkJoin';
3 import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
4 import {SelectedComponentType} from "./store/graph.actions";
5 import {RelationshipModel} from "../../../../models/graph/relationship";
6
7 @Injectable()
8 export class CommonGraphDataService {
9
10     public componentInstances: Array<ComponentInstance>;
11     public componentInstancesRelations: RelationshipModel[];
12     public selectedComponentType: SelectedComponentType;
13
14     constructor() {
15     }
16
17     //------------------------ RELATIONS ---------------------------------//
18     public setRelations = (componentInstancesRelations: RelationshipModel[]) => {
19         this.componentInstancesRelations = this.componentInstancesRelations;
20     }
21
22     public getRelations = (): RelationshipModel[] => {
23         return this.componentInstancesRelations;
24     }
25
26     public addRelation = (componentInstancesRelations: RelationshipModel) => {
27         this.componentInstancesRelations.push(componentInstancesRelations);
28     }
29
30     public deleteRelation(relationToDelete: RelationshipModel) {
31         this.componentInstancesRelations = _.filter(this.componentInstancesRelations, (relationship: RelationshipModel) => {
32             return relationship.relationships[0].relation.id !== relationToDelete.relationships[0].relation.id;
33         });
34     }
35
36     //---------------------------- COMPONENT INSTANCES ------------------------------------//
37     public getComponentInstances = (): Array<ComponentInstance> => {
38         return this.componentInstances;
39     }
40
41     public addComponentInstance = (instance: ComponentInstance) => {
42         return this.componentInstances.push(instance);
43     }
44
45     public updateComponentInstances = (componentInstances: ComponentInstance[]) => {
46         _.unionBy(this.componentInstances, componentInstances, 'uniqueId');
47     }
48
49     public updateInstance = (instance: ComponentInstance) => {
50         this.componentInstances = this.componentInstances.map(componentInstance => instance.uniqueId === componentInstance.uniqueId? instance : componentInstance);
51     }
52
53     public deleteComponentInstance(instanceToDelete: string) {
54         this.componentInstances = _.filter(this.componentInstances, (instance: ComponentInstance) => {
55             return instance.uniqueId !== instanceToDelete;
56         });
57     }
58
59     //----------------------------SELECTED COMPONENT -----------------------//
60
61     public setSelectedComponentType = (selectedType: SelectedComponentType) => {
62         this.selectedComponentType = selectedType;
63     }
64 }