opt-cc/static/src/app/manage/squads/squad-list/squad-list.component.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-03-07 11:56:50 +01:00
import {Component, OnInit} from '@angular/core';
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';
import {UIHelpers} from '../../../utils/global.helpers';
2018-07-03 11:38:20 +02:00
import {MatButtonToggleGroup} from '@angular/material';
2018-10-04 11:48:22 +02:00
import {TranslateService} from '@ngx-translate/core';
2017-05-10 11:04:06 +02:00
@Component({
selector: 'cc-squad-list',
2017-05-10 11:04:06 +02:00
templateUrl: './squad-list.component.html',
styleUrls: ['./squad-list.component.scss']
2017-05-10 11:04:06 +02:00
})
export class SquadListComponent implements OnInit {
selectedSquadId: string | number = null;
squads$: Observable<Squad[]>;
searchTerm = new FormControl();
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,
2018-10-04 11:48:22 +02:00
private route: ActivatedRoute,
private translate: TranslateService) {
2017-05-10 11:04:06 +02:00
}
ngOnInit() {
this.squads$ = this.squadService.squads$;
}
2017-05-10 11:04:06 +02:00
initObservable(observables: any) {
Observable.merge(observables.params as Observable<string>, observables.searchTerm)
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() {
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;
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;
2018-10-04 11:48:22 +02:00
this.translate.get('squad.list.delete.confirm', {
name: squad.name,
fraction: fraction
}).subscribe((confirmQuestion) => {
if (confirm(confirmQuestion)) {
this.squadService.deleteSquad(squad)
.subscribe((res) => {
});
}
});
2017-05-10 11:04:06 +02:00
}
2018-07-03 13:10:24 +02:00
filterSquads(group?: MatButtonToggleGroup) {
this.radioModel = UIHelpers.toggleReleaseButton(this.radioModel, group);
this.squads$ = this.squadService.findSquads(this.searchTerm.value, this.radioModel);
2017-05-10 11:04:06 +02:00
}
}