2017-05-10 11:04:06 +02:00
|
|
|
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 {Rank} from "../../models/model-interfaces";
|
|
|
|
import {RankService} from "../../services/rank-service/rank.service";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'rank-list',
|
|
|
|
templateUrl: './rank-list.component.html',
|
|
|
|
styleUrls: ['./rank-list.component.css']
|
|
|
|
})
|
|
|
|
export class RankListComponent implements OnInit {
|
|
|
|
|
|
|
|
selectedRankId: string | number = null;
|
|
|
|
|
|
|
|
ranks$: Observable<Rank[]>;
|
|
|
|
|
|
|
|
searchTerm = new FormControl();
|
|
|
|
|
|
|
|
fractionRadioSelect: string;
|
|
|
|
|
|
|
|
constructor(private rankService: RankService,
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private location: Location) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
this.ranks$ = this.rankService.ranks$;
|
|
|
|
|
|
|
|
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.rankService.findRanks(query, this.fractionRadioSelect))
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
openNewRankForm() {
|
|
|
|
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRank(rankId: string | number) {
|
|
|
|
this.selectedRankId = rankId;
|
|
|
|
this.router.navigate([{outlets: {'right': ['overview', rankId]}}], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
|
|
|
|
filterRanksByFraction(query = '', fractionFilter) {
|
|
|
|
this.ranks$ = this.rankService.findRanks(query, fractionFilter);
|
|
|
|
}
|
|
|
|
|
2017-05-15 13:44:37 +02:00
|
|
|
deleteRank(rank) {
|
|
|
|
const fraction = rank.fraction === 'OPFOR' ? 'CSAT' : 'NATO';
|
|
|
|
if (confirm('Soll der Rang ' + rank.name + ' (' + fraction + ') wirklich gelöscht werden?')) {
|
|
|
|
this.rankService.deleteRank(rank)
|
|
|
|
.subscribe((res) => {
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
adjustBrowserUrl(queryString = '') {
|
|
|
|
const absoluteUrl = this.location.path().split('?')[0];
|
|
|
|
const queryPart = queryString !== '' ? `query=${queryString}` : '';
|
|
|
|
|
|
|
|
this.location.replaceState(absoluteUrl, queryPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|