chessground
Version:
lichess.org chess ui
84 lines (72 loc) • 2.57 kB
text/typescript
import { HeadlessState } from './state';
import { setVisible, createEl } from './util';
import { colors, files, ranks } from './types';
import { createElement as createSVG, setAttributes } from './svg';
import { Elements } from './types';
export function renderWrap(element: HTMLElement, s: HeadlessState, relative: boolean): Elements {
// .cg-wrap (element passed to Chessground)
// cg-helper (12.5%, display: table)
// cg-container (800%)
// cg-board
// svg.cg-shapes
// defs
// g
// svg.cg-custom-svgs
// g
// coords.ranks
// coords.files
// piece.ghost
element.innerHTML = '';
// ensure the cg-wrap class is set
// so bounds calculation can use the CSS width/height values
// add that class yourself to the element before calling chessground
// for a slight performance improvement! (avoids recomputing style)
element.classList.add('cg-wrap');
for (const c of colors) element.classList.toggle('orientation-' + c, s.orientation === c);
element.classList.toggle('manipulable', !s.viewOnly);
const helper = createEl('cg-helper');
element.appendChild(helper);
const container = createEl('cg-container');
helper.appendChild(container);
const board = createEl('cg-board');
container.appendChild(board);
let svg: SVGElement | undefined;
let customSvg: SVGElement | undefined;
if (s.drawable.visible && !relative) {
svg = setAttributes(createSVG('svg'), { 'class': 'cg-shapes' });
svg.appendChild(createSVG('defs'));
svg.appendChild(createSVG('g'));
customSvg = setAttributes(createSVG('svg'), { 'class': 'cg-custom-svgs' });
customSvg.appendChild(createSVG('g'));
container.appendChild(svg);
container.appendChild(customSvg);
}
if (s.coordinates) {
const orientClass = s.orientation === 'black' ? ' black' : '';
container.appendChild(renderCoords(ranks, 'ranks' + orientClass));
container.appendChild(renderCoords(files, 'files' + orientClass));
}
let ghost: HTMLElement | undefined;
if (s.draggable.showGhost && !relative) {
ghost = createEl('piece', 'ghost');
setVisible(ghost, false);
container.appendChild(ghost);
}
return {
board,
container,
ghost,
svg,
customSvg,
};
}
function renderCoords(elems: readonly string[], className: string): HTMLElement {
const el = createEl('coords', className);
let f: HTMLElement;
for (const elem of elems) {
f = createEl('coord');
f.textContent = elem;
el.appendChild(f);
}
return el;
}