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

313 lines
7.6 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('euromat.results.headline') }}</h1>
2017-08-13 01:23:24 +02:00
</header>
2017-08-10 23:47:40 +02:00
<ul class="party-results">
2017-08-13 23:11:48 +02:00
<li v-for="party of parties">
<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 :src="getPartyLogo(party.token)" width="50" height="50" />
</div>
2017-08-27 14:04:49 +02:00
<h2>
{{ party.token }}
<span>({{ getScorePercentage(party.score) }}%)</span>
</h2>
</div>
2017-08-27 14:04:49 +02:00
<feather-zoom-in class="results-see-more" />
2017-08-15 22:25:25 +02:00
<party-percentage
class="result-percentage"
:value="getScorePercentage(party.score)"
:max="totalScoredPoints" />
</router-link>
2017-08-10 23:47:40 +02:00
</li>
</ul>
2017-08-27 00:50:47 +02:00
<div class="results-ctrls">
<router-link tag="a" class="btn" :to="{ path: '/' }">
{{ $t('euromat.results.buttons.index') }}
</router-link>
2017-08-27 00:50:47 +02:00
<router-link
tag="a"
class="btn btn-dark btn-small"
2017-08-27 00:50:47 +02:00
:to="{ path: this.isGermanLocale ? '/thesen' : '/theses' }">
{{ $t('euromat.results.buttons.startover') }}
<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>
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-13 01:23:24 +02:00
import Progress from '@/components/progress'
2017-08-10 23:47:40 +02:00
2017-08-13 21:42:02 +02:00
// FIXME: There is a bug that not all theses are send to localStorage.
// We have 43 theses now, but only 42 are stored. Hence I check if
// answers is even an object.
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: {
'party-percentage': Progress
},
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: {
isGermanLocale () {
return this.$i18n.locale === 'de'
}
},
2017-08-10 23:47:40 +02:00
methods: {
getPartyPath (token) {
return this.isGermanLocale
? `/partei/${token}`
: `/party/${token}`
},
2017-08-27 14:04:49 +02:00
getPartyLogo (token) {
return require(`@/assets/svg/${token.toLowerCase()}-logo.svg`)
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 }
})
},
getScorePerParty (party) {
return {
token: party.token,
score: this.scores
.map(t => t.scores.find(s => s.party === party.id).score)
.reduce(addUp, 0)
}
2017-08-10 23:14:45 +02:00
}
},
2017-08-13 21:42:02 +02:00
created () {
const emphasized = JSON.parse(sessionStorage.getItem('euromat-emphasized'))
const answers = JSON.parse(sessionStorage.getItem('euromat-answers'))
2017-08-13 21:42:02 +02:00
if (!emphasized) {
this.$router.push({ path: this.isGermanLocale ? '/thesen' : '/theses' })
}
this.emphasized = emphasized
this.answers = answers
2017-08-13 21:42:02 +02:00
2017-08-13 23:11:48 +02:00
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:14:45 +02:00
}
}
</script>
2017-08-10 23:47:40 +02:00
<style lang="scss" scoped>
@import "~styles/animations";
2017-08-27 00:50:47 +02:00
@import "~styles/colors";
2017-08-13 01:23:24 +02:00
@import "~styles/layout";
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-27 00:50:47 +02:00
margin-bottom: $base-gap * 2;
display: flex;
2017-08-13 01:23:24 +02:00
}
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 {
height: 80px;
width: 92%;
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;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
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;
filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.06));
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
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;
}
}
}
2017-08-27 00:50:47 +02:00
.results-ctrls {
margin-top: $base-gap * 2;
border-top: 4px solid $transparent-white;
padding-top: $base-gap * 2;
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>