opt-cc/static/src/app/statistic/war-list/war-list.component.ts

103 lines
3.5 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Campaign, War} from '../../models/model-interfaces';
import {WarService} from '../../services/logs/war.service';
import {LoginService} from '../../services/app-user-service/login-service';
import {CampaignService} from '../../services/logs/campaign.service';
import {RouteConfig} from '../../app.config';
@Component({
selector: 'war-list',
templateUrl: './war-list.component.html',
styleUrls: ['./war-list.component.css', '../../style/list-entry.css', '../../style/select-list.css']
})
export class WarListComponent implements OnInit {
selectedWarId: string | number;
campaigns: Campaign[] = [];
public readonly highscore = 'HIGHSCORE';
constructor(private warService: WarService,
private campaignService: CampaignService,
public loginService: LoginService,
private router: Router,
private route: ActivatedRoute) {
}
ngOnInit() {
this.campaignService.getAllCampaigns().subscribe((items) => {
const subPathWar = 'war/';
const subPathOverview = 'overview/';
this.campaignService.campaigns = items;
this.campaigns = items;
const url = this.router.url;
if (url.endsWith(RouteConfig.statsPath)) {
this.selectOverview(this.campaigns[0]._id);
} else if (url.indexOf(subPathWar) !== -1) {
this.selectedWarId = url.substring(url.lastIndexOf(subPathWar) + subPathWar.length, url.lastIndexOf(')'));
} else if (url.indexOf(subPathOverview) !== -1) {
this.selectedWarId = url.substring(url.lastIndexOf(subPathOverview) + subPathOverview.length, url.lastIndexOf(')'));
}
});
}
selectNewCampaign() {
this.selectedWarId = null;
this.router.navigate([{outlets: {'right': ['new-campaign']}}], {relativeTo: this.route});
}
selectNewWar() {
this.selectedWarId = null;
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route});
}
selectWar(warId) {
if (this.selectedWarId !== warId) {
this.selectedWarId = warId;
this.router.navigate([{outlets: {'right': ['war', warId]}}], {relativeTo: this.route});
}
}
selectOverview(campaignId) {
if (this.selectedWarId !== campaignId) {
this.selectedWarId = campaignId;
this.router.navigate([{outlets: {'right': ['overview', campaignId]}}], {relativeTo: this.route});
}
}
selectHighscore(campaignId) {
if (this.selectedWarId !== campaignId + this.highscore) {
this.selectedWarId = campaignId + this.highscore;
this.router.navigate([{outlets: {'right': ['highscore', campaignId]}}], {relativeTo: this.route});
}
}
deleteWar(war: War) {
if (confirm('Soll die Schlacht ' + war.title + ' wirklich gelöscht werden?')) {
this.warService.deleteWar(war._id)
.subscribe((res) => {
if (this.selectedWarId === war._id) {
this.selectOverview('all');
}
this.campaigns.splice(this.campaigns.indexOf(war), 1);
});
}
}
deleteCampaign(campaign) {
if (confirm('Soll die Kampagne ' + campaign.title + ' wirklich gelöscht werden?')) {
this.campaignService.deleteCampaign(campaign._id)
.subscribe((res) => {
if (this.selectedWarId === campaign._id) {
this.selectOverview('all');
}
this.campaigns.splice(this.campaigns.indexOf(campaign), 1);
});
}
}
}