chessground
Version:
lichess.org chess ui
211 lines • 8.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateBounds = exports.render = void 0;
const util_1 = require("./util");
const board_1 = require("./board");
const util = require("./util");
function render(s) {
const asWhite = board_1.whitePov(s), posToTranslate = s.dom.relative ? util.posToTranslateRel : util.posToTranslateAbs(s.dom.bounds()), translate = s.dom.relative ? util.translateRel : util.translateAbs, boardEl = s.dom.elements.board, pieces = s.pieces, curAnim = s.animation.current, anims = curAnim ? curAnim.plan.anims : new Map(), fadings = curAnim ? curAnim.plan.fadings : new Map(), curDrag = s.draggable.current, squares = computeSquareClasses(s), samePieces = new Set(), sameSquares = new Set(), movedPieces = new Map(), movedSquares = new Map();
let k, el, pieceAtKey, elPieceName, anim, fading, pMvdset, pMvd, sMvdset, sMvd;
el = boardEl.firstChild;
while (el) {
k = el.cgKey;
if (isPieceNode(el)) {
pieceAtKey = pieces.get(k);
anim = anims.get(k);
fading = fadings.get(k);
elPieceName = el.cgPiece;
if (el.cgDragging && (!curDrag || curDrag.orig !== k)) {
el.classList.remove('dragging');
translate(el, posToTranslate(util_1.key2pos(k), asWhite));
el.cgDragging = false;
}
if (!fading && el.cgFading) {
el.cgFading = false;
el.classList.remove('fading');
}
if (pieceAtKey) {
if (anim && el.cgAnimating && elPieceName === pieceNameOf(pieceAtKey)) {
const pos = util_1.key2pos(k);
pos[0] += anim[2];
pos[1] += anim[3];
el.classList.add('anim');
translate(el, posToTranslate(pos, asWhite));
}
else if (el.cgAnimating) {
el.cgAnimating = false;
el.classList.remove('anim');
translate(el, posToTranslate(util_1.key2pos(k), asWhite));
if (s.addPieceZIndex)
el.style.zIndex = posZIndex(util_1.key2pos(k), asWhite);
}
if (elPieceName === pieceNameOf(pieceAtKey) && (!fading || !el.cgFading)) {
samePieces.add(k);
}
else {
if (fading && elPieceName === pieceNameOf(fading)) {
el.classList.add('fading');
el.cgFading = true;
}
else {
appendValue(movedPieces, elPieceName, el);
}
}
}
else {
appendValue(movedPieces, elPieceName, el);
}
}
else if (isSquareNode(el)) {
const cn = el.className;
if (squares.get(k) === cn)
sameSquares.add(k);
else
appendValue(movedSquares, cn, el);
}
el = el.nextSibling;
}
for (const [sk, className] of squares) {
if (!sameSquares.has(sk)) {
sMvdset = movedSquares.get(className);
sMvd = sMvdset && sMvdset.pop();
const translation = posToTranslate(util_1.key2pos(sk), asWhite);
if (sMvd) {
sMvd.cgKey = sk;
translate(sMvd, translation);
}
else {
const squareNode = util_1.createEl('square', className);
squareNode.cgKey = sk;
translate(squareNode, translation);
boardEl.insertBefore(squareNode, boardEl.firstChild);
}
}
}
for (const [k, p] of pieces) {
anim = anims.get(k);
if (!samePieces.has(k)) {
pMvdset = movedPieces.get(pieceNameOf(p));
pMvd = pMvdset && pMvdset.pop();
if (pMvd) {
pMvd.cgKey = k;
if (pMvd.cgFading) {
pMvd.classList.remove('fading');
pMvd.cgFading = false;
}
const pos = util_1.key2pos(k);
if (s.addPieceZIndex)
pMvd.style.zIndex = posZIndex(pos, asWhite);
if (anim) {
pMvd.cgAnimating = true;
pMvd.classList.add('anim');
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pMvd, posToTranslate(pos, asWhite));
}
else {
const pieceName = pieceNameOf(p), pieceNode = util_1.createEl('piece', pieceName), pos = util_1.key2pos(k);
pieceNode.cgPiece = pieceName;
pieceNode.cgKey = k;
if (anim) {
pieceNode.cgAnimating = true;
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pieceNode, posToTranslate(pos, asWhite));
if (s.addPieceZIndex)
pieceNode.style.zIndex = posZIndex(pos, asWhite);
boardEl.appendChild(pieceNode);
}
}
}
for (const nodes of movedPieces.values())
removeNodes(s, nodes);
for (const nodes of movedSquares.values())
removeNodes(s, nodes);
}
exports.render = render;
function updateBounds(s) {
if (s.dom.relative)
return;
const asWhite = board_1.whitePov(s), posToTranslate = util.posToTranslateAbs(s.dom.bounds());
let el = s.dom.elements.board.firstChild;
while (el) {
if ((isPieceNode(el) && !el.cgAnimating) || isSquareNode(el)) {
util.translateAbs(el, posToTranslate(util_1.key2pos(el.cgKey), asWhite));
}
el = el.nextSibling;
}
}
exports.updateBounds = updateBounds;
function isPieceNode(el) {
return el.tagName === 'PIECE';
}
function isSquareNode(el) {
return el.tagName === 'SQUARE';
}
function removeNodes(s, nodes) {
for (const node of nodes)
s.dom.elements.board.removeChild(node);
}
function posZIndex(pos, asWhite) {
let z = 2 + pos[1] * 8 + (7 - pos[0]);
if (asWhite)
z = 67 - z;
return z + '';
}
function pieceNameOf(piece) {
return `${piece.color} ${piece.role}`;
}
function computeSquareClasses(s) {
var _a;
const squares = new Map();
if (s.lastMove && s.highlight.lastMove)
for (const k of s.lastMove) {
addSquare(squares, k, 'last-move');
}
if (s.check && s.highlight.check)
addSquare(squares, s.check, 'check');
if (s.selected) {
addSquare(squares, s.selected, 'selected');
if (s.movable.showDests) {
const dests = (_a = s.movable.dests) === null || _a === void 0 ? void 0 : _a.get(s.selected);
if (dests)
for (const k of dests) {
addSquare(squares, k, 'move-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
const pDests = s.premovable.dests;
if (pDests)
for (const k of pDests) {
addSquare(squares, k, 'premove-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
}
}
const premove = s.premovable.current;
if (premove)
for (const k of premove)
addSquare(squares, k, 'current-premove');
else if (s.predroppable.current)
addSquare(squares, s.predroppable.current.key, 'current-premove');
const o = s.exploding;
if (o)
for (const k of o.keys)
addSquare(squares, k, 'exploding' + o.stage);
return squares;
}
function addSquare(squares, key, klass) {
const classes = squares.get(key);
if (classes)
squares.set(key, `${classes} ${klass}`);
else
squares.set(key, klass);
}
function appendValue(map, key, value) {
const arr = map.get(key);
if (arr)
arr.push(value);
else
map.set(key, [value]);
}
//# sourceMappingURL=render.js.map