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 {Squad} from "../../models/model-interfaces";
|
2017-10-22 18:06:37 +02:00
|
|
|
import {SquadService} from "../../services/army-management/squad.service";
|
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
|
|
|
|
|
|
|
constructor(private squadService: SquadService,
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private location: Location) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
this.squads$ = this.squadService.squads$;
|
|
|
|
|
|
|
|
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()
|
2017-10-08 18:13:20 +02:00
|
|
|
.switchMap(query => this.squadService.findSquads(query, this.radioModel))
|
2017-05-10 11:04:06 +02:00
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const fraction = squad.fraction === 'OPFOR' ? 'CSAT' : 'NATO';
|
|
|
|
if (confirm('Soll das Squad "' + squad.name + '" (' + fraction + ') wirklich gelöscht werden?')) {
|
|
|
|
this.squadService.deleteSquad(squad)
|
|
|
|
.subscribe((res) => {
|
|
|
|
})
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|