Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / service-path-selector / service-path-selector.component.ts
1 import {Component, Input, KeyValueDiffer, IterableDiffers, KeyValueDiffers, DoCheck} from '@angular/core';
2 import {Service} from "app/models/components/service";
3 import {TranslateService} from "app/ng2/shared/translator/translate.service";
4 import {ForwardingPath} from "app/models/forwarding-path";
5 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
6 import {CompositionService} from "app/ng2/pages/composition/composition.service";
7 import {EventListenerService} from "app/services/event-listener-service";
8 import {GRAPH_EVENTS} from "app/utils/constants";
9
10 @Component({
11     selector: 'service-path-selector',
12     templateUrl: './service-path-selector.component.html',
13     styleUrls: ['service-path-selector.component.less']
14 })
15
16 export class ServicePathSelectorComponent {
17
18     defaultSelectedId: string;
19     hideAllValue: string;
20     hideAllId: string = '0';
21     showAllValue: string;
22     showAllId: string = '1';
23
24     paths: Array<ForwardingPath> = [];
25     dropdownOptions: Array<DropdownValue>;
26     differ: KeyValueDiffer<string, ForwardingPath>;
27
28     @Input() drawPath: Function;
29     @Input() deletePaths: Function;
30     @Input() selectedPathId: string;
31
32     constructor(private differs: KeyValueDiffers,
33                 private translateService: TranslateService,
34                 private compositionService: CompositionService,
35                 private eventListenerService: EventListenerService
36     ) {
37
38         this.defaultSelectedId = this.hideAllId;
39         this.convertPathsToDropdownOptions();
40
41         this.translateService.languageChangedObservable.subscribe(lang => {
42             this.hideAllValue = this.translateService.translate("SERVICE_PATH_SELECTOR_HIDE_ALL_VALUE");
43             this.showAllValue = this.translateService.translate("SERVICE_PATH_SELECTOR_SHOW_ALL_VALUE");
44             this.convertPathsToDropdownOptions();
45         });
46
47     }
48
49     ngOnInit(): void {
50
51         this.selectedPathId = this.defaultSelectedId;
52         this.differ = this.differs.find(this.compositionService.forwardingPaths).create();
53         this.updatePaths();
54         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_SERVICE_PATH_CREATED, (createdId) => {
55             this.selectedPathId = createdId;
56             this.updatePaths();
57         } )
58
59     }
60
61     updatePaths(): void {
62
63         const pathsChanged = this.differ.diff(this.compositionService.forwardingPaths);
64
65         if (pathsChanged) {
66             let oldPaths = _.cloneDeep(this.paths);
67             this.populatePathsFromService();
68
69             if (!(_.isEqual(oldPaths, this.paths))) {
70                 this.convertPathsToDropdownOptions();
71
72                 let temp = this.selectedPathId;
73                 this.selectedPathId = '-1';
74
75                 setTimeout(() => {
76                     this.selectedPathId = temp;
77                     this.onSelectPath();
78                 }, 0);
79             }
80         }
81
82     }
83
84     populatePathsFromService(): void {
85
86         this.paths = [];
87
88         _.forEach(this.compositionService.forwardingPaths, path => {
89             this.paths.push(path);
90         });
91         this.paths.sort((a: ForwardingPath, b: ForwardingPath) => {
92             return a.name.localeCompare(b.name);
93         });
94
95     }
96
97     convertPathsToDropdownOptions(): void {
98
99         let result = [
100             new DropdownValue(this.hideAllId, this.hideAllValue),
101             new DropdownValue(this.showAllId, this.showAllValue)
102         ];
103
104         _.forEach(this.paths, (value: ForwardingPath) => {
105             result[result.length] = new DropdownValue(value.uniqueId, value.name);
106         });
107
108         this.dropdownOptions = result;
109
110     }
111
112     onSelectPath = (): void => {
113
114         if (this.selectedPathId !== '-1') {
115             this.deletePaths();
116
117             switch (this.selectedPathId) {
118                 case this.hideAllId:
119                     break;
120
121                 case this.showAllId:
122                     _.forEach(this.paths, path =>
123                         this.drawPath(path)
124                     );
125                     break;
126
127                 default:
128                     let path = this.paths.find(path =>
129                         path.uniqueId === this.selectedPathId
130                     );
131                     if (!path) {
132                         this.selectedPathId = this.defaultSelectedId;
133                         this.onSelectPath(); // currently does nothing in default case, but if one day it does, we want the selection to behave accordingly.
134                         break;
135                     }
136                     this.drawPath(path);
137                     break;
138             }
139         }
140
141     }
142 }