Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[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.tooltip(
26 onClick="window.doClick('modalAdjust')"
27 :aria-label="st.tr['Resize board']"
28 )
29 img.inline(src="/images/icons/resize.svg")
30 button.tooltip(
31 v-if="canAnalyze"
32 @click="$emit('analyze')"
33 :aria-label="st.tr['Analyse']"
34 )
35 img.inline(src="/images/icons/analyse.svg")
36 #downloadDiv(v-if="canDownload")
37 a#download(href="#")
38 button.tooltip(
39 @click="$emit('download')"
40 :aria-label="st.tr['Download'] + ' PGN'"
41 )
42 img.inline(src="/images/icons/download.svg")
43 #scoreInfo(v-if="score!='*'")
44 span.score {{ score }}
45 span.score-msg {{ st.tr[message] }}
46 .moves-list(v-if="!['none','highlight'].includes(show)")
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': highlightBlackmove(moveIdx)}"
57 @click="() => gotoMove(moveIdx+1)"
58 )
59 | {{ notation(moves[moveIdx+1]) }}
60 </template>
61
62 <script>
63 import { store } from "@/store";
64 import { getFullNotation } from "@/utils/notation";
65 import { processModalClick } from "@/utils/modalClick";
66 export default {
67 name: "my-move-list",
68 props: [
69 "moves", "show", "canAnalyze", "canDownload",
70 "cursor", "score", "message", "firstNum"],
71 data: function() {
72 return {
73 st: store.state
74 };
75 },
76 mounted: function() {
77 document.getElementById("adjuster")
78 .addEventListener("click", processModalClick);
79 if ("ontouchstart" in window) {
80 // Disable tooltips on smartphones:
81 document.querySelectorAll("#aboveMoves .tooltip").forEach(elt => {
82 elt.classList.remove("tooltip");
83 });
84 }
85 // Take full width on small screens:
86 let boardSize = parseInt(localStorage.getItem("boardSize"));
87 if (!boardSize) {
88 boardSize =
89 window.innerWidth >= 768
90 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
91 : window.innerWidth;
92 }
93 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
94 document.getElementById("boardContainer").style.width = boardSize + "px";
95 let gameContainer = document.getElementById("gameContainer");
96 gameContainer.style.width = boardSize + movesWidth + "px";
97 document.getElementById("boardSize").value =
98 (boardSize * 100) / (window.innerWidth - movesWidth);
99 // timeout to avoid calling too many time the adjust method
100 let timeoutLaunched = false;
101 window.addEventListener("resize", () => {
102 if (!timeoutLaunched) {
103 timeoutLaunched = true;
104 setTimeout(() => {
105 this.adjustBoard();
106 timeoutLaunched = false;
107 }, 500);
108 }
109 });
110 },
111 watch: {
112 cursor: function(newCursor) {
113 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
114 // $nextTick to wait for table > tr to be rendered
115 this.$nextTick(() => {
116 let curMove = document.querySelector(".td.highlight-lm");
117 if (!curMove && this.moves.length > 0) {
118 // Cursor is before game beginning, and some moves were made:
119 curMove =
120 document.querySelector(".moves-list > .tr:first-child > .td");
121 }
122 if (!!curMove) {
123 curMove.scrollIntoView({
124 behavior: "auto",
125 block: "nearest"
126 });
127 }
128 });
129 }
130 },
131 computed: {
132 evenNumbers: function() {
133 return [...Array(this.moves.length).keys()].filter(i => i%2==0);
134 }
135 },
136 methods: {
137 notation: function(move) {
138 return getFullNotation(move);
139 },
140 highlightBlackmove: function(moveIdx) {
141 return (
142 this.cursor == moveIdx + 1 ||
143 (this.show == "byrow" && this.cursor == moveIdx + 2)
144 );
145 },
146 gotoMove: function(index) {
147 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 }
164 }
165 };
166 </script>
167
168 <style lang="sass" scoped>
169 .moves-list
170 user-select: none
171 cursor: pointer
172 min-height: 1px
173 max-height: 500px
174 overflow: auto
175 background-color: white
176 width: 280px
177 & > .tr
178 clear: both
179 border-bottom: 1px solid lightgrey
180 & > .td
181 float: left
182 padding: 2% 0 2% 2%
183 &:first-child
184 color: grey
185 width: 13%
186 &:not(first-child)
187 width: 40.5%
188
189 @media screen and (max-width: 767px)
190 .moves-list
191 width: 100%
192
193 .td.highlight-lm
194 background-color: plum
195
196 #boardSizeBtnContainer
197 width: 100%
198 text-align: center
199
200 [type="checkbox"]#modalAdjust+div .card
201 padding: 5px
202
203 img.inline
204 height: 22px
205 @media screen and (max-width: 767px)
206 height: 18px
207
208 #scoreInfo
209 margin: 10px 0
210 @media screen and (max-width: 767px)
211 margin: 5px 0
212
213 span.score
214 display: inline-block
215 margin-left: 10px
216 font-weight: bold
217
218 span.score-msg
219 display: inline-block
220 margin-left: 10px
221 font-style: italic
222
223 #downloadDiv
224 display: inline-block
225 margin: 0
226
227 span#rulesBtn
228 cursor: pointer
229 display: inline-block
230 margin: 0 10px
231 font-weight: bold
232
233 button
234 margin: 0
235
236 #aboveMoves button
237 padding-bottom: 5px
238 </style>