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