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";
8 selector: 'service-path-selector',
9 templateUrl: './service-path-selector.component.html',
10 styleUrls:['service-path-selector.component.less'],
11 providers: [TranslateService]
14 export class ServicePathSelectorComponent implements DoCheck {
16 defaultSelectedId: string;
18 hideAllId: string = '0';
20 showAllId: string = '1';
22 paths: Array<ForwardingPath> = [];
23 dropdownOptions: Array<DropdownValue>;
24 differ: KeyValueDiffer;
26 @Input() service: Service;
27 @Input() drawPath: Function;
28 @Input() deletePaths: Function;
29 @Input() selectedPathId: string;
31 constructor(private differs: KeyValueDiffers, private translateService: TranslateService) {
33 this.defaultSelectedId = this.hideAllId;
34 this.convertPathsToDropdownOptions();
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();
46 this.selectedPathId = this.defaultSelectedId;
47 this.differ = this.differs.find(this.service.forwardingPaths).create(null);
53 const pathsChanged = this.differ.diff(this.service.forwardingPaths);
56 let oldPaths = _.cloneDeep(this.paths);
57 this.populatePathsFromService();
59 if (!(_.isEqual(oldPaths, this.paths))) {
60 this.convertPathsToDropdownOptions();
62 let temp = this.selectedPathId;
63 this.selectedPathId = '-1';
66 this.selectedPathId = temp;
74 populatePathsFromService(): void {
77 let {forwardingPaths} = this.service;
79 _.forEach(forwardingPaths, path => {
80 this.paths.push(path);
82 this.paths.sort((a:ForwardingPath, b:ForwardingPath)=> {
83 return a.name.localeCompare(b.name);
88 convertPathsToDropdownOptions(): void {
91 new DropdownValue(this.hideAllId, this.hideAllValue),
92 new DropdownValue(this.showAllId, this.showAllValue)
95 _.forEach(this.paths, (value: ForwardingPath) => {
96 result[result.length] = new DropdownValue(value.uniqueId, value.name);
99 this.dropdownOptions = result;
103 onSelectPath = (): void => {
105 if (this.selectedPathId !== '-1') {
108 switch (this.selectedPathId) {
113 _.forEach(this.paths, path =>
119 let path = this.paths.find(path =>
120 path.uniqueId === this.selectedPathId
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.