2fc7bacbf1f8618ecb500cb9190ba32ee45b12b7
[vchess.git] / client / src / components / MoveList.vue
1 <template lang="pug">
2 div
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 )
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(
26 :class="btnTooltipClass()"
27 onClick="window.doClick('modalAdjust')"
28 :aria-label="st.tr['Resize board']"
29 )
30 img.inline(src="/images/icons/resize.svg")
31 button#analyzeBtn(
32 v-if="canAnalyze"
33 :class="btnTooltipClass()"
34 @click="$emit('analyze')"
35 :aria-label="st.tr['Analyse']"
36 )
37 img.inline(src="/images/icons/analyse.svg")
38 #downloadDiv(v-if="canDownload")
39 a#download(href="#")
40 button(
41 :class="btnTooltipClass()"
42 @click="$emit('download')"
43 :aria-label="st.tr['Download'] + ' PGN'"
44 )
45 img.inline(src="/images/icons/download.svg")
46 #scoreInfo(v-if="score!='*'")
47 span.score {{ score }}
48 span.score-msg {{ st.tr[message] }}
49 .moves-list
50 .tr(v-for="moveIdx in evenNumbers")
51 .td {{ firstNum + moveIdx / 2 }}
52 .td(
53 :class="{'highlight-lm': cursor == moveIdx}"
54 @click="() => gotoMove(moveIdx)"
55 )
56 | {{ notation(moveIdx) }}
57 .td(
58 v-if="moveIdx < moves.length-1"
59 :class="{'highlight-lm': cursor == moveIdx+1}"
60 @click="() => gotoMove(moveIdx+1)"
61 )
62 | {{ notation(moveIdx + 1) }}
63 </template>
64
65 <script>
66 import { store } from "@/store";
67 import { getFullNotation } from "@/utils/notation";
68 import { processModalClick } from "@/utils/modalClick";
69 export default {
70 name: "my-move-list",
71 props: [
72 "moves", "show", "canAnalyze", "canDownload",
73 "cursor", "score", "message", "firstNum"],
74 data: function() {
75 return {
76 st: store.state
77 };
78 },
79 mounted: function() {
80 document.getElementById("adjuster")
81 .addEventListener("click", processModalClick);
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);
96 window.addEventListener("resize", this.adjustBoard);
97 },
98 beforeDestroy: function() {
99 window.removeEventListener("resize", this.adjustBoard);
100 },
101 watch: {
102 cursor: function(newCursor) {
103 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
104 // $nextTick to wait for table > tr to be rendered
105 this.$nextTick(() => {
106 let curMove = document.querySelector(".td.highlight-lm");
107 if (!curMove && this.moves.length > 0) {
108 // Cursor is before game beginning, and some moves were made:
109 curMove =
110 document.querySelector(".moves-list > .tr:first-child > .td");
111 }
112 if (!!curMove) {
113 curMove.scrollIntoView({
114 behavior: "auto",
115 block: "nearest"
116 });
117 }
118 });
119 }
120 },
121 computed: {
122 evenNumbers: function() {
123 return [...Array(this.moves.length).keys()].filter(i => i%2==0);
124 }
125 },
126 methods: {
127 notation: function(moveIdx) {
128 const move = this.moves[moveIdx];
129 if (this.score != "*") return getFullNotation(move);
130 if (
131 ['none','highlight'].includes(this.show) ||
132 (
133 this.show == "byrow" &&
134 moveIdx == this.moves.length-1 &&
135 moveIdx % 2 == 0
136 )
137 ) {
138 return "?";
139 }
140 return getFullNotation(move);
141 },
142 btnTooltipClass: function() {
143 return { tooltip: !("ontouchstart" in window) };
144 },
145 gotoMove: function(index) {
146 // Goto move except if click on current move:
147 if (this.cursor != index) this.$emit("goto-move", index);
148 },
149 adjustBoard: function() {
150 const boardContainer = document.getElementById("boardContainer");
151 if (!boardContainer) return; //no board on page
152 const k = document.getElementById("boardSize").value;
153 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
154 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
155 // Value of 0 is board min size; 100 is window.width [- movesWidth]
156 const boardSize =
157 minBoardWidth +
158 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
159 localStorage.setItem("boardSize", boardSize);
160 boardContainer.style.width = boardSize + "px";
161 document.getElementById("gameContainer").style.width =
162 boardSize + movesWidth + "px";
163 this.$emit("redraw-board");
164 }
165 }
166 };
167 </script>
168
169 <style lang="sass" scoped>
170 .moves-list
171 user-select: none
172 cursor: pointer
173 min-height: 1px
174 max-height: 500px
175 overflow: auto
176 background-color: white
177 width: 280px
178 & > .tr
179 clear: both
180 border-bottom: 1px solid lightgrey
181 & > .td
182 float: left
183 padding: 2% 0 2% 2%
184 &:first-child
185 color: grey
186 width: 13%
187 &:not(first-child)
188 width: 40.5%
189
190 @media screen and (max-width: 767px)
191 .moves-list
192 width: 100%
193
194 .td.highlight-lm
195 background-color: plum
196
197 #boardSizeBtnContainer
198 width: 100%
199 text-align: center
200
201 [type="checkbox"]#modalAdjust+div .card
202 padding: 5px
203
204 img.inline
205 height: 22px
206 @media screen and (max-width: 767px)
207 height: 18px
208
209 #scoreInfo
210 margin: 10px 0
211 @media screen and (max-width: 767px)
212 margin: 5px 0
213
214 span.score
215 display: inline-block
216 margin-left: 10px
217 font-weight: bold
218
219 span.score-msg
220 display: inline-block
221 margin-left: 10px
222 font-style: italic
223
224 #downloadDiv
225 display: inline-block
226 margin: 0
227
228 span#rulesBtn
229 cursor: pointer
230 display: inline-block
231 margin: 0 10px
232 font-weight: bold
233
234 button
235 margin: 0
236 &.active
237 background-color: #48C9B0
238
239 #aboveMoves button
240 padding-bottom: 5px
241 </style>