Fixes
[vchess.git] / client / src / components / MoveList.vue
CommitLineData
430a2038
BA
1<template lang="pug">
2div
3 #scoreInfo(v-if="score!='*'")
4 p {{ score }}
5 p {{ message }}
6 table#movesList
7 tbody
8 tr(v-for="moveIdx in evenNumbers")
9 td {{ moveIdx / 2 + 1 }}
10 td(:class="{'highlight-lm': cursor == moveIdx}"
11 data-label="White move" @click="() => gotoMove(moveIdx)")
12 | {{ moves[moveIdx].notation }}
13 td(v-if="moveIdx < moves.length-1"
14 :class="{'highlight-lm': cursor == moveIdx+1}"
15 data-label="Black move" @click="() => gotoMove(moveIdx+1)")
16 | {{ moves[moveIdx+1].notation }}
17 // Else: just add an empty cell
18 td(v-else)
19</template>
20
cf2343ce 21<script>
a3ab5fdb 22// Component for moves list on the right
cf2343ce
BA
23export default {
24 name: 'my-move-list',
430a2038
BA
25 props: ["moves","cursor","score","message"],
26 watch: {
27 cursor: function(newValue) {
3837d4f7
BA
28 if (newValue < 0)
29 newValue = 0; //avoid rows[-1] --> error
430a2038
BA
30 // $nextTick to wait for table > tr to be rendered
31 this.$nextTick( () => {
32 let rows = document.querySelectorAll('#movesList tr');
33 if (rows.length > 0)
34 {
35 rows[Math.floor(newValue/2)].scrollIntoView({
63ca2b89
BA
36 behavior: "auto",
37 block: "nearest",
430a2038
BA
38 });
39 }
40 });
41 },
42 },
43 computed: {
44 evenNumbers: function() {
45 return [...Array(this.moves.length).keys()].filter(i => i%2==0);
46 },
47 },
48 methods: {
49 gotoMove: function(index) {
50 this.$emit("goto-move", index);
51 },
52 },
53};
54</script>
55
56<style lang="sass" scoped>
57.moves-list
58 min-width: 250px
59td.highlight-lm
60 background-color: plum
61</style>
62
63<!-- Old render method:
64 render(h) {
7ee085c2
BA
65 if (this.moves.length == 0)
66 return;
7ee085c2
BA
67 let tableContent = [];
68 let moveCounter = 0;
69 let tableRow = undefined;
70 let moveCells = undefined;
71 let curCellContent = "";
72 for (let i=0; i<this.moves.length; i++)
73 {
7d9e99bc 74 if (this.moves[i].color == "w")
7ee085c2 75 {
7d9e99bc 76 if (i == 0 || i>0 && this.moves[i-1].color=="b")
7ee085c2
BA
77 {
78 if (!!tableRow)
7d9e99bc
BA
79 {
80 tableRow.children = moveCells;
7ee085c2 81 tableContent.push(tableRow);
7d9e99bc 82 }
7ee085c2
BA
83 moveCells = [
84 h(
85 "td",
7d9e99bc 86 { domProps: { innerHTML: (++moveCounter) + "." } }
7ee085c2
BA
87 )
88 ];
89 tableRow = h(
90 "tr",
7d9e99bc 91 { }
7ee085c2
BA
92 );
93 curCellContent = "";
94 }
7d9e99bc
BA
95 }
96 curCellContent += this.moves[i].notation;
97 if (i < this.moves.length-1 && this.moves[i+1].color == this.moves[i].color)
98 curCellContent += ",";
99 else //color change
100 {
7ee085c2
BA
101 moveCells.push(
102 h(
103 "td",
7d9e99bc
BA
104 {
105 domProps: { innerHTML: curCellContent },
106 on: { click: () => this.gotoMove(i) },
d337a94c 107 "class": { "highlight-lm": this.cursor == i },
7d9e99bc
BA
108 }
109 )
110 );
111 curCellContent = "";
7ee085c2
BA
112 }
113 }
114 // Complete last row, which might not be full:
f21cd6d9 115 if (moveCells.length-1 == 1)
7ee085c2 116 {
f21cd6d9
BA
117 moveCells.push(
118 h(
119 "td",
120 { domProps: { innerHTML: "" } }
121 )
122 );
7ee085c2 123 }
7d9e99bc
BA
124 tableRow.children = moveCells;
125 tableContent.push(tableRow);
430a2038
BA
126 const scoreDiv = h("div",
127 {
128 id: "scoreInfo",
129 style: {
130 display: this.score!="*" ? "block" : "none",
131 },
132 },
133 [
134 h("p", this.score),
135 h("p", this.message),
136 ]
137 );
7ee085c2 138 const movesTable = h(
7e355d68
BA
139 "div",
140 { },
430a2038
BA
141 [
142 scoreDiv,
143 h(
144 "table",
145 {
146 "class": {
147 "moves-list": true,
148 },
6cd07b4d 149 },
430a2038
BA
150 tableContent
151 )
152 ]
7e355d68 153 );
7ee085c2
BA
154 return movesTable;
155 },
430a2038 156-->