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";
9 selector: 'service-path-selector',
10 templateUrl: './service-path-selector.component.html',
11 styleUrls:['service-path-selector.component.less'],
12 providers: [TranslateService]
15 export class ServicePathSelectorComponent implements DoCheck {
17 defaultSelectedId: string;
19 hideAllId: string = '0';
21 showAllId: string = '1';
23 paths: Array<ForwardingPath> = [];
24 dropdownOptions: Array<DropdownValue>;
25 differ: KeyValueDiffer;
27 @Input() service: Service;
28 @Input() drawPath: Function;
29 @Input() deletePaths: Function;
30 @Input() selectedPathId: string;
32 constructor(private differs: KeyValueDiffers, private translateService: TranslateService) {
34 this.defaultSelectedId = this.hideAllId;
35 this.convertPathsToDropdownOptions();
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();
47 this.selectedPathId = this.defaultSelectedId;
48 this.differ = this.differs.find(this.service.forwardingPaths).create(null);
54 const pathsChanged = this.differ.diff(this.service.forwardingPaths);
57 let oldPaths = _.cloneDeep(this.paths);
58 this.populatePathsFromService();
60 if (!(_.isEqual(oldPaths, this.paths))) {
61 this.convertPathsToDropdownOptions();
63 let temp = this.selectedPathId;
64 this.selectedPathId = '-1';
67 this.selectedPathId = temp;
75 populatePathsFromService(): void {
78 let {forwardingPaths} = this.service;
80 _.forEach(forwardingPaths, path => {
81 this.paths.push(path);
83 this.paths.sort((a:ForwardingPath, b:ForwardingPath)=> {
84 return a.name.localeCompare(b.name);
89 convertPathsToDropdownOptions(): void {
92 new DropdownValue(this.hideAllId, this.hideAllValue),
93 new DropdownValue(this.showAllId, this.showAllValue)
96 _.forEach(this.paths, (value: ForwardingPath) => {
97 result[result.length] = new DropdownValue(value.uniqueId, value.name);
100 this.dropdownOptions = result;
104 onSelectPath = (): void => {
106 if (this.selectedPathId !== '-1') {
109 switch (this.selectedPathId) {
114 _.forEach(this.paths, path =>
120 let path = this.paths.find(path =>
121 path.uniqueId === this.selectedPathId
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.