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 props: ["vr","lastMove","mode","orientation","userColor","gameOver"],
7 hints: (!localStorage
["hints"] ? true : localStorage
["hints"] === "1"),
8 bcolor: localStorage
["bcolor"] || "lichess", //lichess, chesscom or chesstempo
9 possibleMoves: [], //filled after each valid click/dragstart
10 choices: [], //promotion pieces, or checkered captures... (as moves)
11 selectedPiece: null, //moving piece (or clicked piece)
13 start: {}, //pixels coordinates + id of starting square (click or drag)
19 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
20 // Precompute hints squares to facilitate rendering
21 let hintSquares
= doubleArray(sizeX
, sizeY
, false);
22 this.possibleMoves
.forEach(m
=> { hintSquares
[m
.end
.x
][m
.end
.y
] = true; });
23 // Also precompute in-check squares
24 let incheckSq
= doubleArray(sizeX
, sizeY
, false);
25 this.incheck
.forEach(sq
=> { incheckSq
[sq
[0]][sq
[1]] = true; });
26 const squareWidth
= 40; //TODO: compute this
30 attrs: { "id": "choices" },
31 'class': { 'row': true },
33 "display": this.choices
.length
>0?"block":"none",
34 "top": "-" + ((sizeY
/2)*squareWidth+squareWidth/2) + "px",
35 "width": (this.choices
.length
* squareWidth
) + "px",
36 "height": squareWidth
+ "px",
39 this.choices
.map(m
=> { //a "choice" is a move
44 ['board'+sizeY
]: true,
47 'width': (100/this.choices
.length
) + "%",
48 'padding-bottom': (100/this.choices
.length
) + "%",
53 attrs: { "src": '/images/pieces/' +
54 V
.getPpath(m
.appear
[0].c
+m
.appear
[0].p
) + '.svg' },
55 'class': { 'choice-piece': true },
57 "click": e
=> { this.play(m
); this.choices
=[]; },
58 // NOTE: add 'touchstart' event to fix a problem on smartphones
59 "touchstart": e
=> { this.play(m
); this.choices
=[]; },
66 // Create board element (+ reserves if needed by variant or mode)
67 const lm
= this.lastMove
;
68 const showLight
= this.hints
&& variant
.name
!= "Dark";
77 [_
.range(sizeX
).map(i
=> {
78 let ci
= (this.orientation
=='w' ? i : sizeX
-i
-1);
85 style: { 'opacity': this.choices
.length
>0?"0.5":"1" },
87 _
.range(sizeY
).map(j
=> {
88 let cj
= (this.orientation
=='w' ? j : sizeY
-j
-1);
90 if (this.vr
.board
[ci
][cj
] != V
.EMPTY
&& (variant
.name
!="Dark"
91 || this.gameOver
|| this.mode
== "analyze"
92 || this.vr
.enlightened
[this.userColor
][ci
][cj
]))
100 'ghost': !!this.selectedPiece
101 && this.selectedPiece
.parentNode
.id
== "sq-"+ci
+"-"+cj
,
104 src: "/images/pieces/" +
105 V
.getPpath(this.vr
.board
[ci
][cj
]) + ".svg",
111 if (this.hints
&& hintSquares
[ci
][cj
])
121 src: "/images/mark.svg",
132 ['board'+sizeY
]: true,
133 'light-square': (i
+j
)%2==0,
134 'dark-square': (i
+j
)%2==1,
136 'in-shadow': variant
.name
=="Dark" && !this.gameOver
137 && this.mode
!= "analyze"
138 && !this.vr
.enlightened
[this.userColor
][ci
][cj
],
139 'highlight': showLight
&& !!lm
&& _
.isMatch(lm
.end
, {x:ci
,y:cj
}),
140 'incheck': showLight
&& incheckSq
[ci
][cj
],
143 id: this.getSquareId({x:ci
,y:cj
}),
152 let elementArray
= [choices
, gameDiv
];
153 if (!!this.vr
.reserve
)
155 const shiftIdx
= (this.userColor
=="w" ? 0 : 1);
156 let myReservePiecesArray
= [];
157 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
159 myReservePiecesArray
.push(h('div',
161 'class': {'board':true, ['board'+sizeY
]:true},
162 attrs: { id: this.getSquareId({x:sizeX
+shiftIdx
,y:i
}) }
167 'class': {"piece":true, "reserve":true},
169 "src": "/images/pieces/" +
170 this.vr
.getReservePpath(this.userColor
,i
) + ".svg",
174 {"class": { "reserve-count": true } },
175 [ this.vr
.reserve
[this.userColor
][V
.RESERVE_PIECES
[i
]] ]
179 let oppReservePiecesArray
= [];
180 const oppCol
= V
.GetOppCol(this.userColor
);
181 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
183 oppReservePiecesArray
.push(h('div',
185 'class': {'board':true, ['board'+sizeY
]:true},
186 attrs: { id: this.getSquareId({x:sizeX
+(1-shiftIdx
),y:i
}) }
191 'class': {"piece":true, "reserve":true},
193 "src": "/images/pieces/" +
194 this.vr
.getReservePpath(oppCol
,i
) + ".svg",
198 {"class": { "reserve-count": true } },
199 [ this.vr
.reserve
[oppCol
][V
.RESERVE_PIECES
[i
]] ]
203 let reserves
= h('div',
215 "reserve-row-1": true,
221 { 'class': { 'row': true }},
222 oppReservePiecesArray
226 elementArray
.push(reserves
);
234 "col-md-offset-1":true,
236 "col-lg-offset-2":true,
238 // NOTE: click = mousedown + mouseup
240 mousedown: this.mousedown
,
241 mousemove: this.mousemove
,
242 mouseup: this.mouseup
,
243 touchstart: this.mousedown
,
244 touchmove: this.mousemove
,
245 touchend: this.mouseup
,
252 // Get the identifier of a HTML square from its numeric coordinates o.x,o.y.
253 getSquareId: function(o
) {
254 // NOTE: a separator is required to allow any size of board
255 return "sq-" + o
.x
+ "-" + o
.y
;
258 getSquareFromId: function(id
) {
259 let idParts
= id
.split('-');
260 return [parseInt(idParts
[1]), parseInt(idParts
[2])];
262 mousedown: function(e
) {
263 e
= e
|| window
.event
;
266 while (!ingame
&& elem
!== null)
268 if (elem
.classList
.contains("game"))
273 elem
= elem
.parentElement
;
275 if (!ingame
) //let default behavior (click on button...)
277 e
.preventDefault(); //disable native drag & drop
278 if (!this.selectedPiece
&& e
.target
.classList
.contains("piece"))
280 // Next few lines to center the piece on mouse cursor
281 let rect
= e
.target
.parentNode
.getBoundingClientRect();
283 x: rect
.x
+ rect
.width
/2,
284 y: rect
.y
+ rect
.width
/2,
285 id: e
.target
.parentNode
.id
287 this.selectedPiece
= e
.target
.cloneNode();
288 this.selectedPiece
.style
.position
= "absolute";
289 this.selectedPiece
.style
.top
= 0;
290 this.selectedPiece
.style
.display
= "inline-block";
291 this.selectedPiece
.style
.zIndex
= 3000;
292 const startSquare
= this.getSquareFromId(e
.target
.parentNode
.id
);
293 this.possibleMoves
= [];
294 const color
= this.mode
=="analyze" || this.gameOver
297 if (this.vr
.canIplay(color
,startSquare
))
298 this.possibleMoves
= this.vr
.getPossibleMovesFrom(startSquare
);
299 // Next line add moving piece just after current image
300 // (required for Crazyhouse reserve)
301 e
.target
.parentNode
.insertBefore(this.selectedPiece
, e
.target
.nextSibling
);
304 mousemove: function(e
) {
305 if (!this.selectedPiece
)
307 e
= e
|| window
.event
;
308 // If there is an active element, move it around
309 if (!!this.selectedPiece
)
311 const [offsetX
,offsetY
] = !!e
.clientX
312 ? [e
.clientX
,e
.clientY
] //desktop browser
313 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
]; //smartphone
314 this.selectedPiece
.style
.left
= (offsetX
-this.start
.x
) + "px";
315 this.selectedPiece
.style
.top
= (offsetY
-this.start
.y
) + "px";
318 mouseup: function(e
) {
319 if (!this.selectedPiece
)
321 e
= e
|| window
.event
;
322 // Read drop target (or parentElement, parentNode... if type == "img")
323 this.selectedPiece
.style
.zIndex
= -3000; //HACK to find square from final coords
324 const [offsetX
,offsetY
] = !!e
.clientX
325 ? [e
.clientX
,e
.clientY
]
326 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
];
327 let landing
= document
.elementFromPoint(offsetX
, offsetY
);
328 this.selectedPiece
.style
.zIndex
= 3000;
329 // Next condition: classList.contains(piece) fails because of marks
330 while (landing
.tagName
== "IMG")
331 landing
= landing
.parentNode
;
332 if (this.start
.id
== landing
.id
)
334 // A click: selectedPiece and possibleMoves are already filled
337 // OK: process move attempt
338 let endSquare
= this.getSquareFromId(landing
.id
);
339 let moves
= this.findMatchingMoves(endSquare
);
340 this.possibleMoves
= [];
341 if (moves
.length
> 1)
342 this.choices
= moves
;
343 else if (moves
.length
==1)
345 // Else: impossible move
346 this.selectedPiece
.parentNode
.removeChild(this.selectedPiece
);
347 delete this.selectedPiece
;
348 this.selectedPiece
= null;
350 findMatchingMoves: function(endSquare
) {
351 // Run through moves list and return the matching set (if promotions...)
353 this.possibleMoves
.forEach(function(m
) {
354 if (endSquare
[0] == m
.end
.x
&& endSquare
[1] == m
.end
.y
)
359 play: function(move) {
360 this.$emit('play-move', move);