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