Fix scrolling to game beginning
[vchess.git] / client / src / components / ChallengeList.vue
CommitLineData
85e5b5c1 1<template lang="pug">
7aa548e7 2div
1c9826a5 3 table(v-if="challenges.length > 0")
4f887105
BA
4 thead
5 tr
602d6bef 6 th {{ st.tr["Variant"] }}
303ad63c 7 th {{ st.tr["With"] }}
f44fd3bf 8 th {{ st.tr["Cadence"] }}
7ba4a5bc 9 th {{ st.tr["Random?"] }}
4f887105 10 tbody
910d631b
BA
11 tr(
12 v-for="c in sortedChallenges"
13 :class="{toyou:c.priority==1,fromyou:c.priority==2}"
14 @click="$emit('click-challenge',c)"
15 )
bd76b456 16 td {{ c.vname }}
303ad63c 17 td {{ withWho(c) }}
bd76b456 18 td {{ c.cadence }}
7ba4a5bc 19 td(:class="getRandomnessClass(c)")
1c9826a5
BA
20 p(v-else)
21 | {{ st.tr["No challenges found :( Click on 'New game'!"] }}
85e5b5c1
BA
22</template>
23
24<script>
0b0dc040 25import { store } from "@/store";
85e5b5c1
BA
26export default {
27 name: "my-challenge-list",
dac39588 28 props: ["challenges"],
0b0dc040
BA
29 data: function() {
30 return {
6808d7a1 31 st: store.state
0b0dc040
BA
32 };
33 },
34 computed: {
35 sortedChallenges: function() {
36 // Show in order: challenges I sent, challenges I received, other challenges
6808d7a1
BA
37 let minAdded = Number.MAX_SAFE_INTEGER;
38 let maxAdded = 0;
0b0dc040
BA
39 let augmentedChalls = this.challenges.map(c => {
40 let priority = 0;
6808d7a1 41 if (!!c.to && c.to == this.st.user.name) priority = 1;
8f4e861c
BA
42 else if (
43 c.from.sid == this.st.user.sid ||
44 (c.from.id > 0 && c.from.id == this.st.user.id)
45 ) {
0b0dc040 46 priority = 2;
8f4e861c 47 }
6808d7a1
BA
48 if (c.added < minAdded) minAdded = c.added;
49 if (c.added > maxAdded) maxAdded = c.added;
50 return Object.assign({}, c, { priority: priority });
0b0dc040 51 });
bd76b456 52 const deltaAdded = maxAdded - minAdded;
6808d7a1 53 return augmentedChalls.sort((c1, c2) => {
bd76b456
BA
54 return c2.priority - c1.priority + (c2.added - c1.added) / deltaAdded;
55 });
6808d7a1 56 }
303ad63c
BA
57 },
58 methods: {
59 withWho: function(c) {
60 if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
61 return c.to || this.st.tr["Any player"];
62 return c.from.name || "@nonymous";
7ba4a5bc
BA
63 },
64 getRandomnessClass: function(c) {
65 return {
66 ["random-" + c.randomness]: true
67 };
303ad63c 68 }
6808d7a1 69 }
85e5b5c1 70};
85e5b5c1 71</script>
bd76b456
BA
72
73<style lang="sass" scoped>
1c9826a5
BA
74p
75 text-align: center
76 font-weight: bold
77
910d631b 78// NOTE: the style applied to <tr> element doesn't work
bd76b456
BA
79tr.fromyou > td
80 font-style: italic
81tr.toyou > td
82 background-color: #fcd785
7ba4a5bc
BA
83
84tr > td:last-child
85 &.random-0
86 background-color: #FF5733
87 &.random-1
88 background-color: #2B63B4
89 &.random-2
90 background-color: #33B42B
bd76b456 91</style>