228971a9bd6d16bfea77a460aca36e76d75375e2
[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 #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")
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': cursor == moveIdx+1}"
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").addEventListener(
78 "click",
79 processModalClick);
80 // Take full width on small screens:
81 let boardSize = parseInt(localStorage.getItem("boardSize"));
82 if (!boardSize) {
83 boardSize =
84 window.innerWidth >= 768
85 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
86 : window.innerWidth;
87 }
88 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
89 document.getElementById("boardContainer").style.width = boardSize + "px";
90 let gameContainer = document.getElementById("gameContainer");
91 gameContainer.style.width = boardSize + movesWidth + "px";
92 document.getElementById("boardSize").value =
93 (boardSize * 100) / (window.innerWidth - movesWidth);
94 // timeout to avoid calling too many time the adjust method
95 let timeoutLaunched = false;
96 window.addEventListener("resize", () => {
97 if (!timeoutLaunched) {
98 timeoutLaunched = true;
99 setTimeout(() => {
100 this.adjustBoard();
101 timeoutLaunched = false;
102 }, 500);
103 }
104 });
105 },
106 watch: {
107 cursor: function(newCursor) {
108 if (window.innerWidth <= 767) return; //scrolling would hide chessboard
109 // $nextTick to wait for table > tr to be rendered
110 this.$nextTick(() => {
111 let curMove = document.querySelector(".td.highlight-lm");
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(move) {
128 return getFullNotation(move);
129 },
130 gotoMove: function(index) {
131 this.$emit("goto-move", index);
132 },
133 adjustBoard: function() {
134 const boardContainer = document.getElementById("boardContainer");
135 if (!boardContainer) return; //no board on page
136 const k = document.getElementById("boardSize").value;
137 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
138 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
139 // Value of 0 is board min size; 100 is window.width [- movesWidth]
140 const boardSize =
141 minBoardWidth +
142 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
143 localStorage.setItem("boardSize", boardSize);
144 boardContainer.style.width = boardSize + "px";
145 document.getElementById("gameContainer").style.width =
146 boardSize + movesWidth + "px";
147 }
148 }
149 };
150 </script>
151
152 <style lang="sass" scoped>
153 .moves-list
154 cursor: pointer
155 min-height: 1px
156 max-height: 500px
157 overflow: auto
158 background-color: white
159 width: 280px
160 & > .tr
161 clear: both
162 border-bottom: 1px solid lightgrey
163 & > .td
164 float: left
165 padding: 2% 0 2% 2%
166 &:first-child
167 color: grey
168 width: 13%
169 &:not(first-child)
170 width: 40.5%
171
172 @media screen and (max-width: 767px)
173 .moves-list
174 width: 100%
175
176 .td.highlight-lm
177 background-color: plum
178
179 #boardSizeBtnContainer
180 width: 100%
181 text-align: center
182
183 [type="checkbox"]#modalAdjust+div .card
184 padding: 5px
185
186 img.inline
187 height: 24px
188 @media screen and (max-width: 767px)
189 height: 18px
190
191 #scoreInfo
192 margin: 10px 0
193 @media screen and (max-width: 767px)
194 margin: 5px 0
195
196 span.score
197 display: inline-block
198 margin-left: 10px
199 font-weight: bold
200
201 span.score-msg
202 display: inline-block
203 margin-left: 10px
204 font-style: italic
205
206 #downloadDiv
207 display: inline-block
208 margin: 0
209
210 span#rulesBtn
211 cursor: pointer
212 display: inline-block
213 margin: 0 10px
214 font-weight: bold
215
216 button
217 margin: 0
218 </style>