| 1 | <script> |
| 2 | import { store } from "@/store"; |
| 3 | // Component for moves list on the right |
| 4 | export default { |
| 5 | name: 'my-move-list', |
| 6 | props: ["moves","cursor","score","message","firstNum"], |
| 7 | watch: { |
| 8 | cursor: function(newCursor) { |
| 9 | if (window.innerWidth <= 767) |
| 10 | return; //moves list is below: scrolling would hide chessboard |
| 11 | // Count grouped moves until the cursor (if multi-moves): |
| 12 | let groupsCount = -1; |
| 13 | let curCol = undefined; |
| 14 | for (let i=0; i<newCursor; i++) |
| 15 | { |
| 16 | const m = this.moves[i]; |
| 17 | if (m.color != curCol) |
| 18 | { |
| 19 | groupsCount++; |
| 20 | curCol = m.color; |
| 21 | } |
| 22 | } |
| 23 | // $nextTick to wait for table > tr to be rendered |
| 24 | this.$nextTick( () => { |
| 25 | let rows = document.querySelectorAll('#movesList tr'); |
| 26 | if (rows.length > 0) |
| 27 | { |
| 28 | rows[Math.floor(Math.max(groupsCount,0)/2)].scrollIntoView({ |
| 29 | behavior: "auto", |
| 30 | block: "nearest", |
| 31 | }); |
| 32 | } |
| 33 | }); |
| 34 | }, |
| 35 | }, |
| 36 | render(h) { |
| 37 | if (this.moves.length == 0) |
| 38 | return; |
| 39 | let tableContent = []; |
| 40 | let moveCounter = 0; |
| 41 | let tableRow = undefined; |
| 42 | let moveCells = undefined; |
| 43 | let curCellContent = ""; |
| 44 | let firstIndex = 0; |
| 45 | for (let i=0; i<this.moves.length; i++) |
| 46 | { |
| 47 | if (this.moves[i].color == "w") |
| 48 | { |
| 49 | if (i == 0 || i>0 && this.moves[i-1].color=="b") |
| 50 | { |
| 51 | if (!!tableRow) |
| 52 | { |
| 53 | tableRow.children = moveCells; |
| 54 | tableContent.push(tableRow); |
| 55 | } |
| 56 | moveCells = [ |
| 57 | h( |
| 58 | "td", |
| 59 | { domProps: { innerHTML: (++moveCounter) + "." } } |
| 60 | ) |
| 61 | ]; |
| 62 | tableRow = h( |
| 63 | "tr", |
| 64 | { } |
| 65 | ); |
| 66 | curCellContent = ""; |
| 67 | firstIndex = i; |
| 68 | } |
| 69 | } |
| 70 | // Next condition is fine because even if the first move is black, |
| 71 | // there will be the "..." which count as white move. |
| 72 | else if (this.moves[i].color == "b" && this.moves[i-1].color == "w") |
| 73 | firstIndex = i; |
| 74 | curCellContent += this.moves[i].notation; |
| 75 | if (i < this.moves.length-1 && this.moves[i+1].color == this.moves[i].color) |
| 76 | curCellContent += ","; |
| 77 | else //color change |
| 78 | { |
| 79 | moveCells.push( |
| 80 | h( |
| 81 | "td", |
| 82 | { |
| 83 | domProps: { innerHTML: curCellContent }, |
| 84 | on: { click: () => this.gotoMove(i) }, |
| 85 | "class": { "highlight-lm": |
| 86 | this.cursor >= firstIndex && this.cursor <= i }, |
| 87 | } |
| 88 | ) |
| 89 | ); |
| 90 | curCellContent = ""; |
| 91 | } |
| 92 | } |
| 93 | // Complete last row, which might not be full: |
| 94 | if (moveCells.length-1 == 1) |
| 95 | { |
| 96 | moveCells.push( |
| 97 | h( |
| 98 | "td", |
| 99 | { domProps: { innerHTML: "" } } |
| 100 | ) |
| 101 | ); |
| 102 | } |
| 103 | tableRow.children = moveCells; |
| 104 | tableContent.push(tableRow); |
| 105 | let rootElements = []; |
| 106 | if (this.score != "*") |
| 107 | { |
| 108 | const scoreDiv = h("div", |
| 109 | { |
| 110 | id: "scoreInfo", |
| 111 | style: { |
| 112 | display: this.score!="*" ? "block" : "none", |
| 113 | }, |
| 114 | }, |
| 115 | [ |
| 116 | h("p", this.score), |
| 117 | h("p", store.state.tr[this.message]), |
| 118 | ] |
| 119 | ); |
| 120 | rootElements.push(scoreDiv); |
| 121 | } |
| 122 | rootElements.push( |
| 123 | h( |
| 124 | "table", |
| 125 | { |
| 126 | "class": { |
| 127 | "moves-list": true, |
| 128 | }, |
| 129 | }, |
| 130 | tableContent |
| 131 | ) |
| 132 | ); |
| 133 | return h( |
| 134 | "div", |
| 135 | { }, |
| 136 | rootElements); |
| 137 | }, |
| 138 | methods: { |
| 139 | gotoMove: function(index) { |
| 140 | this.$emit("goto-move", index); |
| 141 | }, |
| 142 | }, |
| 143 | }; |
| 144 | </script> |
| 145 | |
| 146 | <style lang="sass" scoped> |
| 147 | .moves-list |
| 148 | min-width: 250px |
| 149 | @media screen and (max-width: 767px) |
| 150 | .moves-list |
| 151 | tr |
| 152 | display: flex |
| 153 | margin: 0 |
| 154 | padding: 0 |
| 155 | td |
| 156 | text-align: left |
| 157 | td.highlight-lm |
| 158 | background-color: plum |
| 159 | </style> |