Some fixes, draw lines on board, add 7 variants
[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"] }}
1e02d16d 25 button(
659a9bd2 26 :class="btnTooltipClass()"
feaf1bf7
BA
27 onClick="window.doClick('modalAdjust')"
28 :aria-label="st.tr['Resize board']"
29 )
30 img.inline(src="/images/icons/resize.svg")
1e02d16d
BA
31 button#analyzeBtn(
32 v-if="canAnalyze"
659a9bd2 33 :class="btnTooltipClass()"
b83a675a
BA
34 @click="$emit('analyze')"
35 :aria-label="st.tr['Analyse']"
36 )
37 img.inline(src="/images/icons/analyse.svg")
feaf1bf7
BA
38 #downloadDiv(v-if="canDownload")
39 a#download(href="#")
1e02d16d 40 button(
659a9bd2 41 :class="btnTooltipClass()"
feaf1bf7
BA
42 @click="$emit('download')"
43 :aria-label="st.tr['Download'] + ' PGN'"
44 )
45 img.inline(src="/images/icons/download.svg")
e71161fb 46 #scoreInfo(v-if="score!='*'")
feaf1bf7
BA
47 span.score {{ score }}
48 span.score-msg {{ st.tr[message] }}
57eb158f 49 .moves-list(v-if="!['none','highlight'].includes(show)")
e71161fb 50 .tr(v-for="moveIdx in evenNumbers")
fef153df 51 .td {{ firstNum + moveIdx / 2 }}
e71161fb
BA
52 .td(v-if="moveIdx < moves.length-1 || show == 'all'"
53 :class="{'highlight-lm': cursor == moveIdx}"
54 @click="() => gotoMove(moveIdx)"
55 )
56 | {{ notation(moves[moveIdx]) }}
57 .td(
58 v-if="moveIdx < moves.length-1"
b866a62a 59 :class="{'highlight-lm': highlightBlackmove(moveIdx+1)}"
e71161fb
BA
60 @click="() => gotoMove(moveIdx+1)"
61 )
fef153df 62 | {{ notation(moves[moveIdx + 1]) }}
e71161fb
BA
63</template>
64
cf2343ce 65<script>
fcd299a3 66import { store } from "@/store";
e71161fb 67import { getFullNotation } from "@/utils/notation";
5b3dc10e 68import { processModalClick } from "@/utils/modalClick";
cf2343ce 69export default {
6808d7a1 70 name: "my-move-list",
feaf1bf7
BA
71 props: [
72 "moves", "show", "canAnalyze", "canDownload",
73 "cursor", "score", "message", "firstNum"],
311cba76
BA
74 data: function() {
75 return {
76 st: store.state
77 };
78 },
5b3dc10e 79 mounted: function() {
42a92848
BA
80 document.getElementById("adjuster")
81 .addEventListener("click", processModalClick);
5b3dc10e
BA
82 // Take full width on small screens:
83 let boardSize = parseInt(localStorage.getItem("boardSize"));
84 if (!boardSize) {
85 boardSize =
86 window.innerWidth >= 768
87 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
88 : window.innerWidth;
89 }
90 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
91 document.getElementById("boardContainer").style.width = boardSize + "px";
92 let gameContainer = document.getElementById("gameContainer");
93 gameContainer.style.width = boardSize + movesWidth + "px";
94 document.getElementById("boardSize").value =
95 (boardSize * 100) / (window.innerWidth - movesWidth);
107dc1bd
BA
96 window.addEventListener("resize", this.adjustBoard);
97 },
98 beforeDestroy: function() {
99 window.removeEventListener("resize", this.adjustBoard);
5b3dc10e 100 },
c6b8d37f
BA
101 watch: {
102 cursor: function(newCursor) {
103 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
c6b8d37f
BA
104 // $nextTick to wait for table > tr to be rendered
105 this.$nextTick(() => {
3c186115 106 let curMove = document.querySelector(".td.highlight-lm");
2c5d7b20 107 if (!curMove && this.moves.length > 0) {
c51c301f 108 // Cursor is before game beginning, and some moves were made:
2c5d7b20
BA
109 curMove =
110 document.querySelector(".moves-list > .tr:first-child > .td");
111 }
3c186115
BA
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 },
08a5069c
BA
130 highlightBlackmove: function(moveIdx) {
131 return (
b866a62a 132 this.cursor == moveIdx ||
e6bcc1d8
BA
133 (
134 // If display by rows, hightlight last black move while the white
135 // move is being played:
136 this.show == "byrow" &&
137 this.cursor == moveIdx + 1 &&
138 // ...except if cursor is behind in the game:
139 this.cursor == this.moves.length - 1
140 )
08a5069c
BA
141 );
142 },
1e02d16d
BA
143 btnTooltipClass: function() {
144 return { tooltip: !("ontouchstart" in window) };
145 },
dac39588 146 gotoMove: function(index) {
107dc1bd
BA
147 // Goto move except if click on current move:
148 if (this.cursor != index) this.$emit("goto-move", index);
5b3dc10e
BA
149 },
150 adjustBoard: function() {
151 const boardContainer = document.getElementById("boardContainer");
152 if (!boardContainer) return; //no board on page
153 const k = document.getElementById("boardSize").value;
154 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
155 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
156 // Value of 0 is board min size; 100 is window.width [- movesWidth]
157 const boardSize =
158 minBoardWidth +
159 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
160 localStorage.setItem("boardSize", boardSize);
161 boardContainer.style.width = boardSize + "px";
162 document.getElementById("gameContainer").style.width =
163 boardSize + movesWidth + "px";
107dc1bd 164 this.$emit("redraw-board");
6808d7a1
BA
165 }
166 }
430a2038
BA
167};
168</script>
169
170<style lang="sass" scoped>
171.moves-list
28b32b4f 172 user-select: none
8477e53d
BA
173 cursor: pointer
174 min-height: 1px
175 max-height: 500px
176 overflow: auto
177 background-color: white
178 width: 280px
179 & > .tr
180 clear: both
181 border-bottom: 1px solid lightgrey
182 & > .td
183 float: left
f9c36b2d 184 padding: 2% 0 2% 2%
8477e53d
BA
185 &:first-child
186 color: grey
f9c36b2d 187 width: 13%
8477e53d 188 &:not(first-child)
f9c36b2d 189 width: 40.5%
8477e53d
BA
190
191@media screen and (max-width: 767px)
192 .moves-list
193 width: 100%
910d631b 194
8477e53d 195.td.highlight-lm
430a2038 196 background-color: plum
5b3dc10e
BA
197
198#boardSizeBtnContainer
199 width: 100%
200 text-align: center
201
5b3dc10e
BA
202[type="checkbox"]#modalAdjust+div .card
203 padding: 5px
feaf1bf7
BA
204
205img.inline
54ec15eb 206 height: 22px
feaf1bf7
BA
207 @media screen and (max-width: 767px)
208 height: 18px
209
0cc44e58
BA
210#scoreInfo
211 margin: 10px 0
212 @media screen and (max-width: 767px)
213 margin: 5px 0
214
feaf1bf7
BA
215span.score
216 display: inline-block
217 margin-left: 10px
218 font-weight: bold
219
220span.score-msg
221 display: inline-block
222 margin-left: 10px
223 font-style: italic
224
225#downloadDiv
226 display: inline-block
227 margin: 0
228
229span#rulesBtn
230 cursor: pointer
231 display: inline-block
232 margin: 0 10px
233 font-weight: bold
234
235button
236 margin: 0
07052665 237 &.active
236485b5 238 background-color: #48C9B0
5b4de147 239
42a92848 240#aboveMoves button
5b4de147 241 padding-bottom: 5px
430a2038 242</style>