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