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";
11 selector: 'service-path-selector',
12 templateUrl: './service-path-selector.component.html',
13 styleUrls: ['service-path-selector.component.less']
16 export class ServicePathSelectorComponent {
18 defaultSelectedId: string;
20 hideAllId: string = '0';
22 showAllId: string = '1';
24 paths: Array<ForwardingPath> = [];
25 dropdownOptions: Array<DropdownValue>;
26 differ: KeyValueDiffer<string, ForwardingPath>;
28 @Input() drawPath: Function;
29 @Input() deletePaths: Function;
30 @Input() selectedPathId: string;
32 constructor(private differs: KeyValueDiffers,
33 private translateService: TranslateService,
34 private compositionService: CompositionService,
35 private eventListenerService: EventListenerService
38 this.defaultSelectedId = this.hideAllId;
39 this.convertPathsToDropdownOptions();
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();
51 this.selectedPathId = this.defaultSelectedId;
52 this.differ = this.differs.find(this.compositionService.forwardingPaths).create();
54 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_SERVICE_PATH_CREATED, (createdId) => {
55 this.selectedPathId = createdId;
63 const pathsChanged = this.differ.diff(this.compositionService.forwardingPaths);
66 let oldPaths = _.cloneDeep(this.paths);
67 this.populatePathsFromService();
69 if (!(_.isEqual(oldPaths, this.paths))) {
70 this.convertPathsToDropdownOptions();
72 let temp = this.selectedPathId;
73 this.selectedPathId = '-1';
76 this.selectedPathId = temp;
84 populatePathsFromService(): void {
88 _.forEach(this.compositionService.forwardingPaths, path => {
89 this.paths.push(path);
91 this.paths.sort((a: ForwardingPath, b: ForwardingPath) => {
92 return a.name.localeCompare(b.name);
97 convertPathsToDropdownOptions(): void {
100 new DropdownValue(this.hideAllId, this.hideAllValue),
101 new DropdownValue(this.showAllId, this.showAllValue)
104 _.forEach(this.paths, (value: ForwardingPath) => {
105 result[result.length] = new DropdownValue(value.uniqueId, value.name);
108 this.dropdownOptions = result;
112 onSelectPath = (): void => {
114 if (this.selectedPathId !== '-1') {
117 switch (this.selectedPathId) {
122 _.forEach(this.paths, path =>
128 let path = this.paths.find(path =>
129 path.uniqueId === this.selectedPathId
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.