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

124 lines
4.1 KiB
TypeScript

import {Component, Input, OnChanges, OnInit, SimpleChanges} 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']
})
export class WarListComponent implements OnInit, OnChanges {
@Input() campaign: Campaign;
@Input() collapsed: boolean;
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) {
}
ngOnChanges(changes: SimpleChanges) {
if (changes.campaign) this.selectedWarId = this.campaign._id;
}
ngOnInit() {
this.campaignService.getAllCampaignsWithWars().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.campaign._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': ['campaign']}}], {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;
setTimeout(_ => {
window.dispatchEvent(new Event('resize'));
});
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});
}
}
selectNewWar() {
this.selectedWarId = null;
this.router.navigate([{outlets: {'right': ['submit-war']}}], {relativeTo: this.route});
}
editWar(warId) {
this.selectedWarId = warId;
this.router.navigate([{outlets: {'right': ['submit-war', warId]}}], {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.forEach(campaign => {
campaign.wars.splice(campaign.wars.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);
});
}
}
editCampaign(selectCampaign) {
this.router.navigate([{outlets: {'right': ['campaign', selectCampaign._id]}}], {relativeTo: this.route});
}
}