2018-03-07 11:56:50 +01:00
|
|
|
import {Component, OnInit} from '@angular/core';
|
|
|
|
import {Location} from '@angular/common';
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2018-03-07 11:56:50 +01:00
|
|
|
import {FormControl} from '@angular/forms';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Squad} from '../../models/model-interfaces';
|
|
|
|
import {SquadService} from '../../services/army-management/squad.service';
|
|
|
|
import {Fraction} from '../../utils/fraction.enum';
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'squad-list',
|
|
|
|
templateUrl: './squad-list.component.html',
|
2017-10-06 20:11:18 +02:00
|
|
|
styleUrls: ['./squad-list.component.css', '../../style/select-list.css']
|
2017-05-10 11:04:06 +02:00
|
|
|
})
|
|
|
|
export class SquadListComponent implements OnInit {
|
|
|
|
|
|
|
|
selectedSquadId: string | number = null;
|
|
|
|
|
|
|
|
squads$: Observable<Squad[]>;
|
|
|
|
|
|
|
|
searchTerm = new FormControl();
|
|
|
|
|
2017-10-08 18:13:20 +02:00
|
|
|
public radioModel: string;
|
2017-05-10 11:04:06 +02:00
|
|
|
|
2017-11-08 19:35:34 +01:00
|
|
|
readonly fraction = Fraction;
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
constructor(private squadService: SquadService,
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private location: Location) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
this.squads$ = this.squadService.squads$;
|
|
|
|
|
|
|
|
const paramsStream = this.route.queryParams
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(params => decodeURI(params['query'] || ''))
|
|
|
|
.do(query => this.searchTerm.setValue(query));
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
const searchTermStream = this.searchTerm.valueChanges
|
2018-02-26 09:04:27 +01:00
|
|
|
.debounceTime(400)
|
|
|
|
.do(query => this.adjustBrowserUrl(query));
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
Observable.merge(paramsStream, searchTermStream)
|
2018-02-26 09:04:27 +01:00
|
|
|
.distinctUntilChanged()
|
|
|
|
.switchMap(query => this.squadService.findSquads(query, this.radioModel))
|
|
|
|
.subscribe();
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
openNewSquadForm() {
|
2017-05-16 20:06:00 +02:00
|
|
|
this.selectedSquadId = null;
|
2017-05-10 11:04:06 +02:00
|
|
|
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
|
|
|
|
selectSquad(squadId: string | number) {
|
|
|
|
this.selectedSquadId = squadId;
|
2017-05-16 20:06:00 +02:00
|
|
|
this.router.navigate([{outlets: {'right': ['edit', squadId]}}], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteSquad(squad) {
|
2017-11-08 19:35:34 +01:00
|
|
|
const fraction = squad.fraction === 'OPFOR' ? Fraction.OPFOR : Fraction.BLUFOR;
|
2017-05-16 20:06:00 +02:00
|
|
|
if (confirm('Soll das Squad "' + squad.name + '" (' + fraction + ') wirklich gelöscht werden?')) {
|
|
|
|
this.squadService.deleteSquad(squad)
|
2018-02-26 09:04:27 +01:00
|
|
|
.subscribe((res) => {
|
|
|
|
})
|
2017-05-16 20:06:00 +02:00
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:13:20 +02:00
|
|
|
filterSquads() {
|
|
|
|
this.squads$ = this.squadService.findSquads(this.searchTerm.value, this.radioModel);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|