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)
17 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
18 // Precompute hints squares to facilitate rendering
19 let hintSquares
= doubleArray(sizeX
, sizeY
, false);
20 this.possibleMoves
.forEach(m
=> { hintSquares
[m
.end
.x
][m
.end
.y
] = true; });
21 // Also precompute in-check squares
22 let incheckSq
= doubleArray(sizeX
, sizeY
, false);
23 this.incheck
.forEach(sq
=> { incheckSq
[sq
[0]][sq
[1]] = true; });
27 attrs: { "id": "choices" },
28 'class': { 'row': true },
30 "display": this.choices
.length
>0?"block":"none",
31 "top": "-" + ((sizeY
/2)*squareWidth+squareWidth/2) + "px",
32 "width": (this.choices
.length
* squareWidth
) + "px",
33 "height": squareWidth
+ "px",
36 this.choices
.map(m
=> { //a "choice" is a move
41 ['board'+sizeY
]: true,
44 'width': (100/this.choices
.length
) + "%",
45 'padding-bottom': (100/this.choices
.length
) + "%",
50 attrs: { "src": '/images/pieces/' +
51 V
.getPpath(m
.appear
[0].c
+m
.appear
[0].p
) + '.svg' },
52 'class': { 'choice-piece': true },
54 "click": e
=> { this.play(m
); this.choices
=[]; },
55 // NOTE: add 'touchstart' event to fix a problem on smartphones
56 "touchstart": e
=> { this.play(m
); this.choices
=[]; },
63 // Create board element (+ reserves if needed by variant or mode)
64 const lm
= this.lastMove
;
65 const showLight
= this.hints
&& variant
.name
!= "Dark";
74 [_
.range(sizeX
).map(i
=> {
75 let ci
= (this.orientation
=='w' ? i : sizeX
-i
-1);
82 style: { 'opacity': this.choices
.length
>0?"0.5":"1" },
84 _
.range(sizeY
).map(j
=> {
85 let cj
= (this.orientation
=='w' ? j : sizeY
-j
-1);
87 if (this.vr
.board
[ci
][cj
] != V
.EMPTY
&& (variant
.name
!="Dark"
88 || this.gameOver
|| this.vr
.enlightened
[this.userColor
][ci
][cj
]))
96 'ghost': !!this.selectedPiece
97 && this.selectedPiece
.parentNode
.id
== "sq-"+ci
+"-"+cj
,
100 src: "/images/pieces/" +
101 V
.getPpath(this.vr
.board
[ci
][cj
]) + ".svg",
107 if (this.hints
&& hintSquares
[ci
][cj
])
117 src: "/images/mark.svg",
128 ['board'+sizeY
]: true,
129 'light-square': (i
+j
)%2==0,
130 'dark-square': (i
+j
)%2==1,
132 'in-shadow': variant
.name
=="Dark" && !this.gameOver
133 && !this.vr
.enlightened
[this.userColor
][ci
][cj
],
134 'highlight': showLight
&& !!lm
&& _
.isMatch(lm
.end
, {x:ci
,y:cj
}),
135 'incheck': showLight
&& incheckSq
[ci
][cj
],
138 id: this.getSquareId({x:ci
,y:cj
}),
147 let elementArray
= [choices
, gameDiv
];
148 if (!!this.vr
.reserve
)
150 const shiftIdx
= (this.userColor
=="w" ? 0 : 1);
151 let myReservePiecesArray
= [];
152 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
154 myReservePiecesArray
.push(h('div',
156 'class': {'board':true, ['board'+sizeY
]:true},
157 attrs: { id: this.getSquareId({x:sizeX
+shiftIdx
,y:i
}) }
162 'class': {"piece":true, "reserve":true},
164 "src": "/images/pieces/" +
165 this.vr
.getReservePpath(this.userColor
,i
) + ".svg",
169 {"class": { "reserve-count": true } },
170 [ this.vr
.reserve
[this.userColor
][V
.RESERVE_PIECES
[i
]] ]
174 let oppReservePiecesArray
= [];
175 const oppCol
= V
.GetOppCol(this.userColor
);
176 for (let i
=0; i
<V
.RESERVE_PIECES
.length
; i
++)
178 oppReservePiecesArray
.push(h('div',
180 'class': {'board':true, ['board'+sizeY
]:true},
181 attrs: { id: this.getSquareId({x:sizeX
+(1-shiftIdx
),y:i
}) }
186 'class': {"piece":true, "reserve":true},
188 "src": "/images/pieces/" +
189 this.vr
.getReservePpath(oppCol
,i
) + ".svg",
193 {"class": { "reserve-count": true } },
194 [ this.vr
.reserve
[oppCol
][V
.RESERVE_PIECES
[i
]] ]
198 let reserves
= h('div',
210 "reserve-row-1": true,
216 { 'class': { 'row': true }},
217 oppReservePiecesArray
221 elementArray
.push(reserves
);
229 "col-md-offset-1":true,
231 "col-lg-offset-2":true,
233 // NOTE: click = mousedown + mouseup
235 mousedown: this.mousedown
,
236 mousemove: this.mousemove
,
237 mouseup: this.mouseup
,
238 touchstart: this.mousedown
,
239 touchmove: this.mousemove
,
240 touchend: this.mouseup
,
247 // Get the identifier of a HTML square from its numeric coordinates o.x,o.y.
248 getSquareId: function(o
) {
249 // NOTE: a separator is required to allow any size of board
250 return "sq-" + o
.x
+ "-" + o
.y
;
253 getSquareFromId: function(id
) {
254 let idParts
= id
.split('-');
255 return [parseInt(idParts
[1]), parseInt(idParts
[2])];
257 mousedown: function(e
) {
258 e
= e
|| window
.event
;
261 while (!ingame
&& elem
!== null)
263 if (elem
.classList
.contains("game"))
268 elem
= elem
.parentElement
;
270 if (!ingame
) //let default behavior (click on button...)
272 e
.preventDefault(); //disable native drag & drop
273 if (!this.selectedPiece
&& e
.target
.classList
.contains("piece"))
275 // Next few lines to center the piece on mouse cursor
276 let rect
= e
.target
.parentNode
.getBoundingClientRect();
278 x: rect
.x
+ rect
.width
/2,
279 y: rect
.y
+ rect
.width
/2,
280 id: e
.target
.parentNode
.id
282 this.selectedPiece
= e
.target
.cloneNode();
283 this.selectedPiece
.style
.position
= "absolute";
284 this.selectedPiece
.style
.top
= 0;
285 this.selectedPiece
.style
.display
= "inline-block";
286 this.selectedPiece
.style
.zIndex
= 3000;
287 const startSquare
= this.getSquareFromId(e
.target
.parentNode
.id
);
288 this.possibleMoves
= [];
289 const color
= this.mode
=="analyze" || this.gameOver
292 if (this.vr
.canIplay(color
,startSquare
))
293 this.possibleMoves
= this.vr
.getPossibleMovesFrom(startSquare
);
294 // Next line add moving piece just after current image
295 // (required for Crazyhouse reserve)
296 e
.target
.parentNode
.insertBefore(this.selectedPiece
, e
.target
.nextSibling
);
299 mousemove: function(e
) {
300 if (!this.selectedPiece
)
302 e
= e
|| window
.event
;
303 // If there is an active element, move it around
304 if (!!this.selectedPiece
)
306 const [offsetX
,offsetY
] = !!e
.clientX
307 ? [e
.clientX
,e
.clientY
] //desktop browser
308 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
]; //smartphone
309 this.selectedPiece
.style
.left
= (offsetX
-this.start
.x
) + "px";
310 this.selectedPiece
.style
.top
= (offsetY
-this.start
.y
) + "px";
313 mouseup: function(e
) {
314 if (!this.selectedPiece
)
316 e
= e
|| window
.event
;
317 // Read drop target (or parentElement, parentNode... if type == "img")
318 this.selectedPiece
.style
.zIndex
= -3000; //HACK to find square from final coords
319 const [offsetX
,offsetY
] = !!e
.clientX
320 ? [e
.clientX
,e
.clientY
]
321 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
];
322 let landing
= document
.elementFromPoint(offsetX
, offsetY
);
323 this.selectedPiece
.style
.zIndex
= 3000;
324 // Next condition: classList.contains(piece) fails because of marks
325 while (landing
.tagName
== "IMG")
326 landing
= landing
.parentNode
;
327 if (this.start
.id
== landing
.id
)
329 // A click: selectedPiece and possibleMoves are already filled
332 // OK: process move attempt
333 let endSquare
= this.getSquareFromId(landing
.id
);
334 let moves
= this.findMatchingMoves(endSquare
);
335 this.possibleMoves
= [];
336 if (moves
.length
> 1)
337 this.choices
= moves
;
338 else if (moves
.length
==1)
340 // Else: impossible move
341 this.selectedPiece
.parentNode
.removeChild(this.selectedPiece
);
342 delete this.selectedPiece
;
343 this.selectedPiece
= null;
345 findMatchingMoves: function(endSquare
) {
346 // Run through moves list and return the matching set (if promotions...)
348 this.possibleMoves
.forEach(function(m
) {
349 if (endSquare
[0] == m
.end
.x
&& endSquare
[1] == m
.end
.y
)
354 play: function(move) {
355 this.$emit('play-move', move);