Fix error iterating on getElementsByClassName on mobile browsers
[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);
5b4de147
BA
80 if ("ontouchstart" in window) {
81 // Disable tooltips on smartphones:
abb5917e
BA
82 document.querySelectorAll("#aboveMoves .tooltip").forEach(elt => {
83 elt.classList.remove("tooltip")
5b4de147
BA
84 });
85 }
5b3dc10e
BA
86 // Take full width on small screens:
87 let boardSize = parseInt(localStorage.getItem("boardSize"));
88 if (!boardSize) {
89 boardSize =
90 window.innerWidth >= 768
91 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
92 : window.innerWidth;
93 }
94 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
95 document.getElementById("boardContainer").style.width = boardSize + "px";
96 let gameContainer = document.getElementById("gameContainer");
97 gameContainer.style.width = boardSize + movesWidth + "px";
98 document.getElementById("boardSize").value =
99 (boardSize * 100) / (window.innerWidth - movesWidth);
100 // timeout to avoid calling too many time the adjust method
101 let timeoutLaunched = false;
102 window.addEventListener("resize", () => {
103 if (!timeoutLaunched) {
104 timeoutLaunched = true;
105 setTimeout(() => {
106 this.adjustBoard();
107 timeoutLaunched = false;
108 }, 500);
109 }
110 });
111 },
c6b8d37f
BA
112 watch: {
113 cursor: function(newCursor) {
114 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
c6b8d37f
BA
115 // $nextTick to wait for table > tr to be rendered
116 this.$nextTick(() => {
3c186115
BA
117 let curMove = document.querySelector(".td.highlight-lm");
118 if (!curMove && this.moves.length > 0)
c51c301f 119 // Cursor is before game beginning, and some moves were made:
3c186115
BA
120 curMove = document.querySelector(".moves-list > .tr:first-child > .td");
121 if (!!curMove) {
122 curMove.scrollIntoView({
c6b8d37f
BA
123 behavior: "auto",
124 block: "nearest"
125 });
126 }
127 });
128 }
129 },
e71161fb
BA
130 computed: {
131 evenNumbers: function() {
132 return [...Array(this.moves.length).keys()].filter(i => i%2==0);
133 }
134 },
430a2038 135 methods: {
e71161fb
BA
136 notation: function(move) {
137 return getFullNotation(move);
138 },
dac39588
BA
139 gotoMove: function(index) {
140 this.$emit("goto-move", index);
5b3dc10e
BA
141 },
142 adjustBoard: function() {
143 const boardContainer = document.getElementById("boardContainer");
144 if (!boardContainer) return; //no board on page
145 const k = document.getElementById("boardSize").value;
146 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
147 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
148 // Value of 0 is board min size; 100 is window.width [- movesWidth]
149 const boardSize =
150 minBoardWidth +
151 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
152 localStorage.setItem("boardSize", boardSize);
153 boardContainer.style.width = boardSize + "px";
154 document.getElementById("gameContainer").style.width =
155 boardSize + movesWidth + "px";
6808d7a1
BA
156 }
157 }
430a2038
BA
158};
159</script>
160
161<style lang="sass" scoped>
162.moves-list
8477e53d
BA
163 cursor: pointer
164 min-height: 1px
165 max-height: 500px
166 overflow: auto
167 background-color: white
168 width: 280px
169 & > .tr
170 clear: both
171 border-bottom: 1px solid lightgrey
172 & > .td
173 float: left
f9c36b2d 174 padding: 2% 0 2% 2%
8477e53d
BA
175 &:first-child
176 color: grey
f9c36b2d 177 width: 13%
8477e53d 178 &:not(first-child)
f9c36b2d 179 width: 40.5%
8477e53d
BA
180
181@media screen and (max-width: 767px)
182 .moves-list
183 width: 100%
910d631b 184
8477e53d 185.td.highlight-lm
430a2038 186 background-color: plum
5b3dc10e
BA
187
188#boardSizeBtnContainer
189 width: 100%
190 text-align: center
191
5b3dc10e
BA
192[type="checkbox"]#modalAdjust+div .card
193 padding: 5px
feaf1bf7
BA
194
195img.inline
196 height: 24px
197 @media screen and (max-width: 767px)
198 height: 18px
199
0cc44e58
BA
200#scoreInfo
201 margin: 10px 0
202 @media screen and (max-width: 767px)
203 margin: 5px 0
204
feaf1bf7
BA
205span.score
206 display: inline-block
207 margin-left: 10px
208 font-weight: bold
209
210span.score-msg
211 display: inline-block
212 margin-left: 10px
213 font-style: italic
214
215#downloadDiv
216 display: inline-block
217 margin: 0
218
219span#rulesBtn
220 cursor: pointer
221 display: inline-block
222 margin: 0 10px
223 font-weight: bold
224
225button
226 margin: 0
5b4de147
BA
227
228button.tooltip
229 padding-bottom: 5px
430a2038 230</style>