1 Vue
.component('my-board', {
2 // Last move cannot be guessed from here, and is required to highlight squares
3 // vr: object to check moves, print board...
4 // mode: HH, HC or analyze
5 // userColor: for mode HH or HC
6 props: ["vr","lastMove","mode","orientation","userColor"],
9 hints: (!localStorage
["hints"] ? true : localStorage
["hints"] === "1"),
10 bcolor: localStorage
["bcolor"] || "lichess", //lichess, chesscom or chesstempo
11 possibleMoves: [], //filled after each valid click/dragstart
12 choices: [], //promotion pieces, or checkered captures... (as moves)
13 selectedPiece: null, //moving piece (or clicked piece)
15 start: {}, //pixels coordinates + id of starting square (click or drag)
21 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
22 // Precompute hints squares to facilitate rendering
23 let hintSquares
= doubleArray(sizeX
, sizeY
, false);
24 this.possibleMoves
.forEach(m
=> { hintSquares
[m
.end
.x
][m
.end
.y
] = true; });
25 // Also precompute in-check squares
26 let incheckSq
= doubleArray(sizeX
, sizeY
, false);
27 this.incheck
.forEach(sq
=> { incheckSq
[sq
[0]][sq
[1]] = true; });
28 const squareWidth
= 40; //TODO: compute this
32 attrs: { "id": "choices" },
33 'class': { 'row': true },
35 "display": this.choices
.length
>0?"block":"none",
36 "top": "-" + ((sizeY
/2)*squareWidth+squareWidth/2) + "px",
37 "width": (this.choices
.length
* squareWidth
) + "px",
38 "height": squareWidth
+ "px",
41 this.choices
.map(m
=> { //a "choice" is a move
46 ['board'+sizeY
]: true,
49 'width': (100/this.choices
.length
) + "%",
50 'padding-bottom': (100/this.choices
.length
) + "%",
55 attrs: { "src": '/images/pieces/' +
56 V
.getPpath(m
.appear
[0].c
+m
.appear
[0].p
) + '.svg' },
57 'class': { 'choice-piece': true },
59 "click": e
=> { this.play(m
); this.choices
=[]; },
60 // NOTE: add 'touchstart' event to fix a problem on smartphones
61 "touchstart": e
=> { this.play(m
); this.choices
=[]; },
68 // Create board element (+ reserves if needed by variant or mode)
69 const lm
= this.lastMove
;
70 const showLight
= this.hints
&& variant
.name
!= "Dark";
79 [_
.range(sizeX
).map(i
=> {
80 let ci
= (this.orientation
=='w' ? i : sizeX
-i
-1);
87 style: { 'opacity': this.choices
.length
>0?"0.5":"1" },
89 _
.range(sizeY
).map(j
=> {
90 let cj
= (this.orientation
=='w' ? j : sizeY
-j
-1);
92 if (this.vr
.board
[ci
][cj
] != V
.EMPTY
&& (variant
.name
!="Dark"
93 || this.gameOver
|| this.mode
== "analyze"
94 || this.vr
.enlightened
[this.userColor
][ci
][cj
]))
102 'ghost': !!this.selectedPiece
103 && this.selectedPiece
.parentNode
.id
== "sq-"+ci
+"-"+cj
,
106 src: "/images/pieces/" +
107 V
.getPpath(this.vr
.board
[ci
][cj
]) + ".svg",
113 if (this.hints
&& hintSquares
[ci
][cj
])
123 src: "/images/mark.svg",
134 ['board'+sizeY
]: true,
135 'light-square': (i
+j
)%2==0,
136 'dark-square': (i
+j
)%2==1,
138 'in-shadow': variant
.name
=="Dark" && !this.gameOver
139 && this.mode
!= "analyze"
140 && !this.vr
.enlightened
[this.userColor
][ci
][cj
],
141 'highlight': showLight
&& !!lm
&& _
.isMatch(lm
.end
, {x:ci
,y:cj
}),
142 'incheck': showLight
&& incheckSq
[ci
][cj
],
145 id: getSquareId({x:ci
,y:cj
}),
154 let elementArray
= [choices
, gameDiv
];
155 if (!!this.vr
.reserve
)
157 const shiftIdx
= (this.userColor
=="w" ? 0 : 1);
158 let myReservePiecesArray
= [];
159 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
161 myReservePiecesArray
.push(h('div',
163 'class': {'board':true, ['board'+sizeY
]:true},
164 attrs: { id: getSquareId({x:sizeX
+shiftIdx
,y:i
}) }
169 'class': {"piece":true, "reserve":true},
171 "src": "/images/pieces/" +
172 this.vr
.getReservePpath(this.userColor
,i
) + ".svg",
176 {"class": { "reserve-count": true } },
177 [ this.vr
.reserve
[this.userColor
][V
.RESERVE_PIECES
[i
]] ]
181 let oppReservePiecesArray
= [];
182 const oppCol
= V
.GetOppCol(this.userColor
);
183 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
185 oppReservePiecesArray
.push(h('div',
187 'class': {'board':true, ['board'+sizeY
]:true},
188 attrs: { id: getSquareId({x:sizeX
+(1-shiftIdx
),y:i
}) }
193 'class': {"piece":true, "reserve":true},
195 "src": "/images/pieces/" +
196 this.vr
.getReservePpath(oppCol
,i
) + ".svg",
200 {"class": { "reserve-count": true } },
201 [ this.vr
.reserve
[oppCol
][V
.RESERVE_PIECES
[i
]] ]
205 let reserves
= h('div',
217 "reserve-row-1": true,
223 { 'class': { 'row': true }},
224 oppReservePiecesArray
228 elementArray
.push(reserves
);
236 "col-md-offset-1":true,
238 "col-lg-offset-2":true,
240 // NOTE: click = mousedown + mouseup
242 mousedown: this.mousedown
,
243 mousemove: this.mousemove
,
244 mouseup: this.mouseup
,
245 touchstart: this.mousedown
,
246 touchmove: this.mousemove
,
247 touchend: this.mouseup
,
254 mousedown: function(e
) {
255 e
= e
|| window
.event
;
258 while (!ingame
&& elem
!== null)
260 if (elem
.classList
.contains("game"))
265 elem
= elem
.parentElement
;
267 if (!ingame
) //let default behavior (click on button...)
269 e
.preventDefault(); //disable native drag & drop
270 if (!this.selectedPiece
&& e
.target
.classList
.contains("piece"))
272 // Next few lines to center the piece on mouse cursor
273 let rect
= e
.target
.parentNode
.getBoundingClientRect();
275 x: rect
.x
+ rect
.width
/2,
276 y: rect
.y
+ rect
.width
/2,
277 id: e
.target
.parentNode
.id
279 this.selectedPiece
= e
.target
.cloneNode();
280 this.selectedPiece
.style
.position
= "absolute";
281 this.selectedPiece
.style
.top
= 0;
282 this.selectedPiece
.style
.display
= "inline-block";
283 this.selectedPiece
.style
.zIndex
= 3000;
284 const startSquare
= getSquareFromId(e
.target
.parentNode
.id
);
285 this.possibleMoves
= [];
286 const color
= this.mode
=="analyze" || this.gameOver
289 if (this.vr
.canIplay(color
,startSquare
))
290 this.possibleMoves
= this.vr
.getPossibleMovesFrom(startSquare
);
291 // Next line add moving piece just after current image
292 // (required for Crazyhouse reserve)
293 e
.target
.parentNode
.insertBefore(this.selectedPiece
, e
.target
.nextSibling
);
296 mousemove: function(e
) {
297 if (!this.selectedPiece
)
299 e
= e
|| window
.event
;
300 // If there is an active element, move it around
301 if (!!this.selectedPiece
)
303 const [offsetX
,offsetY
] = !!e
.clientX
304 ? [e
.clientX
,e
.clientY
] //desktop browser
305 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
]; //smartphone
306 this.selectedPiece
.style
.left
= (offsetX
-this.start
.x
) + "px";
307 this.selectedPiece
.style
.top
= (offsetY
-this.start
.y
) + "px";
310 mouseup: function(e
) {
311 if (!this.selectedPiece
)
313 e
= e
|| window
.event
;
314 // Read drop target (or parentElement, parentNode... if type == "img")
315 this.selectedPiece
.style
.zIndex
= -3000; //HACK to find square from final coords
316 const [offsetX
,offsetY
] = !!e
.clientX
317 ? [e
.clientX
,e
.clientY
]
318 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
];
319 let landing
= document
.elementFromPoint(offsetX
, offsetY
);
320 this.selectedPiece
.style
.zIndex
= 3000;
321 // Next condition: classList.contains(piece) fails because of marks
322 while (landing
.tagName
== "IMG")
323 landing
= landing
.parentNode
;
324 if (this.start
.id
== landing
.id
)
326 // A click: selectedPiece and possibleMoves are already filled
329 // OK: process move attempt
330 let endSquare
= getSquareFromId(landing
.id
);
331 let moves
= this.findMatchingMoves(endSquare
);
332 this.possibleMoves
= [];
333 if (moves
.length
> 1)
334 this.choices
= moves
;
335 else if (moves
.length
==1)
337 // Else: impossible move
338 this.selectedPiece
.parentNode
.removeChild(this.selectedPiece
);
339 delete this.selectedPiece
;
340 this.selectedPiece
= null;
342 findMatchingMoves: function(endSquare
) {
343 // Run through moves list and return the matching set (if promotions...)
345 this.possibleMoves
.forEach(function(m
) {
346 if (endSquare
[0] == m
.end
.x
&& endSquare
[1] == m
.end
.y
)
351 play: function(move) {
352 this.$emit('play-move', move);