kandimat-user-app/src/app/euromat/components/results.vue

443 lines
11 KiB
Vue
Raw Normal View History

2017-08-10 23:14:45 +02:00
<template>
<section>
2017-08-13 01:23:24 +02:00
<header class="results-header">
<h1>{{ $t('results.headline') }}</h1>
2017-08-13 01:23:24 +02:00
</header>
2017-08-10 23:47:40 +02:00
2017-08-29 21:20:54 +02:00
<div class="results-content">
<p>{{ $t('results.entry') }}</p>
<span>{{ $t('results.hint') }}</span>
2017-08-29 21:20:54 +02:00
</div>
2017-08-10 23:47:40 +02:00
<ul class="party-results">
2019-03-20 23:39:49 +01:00
<li v-for="party of parties" :key="party.token">
<router-link :to="{ path: getPartyPath(party.token.toLowerCase()) }">
2017-08-27 14:04:49 +02:00
<div class="result-party-info">
<div class="result-party-logo">
<img
v-if="hasPartyLogo(party.token)"
:src="getPartyLogo(party.token)"
2019-03-20 23:39:49 +01:00
width="50"
height="50"
:alt="party.token"
>
<span v-else>{{ party.token }}</span>
2017-08-27 14:04:49 +02:00
</div>
2017-08-27 15:43:14 +02:00
<h2>{{ getScorePercentage(party.score) }}%</h2>
2017-08-27 14:04:49 +02:00
</div>
2017-08-27 14:04:49 +02:00
<feather-zoom-in class="results-see-more" />
2017-08-27 23:03:48 +02:00
<v-progress
2017-08-15 22:25:25 +02:00
class="result-percentage"
2017-08-27 17:01:10 +02:00
:value="party.score"
2019-03-20 23:39:49 +01:00
:max="totalScoredPoints"
/>
2017-08-15 22:25:25 +02:00
</router-link>
<div v-if="party.nationalParty" class="party-results-national">
<feather-corner-down-right />
<span>
{{ $t('results.nationalParty') }}:
<a
class="party-results-national-logo"
:href="party.nationalParty.program"
target="_blank"
rel="noopener"
>
<div v-if="hasPartyLogo(party.nationalParty.token)">
<img
:src="getPartyLogo(party.nationalParty.token)"
:alt="party.nationalParty.name"
:title="party.nationalParty.name"
width="40"
height="40"
>
</div>
<span v-else>{{ party.nationalParty.token }}</span>
</a>
</span>
</div>
2017-08-10 23:47:40 +02:00
</li>
</ul>
2017-08-27 00:50:47 +02:00
<div class="results-ctrls">
<p>{{ $t('results.thanks') }}</p>
2019-03-20 23:39:49 +01:00
<router-link tag="a"
class="btn"
:to="{ path: `/${$i18n.locale}/` }"
2019-03-20 23:39:49 +01:00
>
{{ $t('results.indexBtn') }}
</router-link>
2017-08-27 00:50:47 +02:00
<router-link
tag="a"
class="btn btn-dark btn-small"
:to="{ path: startOverUrl }"
2019-03-20 23:39:49 +01:00
>
{{ $t('results.startoverBtn') }}
<feather-rotate-cw />
2017-08-27 00:50:47 +02:00
</router-link>
</div>
2017-08-10 23:14:45 +02:00
</section>
</template>
<script>
import { getUserLanguage, getTranslatedUrl } from '@/i18n/helper'
2017-08-13 23:11:48 +02:00
import {
MAX_POINTS,
BASE_POINTS,
MIN_POINTS,
EMPHASIS_POINTS,
getScoringGrid
} from '@/app/euromat/scoring'
2017-08-14 22:28:12 +02:00
import { parties } from '@/data'
2017-08-10 23:47:40 +02:00
2017-08-13 23:11:48 +02:00
const addUp = (a, b) => a + b
2017-08-10 23:14:45 +02:00
export default {
name: 'Results',
2017-08-13 01:23:24 +02:00
components: {
'feather-zoom-in': () =>
import('vue-feather-icons/icons/ZoomInIcon' /* webpackChunkName: "icons" */),
'feather-rotate-cw': () =>
import('vue-feather-icons/icons/RotateCwIcon' /* webpackChunkName: "icons" */),
'feather-corner-down-right': () =>
import('vue-feather-icons/icons/CornerDownRightIcon' /* webpackChunkName: "icons" */)
2017-08-13 01:23:24 +02:00
},
2017-08-10 23:14:45 +02:00
data () {
return {
2017-08-13 23:11:48 +02:00
scoringGrid: [],
2017-08-13 21:42:02 +02:00
answers: [],
emphasized: [],
2017-08-13 23:11:48 +02:00
scores: [],
2017-08-14 22:28:12 +02:00
parties,
2017-08-13 21:42:02 +02:00
totalScoredPoints: 0
2017-08-10 23:47:40 +02:00
}
},
computed: {
startOverUrl () {
return getTranslatedUrl('theses')
}
},
2019-03-20 23:39:49 +01:00
created () {
let emphasized
let answers
if (this.$browser.supports('sessionStorage')) {
emphasized = JSON.parse(sessionStorage.getItem('euromat-emphasized'))
answers = JSON.parse(sessionStorage.getItem('euromat-answers'))
} else {
emphasized = JSON.parse(this.$root.$data.backupStorage.emphasized)
answers = JSON.parse(this.$root.$data.backupStorage.answers)
}
if (!emphasized) {
this.$router.push({ path: getTranslatedUrl('theses') })
2019-03-20 23:39:49 +01:00
}
this.emphasized = emphasized
this.answers = answers
this.scoringGrid = getScoringGrid(this.answers, this.emphasized)
this.scores = this.getScorePoints(this.scoringGrid)
this.parties = this.parties
.map(this.getScorePerParty)
.sort((a, b) => a.score - b.score)
.reverse()
this.totalScoredPoints = this.scores
.map(s => s.highestScore)
.reduce(addUp, 0)
},
2017-08-10 23:47:40 +02:00
methods: {
getPartyPath (token) {
return `${getTranslatedUrl('party')}/${token}`
},
2017-08-27 14:04:49 +02:00
getPartyLogo (token) {
try {
2019-04-17 23:13:27 +02:00
return require(`@/assets/svg/${token.toLowerCase().replace(/\s/g, '-')}-logo.svg`)
} catch (error) {
console.warn(`No logo found for party "${token}", falling back to initials.`, error.message)
return false
}
},
hasPartyLogo (token) {
try {
require(`@/assets/svg/${token.toLowerCase().replace(/\s/g, '-')}-logo.svg`)
return true
} catch (error) {
return false
}
2017-08-13 21:42:02 +02:00
},
getScorePercentage (score) {
return (score / this.totalScoredPoints * 100).toFixed(2)
},
2017-08-13 23:11:48 +02:00
evalPoints (party, user, emphasis) {
2017-08-13 21:42:02 +02:00
let score = 0
2017-08-13 23:11:48 +02:00
if (user.position === party.position) {
score = MAX_POINTS
2017-08-13 21:42:02 +02:00
} else if (
2017-08-13 23:11:48 +02:00
(user.position === 'positive' && party.position === 'neutral') ||
(user.position === 'neutral' && party.position === 'positive') ||
(user.position === 'negative' && party.position === 'neutral')
2017-08-13 21:42:02 +02:00
) {
2017-08-13 23:11:48 +02:00
score = BASE_POINTS
2017-08-13 21:42:02 +02:00
} else if (
2017-08-13 23:11:48 +02:00
(user.position === 'positive' && party.position === 'negative') ||
(user.position === 'neutral' && party.position === 'negative') ||
(user.position === 'negative' && party.position === 'positive')
2017-08-13 21:42:02 +02:00
) {
2017-08-13 23:11:48 +02:00
score = MIN_POINTS
2017-08-13 21:42:02 +02:00
}
2017-08-13 23:11:48 +02:00
return {
party: party.party,
score: emphasis ? score * EMPHASIS_POINTS : score
}
},
getHighestScore (scores) {
const highestScore = Math.max(...scores.map(s => s.score))
2017-08-13 21:42:02 +02:00
2017-08-13 23:11:48 +02:00
if (!highestScore) {
return MIN_POINTS
}
return highestScore === 1
? MAX_POINTS
: highestScore
},
getScorePoints (grid) {
// 1. Iterate over scoringGrid
// 2. Get user and party positions of each thesis
// 3. Evaluate points based on calculation model for each party
// 4. Count the highest score per thesis
// 5. Return a new object for each thesis row with results
return grid.map(row => {
2017-08-14 22:28:12 +02:00
const partiesFromRow = row.positions.filter(p => p.type === 'party')
2017-08-13 23:11:48 +02:00
const user = row.positions[row.positions.length - 1]
2017-08-14 22:28:12 +02:00
const scores = partiesFromRow.map(party => this.evalPoints(party, user, row.emphasis))
2017-08-13 23:11:48 +02:00
const highestScore = this.getHighestScore(scores)
return {
thesis: row.thesis,
highestScore,
scores
}
2017-08-13 23:11:48 +02:00
})
},
getScorePerParty (party) {
return {
token: party.token,
score: this.scores
.map(t => t.scores.find(s => s.party === party.id).score)
.reduce(addUp, 0),
nationalParty: party['national_parties'][getUserLanguage().country]
2017-08-13 23:11:48 +02:00
}
2017-08-10 23:14:45 +02:00
}
}
}
</script>
2017-08-10 23:47:40 +02:00
<style lang="scss" scoped>
2019-03-20 23:19:13 +01:00
@import "~@/styles/animations";
@import "~@/styles/colors";
@import "~@/styles/layout";
2017-08-13 01:23:24 +02:00
$result-bar-length: 92%;
2017-08-27 00:50:47 +02:00
section {
width: 95%;
margin: 0 auto;
}
2017-08-13 01:23:24 +02:00
.results-header {
2017-08-29 21:20:54 +02:00
margin-bottom: $base-gap;
2017-08-27 15:43:14 +02:00
h1 {
margin-bottom: $small-gap;
}
2017-08-13 01:23:24 +02:00
}
2017-08-29 21:20:54 +02:00
.results-content {
margin-bottom: $base-gap;
span {
margin-top: $small-gap;
color: $text-color-secondary;
font-size: $font-size-small;
}
}
2017-08-10 23:47:40 +02:00
.party-results {
list-style: none;
width: 100%;
2017-08-27 00:50:47 +02:00
counter-reset: result;
2017-08-10 23:47:40 +02:00
li {
display: flex;
2017-08-13 01:23:24 +02:00
flex-direction: column;
2017-08-27 00:50:47 +02:00
align-items: flex-end;
2017-08-13 01:23:24 +02:00
margin-bottom: $base-gap;
2017-08-27 00:50:47 +02:00
position: relative;
&:hover .results-see-more {
opacity: 1;
}
2017-08-27 00:50:47 +02:00
&::before {
counter-increment: result;
content: counter(result) ".";
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
color: $text-color-secondary;
font-size: $font-size-xlarge;
font-weight: 600;
}
2017-08-27 14:04:49 +02:00
@media (max-width: 650px) {
&::before {
display: none;
}
}
2017-08-27 00:50:47 +02:00
}
a:not(.party-results-national-logo) {
2017-08-27 00:50:47 +02:00
height: 80px;
width: $result-bar-length;
2017-08-27 00:50:47 +02:00
position: relative;
display: flex;
justify-content: space-between;
2017-08-27 00:50:47 +02:00
align-items: center;
2017-08-27 14:04:49 +02:00
@media (max-width: 650px) {
width: 100%;
}
2017-08-27 00:50:47 +02:00
}
h2,
.results-see-more {
2017-08-27 00:50:47 +02:00
position: relative;
z-index: 1;
}
h2 {
2017-08-27 00:50:47 +02:00
color: $text-color-base;
font-weight: 600;
2017-08-27 16:16:05 +02:00
text-shadow: $text-shadow;
2017-08-27 00:50:47 +02:00
span {
font-weight: 400;
}
2017-08-10 23:47:40 +02:00
}
2017-08-15 22:25:25 +02:00
.results-see-more {
stroke: $text-color-base;
2017-08-27 16:16:05 +02:00
filter: drop-shadow($text-shadow);
height: 32px;
width: 32px;
opacity: 0;
transition: opacity 150ms $easeOutBack;
2017-08-27 14:04:49 +02:00
margin-right: $base-gap;
}
2017-08-15 22:25:25 +02:00
.result-percentage {
2017-08-27 00:50:47 +02:00
height: 100%;
position: absolute;
top: 0;
left: 0;
2017-08-15 22:25:25 +02:00
}
2017-08-10 23:47:40 +02:00
}
2017-08-27 00:50:47 +02:00
.party-results-national {
width: $result-bar-length;
display: flex;
justify-content: flex-start;
align-items: center;
padding-top: calc(#{$small-gap} / 2);
padding-left: $small-gap;
svg {
margin-right: calc(#{$small-gap} / 2);
}
> span {
display: inline-flex;
align-items: center;
}
.party-results-national-logo {
display: inline-block;
font-weight: 700;
margin-left: calc(#{$small-gap} / 2);
> div {
width: 50px;
height: auto;
display: inline-flex;
justify-content: center;
align-items: center;
}
img {
object-fit: contain;
width: 80%;
}
}
@media (max-width: 650px) {
width: 100%;
}
}
2017-08-27 14:04:49 +02:00
.result-party-info {
display: flex;
height: calc(100% - 4px);
align-items: center;
justify-content: center;
.result-party-logo {
margin-right: $small-gap;
position: relative;
z-index: 1;
background: $background-secondary;
border-radius: $border-radius;
width: 80px;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
margin-left: 2px;
img {
object-fit: contain;
}
span {
color: $text-color-invert;
font-weight: 700;
}
2017-08-27 14:04:49 +02:00
}
}
2017-08-27 00:50:47 +02:00
.results-ctrls {
margin-top: $base-gap * 2;
border-top: 4px solid $transparent-white;
2017-08-27 15:43:14 +02:00
padding-top: $small-gap;
p {
margin-bottom: $small-gap;
}
2017-08-27 14:04:49 +02:00
a:first-of-type {
margin-right: $small-gap;
}
2017-08-27 00:50:47 +02:00
}
2017-08-10 23:47:40 +02:00
</style>