76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import {Component, OnInit} from '@angular/core';
|
|
|
|
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';
|
|
import {MatButtonToggleGroup} from '@angular/material';
|
|
import {TranslateService} from '@ngx-translate/core';
|
|
|
|
@Component({
|
|
selector: 'cc-squad-list',
|
|
templateUrl: './squad-list.component.html',
|
|
styleUrls: ['./squad-list.component.scss']
|
|
})
|
|
export class SquadListComponent implements OnInit {
|
|
|
|
selectedSquadId: string | number = null;
|
|
|
|
squads$: Observable<Squad[]>;
|
|
|
|
searchTerm = new FormControl();
|
|
|
|
public radioModel: string;
|
|
|
|
readonly fraction = Fraction;
|
|
|
|
constructor(private squadService: SquadService,
|
|
private router: Router,
|
|
private route: ActivatedRoute,
|
|
private translate: TranslateService) {
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.squads$ = this.squadService.squads$;
|
|
}
|
|
|
|
initObservable(observables: any) {
|
|
Observable.merge(observables.params as Observable<string>, observables.searchTerm)
|
|
.distinctUntilChanged()
|
|
.switchMap(query => this.squadService.findSquads(query, this.radioModel))
|
|
.subscribe();
|
|
}
|
|
|
|
openNewSquadForm() {
|
|
this.selectedSquadId = null;
|
|
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) {
|
|
const fraction = squad.fraction === 'OPFOR' ? Fraction.OPFOR : Fraction.BLUFOR;
|
|
this.translate.get('squad.list.delete.confirm', {
|
|
name: squad.name,
|
|
fraction: fraction
|
|
}).subscribe((confirmQuestion) => {
|
|
if (confirm(confirmQuestion)) {
|
|
this.squadService.deleteSquad(squad)
|
|
.subscribe((res) => {
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
filterSquads(group?: MatButtonToggleGroup) {
|
|
this.radioModel = UIHelpers.toggleReleaseButton(this.radioModel, group);
|
|
this.squads$ = this.squadService.findSquads(this.searchTerm.value, this.radioModel);
|
|
}
|
|
}
|