lila/public/javascripts/vendor/chessground.min.js

2 lines
26 KiB
JavaScript

var Chessground=function(){"use strict";const e=["white","black"],t=["a","b","c","d","e","f","g","h"],o=["1","2","3","4","5","6","7","8"],n=[...o].reverse(),r=Array.prototype.concat(...t.map((e=>o.map((t=>e+t))))),s=e=>r[8*e[0]+e[1]],i=e=>[e.charCodeAt(0)-97,e.charCodeAt(1)-49],c=r.map(i);const a=()=>{let e;return{start(){e=performance.now()},cancel(){e=void 0},stop(){if(!e)return 0;const t=performance.now()-e;return e=void 0,t}}},l=e=>"white"===e?"black":"white",d=(e,t)=>{const o=e[0]-t[0],n=e[1]-t[1];return o*o+n*n},u=(e,t)=>e.role===t.role&&e.color===t.color,p=e=>(t,o)=>[(o?t[0]:7-t[0])*e.width/8,(o?7-t[1]:t[1])*e.height/8],f=(e,t)=>{e.style.transform=`translate(${t[0]}px,${t[1]}px)`},g=(e,t)=>{e.style.visibility=t?"visible":"hidden"},h=e=>{var t;return e.clientX||0===e.clientX?[e.clientX,e.clientY]:(null===(t=e.targetTouches)||void 0===t?void 0:t[0])?[e.targetTouches[0].clientX,e.targetTouches[0].clientY]:void 0},m=e=>2===e.buttons||2===e.button,v=(e,t)=>{const o=document.createElement(e);return t&&(o.className=t),o};function b(e,t,o){const n=i(e);return t||(n[0]=7-n[0],n[1]=7-n[1]),[o.left+o.width*n[0]/8+o.width/16,o.top+o.height*(7-n[1])/8+o.height/16]}function w(e,t){return Math.abs(e-t)}const y=(e,t,o,n)=>{const r=w(e,o),s=w(t,n);return 1===r&&2===s||2===r&&1===s},k=(e,t,o,n)=>w(e,o)===w(t,n),C=(e,t,o,n)=>e===o||t===n,M=(e,t,o,n)=>k(e,t,o,n)||C(e,t,o,n);function S(e,t,o){const n=e.get(t);if(!n)return[];const r=i(t),a=n.role,l="pawn"===a?(d=n.color,(e,t,o,n)=>w(e,o)<2&&("white"===d?n===t+1||t<=1&&n===t+2&&e===o:n===t-1||t>=6&&n===t-2&&e===o)):"knight"===a?y:"bishop"===a?k:"rook"===a?C:"queen"===a?M:function(e,t,o){return(n,r,s,i)=>w(n,s)<2&&w(r,i)<2||o&&r===i&&r===("white"===e?0:7)&&(4===n&&(2===s&&t.includes(0)||6===s&&t.includes(7))||t.includes(s))}(n.color,function(e,t){const o="white"===t?"1":"8",n=[];for(const[r,s]of e)r[1]===o&&s.color===t&&"rook"===s.role&&n.push(i(r)[0]);return n}(e,n.color),o);var d;return c.filter((e=>(r[0]!==e[0]||r[1]!==e[1])&&l(r[0],r[1],e[0],e[1]))).map(s)}function P(e,...t){e&&setTimeout((()=>e(...t)),1)}function x(e){e.premovable.current&&(e.premovable.current=void 0,P(e.premovable.events.unset))}function K(e){const t=e.predroppable;t.current&&(t.current=void 0,P(t.events.unset))}function A(e,t,o){const n=e.pieces.get(t),r=e.pieces.get(o);if(t===o||!n)return!1;const c=r&&r.color!==n.color?r:void 0;return o===e.selected&&E(e),P(e.events.move,t,o,c),function(e,t,o){if(!e.autoCastle)return!1;const n=e.pieces.get(t);if(!n||"king"!==n.role)return!1;const r=i(t),c=i(o);if(0!==r[1]&&7!==r[1]||r[1]!==c[1])return!1;4!==r[0]||e.pieces.has(o)||(6===c[0]?o=s([7,c[1]]):2===c[0]&&(o=s([0,c[1]])));const a=e.pieces.get(o);return!(!a||a.color!==n.color||"rook"!==a.role||(e.pieces.delete(t),e.pieces.delete(o),r[0]<c[0]?(e.pieces.set(s([6,c[1]]),n),e.pieces.set(s([5,c[1]]),a)):(e.pieces.set(s([2,c[1]]),n),e.pieces.set(s([3,c[1]]),a)),0))}(e,t,o)||(e.pieces.set(o,n),e.pieces.delete(t)),e.lastMove=[t,o],e.check=void 0,P(e.events.change),c||!0}function q(e,t,o,n){if(e.pieces.has(o)){if(!n)return!1;e.pieces.delete(o)}return P(e.events.dropNewPiece,t,o),e.pieces.set(o,t),e.lastMove=[o],e.check=void 0,P(e.events.change),e.movable.dests=void 0,e.turnColor=l(e.turnColor),!0}function L(e,t,o){const n=A(e,t,o);return n&&(e.movable.dests=void 0,e.turnColor=l(e.turnColor),e.animation.current=void 0),n}function N(e,t,o){if($(e,t,o)){const n=L(e,t,o);if(n){const r=e.hold.stop();E(e);const s={premove:!1,ctrlKey:e.stats.ctrlKey,holdTime:r};return!0!==n&&(s.captured=n),P(e.movable.events.after,t,o,s),!0}}else if(function(e,t,o){return t!==o&&F(e,t)&&S(e.pieces,t,e.premovable.castle).includes(o)}(e,t,o))return function(e,t,o,n){K(e),e.premovable.current=[t,o],P(e.premovable.events.set,t,o,n)}(e,t,o,{ctrlKey:e.stats.ctrlKey}),E(e),!0;return E(e),!1}function D(e,t,o,n){const r=e.pieces.get(t);r&&(function(e,t,o){const n=e.pieces.get(t);return!(!n||t!==o&&e.pieces.has(o)||"both"!==e.movable.color&&(e.movable.color!==n.color||e.turnColor!==n.color))}(e,t,o)||n)?(e.pieces.delete(t),q(e,r,o,n),P(e.movable.events.afterNewPiece,r.role,o,{premove:!1,predrop:!1})):r&&function(e,t,o){const n=e.pieces.get(t),r=e.pieces.get(o);return!!n&&(!r||r.color!==e.movable.color)&&e.predroppable.enabled&&("pawn"!==n.role||"1"!==o[1]&&"8"!==o[1])&&e.movable.color===n.color&&e.turnColor!==n.color}(e,t,o)?function(e,t,o){x(e),e.predroppable.current={role:t,key:o},P(e.predroppable.events.set,t,o)}(e,r.role,o):(x(e),K(e)),e.pieces.delete(t),E(e)}function T(e,t,o){if(P(e.events.select,t),e.selected){if(e.selected===t&&!e.draggable.enabled)return E(e),void e.hold.cancel();if((e.selectable.enabled||o)&&e.selected!==t&&N(e,e.selected,t))return void(e.stats.dragged=!1)}(W(e,t)||F(e,t))&&(O(e,t),e.hold.start())}function O(e,t){e.selected=t,F(e,t)?e.premovable.dests=S(e.pieces,t,e.premovable.castle):e.premovable.dests=void 0}function E(e){e.selected=void 0,e.premovable.dests=void 0,e.hold.cancel()}function W(e,t){const o=e.pieces.get(t);return!!o&&("both"===e.movable.color||e.movable.color===o.color&&e.turnColor===o.color)}function $(e,t,o){var n,r;return t!==o&&W(e,t)&&(e.movable.free||!!(null===(r=null===(n=e.movable.dests)||void 0===n?void 0:n.get(t))||void 0===r?void 0:r.includes(o)))}function F(e,t){const o=e.pieces.get(t);return!!o&&e.premovable.enabled&&e.movable.color===o.color&&e.turnColor!==o.color}function R(e){const t=e.premovable.current;if(!t)return!1;const o=t[0],n=t[1];let r=!1;if($(e,o,n)){const t=L(e,o,n);if(t){const s={premove:!0};!0!==t&&(s.captured=t),P(e.movable.events.after,o,n,s),r=!0}}return x(e),r}function z(e){x(e),K(e),E(e)}function B(e){e.movable.color=e.movable.dests=e.animation.current=void 0,z(e)}function H(e,t,o){let n=Math.floor(8*(e[0]-o.left)/o.width);t||(n=7-n);let r=7-Math.floor(8*(e[1]-o.top)/o.height);return t||(r=7-r),n>=0&&n<8&&r>=0&&r<8?s([n,r]):void 0}function I(e){return"white"===e.orientation}const V="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",j={p:"pawn",r:"rook",n:"knight",b:"bishop",q:"queen",k:"king"},G={pawn:"p",rook:"r",knight:"n",bishop:"b",queen:"q",king:"k"};function U(e){"start"===e&&(e=V);const t=new Map;let o=7,n=0;for(const r of e)switch(r){case" ":return t;case"/":if(--o,o<0)return t;n=0;break;case"~":{const e=t.get(s([n,o]));e&&(e.promoted=!0);break}default:{const e=r.charCodeAt(0);if(e<57)n+=e-48;else{const e=r.toLowerCase();t.set(s([n,o]),{role:j[e],color:r===e?"black":"white"}),++n}}}return t}function X(e,t){var o,n;if((null===(o=t.movable)||void 0===o?void 0:o.dests)&&(e.movable.dests=void 0),(null===(n=t.drawable)||void 0===n?void 0:n.autoShapes)&&(e.drawable.autoShapes=[]),Z(e,t),t.fen&&(e.pieces=U(t.fen),e.drawable.shapes=[]),"check"in t&&function(e,t){if(e.check=void 0,!0===t&&(t=e.turnColor),t)for(const[o,n]of e.pieces)"king"===n.role&&n.color===t&&(e.check=o)}(e,t.check||!1),"lastMove"in t&&!t.lastMove?e.lastMove=void 0:t.lastMove&&(e.lastMove=t.lastMove),e.selected&&O(e,e.selected),(!e.animation.duration||e.animation.duration<100)&&(e.animation.enabled=!1),!e.movable.rookCastle&&e.movable.dests){const t="white"===e.movable.color?"1":"8",o="e"+t,n=e.movable.dests.get(o),r=e.pieces.get(o);if(!n||!r||"king"!==r.role)return;e.movable.dests.set(o,n.filter((e=>!(e==="a"+t&&n.includes("c"+t)||e==="h"+t&&n.includes("g"+t)))))}}function Z(e,t){for(const o in t)Y(e[o])&&Y(t[o])?Z(e[o],t[o]):e[o]=t[o]}function Y(e){return"object"==typeof e}function Q(e,t){return t.animation.enabled?function(e,t){const o=new Map(t.pieces),n=e(t),s=function(e,t){const o=new Map,n=[],s=new Map,i=[],c=[],a=new Map;let l,d,p;for(const[t,o]of e)a.set(t,_(t,o));for(const e of r)l=t.pieces.get(e),d=a.get(e),l?d?u(l,d.piece)||(i.push(d),c.push(_(e,l))):c.push(_(e,l)):d&&i.push(d);for(const e of c)d=ee(e,i.filter((t=>u(e.piece,t.piece)))),d&&(p=[d.pos[0]-e.pos[0],d.pos[1]-e.pos[1]],o.set(e.key,p.concat(p)),n.push(d.key));for(const e of i)n.includes(e.key)||s.set(e.key,e.piece);return{anims:o,fadings:s}}(o,t);if(s.anims.size||s.fadings.size){const e=t.animation.current&&t.animation.current.start;t.animation.current={start:performance.now(),frequency:1/t.animation.duration,plan:s},e||te(t,performance.now())}else t.dom.redraw();return n}(e,t):J(e,t)}function J(e,t){const o=e(t);return t.dom.redraw(),o}function _(e,t){return{key:e,pos:i(e),piece:t}}function ee(e,t){return t.sort(((t,o)=>d(e.pos,t.pos)-d(e.pos,o.pos)))[0]}function te(e,t){const o=e.animation.current;if(void 0===o)return void(e.dom.destroyed||e.dom.redrawNow());const n=1-(t-o.start)*o.frequency;if(n<=0)e.animation.current=void 0,e.dom.redrawNow();else{const t=(r=n)<.5?4*r*r*r:(r-1)*(2*r-2)*(2*r-2)+1;for(const e of o.plan.anims.values())e[2]=e[0]*t,e[3]=e[1]*t;e.dom.redrawNow(!0),requestAnimationFrame(((t=performance.now())=>te(e,t)))}var r}const oe=["green","red","blue","yellow"];function ne(e,t){if(t.touches&&t.touches.length>1)return;t.stopPropagation(),t.preventDefault(),t.ctrlKey?E(e):z(e);const o=h(t),n=H(o,I(e),e.dom.bounds());n&&(e.drawable.current={orig:n,pos:o,brush:ae(t),snapToValidMove:e.drawable.defaultSnapToValidMove},re(e))}function re(e){requestAnimationFrame((()=>{const t=e.drawable.current;if(t){const o=H(t.pos,I(e),e.dom.bounds());o||(t.snapToValidMove=!1);const n=t.snapToValidMove?function(e,t,o,n){const r=i(e),a=c.filter((e=>M(r[0],r[1],e[0],e[1])||y(r[0],r[1],e[0],e[1]))),l=a.map((e=>b(s(e),o,n))).map((e=>d(t,e))),[,u]=l.reduce(((e,t,o)=>e[0]<t?e:[t,o]),[l[0],0]);return s(a[u])}(t.orig,t.pos,I(e),e.dom.bounds()):o;n!==t.mouseSq&&(t.mouseSq=n,t.dest=n!==t.orig?n:void 0,e.dom.redrawNow()),re(e)}}))}function se(e,t){e.drawable.current&&(e.drawable.current.pos=h(t))}function ie(e){const t=e.drawable.current;t&&(t.mouseSq&&function(e,t){const o=e=>e.orig===t.orig&&e.dest===t.dest,n=e.shapes.find(o);n&&(e.shapes=e.shapes.filter((e=>!o(e))));n&&n.brush===t.brush||e.shapes.push(t);le(e)}(e.drawable,t),ce(e))}function ce(e){e.drawable.current&&(e.drawable.current=void 0,e.dom.redraw())}function ae(e){var t;const o=(e.shiftKey||e.ctrlKey)&&m(e),n=e.altKey||e.metaKey||(null===(t=e.getModifierState)||void 0===t?void 0:t.call(e,"AltGraph"));return oe[(o?1:0)+(n?2:0)]}function le(e){e.onChange&&e.onChange(e.shapes)}function de(e,t){if(!t.isTrusted||void 0!==t.button&&0!==t.button)return;if(t.touches&&t.touches.length>1)return;const o=e.dom.bounds(),n=h(t),r=H(n,I(e),o);if(!r)return;const s=e.pieces.get(r),c=e.selected;var a;c||!e.drawable.enabled||!e.drawable.eraseOnClick&&s&&s.color===e.turnColor||(a=e).drawable.shapes.length&&(a.drawable.shapes=[],a.dom.redraw(),le(a.drawable)),!1===t.cancelable||t.touches&&e.movable.color&&!s&&!c&&!function(e,t){const o=I(e),n=e.dom.bounds(),r=Math.pow(n.width/8,2);for(const s in e.pieces){const e=b(s,o,n);if(d(e,t)<=r)return!0}return!1}(e,n)||t.preventDefault();const l=!!e.premovable.current,u=!!e.predroppable.current;e.stats.ctrlKey=t.ctrlKey,e.selected&&$(e,e.selected,r)?Q((e=>T(e,r)),e):T(e,r);const m=e.selected===r,v=me(e,r);if(s&&v&&m&&function(e,t){const o=e.pieces.get(t);return!!o&&e.draggable.enabled&&("both"===e.movable.color||e.movable.color===o.color&&(e.turnColor===o.color||e.premovable.enabled))}(e,r)){e.draggable.current={orig:r,piece:s,origPos:n,pos:n,started:e.draggable.autoDistance&&e.stats.dragged,element:v,previouslySelected:c,originTarget:t.target},v.cgDragging=!0,v.classList.add("dragging");const a=e.dom.elements.ghost;a&&(a.className=`ghost ${s.color} ${s.role}`,f(a,p(o)(i(r),I(e))),g(a,!0)),ue(e)}else l&&x(e),u&&K(e);e.dom.redraw()}function ue(e){requestAnimationFrame((()=>{var t;const o=e.draggable.current;if(!o)return;(null===(t=e.animation.current)||void 0===t?void 0:t.plan.anims.has(o.orig))&&(e.animation.current=void 0);const n=e.pieces.get(o.orig);if(n&&u(n,o.piece)){if(!o.started&&d(o.pos,o.origPos)>=Math.pow(e.draggable.distance,2)&&(o.started=!0),o.started){if("function"==typeof o.element){const e=o.element();if(!e)return;e.cgDragging=!0,e.classList.add("dragging"),o.element=e}const t=e.dom.bounds();f(o.element,[o.pos[0]-t.left-t.width/16,o.pos[1]-t.top-t.height/16])}}else ge(e);ue(e)}))}function pe(e,t){e.draggable.current&&(!t.touches||t.touches.length<2)&&(e.draggable.current.pos=h(t))}function fe(e,t){const o=e.draggable.current;if(!o)return;if("touchend"===t.type&&!1!==t.cancelable&&t.preventDefault(),"touchend"===t.type&&o.originTarget!==t.target&&!o.newPiece)return void(e.draggable.current=void 0);x(e),K(e);const n=H(h(t)||o.pos,I(e),e.dom.bounds());n&&o.started&&o.orig!==n?o.newPiece?D(e,o.orig,n,o.force):(e.stats.ctrlKey=t.ctrlKey,N(e,o.orig,n)&&(e.stats.dragged=!0)):o.newPiece?e.pieces.delete(o.orig):e.draggable.deleteOnDropOff&&!n&&(e.pieces.delete(o.orig),P(e.events.change)),(o.orig!==o.previouslySelected||o.orig!==n&&n)&&e.selectable.enabled||E(e),he(e),e.draggable.current=void 0,e.dom.redraw()}function ge(e){const t=e.draggable.current;t&&(t.newPiece&&e.pieces.delete(t.orig),e.draggable.current=void 0,E(e),he(e),e.dom.redraw())}function he(e){const t=e.dom.elements;t.ghost&&g(t.ghost,!1)}function me(e,t){let o=e.dom.elements.board.firstChild;for(;o;){if(o.cgKey===t&&"PIECE"===o.tagName)return o;o=o.nextSibling}}function ve(e,t){e.exploding&&(t?e.exploding.stage=t:e.exploding=void 0,e.dom.redraw())}function be(e,o){function r(){!function(e){e.orientation=l(e.orientation),e.animation.current=e.draggable.current=e.selected=void 0}(e),o()}return{set(t){t.orientation&&t.orientation!==e.orientation&&r(),(t.fen?Q:J)((e=>X(e,t)),e)},state:e,getFen:()=>{return o=e.pieces,n.map((e=>t.map((t=>{const n=o.get(t+e);if(n){const e=G[n.role];return"white"===n.color?e.toUpperCase():e}return"1"})).join(""))).join("/").replace(/1{2,}/g,(e=>e.length.toString()));var o},toggleOrientation:r,setPieces(t){Q((e=>function(e,t){for(const[o,n]of t)n?e.pieces.set(o,n):e.pieces.delete(o)}(e,t)),e)},selectSquare(t,o){t?Q((e=>T(e,t,o)),e):e.selected&&(E(e),e.dom.redraw())},move(t,o){Q((e=>A(e,t,o)),e)},newPiece(t,o){Q((e=>q(e,t,o)),e)},playPremove(){if(e.premovable.current){if(Q(R,e))return!0;e.dom.redraw()}return!1},playPredrop(t){if(e.predroppable.current){const o=function(e,t){const o=e.predroppable.current;let n=!1;if(!o)return!1;t(o)&&q(e,{role:o.role,color:e.movable.color},o.key)&&(P(e.movable.events.afterNewPiece,o.role,o.key,{premove:!1,predrop:!0}),n=!0);return K(e),n}(e,t);return e.dom.redraw(),o}return!1},cancelPremove(){J(x,e)},cancelPredrop(){J(K,e)},cancelMove(){J((e=>{z(e),ge(e)}),e)},stop(){J((e=>{B(e),ge(e)}),e)},explode(t){!function(e,t){e.exploding={stage:1,keys:t},e.dom.redraw(),setTimeout((()=>{ve(e,2),setTimeout((()=>ve(e,void 0)),120)}),120)}(e,t)},setAutoShapes(t){J((e=>e.drawable.autoShapes=t),e)},setShapes(t){J((e=>e.drawable.shapes=t),e)},getKeyAtDomPos:t=>H(t,I(e),e.dom.bounds()),redrawAll:o,dragNewPiece(t,o,n){!function(e,t,o,n){const r="a0";e.pieces.set(r,t),e.dom.redraw();const s=h(o);e.draggable.current={orig:r,piece:t,origPos:s,pos:s,started:!0,element:()=>me(e,r),originTarget:o.target,newPiece:!0,force:!!n},ue(e)}(e,t,o,n)},destroy(){B(e),e.dom.unbind&&e.dom.unbind(),e.dom.destroyed=!0}}}function we(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function ye(e,t,o){const n=e.drawable,r=n.current,s=r&&r.mouseSq?r:void 0,i=new Map,c=e.dom.bounds();for(const e of n.shapes.concat(n.autoShapes).concat(s?[s]:[]))e.dest&&i.set(e.dest,(i.get(e.dest)||0)+1);const a=n.shapes.concat(n.autoShapes).map((e=>({shape:e,current:!1,hash:Ce(e,i,!1,c)})));s&&a.push({shape:s,current:!0,hash:Ce(s,i,!0,c)});const l=a.map((e=>e.hash)).join(";");if(l===e.drawable.prevSvgHash)return;e.drawable.prevSvgHash=l;const d=t.querySelector("defs"),u=t.querySelector("g"),p=o.querySelector("g");!function(e,t,o){const n=new Map;let r;for(const o of t)o.shape.dest&&(r=e.brushes[o.shape.brush],o.shape.modifiers&&(r=qe(r,o.shape.modifiers)),n.set(r.key,r));const s=new Set;let i=o.firstChild;for(;i;)s.add(i.getAttribute("cgKey")),i=i.nextSibling;for(const[e,t]of n.entries())s.has(e)||o.appendChild(xe(t))}(n,a,d),ke(e,a.filter((e=>!e.shape.customSvg)),n.brushes,i,u),ke(e,a.filter((e=>e.shape.customSvg)),n.brushes,i,p)}function ke(e,t,o,n,r){const s=e.dom.bounds(),i=new Map,c=[];for(const e of t)i.set(e.hash,!1);let a,l=r.firstChild;for(;l;)a=l.getAttribute("cgHash"),i.has(a)?i.set(a,!0):c.push(l),l=l.nextSibling;for(const e of c)r.removeChild(e);for(const c of t)i.get(c.hash)||r.appendChild(Pe(e,c,o,n,s))}function Ce({orig:e,dest:t,brush:o,piece:n,modifiers:r,customSvg:s},i,c,a){return[a.width,a.height,c,e,t,o,t&&(i.get(t)||0)>1,n&&Me(n),r&&(l=r,""+(l.lineWidth||"")),s&&Se(s)].filter((e=>e)).join(",");var l}function Me(e){return[e.color,e.role,e.scale].filter((e=>e)).join(",")}function Se(e){let t=0;for(let o=0;o<e.length;o++)t=(t<<5)-t+e.charCodeAt(o)>>>0;return"custom-"+t.toString()}function Pe(e,{shape:t,current:o,hash:n},r,s,c){let a;if(t.customSvg){const o=Ae(i(t.orig),e.orientation);a=function(e,t,o){const{width:n,height:r}=o,s=n/8,i=r/8,c=t[0]*s,a=(7-t[1])*i,l=Ke(we("g"),{transform:`translate(${c},${a})`}),d=Ke(we("svg"),{width:s,height:i,viewBox:"0 0 100 100"});return l.appendChild(d),d.innerHTML=e,l}(t.customSvg,o,c)}else if(t.piece)a=function(e,t,o,n){const r=De(t,n),s=n.width/8*(o.scale||1),i=o.color[0]+("knight"===o.role?"n":o.role[0]).toUpperCase();return Ke(we("image"),{className:`${o.role} ${o.color}`,x:r[0]-s/2,y:r[1]-s/2,width:s,height:s,href:e+i+".svg"})}(e.drawable.pieces.baseUrl,Ae(i(t.orig),e.orientation),t.piece,c);else{const n=Ae(i(t.orig),e.orientation);if(t.dest){let l=r[t.brush];t.modifiers&&(l=qe(l,t.modifiers)),a=function(e,t,o,n,r,s){const i=function(e,t){return(t?20:10)/512*e.width}(s,r&&!n),c=De(t,s),a=De(o,s),l=a[0]-c[0],d=a[1]-c[1],u=Math.atan2(d,l),p=Math.cos(u)*i,f=Math.sin(u)*i;return Ke(we("line"),{stroke:e.color,"stroke-width":Le(e,n,s),"stroke-linecap":"round","marker-end":"url(#arrowhead-"+e.key+")",opacity:Ne(e,n),x1:c[0],y1:c[1],x2:a[0]-p,y2:a[1]-f})}(l,n,Ae(i(t.dest),e.orientation),o,(s.get(t.dest)||0)>1,c)}else a=function(e,t,o,n){const r=De(t,n),s=function(e){const t=e.width/512;return[3*t,4*t]}(n),i=(n.width+n.height)/32;return Ke(we("circle"),{stroke:e.color,"stroke-width":s[o?0:1],fill:"none",opacity:Ne(e,o),cx:r[0],cy:r[1],r:i-s[1]/2})}(r[t.brush],n,o,c)}return a.setAttribute("cgHash",n),a}function xe(e){const t=Ke(we("marker"),{id:"arrowhead-"+e.key,orient:"auto",markerWidth:4,markerHeight:8,refX:2.05,refY:2.01});return t.appendChild(Ke(we("path"),{d:"M0,0 V4 L3,2 Z",fill:e.color})),t.setAttribute("cgKey",e.key),t}function Ke(e,t){for(const o in t)e.setAttribute(o,t[o]);return e}function Ae(e,t){return"white"===t?e:[7-e[0],7-e[1]]}function qe(e,t){return{color:e.color,opacity:Math.round(10*e.opacity)/10,lineWidth:Math.round(t.lineWidth||e.lineWidth),key:[e.key,t.lineWidth].filter((e=>e)).join("")}}function Le(e,t,o){return(e.lineWidth||10)*(t?.85:1)/512*o.width}function Ne(e,t){return(e.opacity||1)*(t?.9:1)}function De(e,t){return[(e[0]+.5)*t.width/8,(7.5-e[1])*t.height/8]}function Te(e,t){const o=v("coords",t);let n;for(const t of e)n=v("coord"),n.textContent=t,o.appendChild(n);return o}function Oe(e,t){const o=e.dom.elements.board;if("ResizeObserver"in window&&new ResizeObserver(t).observe(e.dom.elements.wrap),e.viewOnly)return;const n=function(e){return t=>{e.draggable.current?ge(e):e.drawable.current?ce(e):t.shiftKey||m(t)?e.drawable.enabled&&ne(e,t):e.viewOnly||(e.dropmode.active?function(e,t){if(!e.dropmode.active)return;x(e),K(e);const o=e.dropmode.piece;if(o){e.pieces.set("a0",o);const n=h(t),r=n&&H(n,I(e),e.dom.bounds());r&&D(e,"a0",r)}e.dom.redraw()}(e,t):de(e,t))}}(e);o.addEventListener("touchstart",n,{passive:!1}),o.addEventListener("mousedown",n,{passive:!1}),(e.disableContextMenu||e.drawable.enabled)&&o.addEventListener("contextmenu",(e=>e.preventDefault()))}function Ee(e,t,o,n){return e.addEventListener(t,o,n),()=>e.removeEventListener(t,o,n)}function We(e,t,o){return n=>{e.drawable.current?e.drawable.enabled&&o(e,n):e.viewOnly||t(e,n)}}function $e(e){const t=I(e),o=p(e.dom.bounds()),n=e.dom.elements.board,r=e.pieces,s=e.animation.current,c=s?s.plan.anims:new Map,a=s?s.plan.fadings:new Map,l=e.draggable.current,d=function(e){var t;const o=new Map;if(e.lastMove&&e.highlight.lastMove)for(const t of e.lastMove)Ve(o,t,"last-move");e.check&&e.highlight.check&&Ve(o,e.check,"check");if(e.selected&&(Ve(o,e.selected,"selected"),e.movable.showDests)){const n=null===(t=e.movable.dests)||void 0===t?void 0:t.get(e.selected);if(n)for(const t of n)Ve(o,t,"move-dest"+(e.pieces.has(t)?" oc":""));const r=e.premovable.dests;if(r)for(const t of r)Ve(o,t,"premove-dest"+(e.pieces.has(t)?" oc":""))}const n=e.premovable.current;if(n)for(const e of n)Ve(o,e,"current-premove");else e.predroppable.current&&Ve(o,e.predroppable.current.key,"current-premove");const r=e.exploding;if(r)for(const e of r.keys)Ve(o,e,"exploding"+r.stage);return o}(e),u=new Set,g=new Set,h=new Map,m=new Map;let b,w,y,k,C,M,S,P,x,K;for(w=n.firstChild;w;){if(b=w.cgKey,Re(w))if(y=r.get(b),C=c.get(b),M=a.get(b),k=w.cgPiece,!w.cgDragging||l&&l.orig===b||(w.classList.remove("dragging"),f(w,o(i(b),t)),w.cgDragging=!1),!M&&w.cgFading&&(w.cgFading=!1,w.classList.remove("fading")),y){if(C&&w.cgAnimating&&k===Ie(y)){const e=i(b);e[0]+=C[2],e[1]+=C[3],w.classList.add("anim"),f(w,o(e,t))}else w.cgAnimating&&(w.cgAnimating=!1,w.classList.remove("anim"),f(w,o(i(b),t)),e.addPieceZIndex&&(w.style.zIndex=He(i(b),t)));k!==Ie(y)||M&&w.cgFading?M&&k===Ie(M)?(w.classList.add("fading"),w.cgFading=!0):je(h,k,w):u.add(b)}else je(h,k,w);else if(ze(w)){const e=w.className;d.get(b)===e?g.add(b):je(m,e,w)}w=w.nextSibling}for(const[e,r]of d)if(!g.has(e)){x=m.get(r),K=x&&x.pop();const s=o(i(e),t);if(K)K.cgKey=e,f(K,s);else{const t=v("square",r);t.cgKey=e,f(t,s),n.insertBefore(t,n.firstChild)}}for(const[s,a]of r)if(C=c.get(s),!u.has(s))if(S=h.get(Ie(a)),P=S&&S.pop(),P){P.cgKey=s,P.cgFading&&(P.classList.remove("fading"),P.cgFading=!1);const n=i(s);e.addPieceZIndex&&(P.style.zIndex=He(n,t)),C&&(P.cgAnimating=!0,P.classList.add("anim"),n[0]+=C[2],n[1]+=C[3]),f(P,o(n,t))}else{const r=Ie(a),c=v("piece",r),l=i(s);c.cgPiece=r,c.cgKey=s,C&&(c.cgAnimating=!0,l[0]+=C[2],l[1]+=C[3]),f(c,o(l,t)),e.addPieceZIndex&&(c.style.zIndex=He(l,t)),n.appendChild(c)}for(const t of h.values())Be(e,t);for(const t of m.values())Be(e,t)}function Fe(e){const t=e.dom.elements.wrap.getBoundingClientRect(),o=e.dom.elements.container,n=t.height/t.width,r=8*Math.floor(t.width*window.devicePixelRatio/8)/window.devicePixelRatio,s=r*n;o.style.width=r+"px",o.style.height=s+"px",e.dom.bounds.clear(),e.addDimensionsCssVars&&(document.documentElement.style.setProperty("--cg-width",r+"px"),document.documentElement.style.setProperty("--cg-height",s+"px"))}function Re(e){return"PIECE"===e.tagName}function ze(e){return"SQUARE"===e.tagName}function Be(e,t){for(const o of t)e.dom.elements.board.removeChild(o)}function He(e,t){let o=2+8*e[1]+(7-e[0]);return t&&(o=67-o),o+""}function Ie(e){return`${e.color} ${e.role}`}function Ve(e,t,o){const n=e.get(t);n?e.set(t,`${n} ${o}`):e.set(t,o)}function je(e,t,o){const n=e.get(t);n?n.push(o):e.set(t,[o])}function Ge(e){let t=!1;return()=>{t||(t=!0,requestAnimationFrame((()=>{e(),t=!1})))}}return function(n,r){const s={pieces:U(V),orientation:"white",turnColor:"white",coordinates:!0,autoCastle:!0,viewOnly:!1,disableContextMenu:!1,addPieceZIndex:!1,addDimensionsCssVars:!1,pieceKey:!1,highlight:{lastMove:!0,check:!0},animation:{enabled:!0,duration:200},movable:{free:!0,color:"both",showDests:!0,events:{},rookCastle:!0},premovable:{enabled:!0,showDests:!0,castle:!0,events:{}},predroppable:{enabled:!1,events:{}},draggable:{enabled:!0,distance:3,autoDistance:!0,showGhost:!0,deleteOnDropOff:!1},dropmode:{active:!1},selectable:{enabled:!0},stats:{dragged:!("ontouchstart"in window)},events:{},drawable:{enabled:!0,visible:!0,defaultSnapToValidMove:!0,eraseOnClick:!0,shapes:[],autoShapes:[],brushes:{green:{key:"g",color:"#15781B",opacity:1,lineWidth:10},red:{key:"r",color:"#882020",opacity:1,lineWidth:10},blue:{key:"b",color:"#003088",opacity:1,lineWidth:10},yellow:{key:"y",color:"#e68f00",opacity:1,lineWidth:10},paleBlue:{key:"pb",color:"#003088",opacity:.4,lineWidth:15},paleGreen:{key:"pg",color:"#15781B",opacity:.4,lineWidth:15},paleRed:{key:"pr",color:"#882020",opacity:.4,lineWidth:15},paleGrey:{key:"pgr",color:"#4a4a4a",opacity:.35,lineWidth:15}},pieces:{baseUrl:"https://lichess1.org/assets/piece/cburnett/"},prevSvgHash:""},hold:a()};function c(){const r="dom"in s?s.dom.unbind:void 0,c=function(n,r){n.innerHTML="",n.classList.add("cg-wrap");for(const t of e)n.classList.toggle("orientation-"+t,r.orientation===t);n.classList.toggle("manipulable",!r.viewOnly);const s=v("cg-container");n.appendChild(s);const i=v("cg-board");let c,a,l;if(s.appendChild(i),r.drawable.visible&&(c=Ke(we("svg"),{class:"cg-shapes"}),c.appendChild(we("defs")),c.appendChild(we("g")),a=Ke(we("svg"),{class:"cg-custom-svgs"}),a.appendChild(we("g")),s.appendChild(c),s.appendChild(a)),r.coordinates){const e="black"===r.orientation?" black":"";s.appendChild(Te(o,"ranks"+e)),s.appendChild(Te(t,"files"+e))}return r.draggable.showGhost&&(l=v("piece","ghost"),g(l,!1),s.appendChild(l)),{board:i,container:s,wrap:n,ghost:l,svg:c,customSvg:a}}(n,s),a=function(e){let t;const o=()=>(void 0===t&&(t=e()),t);return o.clear=()=>{t=void 0},o}((()=>c.board.getBoundingClientRect())),l=e=>{$e(u),!e&&c.svg&&ye(u,c.svg,c.customSvg)},d=()=>{Fe(u),function(e){const t=I(e),o=p(e.dom.bounds());let n=e.dom.elements.board.firstChild;for(;n;)(Re(n)&&!n.cgAnimating||ze(n))&&f(n,o(i(n.cgKey),t)),n=n.nextSibling}(u),c.svg&&ye(u,c.svg,c.customSvg)},u=s;return u.dom={elements:c,bounds:a,redraw:Ge(l),redrawNow:l,unbind:r},u.drawable.prevSvgHash="",Fe(u),l(!1),Oe(u,d),r||(u.dom.unbind=function(e,t){const o=[];if("ResizeObserver"in window||o.push(Ee(document.body,"chessground.resize",t)),!e.viewOnly){const t=We(e,pe,se),n=We(e,fe,ie);for(const e of["touchmove","mousemove"])o.push(Ee(document,e,t));for(const e of["touchend","mouseup"])o.push(Ee(document,e,n));const r=()=>e.dom.bounds.clear();o.push(Ee(document,"scroll",r,{capture:!0,passive:!0})),o.push(Ee(window,"resize",r,{passive:!0}))}return()=>o.forEach((e=>e()))}(u,d)),u.events.insert&&u.events.insert(c),u}return X(s,r||{}),be(c(),c)}}();