<commit_before>var defined = require('common').defined;

module.exports = function(element, cfg) {
  var data = cfg.data;
  lichess.openInMobileApp('/analyse/' + data.game.id);
  var $watchers = $('#site_header div.watchers').watchers();
  var analyse, $panels;
  lichess.socket = lichess.StrongSocket(
    data.url.socket,
    data.player.version, {
      options: {
        name: "analyse"
      },
      params: {
        userTv: $('.user_tv').data('user-tv')
      },
      receive: function(t, d) {
        analyse.socketReceive(t, d);
      },
      events: {
        analysisProgress: function(d) {
          if (!lichess.advantageChart) startAdvantageChart();
          else if (lichess.advantageChart.update) lichess.advantageChart.update(data);
          if (d.tree.eval) $("#adv_chart_loader").remove();
        },
        crowd: function(event) {
          $watchers.watchers("set", event.watchers);
        }
      }
    });

  var $timeChart = $("#movetimes_chart");
  var $inputFen = $('input.fen', element);
  var unselect = function(chart) {
    chart.getSelectedPoints().forEach(function(point) {
      point.select(false);
    });
  };
  var lastFen;
  cfg.onChange = function(fen, path, mainlinePly) {
    if (fen === lastFen) return;
    lastFen = fen = fen || lastFen;
    var chart, point, $chart = $("#adv_chart");
    $inputFen.val(fen);
    if ($chart.length) try {
      chart = $chart.highcharts();
      if (chart) {
        if (mainlinePly === false) unselect(chart);
        else {
          point = chart.series[0].data[mainlinePly - 1 - cfg.data.game.startedAtTurn];
          if (defined(point)) point.select();
          else unselect(chart);
        }
      }
    } catch (e) {}
    if ($timeChart.length) try {
      chart = $timeChart.highcharts();
      if (chart) {
        if (mainlinePly === false) unselect(chart);
        else {
          var white = mainlinePly % 2 !== 0;
          var serie = white ? 0 : 1;
          var turn = Math.floor((mainlinePly - 1 - cfg.data.game.startedAtTurn) / 2);
          point = chart.series[serie].data[turn];
          if (defined(point)) point.select();
          else unselect(chart);
        }
      }
    } catch (e) {}
  };
  cfg.onToggleComputer = function(v) {
    setTimeout(function() {
      if (v) $('div.analysis_menu a.computer_analysis').mousedown();
      else $('div.analysis_menu a:eq(1)').mousedown();
    }.bind(this), 50);
  };
  cfg.initialPly = 'url';
  cfg.element = element.querySelector('.analyse');
  cfg.socketSend = lichess.socket.send;
  analyse = LichessAnalyse.mithril(cfg);
  cfg.jumpToIndex = analyse.jumpToIndex;

  if (cfg.chat) {
    cfg.chat.parseMoves = true;
    lichess.makeChat('chat', cfg.chat);
  }

  setTimeout(function() {
    $('.underboard_content', element).appendTo($('.underboard .center', element)).removeClass('none');
  }, 200);

  var chartLoader = function() {
    return '<div id="adv_chart_loader">' +
      '<span>' + lichess.engineName + '<br>server analysis</span>' +
      lichess.spinnerHtml +
      '</div>'
  };
  var startAdvantageChart = function() {
    if (lichess.advantageChart) return;
    var loading = !data.treeParts[0].eval || !Object.keys(data.treeParts[0].eval).length;
    var $panel = $panels.filter('.computer_analysis');
    if (!$("#adv_chart").length) $panel.html('<div id="adv_chart"></div>' + (loading ? chartLoader() : ''));
    else if (loading && !$("#adv_chart_loader").length) $panel.append(chartLoader());
    lichess.loadScript('/assets/javascripts/chart/advantage.js').then(function() {
      lichess.advantageChart(data);
    });
  };

  $panels = $('div.analysis_panels > div');
  var $menu = $('div.analysis_menu');
  var storage = lichess.storage.make('analysis.panel');
  var setPanel = function(panel) {
    $menu.children('.active').removeClass('active').end().find('.' + panel).addClass('active');
    $panels.removeClass('active').filter('.' + panel).addClass('active');
    if (panel === 'move_times' && !lichess.movetimeChart) try {
      lichess.loadScript('/assets/javascripts/chart/movetime.js').then(function() {<commit_msg>fix #2683 and introduce new bugs instead - yet to be found<commit_after>var defined = require('common').defined;

module.exports = function(element, cfg) {
  var data = cfg.data;
  lichess.openInMobileApp('/analyse/' + data.game.id);
  var $watchers = $('#site_header div.watchers').watchers();
  var analyse, $panels;
  lichess.socket = lichess.StrongSocket(
    data.url.socket,
    data.player.version, {
      options: {
        name: "analyse"
      },
      params: {
        userTv: $('.user_tv').data('user-tv')
      },
      receive: function(t, d) {
        analyse.socketReceive(t, d);
      },
      events: {
        analysisProgress: function(d) {
          if (!lichess.advantageChart) startAdvantageChart();
          else if (lichess.advantageChart.update) lichess.advantageChart.update(data);
          if (d.tree.eval) $("#adv_chart_loader").remove();
        },
        crowd: function(event) {
          $watchers.watchers("set", event.watchers);
        }
      }
    });

  var $timeChart = $("#movetimes_chart");
  var $inputFen = $('input.fen', element);
  var unselect = function(chart) {
    chart.getSelectedPoints().forEach(function(point) {
      point.select(false);
    });
  };
  var lastFen;
  cfg.onChange = function(fen, path, mainlinePly) {
    if (fen === lastFen) return;
    lastFen = fen = fen || lastFen;
    var chart, point, $chart = $("#adv_chart");
    $inputFen.val(fen);
    if ($chart.length) try {
      chart = $chart.highcharts();
      if (chart) {
        if (mainlinePly === false) unselect(chart);
        else {
          point = chart.series[0].data[mainlinePly - 1 - cfg.data.game.startedAtTurn];
          if (defined(point)) point.select();
          else unselect(chart);
        }
      }
    } catch (e) {}
    if ($timeChart.length) try {
      chart = $timeChart.highcharts();
      if (chart) {
        if (mainlinePly === false) unselect(chart);
        else {
          var white = mainlinePly % 2 !== 0;
          var serie = white ? 0 : 1;
          var turn = Math.floor((mainlinePly - 1 - cfg.data.game.startedAtTurn) / 2);
          point = chart.series[serie].data[turn];
          if (defined(point)) point.select();
          else unselect(chart);
        }
      }
    } catch (e) {}
  };
  cfg.onToggleComputer = function(v) {
    setTimeout(function() {
      if (v) $('div.analysis_menu a.computer_analysis').mousedown();
      else $('div.analysis_menu a:eq(1)').mousedown();
    }.bind(this), 50);
  };
  cfg.initialPly = 'url';
  cfg.element = element.querySelector('.analyse');
  cfg.socketSend = lichess.socket.send;
  analyse = LichessAnalyse.mithril(cfg);
  cfg.jumpToIndex = analyse.jumpToIndex;

  if (cfg.chat) {
    cfg.chat.parseMoves = true;
    lichess.makeChat('chat', cfg.chat);
  }

  $('.underboard_content', element).appendTo($('.underboard .center', element)).removeClass('none');

  var chartLoader = function() {
    return '<div id="adv_chart_loader">' +
      '<span>' + lichess.engineName + '<br>server analysis</span>' +
      lichess.spinnerHtml +
      '</div>'
  };
  var startAdvantageChart = function() {
    if (lichess.advantageChart) return;
    var loading = !data.treeParts[0].eval || !Object.keys(data.treeParts[0].eval).length;
    var $panel = $panels.filter('.computer_analysis');
    if (!$("#adv_chart").length) $panel.html('<div id="adv_chart"></div>' + (loading ? chartLoader() : ''));
    else if (loading && !$("#adv_chart_loader").length) $panel.append(chartLoader());
    lichess.loadScript('/assets/javascripts/chart/advantage.js').then(function() {
      lichess.advantageChart(data);
    });
  };

  $panels = $('div.analysis_panels > div');
  var $menu = $('div.analysis_menu');
  var storage = lichess.storage.make('analysis.panel');
  var setPanel = function(panel) {
    $menu.children('.active').removeClass('active').end().find('.' + panel).addClass('active');
    $panels.removeClass('active').filter('.' + panel).addClass('active');
    if (panel === 'move_times' && !lichess.movetimeChart) try {
      lichess.loadScript('/assets/javascripts/chart/movetime.js').then(function() {
{'fraction_non_alphanumeric': 0.176056338028169, 'fraction_numerical': 0.004006799417192812, 'mean_word_length': 2.97346840328027, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '2454317', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2644, 'n_words': 480}