a350c705b2fb88c3c9aa337a8ff5c65373ab261e
[vchess.git] / client / src / components / ChallengeList.vue
1 <template lang="pug">
2 div
3 table
4 thead
5 tr
6 th {{ st.tr["Variant"] }}
7 th {{ st.tr["With"] }}
8 th {{ st.tr["Cadence"] }}
9 tbody
10 tr(
11 v-for="c in sortedChallenges"
12 :class="{toyou:c.priority==1,fromyou:c.priority==2}"
13 @click="$emit('click-challenge',c)"
14 )
15 td {{ c.vname }}
16 td {{ withWho(c) }}
17 td {{ c.cadence }}
18 </template>
19
20 <script>
21 import { store } from "@/store";
22 export default {
23 name: "my-challenge-list",
24 props: ["challenges"],
25 data: function() {
26 return {
27 st: store.state
28 };
29 },
30 computed: {
31 sortedChallenges: function() {
32 // Show in order: challenges I sent, challenges I received, other challenges
33 let minAdded = Number.MAX_SAFE_INTEGER;
34 let maxAdded = 0;
35 let augmentedChalls = this.challenges.map(c => {
36 let priority = 0;
37 if (!!c.to && c.to == this.st.user.name) priority = 1;
38 else if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
39 priority = 2;
40 if (c.added < minAdded) minAdded = c.added;
41 if (c.added > maxAdded) maxAdded = c.added;
42 return Object.assign({}, c, { priority: priority });
43 });
44 const deltaAdded = maxAdded - minAdded;
45 return augmentedChalls.sort((c1, c2) => {
46 return c2.priority - c1.priority + (c2.added - c1.added) / deltaAdded;
47 });
48 }
49 },
50 methods: {
51 withWho: function(c) {
52 if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
53 return c.to || this.st.tr["Any player"];
54 return c.from.name || "@nonymous";
55 }
56 }
57 };
58 </script>
59
60 <style lang="sass" scoped>
61 // NOTE: the style applied to <tr> element doesn't work
62 tr.fromyou > td
63 font-style: italic
64 tr.toyou > td
65 background-color: #fcd785
66 </style>