shogiground
Version:
lishogi.org shogi ui
78 lines • 3.48 kB
JavaScript
import { wrapBoard, wrapHand } from './wrap.js';
import * as events from './events.js';
import { renderHand } from './hands.js';
import { render } from './render.js';
function attachBoard(state, boardWrap) {
const elements = wrapBoard(boardWrap, state);
// in case of inlined hands
if (elements.hands)
attachHands(state, elements.hands.top, elements.hands.bottom);
state.dom.wrapElements.board = boardWrap;
state.dom.elements.board = elements;
state.dom.bounds.board.bounds.clear();
events.bindBoard(state, elements);
state.drawable.prevSvgHash = '';
state.promotion.prevPromotionHash = '';
render(state, elements);
}
function attachHands(state, handTopWrap, handBottomWrap) {
if (!state.dom.elements.hands)
state.dom.elements.hands = {};
if (!state.dom.wrapElements.hands)
state.dom.wrapElements.hands = {};
if (handTopWrap) {
const handTop = wrapHand(handTopWrap, 'top', state);
state.dom.wrapElements.hands.top = handTopWrap;
state.dom.elements.hands.top = handTop;
events.bindHand(state, handTop);
renderHand(state, handTop);
}
if (handBottomWrap) {
const handBottom = wrapHand(handBottomWrap, 'bottom', state);
state.dom.wrapElements.hands.bottom = handBottomWrap;
state.dom.elements.hands.bottom = handBottom;
events.bindHand(state, handBottom);
renderHand(state, handBottom);
}
if (handTopWrap || handBottomWrap) {
state.dom.bounds.hands.bounds.clear();
state.dom.bounds.hands.pieceBounds.clear();
}
}
export function redrawAll(wrapElements, state) {
var _a, _b, _c, _d;
if (wrapElements.board)
attachBoard(state, wrapElements.board);
if (wrapElements.hands && !state.hands.inlined)
attachHands(state, wrapElements.hands.top, wrapElements.hands.bottom);
// shapes might depend both on board and hands - redraw only after both are done
state.dom.redrawShapes();
if (state.events.insert)
state.events.insert(wrapElements.board && state.dom.elements.board, {
top: ((_a = wrapElements.hands) === null || _a === void 0 ? void 0 : _a.top) && ((_b = state.dom.elements.hands) === null || _b === void 0 ? void 0 : _b.top),
bottom: ((_c = wrapElements.hands) === null || _c === void 0 ? void 0 : _c.bottom) && ((_d = state.dom.elements.hands) === null || _d === void 0 ? void 0 : _d.bottom),
});
}
export function detachElements(web, state) {
var _a, _b, _c, _d;
if (web.board) {
state.dom.wrapElements.board = undefined;
state.dom.elements.board = undefined;
state.dom.bounds.board.bounds.clear();
}
if (state.dom.elements.hands && state.dom.wrapElements.hands) {
if ((_a = web.hands) === null || _a === void 0 ? void 0 : _a.top) {
state.dom.wrapElements.hands.top = undefined;
state.dom.elements.hands.top = undefined;
}
if ((_b = web.hands) === null || _b === void 0 ? void 0 : _b.bottom) {
state.dom.wrapElements.hands.bottom = undefined;
state.dom.elements.hands.bottom = undefined;
}
if (((_c = web.hands) === null || _c === void 0 ? void 0 : _c.top) || ((_d = web.hands) === null || _d === void 0 ? void 0 : _d.bottom)) {
state.dom.bounds.hands.bounds.clear();
state.dom.bounds.hands.pieceBounds.clear();
}
}
}
//# sourceMappingURL=dom.js.map