X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=base_rules.js;h=6a68f84dbef6f98b575f756f5b614c686ab79a2f;hp=ced3eedd28d6bf560c48c5161f228fa24f0dd265;hb=a5da62690c1314425a576302466b1d4ad406d80d;hpb=96638998852867c04c6356b3c807fd49be848eb0 diff --git a/base_rules.js b/base_rules.js index ced3eed..6a68f84 100644 --- a/base_rules.js +++ b/base_rules.js @@ -966,22 +966,47 @@ export default class ChessRules { curPiece.remove(); }; + const resize = (e) => this.rescale(e.deltaY < 0 ? "up" : "down"); + if ('onmousedown' in window) { - document.addEventListener("mousedown", mousedown); - document.addEventListener("mousemove", mousemove); - document.addEventListener("mouseup", mouseup); - document.addEventListener("wheel", - (e) => this.rescale(e.deltaY < 0 ? "up" : "down")); + this.mouseListeners = [ + {type: "mousedown", listener: mousedown}, + {type: "mousemove", listener: mousemove}, + {type: "mouseup", listener: mouseup}, + {type: "wheel", listener: resize} + ]; + this.mouseListeners.forEach(ml => { + document.addEventListener(ml.type, ml.listener); + }); } if ('ontouchstart' in window) { - // https://stackoverflow.com/a/42509310/12660887 - document.addEventListener("touchstart", mousedown, {passive: false}); - document.addEventListener("touchmove", mousemove, {passive: false}); - document.addEventListener("touchend", mouseup, {passive: false}); + this.touchListeners = [ + {type: "touchstart", listener: mousedown}, + {type: "touchmove", listener: mousemove}, + {type: "touchend", listener: mouseup} + ]; + this.touchListeners.forEach(tl => { + // https://stackoverflow.com/a/42509310/12660887 + document.addEventListener(tl.type, tl.listener, {passive: false}); + }); } // TODO: onpointerdown/move/up ? See reveal.js /controllers/touch.js } + removeListeners() { + if ('onmousedown' in window) { + this.mouseListeners.forEach(ml => { + document.removeEventListener(ml.type, ml.listener); + }); + } + if ('ontouchstart' in window) { + this.touchListeners.forEach(tl => { + // https://stackoverflow.com/a/42509310/12660887 + document.removeEventListener(tl.type, tl.listener); + }); + } + } + showChoices(moves, r) { let container = document.getElementById(this.containerId); let chessboard = container.querySelector(".chessboard");