<commit_before>import * as ground from './ground';
import * as cg from 'chessground/types';
import xhr = require('./xhr');
import { key2pos } from 'chessground/util';
import { bind } from './util';
import { h } from 'snabbdom'

let promoting: any | undefined;
let prePromotionRole: cg.Role | undefined;

function sendPromotion(ctrl, orig, dest, role, meta) {
  ground.promote(ctrl.chessground, dest, role);
  ctrl.sendMove(orig, dest, role, meta);
  return true;
}

export function start(ctrl, orig, dest, meta) {
  var d = ctrl.data;
  var piece = ctrl.chessground.state.pieces[dest];
  var premovePiece = ctrl.chessground.state.pieces[orig];
  if (((piece && piece.role === 'pawn') || (premovePiece && premovePiece.role === 'pawn')) && (
    (dest[1] == 8 && d.player.color === 'white') ||
      (dest[1] == 1 && d.player.color === 'black'))) {
    if (prePromotionRole && meta.premove) return sendPromotion(ctrl, orig, dest, prePromotionRole, meta);
    if (!meta.ctrlKey && (d.pref.autoQueen === 3 || (d.pref.autoQueen === 2 && premovePiece))) {
      if (premovePiece) setPrePromotion(ctrl, dest, 'queen');
      else sendPromotion(ctrl, orig, dest, 'queen', meta);
      return true;
    }
    promoting = {
      move: [orig, dest],
      pre: !!premovePiece,
      meta: meta
    };
    ctrl.redraw();
    return true;
  }
  return false;
}

function setPrePromotion(ctrl, dest, role) {
  prePromotionRole = role;
  ctrl.chessground.setAutoShapes([{
    orig: dest,
    piece: {
      color: ctrl.data.player.color,
      role: role,
      opacity: 0.8
    }
  }]);
}

export function cancelPrePromotion(ctrl) {
  if (prePromotionRole) {
    ctrl.chessground.setAutoShapes([]);
    prePromotionRole = undefined;
    ctrl.redraw();
  }
}

function finish(ctrl, role) {
  if (promoting) {
    if (promoting.pre) setPrePromotion(ctrl, promoting.move[1], role);
    else sendPromotion(ctrl, promoting.move[0], promoting.move[1], role, promoting.meta);
  }
  promoting = undefined;
}

export function cancel(ctrl) {
  cancelPrePromotion(ctrl);
  ctrl.chessground.cancelPremove();
  if (promoting) xhr.reload(ctrl).then(ctrl.reload);
  promoting = undefined;
}

function renderPromotion(ctrl, dest, pieces, color, orientation) {
  var left = (8 - key2pos(dest)[0]) * 12.5;
  if (orientation === 'white') left = 87.5 - left;
  var vertical = color === orientation ? 'top' : 'bottom';

  return h('div#promotion_choice.' + vertical, {
    hook: {
      insert: vnode => {
        const el = vnode.elm as HTMLElement;
        el.addEventListener('click', () => cancel(ctrl));
        el.addEventListener('contextmenu', () => false);
      }<commit_msg>disable contextmenu in promotion UI<commit_after>import * as ground from './ground';
import * as cg from 'chessground/types';
import xhr = require('./xhr');
import { key2pos } from 'chessground/util';
import { bind } from './util';
import { h } from 'snabbdom'

let promoting: any | undefined;
let prePromotionRole: cg.Role | undefined;

function sendPromotion(ctrl, orig, dest, role, meta) {
  ground.promote(ctrl.chessground, dest, role);
  ctrl.sendMove(orig, dest, role, meta);
  return true;
}

export function start(ctrl, orig, dest, meta) {
  var d = ctrl.data;
  var piece = ctrl.chessground.state.pieces[dest];
  var premovePiece = ctrl.chessground.state.pieces[orig];
  if (((piece && piece.role === 'pawn') || (premovePiece && premovePiece.role === 'pawn')) && (
    (dest[1] == 8 && d.player.color === 'white') ||
      (dest[1] == 1 && d.player.color === 'black'))) {
    if (prePromotionRole && meta.premove) return sendPromotion(ctrl, orig, dest, prePromotionRole, meta);
    if (!meta.ctrlKey && (d.pref.autoQueen === 3 || (d.pref.autoQueen === 2 && premovePiece))) {
      if (premovePiece) setPrePromotion(ctrl, dest, 'queen');
      else sendPromotion(ctrl, orig, dest, 'queen', meta);
      return true;
    }
    promoting = {
      move: [orig, dest],
      pre: !!premovePiece,
      meta: meta
    };
    ctrl.redraw();
    return true;
  }
  return false;
}

function setPrePromotion(ctrl, dest, role) {
  prePromotionRole = role;
  ctrl.chessground.setAutoShapes([{
    orig: dest,
    piece: {
      color: ctrl.data.player.color,
      role: role,
      opacity: 0.8
    }
  }]);
}

export function cancelPrePromotion(ctrl) {
  if (prePromotionRole) {
    ctrl.chessground.setAutoShapes([]);
    prePromotionRole = undefined;
    ctrl.redraw();
  }
}

function finish(ctrl, role) {
  if (promoting) {
    if (promoting.pre) setPrePromotion(ctrl, promoting.move[1], role);
    else sendPromotion(ctrl, promoting.move[0], promoting.move[1], role, promoting.meta);
  }
  promoting = undefined;
}

export function cancel(ctrl) {
  cancelPrePromotion(ctrl);
  ctrl.chessground.cancelPremove();
  if (promoting) xhr.reload(ctrl).then(ctrl.reload);
  promoting = undefined;
}

function renderPromotion(ctrl, dest, pieces, color, orientation) {
  var left = (8 - key2pos(dest)[0]) * 12.5;
  if (orientation === 'white') left = 87.5 - left;
  var vertical = color === orientation ? 'top' : 'bottom';

  return h('div#promotion_choice.' + vertical, {
    hook: {
      insert: vnode => {
        const el = vnode.elm as HTMLElement;
        el.addEventListener('click', () => cancel(ctrl));
        el.addEventListener('contextmenu', e => {
          e.preventDefault();
          return false;
        });
      }
{'fraction_non_alphanumeric': 0.1599479843953186, 'fraction_numerical': 0.007802340702210663, 'mean_word_length': 3.509212730318258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5187003', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1794, 'n_words': 429}