From 8e70ca831fc54dc7876384baf595e91f9dd9b543 Mon Sep 17 00:00:00 2001 From: HardiReady Date: Wed, 8 Aug 2018 21:42:57 +0200 Subject: [PATCH] Redirect to campaign/war/highscore by url --- static/src/app/app.component.ts | 2 +- .../campaign/highscore/highscore.component.ts | 7 +--- static/src/app/statistic/stats.component.ts | 30 +++++++++++++- .../war/war-list/war-list.component.html | 1 - .../war/war-list/war-list.component.ts | 41 +++++++++++-------- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/static/src/app/app.component.ts b/static/src/app/app.component.ts index 78be8bc..aefd2a0 100644 --- a/static/src/app/app.component.ts +++ b/static/src/app/app.component.ts @@ -73,7 +73,7 @@ export class AppComponent implements OnInit { const currentUrl = this.router.url; // scroll to top on route from army overview to user detail and back - if (currentUrl.includes('/overview')) { + if (currentUrl.includes('/overview') || currentUrl.includes('/public')) { this.scrollToTop(); } } diff --git a/static/src/app/statistic/campaign/highscore/highscore.component.ts b/static/src/app/statistic/campaign/highscore/highscore.component.ts index d218b99..163722f 100644 --- a/static/src/app/statistic/campaign/highscore/highscore.component.ts +++ b/static/src/app/statistic/campaign/highscore/highscore.component.ts @@ -10,7 +10,7 @@ import {PlayerUtils} from '../../../utils/player-utils'; @Component({ - selector: 'stats-highscore', + selector: 'cc-stats-highscore', templateUrl: './highscore.component.html', styleUrls: ['./highscore.component.css', '../../../style/list-entry.css', '../../../style/overview.css'] }) @@ -20,8 +20,6 @@ export class StatisticHighScoreComponent implements OnInit { id = ''; - title = ''; - searchTerm = new FormControl(); players: Player = {}; @@ -60,9 +58,6 @@ export class StatisticHighScoreComponent implements OnInit { } initData() { - this.title = this.campaignService.campaigns - .filter(camp => camp._id === this.id).pop().title; - this.playerService.getCampaignHighscore(this.id).subscribe(players => { this.players = players; this.playersStored = players; diff --git a/static/src/app/statistic/stats.component.ts b/static/src/app/statistic/stats.component.ts index 9d7a5cf..72d1043 100644 --- a/static/src/app/statistic/stats.component.ts +++ b/static/src/app/statistic/stats.component.ts @@ -24,11 +24,38 @@ export class StatisticComponent implements OnInit { ngOnInit() { this.campaignService.getAllCampaignsWithWars().subscribe((campaigns) => { this.campaigns = campaigns; + // TODO next line has to die! this.campaignService.campaigns = campaigns; - this.switchCampaign(campaigns[0]) + this.selectedCampaign = this.resolveCampaignFromUrl(); + this.switchCampaign(this.selectedCampaign); }); } + resolveCampaignFromUrl() { + const url = this.router.url; + + const idFetchPattern = /right:.*\/(.*)\)$/; + + if (url.includes('right:overview') || url.includes('right:highscore')) { + const id = idFetchPattern.exec(url)[1]; + if (id === 'all') { + return {_id: id} + } + const filteredCampaigns = this.campaigns.filter(c => c._id === id); + if (filteredCampaigns.length === 1) { + return filteredCampaigns[0]; + } + } + if (url.includes('right:war')) { + const id = idFetchPattern.exec(url)[1]; + const filteredCampaigns = this.campaigns.filter(c => c.wars.filter(war => war._id === id).length > 0); + if (filteredCampaigns.length === 1) { + return filteredCampaigns[0]; + } + } + return this.campaigns[0] + } + switchCampaign(campaign) { this.selectedCampaign = campaign; if (campaign._id === 'all' || this.router.url.includes('/overview/all')) { @@ -36,7 +63,6 @@ export class StatisticComponent implements OnInit { window.dispatchEvent(new Event('resize')); }); } - this.router.navigate([{outlets: {'right': ['overview', campaign._id]}}], {relativeTo: this.route}); } toggleCollapse() { diff --git a/static/src/app/statistic/war/war-list/war-list.component.html b/static/src/app/statistic/war/war-list/war-list.component.html index 265d2a3..84992dc 100644 --- a/static/src/app/statistic/war/war-list/war-list.component.html +++ b/static/src/app/statistic/war/war-list/war-list.component.html @@ -7,7 +7,6 @@ -
diff --git a/static/src/app/statistic/war/war-list/war-list.component.ts b/static/src/app/statistic/war/war-list/war-list.component.ts index 8f21340..55cc855 100644 --- a/static/src/app/statistic/war/war-list/war-list.component.ts +++ b/static/src/app/statistic/war/war-list/war-list.component.ts @@ -1,4 +1,4 @@ -import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core'; +import {AfterViewInit, Component, Input, OnChanges, SimpleChanges} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Campaign, War} from '../../../models/model-interfaces'; import {WarService} from '../../../services/logs/war.service'; @@ -11,7 +11,7 @@ import {RouteConfig} from '../../../app.config'; templateUrl: './war-list.component.html', styleUrls: ['./war-list.component.css'] }) -export class WarListComponent implements OnInit, OnChanges { +export class WarListComponent implements OnChanges, AfterViewInit { @Input() campaign: Campaign; @@ -19,6 +19,8 @@ export class WarListComponent implements OnInit, OnChanges { selectedWarId: string | number; + initialized = false; + public readonly highscore = 'HIGHSCORE'; constructor(private warService: WarService, @@ -28,23 +30,30 @@ export class WarListComponent implements OnInit, OnChanges { private route: ActivatedRoute) { } - ngOnChanges(changes: SimpleChanges) { - if (changes.campaign) { - this.selectedWarId = this.campaign._id; - } + ngAfterViewInit() { + this.initialized = true; } - ngOnInit() { - const url = this.router.url; - const subPathWar = 'war/'; - const subPathOverview = 'overview/'; + ngOnChanges(changes: SimpleChanges) { + if (this.initialized && changes.campaign) { + const url = this.router.url; + const subPathWar = 'war/'; + const subPathHighscore = 'highscore/'; + const subPathOverview = 'overview/'; - 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(')')); + if (url.endsWith(RouteConfig.statsPath) || url.includes(subPathOverview)) { + this.selectOverview(this.campaign._id); + } else { + const idFetchPattern = /right:.*\/(.*)\)$/; + const id = idFetchPattern.exec(url); + if (id.length === 2) { + if (url.includes(subPathWar)) { + this.selectWar(id[1]); + } else if (url.includes(subPathHighscore)) { + this.selectHighscore(id[1]); + } + } + } } }