import {Component, OnInit} from '@angular/core'; import {Location} from '@angular/common'; import {FormControl} from '@angular/forms'; import {ActivatedRoute, Router} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Decoration} from '../../models/model-interfaces'; import {DecorationService} from '../../services/army-management/decoration.service'; import {Fraction} from '../../utils/fraction.enum'; import {MatButtonToggleGroup} from '@angular/material'; import {UIHelpers} from '../../utils/global.helpers'; @Component({ selector: 'decoration-list', templateUrl: './decoration-list.component.html', styleUrls: ['./decoration-list.component.css', '../../style/select-list.css'] }) export class DecorationListComponent implements OnInit { selectedDecorationId: string | number = null; decorations$: Observable; searchTerm = new FormControl(); radioModel: string = ''; readonly fraction = Fraction; constructor(private decorationService: DecorationService, private router: Router, private route: ActivatedRoute, private location: Location) { } ngOnInit() { this.decorations$ = this.decorationService.decorations$; const paramsStream = this.route.queryParams .map(params => decodeURI(params['query'] || '')) .do(query => this.searchTerm.setValue(query)); const searchTermStream = this.searchTerm.valueChanges .debounceTime(400) .do(query => this.adjustBrowserUrl(query)); Observable.merge(paramsStream, searchTermStream) .distinctUntilChanged() .switchMap(query => this.decorationService.findDecorations(query, this.radioModel)) .subscribe(); } openNewDecorationForm() { this.selectedDecorationId = null; this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route}); } selectDecoration(decorationId: string | number) { this.selectedDecorationId = decorationId; this.router.navigate([{outlets: {'right': ['edit', decorationId]}}], {relativeTo: this.route}); } deleteDecoration(decoration) { let fraction = 'Global'; if (decoration.fraction === 'BLUFOR') { fraction = Fraction.BLUFOR; } else if (decoration.fraction === 'OPFOR') { fraction = Fraction.OPFOR; } if (confirm('Soll die Auszeichnung "' + decoration.name + '" (' + fraction + ') wirklich gelöscht werden?')) { this.decorationService.deleteDecoration(decoration) .subscribe((res) => { }); } } filterDecorations(group?: MatButtonToggleGroup) { this.radioModel = UIHelpers.toggleReleaseButton(this.radioModel, group); this.decorations$ = this.decorationService.findDecorations(this.searchTerm.value, this.radioModel); } adjustBrowserUrl(queryString = '') { const absoluteUrl = this.location.path().split('?')[0]; const queryPart = queryString !== '' ? `query=${queryString}` : ''; this.location.replaceState(absoluteUrl, queryPart); } }