<commit_before>import drag from './drag';
import util from './util';
import * as Vnode from 'mithril/render/vnode';

export default function renderBoard(ctrl) {
  return Vnode(
    'div',
    undefined,
    {
      className: [
        'cg-board',
        ctrl.data.viewOnly ? 'view-only' : 'manipulable'
      ].join(' '),
      oncreate: function(vnode) {
        ctrl.data.element = vnode.dom;
        ctrl.data.render = function() {
          diffBoard(ctrl);
        };
        ctrl.data.renderRAF = function() {
          ctrl.data.batchRAF(ctrl.data.render);
        };

        if (!ctrl.data.bounds) {
          ctrl.data.bounds = vnode.dom.getBoundingClientRect();
        }

        if (!ctrl.data.viewOnly) {
          ctrl.data.render();
          bindEvents(ctrl, vnode.dom);
        }

        if (ctrl.data.coordinates) {
          makeCoords(ctrl.data.element.parentNode, !!ctrl.data.symmetricCoordinates);
          if (ctrl.data.symmetricCoordinates) {
            makeSymmCoords(ctrl.data.element.parentNode);
          }
        }
      }
    },
    ctrl.data.viewOnly ? renderContent(ctrl) : [],
    undefined,
    undefined
  );
}

function diffBoard(ctrl) {
  const d = ctrl.data;
  const asWhite = d.orientation === 'white';
  const orientationChange = d.prevOrientation && d.prevOrientation !== d.orientation;
  d.prevOrientation = d.orientation;
  const bounds = d.bounds;
  const pieces = ctrl.data.pieces;
  const anims = ctrl.data.animation.current.anims;
  const capturedPieces = ctrl.data.animation.current.capturedPieces;
  const squares = computeSquareClassesMap(ctrl);
  const samePieces = new Set();
  const sameSquares = new Set();
  const movedPieces = new Map();
  const movedSquares = new Map();
  const piecesKeys = Object.keys(pieces);
  let el, squareClassAtKey, pieceAtKey, pieceId, anim, captured, translate;
  let mvdset, mvd;

  let otbTurnFlipChange, otbModeChange;
  if (d.otb) {
    otbTurnFlipChange = d.prevTurnColor && d.prevTurnColor !== d.turnColor;
    otbModeChange = d.prevOtbMode && d.prevOtbMode !== d.otbMode;
    d.prevOtbMode = d.otbMode;
    d.prevTurnColor = d.turnColor;
  }

  // walk over all board dom elements, apply animations and flag moved pieces
  el = ctrl.data.element.firstChild;
  while (el) {
    let k = el.cgKey;
    pieceAtKey = pieces[k];
    squareClassAtKey = squares.get(k);
    pieceId = el.cgRole + el.cgColor;
    anim = anims && anims[k];
    captured = capturedPieces && capturedPieces[k];
    if (el.tagName === 'PIECE') {
      // if piece not being dragged anymore, remove dragging style
      if (el.cgDragging && d.draggable.current.orig !== k) {
        el.classList.remove('dragging');
        el.classList.remove('magnified');
        translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
        el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
        el.cgDragging = false;
      }
      // there is now a piece at this dom key
      if (pieceAtKey) {
        // continue animation if already animating and same color
        // (otherwise it could animate a captured piece)
        if (anim && el.cgAnimating && el.cgColor === pieceAtKey.color) {
          translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
          translate[0] += anim[1][0];
          translate[1] += anim[1][1];
          el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
        } else if (el.cgAnimating) {
          translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
          el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
          el.cgAnimating = false;
        }
        // same piece: flag as same
        if (!orientationChange && !otbTurnFlipChange && !otbModeChange && el.cgColor === pieceAtKey.color && el.cgRole === pieceAtKey.role) {
          samePieces.add(k);
        }
        // different piece: flag as moved unless it is a captured piece
        else {
          if (captured && captured.role === el.cgRole && captured.color === el.cgColor) {
            el.classList.add('captured');
          } else {
            movedPieces.set(pieceId, (movedPieces.get(pieceId) || []).concat(el));
          }
        }
      }
      // no piece: flag as moved
      else {
        movedPieces.set(pieceId, (movedPieces.get(pieceId) || []).concat(el));
      }
    }
    else if (el.tagName === 'SQUARE') {
      if (!orientationChange && squareClassAtKey === el.className) {
        sameSquares.add(k);
      }
      else {
        movedSquares.set(
          el.className,
          (movedSquares.get(el.className) || []).concat(el)
        );
      }<commit_msg>Fix captured class who'd remain under some circumstances<commit_after>import drag from './drag';
import util from './util';
import * as Vnode from 'mithril/render/vnode';

export default function renderBoard(ctrl) {
  return Vnode(
    'div',
    undefined,
    {
      className: [
        'cg-board',
        ctrl.data.viewOnly ? 'view-only' : 'manipulable'
      ].join(' '),
      oncreate: function(vnode) {
        ctrl.data.element = vnode.dom;
        ctrl.data.render = function() {
          diffBoard(ctrl);
        };
        ctrl.data.renderRAF = function() {
          ctrl.data.batchRAF(ctrl.data.render);
        };

        if (!ctrl.data.bounds) {
          ctrl.data.bounds = vnode.dom.getBoundingClientRect();
        }

        if (!ctrl.data.viewOnly) {
          ctrl.data.render();
          bindEvents(ctrl, vnode.dom);
        }

        if (ctrl.data.coordinates) {
          makeCoords(ctrl.data.element.parentNode, !!ctrl.data.symmetricCoordinates);
          if (ctrl.data.symmetricCoordinates) {
            makeSymmCoords(ctrl.data.element.parentNode);
          }
        }
      }
    },
    ctrl.data.viewOnly ? renderContent(ctrl) : [],
    undefined,
    undefined
  );
}

function diffBoard(ctrl) {
  const d = ctrl.data;
  const asWhite = d.orientation === 'white';
  const orientationChange = d.prevOrientation && d.prevOrientation !== d.orientation;
  d.prevOrientation = d.orientation;
  const bounds = d.bounds;
  const pieces = ctrl.data.pieces;
  const anims = ctrl.data.animation.current.anims;
  const capturedPieces = ctrl.data.animation.current.capturedPieces;
  const squares = computeSquareClassesMap(ctrl);
  const samePieces = new Set();
  const sameSquares = new Set();
  const movedPieces = new Map();
  const movedSquares = new Map();
  const piecesKeys = Object.keys(pieces);
  let el, squareClassAtKey, pieceAtKey, pieceId, anim, captured, translate;
  let mvdset, mvd;

  let otbTurnFlipChange, otbModeChange;
  if (d.otb) {
    otbTurnFlipChange = d.prevTurnColor && d.prevTurnColor !== d.turnColor;
    otbModeChange = d.prevOtbMode && d.prevOtbMode !== d.otbMode;
    d.prevOtbMode = d.otbMode;
    d.prevTurnColor = d.turnColor;
  }

  // walk over all board dom elements, apply animations and flag moved pieces
  el = ctrl.data.element.firstChild;
  while (el) {
    let k = el.cgKey;
    pieceAtKey = pieces[k];
    squareClassAtKey = squares.get(k);
    pieceId = el.cgRole + el.cgColor;
    anim = anims && anims[k];
    captured = capturedPieces && capturedPieces[k];
    if (el.tagName === 'PIECE') {
      // if piece not being dragged anymore, remove dragging style
      if (el.cgDragging && d.draggable.current.orig !== k) {
        el.classList.remove('dragging');
        el.classList.remove('magnified');
        translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
        el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
        el.cgDragging = false;
      }
      // remove captured class if it still remains
      // (not sure yet if that branch is reachable)
      if (!captured && el.cgCaptured) {
        el.cgCaptured = false;
        el.classList.remove('captured');
      }
      // there is now a piece at this dom key
      if (pieceAtKey) {
        // continue animation if already animating and same color
        // (otherwise it could animate a captured piece)
        if (anim && el.cgAnimating && el.cgColor === pieceAtKey.color) {
          translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
          translate[0] += anim[1][0];
          translate[1] += anim[1][1];
          el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
        } else if (el.cgAnimating) {
          translate = util.posToTranslate(util.key2pos(k), asWhite, bounds);
          el.style.transform = util.transform(d, el.cgColor, util.translate(translate));
          el.cgAnimating = false;
        }
        // same piece: flag as same
        if (!orientationChange && !otbTurnFlipChange && !otbModeChange && el.cgColor === pieceAtKey.color && el.cgRole === pieceAtKey.role) {
          samePieces.add(k);
        }
        // different piece: flag as moved unless it is a captured piece
        else {
          if (captured && !el.cgCaptured && captured.role === el.cgRole && captured.color === el.cgColor) {
            el.cgCaptured = true;
            el.classList.add('captured');
          } else {
            movedPieces.set(pieceId, (movedPieces.get(pieceId) || []).concat(el));
          }
        }
      }
      // no piece: flag as moved
      else {
        movedPieces.set(pieceId, (movedPieces.get(pieceId) || []).concat(el));
      }
    }
    else if (el.tagName === 'SQUARE') {
      if (!orientationChange && squareClassAtKey === el.className) {
        sameSquares.add(k);
      }
      else {
        movedSquares.set(
          el.className,
          (movedSquares.get(el.className) || []).concat(el)
        );
      }
{'fraction_non_alphanumeric': 0.13330581923235657, 'fraction_numerical': 0.0018572018159306646, 'mean_word_length': 2.4494661921708185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256316', 'n_tokens_mistral': 3077, 'n_tokens_neox': 3021, 'n_words': 653}