<commit_before>/* eslint-env browser, jquery */
/* global io */

const api = (method, endpoint, params = {}) => {
  const url = `/api${endpoint}`;

  if (method === 'GET') {
    return fetch(`${url}?${$.param(params)}`, {
      method: 'GET',
      credentials: 'include',
    }).then(res => res.json());
  } else if (method === 'POST') {
    const csrfToken = $('meta[name=csrf-token]').attr('content');
    params._csrf = csrfToken;

    return fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      body: $.param(params),
      credentials: 'include',
    }).then(res => res.json());
  }

  return Promise.reject();
};

const updateLanguages = () => {
  api.get('/languages').then((languages) => {
    $('.language').each((index, languageEl) => {
      const $language = $(languageEl);
      const language = languages.find(language => language.slug === $language.data('slug'));

      if (language && language.owner) {
        $language.css({
          backgroundColor: language.owner.color,
          color: 'white',
        });
      } else {
        $language.css({
          backgroundColor: '',
          color: '',
        });
      }
    });
  });
};

api.get = api.bind(null, 'GET');
api.post = api.bind(null, 'POST');

$(document).ready(() => {
  const $modal = $('#language-modal');
  let pendingSubmission = {};

  $('.language').each((index, language) => {
    const $language = $(language);

    $language.click(() => {
      // Set title
      $modal.find('.modal-title').text($language.find('.name').text());

      $modal.find('.code').val('').attr('readonly', true);
      $modal.find('.submit-code').attr('disabled', true);
      $modal.find('.result').removeClass('bg-warning bg-success').hide();
      $modal.data('language', $language.data('slug'));

      api.get(`/languages/${$language.data('slug')}`).then((language) => {<commit_msg>Only execute JS in root page<commit_after>/* eslint-env browser, jquery */
/* global io */

const api = (method, endpoint, params = {}) => {
  const url = `/api${endpoint}`;

  if (method === 'GET') {
    return fetch(`${url}?${$.param(params)}`, {
      method: 'GET',
      credentials: 'include',
    }).then(res => res.json());
  } else if (method === 'POST') {
    const csrfToken = $('meta[name=csrf-token]').attr('content');
    params._csrf = csrfToken;

    return fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      body: $.param(params),
      credentials: 'include',
    }).then(res => res.json());
  }

  return Promise.reject();
};

const updateLanguages = () => {
  api.get('/languages').then((languages) => {
    $('.language').each((index, languageEl) => {
      const $language = $(languageEl);
      const language = languages.find(language => language.slug === $language.data('slug'));

      if (language && language.owner) {
        $language.css({
          backgroundColor: language.owner.color,
          color: 'white',
        });
      } else {
        $language.css({
          backgroundColor: '',
          color: '',
        });
      }
    });
  });
};

api.get = api.bind(null, 'GET');
api.post = api.bind(null, 'POST');

$(document).ready(() => {
  if (location.pathname !== '/') {
    return;
  }

  const $modal = $('#language-modal');
  let pendingSubmission = {};

  $('.language').each((index, language) => {
    const $language = $(language);

    $language.click(() => {
      // Set title
      $modal.find('.modal-title').text($language.find('.name').text());

      $modal.find('.code').val('').attr('readonly', true);
      $modal.find('.submit-code').attr('disabled', true);
      $modal.find('.result').removeClass('bg-warning bg-success').hide();
      $modal.data('language', $language.data('slug'));

      api.get(`/languages/${$language.data('slug')}`).then((language) => {
{'fraction_non_alphanumeric': 0.22558667676003027, 'fraction_numerical': 0.0005046681806712087, 'mean_word_length': 2.8262548262548264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2834424', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1234, 'n_words': 216}