Improve style, implement 'next' for corr games. TODO: rematch logic
[vchess.git] / client / src / components / MoveList.vue
CommitLineData
e71161fb
BA
1<template lang="pug">
2div
5b3dc10e
BA
3 input#modalAdjust.modal(type="checkbox")
4 div#adjuster(
5 role="dialog"
6 data-checkbox="modalAdjust"
7 )
8 .card.text-center
9 label.modal-close(for="modalAdjust")
10 label(for="boardSize") {{ st.tr["Board size"] }}
11 input#boardSize.slider(
12 type="range"
13 min="0"
14 max="100"
15 value="50"
16 @input="adjustBoard()"
17 )
feaf1bf7
BA
18 #aboveMoves
19 // NOTE: variants pages already have a "Rules" link on top
20 span#rulesBtn(
21 v-if="!$route.path.match('/variants/')"
22 @click="$emit('showrules')"
23 )
24 | {{ st.tr["Rules"] }}
25 button.tooltip(
26 onClick="window.doClick('modalAdjust')"
27 :aria-label="st.tr['Resize board']"
28 )
29 img.inline(src="/images/icons/resize.svg")
30 #downloadDiv(v-if="canDownload")
31 a#download(href="#")
32 button.tooltip(
33 @click="$emit('download')"
34 :aria-label="st.tr['Download'] + ' PGN'"
35 )
36 img.inline(src="/images/icons/download.svg")
37 button.tooltip(
38 v-if="canAnalyze"
39 @click="$emit('analyze')"
40 :aria-label="st.tr['Analyse']"
41 )
42 img.inline(src="/images/icons/analyse.svg")
e71161fb 43 #scoreInfo(v-if="score!='*'")
feaf1bf7
BA
44 span.score {{ score }}
45 span.score-msg {{ st.tr[message] }}
57eb158f 46 .moves-list(v-if="!['none','highlight'].includes(show)")
e71161fb
BA
47 .tr(v-for="moveIdx in evenNumbers")
48 .td {{ firstNum + moveIdx / 2 + 1 }}
49 .td(v-if="moveIdx < moves.length-1 || show == 'all'"
50 :class="{'highlight-lm': cursor == moveIdx}"
51 @click="() => gotoMove(moveIdx)"
52 )
53 | {{ notation(moves[moveIdx]) }}
54 .td(
55 v-if="moveIdx < moves.length-1"
56 :class="{'highlight-lm': cursor == moveIdx+1}"
57 @click="() => gotoMove(moveIdx+1)"
58 )
59 | {{ notation(moves[moveIdx+1]) }}
e71161fb
BA
60</template>
61
cf2343ce 62<script>
fcd299a3 63import { store } from "@/store";
e71161fb 64import { getFullNotation } from "@/utils/notation";
5b3dc10e 65import { processModalClick } from "@/utils/modalClick";
cf2343ce 66export default {
6808d7a1 67 name: "my-move-list",
feaf1bf7
BA
68 props: [
69 "moves", "show", "canAnalyze", "canDownload",
70 "cursor", "score", "message", "firstNum"],
311cba76
BA
71 data: function() {
72 return {
73 st: store.state
74 };
75 },
5b3dc10e
BA
76 mounted: function() {
77 document.getElementById("adjuster").addEventListener(
78 "click",
79 processModalClick);
80 // Take full width on small screens:
81 let boardSize = parseInt(localStorage.getItem("boardSize"));
82 if (!boardSize) {
83 boardSize =
84 window.innerWidth >= 768
85 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
86 : window.innerWidth;
87 }
88 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
89 document.getElementById("boardContainer").style.width = boardSize + "px";
90 let gameContainer = document.getElementById("gameContainer");
91 gameContainer.style.width = boardSize + movesWidth + "px";
92 document.getElementById("boardSize").value =
93 (boardSize * 100) / (window.innerWidth - movesWidth);
94 // timeout to avoid calling too many time the adjust method
95 let timeoutLaunched = false;
96 window.addEventListener("resize", () => {
97 if (!timeoutLaunched) {
98 timeoutLaunched = true;
99 setTimeout(() => {
100 this.adjustBoard();
101 timeoutLaunched = false;
102 }, 500);
103 }
104 });
105 },
c6b8d37f
BA
106 watch: {
107 cursor: function(newCursor) {
108 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
c6b8d37f
BA
109 // $nextTick to wait for table > tr to be rendered
110 this.$nextTick(() => {
8477e53d
BA
111 let curMove = document.querySelector(".td.highlight-lm");
112 if (curMove) {
113 curMove.scrollIntoView({
c6b8d37f
BA
114 behavior: "auto",
115 block: "nearest"
116 });
117 }
118 });
119 }
120 },
e71161fb
BA
121 computed: {
122 evenNumbers: function() {
123 return [...Array(this.moves.length).keys()].filter(i => i%2==0);
124 }
125 },
430a2038 126 methods: {
e71161fb
BA
127 notation: function(move) {
128 return getFullNotation(move);
129 },
dac39588
BA
130 gotoMove: function(index) {
131 this.$emit("goto-move", index);
5b3dc10e
BA
132 },
133 adjustBoard: function() {
134 const boardContainer = document.getElementById("boardContainer");
135 if (!boardContainer) return; //no board on page
136 const k = document.getElementById("boardSize").value;
137 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
138 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
139 // Value of 0 is board min size; 100 is window.width [- movesWidth]
140 const boardSize =
141 minBoardWidth +
142 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
143 localStorage.setItem("boardSize", boardSize);
144 boardContainer.style.width = boardSize + "px";
145 document.getElementById("gameContainer").style.width =
146 boardSize + movesWidth + "px";
6808d7a1
BA
147 }
148 }
430a2038
BA
149};
150</script>
151
152<style lang="sass" scoped>
153.moves-list
8477e53d
BA
154 cursor: pointer
155 min-height: 1px
156 max-height: 500px
157 overflow: auto
158 background-color: white
159 width: 280px
160 & > .tr
161 clear: both
162 border-bottom: 1px solid lightgrey
163 & > .td
164 float: left
f9c36b2d 165 padding: 2% 0 2% 2%
8477e53d
BA
166 &:first-child
167 color: grey
f9c36b2d 168 width: 13%
8477e53d 169 &:not(first-child)
f9c36b2d 170 width: 40.5%
8477e53d
BA
171
172@media screen and (max-width: 767px)
173 .moves-list
174 width: 100%
910d631b 175
8477e53d 176.td.highlight-lm
430a2038 177 background-color: plum
5b3dc10e
BA
178
179#boardSizeBtnContainer
180 width: 100%
181 text-align: center
182
5b3dc10e
BA
183[type="checkbox"]#modalAdjust+div .card
184 padding: 5px
feaf1bf7
BA
185
186img.inline
187 height: 24px
188 @media screen and (max-width: 767px)
189 height: 18px
190
191span.score
192 display: inline-block
193 margin-left: 10px
194 font-weight: bold
195
196span.score-msg
197 display: inline-block
198 margin-left: 10px
199 font-style: italic
200
201#downloadDiv
202 display: inline-block
203 margin: 0
204
205span#rulesBtn
206 cursor: pointer
207 display: inline-block
208 margin: 0 10px
209 font-weight: bold
210
211button
212 margin: 0
430a2038 213</style>