chessground
Version:
lichess.org chess ui
79 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bindDocument = exports.bindBoard = void 0;
const drag = require("./drag");
const draw = require("./draw");
const drop_1 = require("./drop");
const util_1 = require("./util");
function bindBoard(s, boundsUpdated) {
const boardEl = s.dom.elements.board;
if (!s.dom.relative && s.resizable && 'ResizeObserver' in window) {
const observer = new window['ResizeObserver'](boundsUpdated);
observer.observe(boardEl);
}
if (s.viewOnly)
return;
const onStart = startDragOrDraw(s);
boardEl.addEventListener('touchstart', onStart, {
passive: false,
});
boardEl.addEventListener('mousedown', onStart, {
passive: false,
});
if (s.disableContextMenu || s.drawable.enabled) {
boardEl.addEventListener('contextmenu', e => e.preventDefault());
}
}
exports.bindBoard = bindBoard;
function bindDocument(s, boundsUpdated) {
const unbinds = [];
if (!s.dom.relative && s.resizable && !('ResizeObserver' in window)) {
unbinds.push(unbindable(document.body, 'chessground.resize', boundsUpdated));
}
if (!s.viewOnly) {
const onmove = dragOrDraw(s, drag.move, draw.move);
const onend = dragOrDraw(s, drag.end, draw.end);
for (const ev of ['touchmove', 'mousemove'])
unbinds.push(unbindable(document, ev, onmove));
for (const ev of ['touchend', 'mouseup'])
unbinds.push(unbindable(document, ev, onend));
const onScroll = () => s.dom.bounds.clear();
unbinds.push(unbindable(document, 'scroll', onScroll, { capture: true, passive: true }));
unbinds.push(unbindable(window, 'resize', onScroll, { passive: true }));
}
return () => unbinds.forEach(f => f());
}
exports.bindDocument = bindDocument;
function unbindable(el, eventName, callback, options) {
el.addEventListener(eventName, callback, options);
return () => el.removeEventListener(eventName, callback, options);
}
function startDragOrDraw(s) {
return e => {
if (s.draggable.current)
drag.cancel(s);
else if (s.drawable.current)
draw.cancel(s);
else if (e.shiftKey || util_1.isRightButton(e)) {
if (s.drawable.enabled)
draw.start(s, e);
}
else if (!s.viewOnly) {
if (s.dropmode.active)
drop_1.drop(s, e);
else
drag.start(s, e);
}
};
}
function dragOrDraw(s, withDrag, withDraw) {
return e => {
if (s.drawable.current) {
if (s.drawable.enabled)
withDraw(s, e);
}
else if (!s.viewOnly)
withDrag(s, e);
};
}
//# sourceMappingURL=events.js.map