| 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 | div#boardSizeBtnContainer |
| 19 | button#boardSizeBtn(onClick="window.doClick('modalAdjust')") |
| 20 | | {{ st.tr["Set board size"] }} |
| 21 | #scoreInfo(v-if="score!='*'") |
| 22 | p {{ score }} |
| 23 | p {{ st.tr[message] }} |
| 24 | .moves-list(v-if="!['none','highlight'].includes(show)") |
| 25 | .tr(v-for="moveIdx in evenNumbers") |
| 26 | .td {{ firstNum + moveIdx / 2 + 1 }} |
| 27 | .td(v-if="moveIdx < moves.length-1 || show == 'all'" |
| 28 | :class="{'highlight-lm': cursor == moveIdx}" |
| 29 | @click="() => gotoMove(moveIdx)" |
| 30 | ) |
| 31 | | {{ notation(moves[moveIdx]) }} |
| 32 | .td( |
| 33 | v-if="moveIdx < moves.length-1" |
| 34 | :class="{'highlight-lm': cursor == moveIdx+1}" |
| 35 | @click="() => gotoMove(moveIdx+1)" |
| 36 | ) |
| 37 | | {{ notation(moves[moveIdx+1]) }} |
| 38 | </template> |
| 39 | |
| 40 | <script> |
| 41 | import { store } from "@/store"; |
| 42 | import { getFullNotation } from "@/utils/notation"; |
| 43 | import { processModalClick } from "@/utils/modalClick"; |
| 44 | export default { |
| 45 | name: "my-move-list", |
| 46 | props: ["moves", "show", "cursor", "score", "message", "firstNum"], |
| 47 | data: function() { |
| 48 | return { |
| 49 | st: store.state |
| 50 | }; |
| 51 | }, |
| 52 | mounted: function() { |
| 53 | document.getElementById("adjuster").addEventListener( |
| 54 | "click", |
| 55 | processModalClick); |
| 56 | // Take full width on small screens: |
| 57 | let boardSize = parseInt(localStorage.getItem("boardSize")); |
| 58 | if (!boardSize) { |
| 59 | boardSize = |
| 60 | window.innerWidth >= 768 |
| 61 | ? 0.75 * Math.min(window.innerWidth, window.innerHeight) |
| 62 | : window.innerWidth; |
| 63 | } |
| 64 | const movesWidth = window.innerWidth >= 768 ? 280 : 0; |
| 65 | document.getElementById("boardContainer").style.width = boardSize + "px"; |
| 66 | let gameContainer = document.getElementById("gameContainer"); |
| 67 | gameContainer.style.width = boardSize + movesWidth + "px"; |
| 68 | document.getElementById("boardSize").value = |
| 69 | (boardSize * 100) / (window.innerWidth - movesWidth); |
| 70 | // timeout to avoid calling too many time the adjust method |
| 71 | let timeoutLaunched = false; |
| 72 | window.addEventListener("resize", () => { |
| 73 | if (!timeoutLaunched) { |
| 74 | timeoutLaunched = true; |
| 75 | setTimeout(() => { |
| 76 | this.adjustBoard(); |
| 77 | timeoutLaunched = false; |
| 78 | }, 500); |
| 79 | } |
| 80 | }); |
| 81 | }, |
| 82 | watch: { |
| 83 | cursor: function(newCursor) { |
| 84 | if (window.innerWidth <= 767) return; //scrolling would hide chessboard |
| 85 | // $nextTick to wait for table > tr to be rendered |
| 86 | this.$nextTick(() => { |
| 87 | let curMove = document.querySelector(".td.highlight-lm"); |
| 88 | if (curMove) { |
| 89 | curMove.scrollIntoView({ |
| 90 | behavior: "auto", |
| 91 | block: "nearest" |
| 92 | }); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | }, |
| 97 | computed: { |
| 98 | evenNumbers: function() { |
| 99 | return [...Array(this.moves.length).keys()].filter(i => i%2==0); |
| 100 | } |
| 101 | }, |
| 102 | methods: { |
| 103 | notation: function(move) { |
| 104 | return getFullNotation(move); |
| 105 | }, |
| 106 | gotoMove: function(index) { |
| 107 | this.$emit("goto-move", index); |
| 108 | }, |
| 109 | adjustBoard: function() { |
| 110 | const boardContainer = document.getElementById("boardContainer"); |
| 111 | if (!boardContainer) return; //no board on page |
| 112 | const k = document.getElementById("boardSize").value; |
| 113 | const movesWidth = window.innerWidth >= 768 ? 280 : 0; |
| 114 | const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary... |
| 115 | // Value of 0 is board min size; 100 is window.width [- movesWidth] |
| 116 | const boardSize = |
| 117 | minBoardWidth + |
| 118 | (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100; |
| 119 | localStorage.setItem("boardSize", boardSize); |
| 120 | boardContainer.style.width = boardSize + "px"; |
| 121 | document.getElementById("gameContainer").style.width = |
| 122 | boardSize + movesWidth + "px"; |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | </script> |
| 127 | |
| 128 | <style lang="sass" scoped> |
| 129 | .moves-list |
| 130 | cursor: pointer |
| 131 | min-height: 1px |
| 132 | max-height: 500px |
| 133 | overflow: auto |
| 134 | background-color: white |
| 135 | width: 280px |
| 136 | & > .tr |
| 137 | clear: both |
| 138 | border-bottom: 1px solid lightgrey |
| 139 | & > .td |
| 140 | float: left |
| 141 | padding: 2% 0 2% 2% |
| 142 | &:first-child |
| 143 | color: grey |
| 144 | width: 13% |
| 145 | &:not(first-child) |
| 146 | width: 40.5% |
| 147 | |
| 148 | @media screen and (max-width: 767px) |
| 149 | .moves-list |
| 150 | width: 100% |
| 151 | |
| 152 | .td.highlight-lm |
| 153 | background-color: plum |
| 154 | |
| 155 | #boardSizeBtnContainer |
| 156 | width: 100% |
| 157 | text-align: center |
| 158 | |
| 159 | button#boardSizeBtn |
| 160 | margin: 0 |
| 161 | |
| 162 | [type="checkbox"]#modalAdjust+div .card |
| 163 | padding: 5px |
| 164 | </style> |