<commit_before>$(document).ready(function() {
  $('.new-dancers-idiom').change(function(e) {
    console.log("TODO "+$(this).val());
  });

  $('.new-move-idiom').change(function(e) {
    var term = $(this).val();
    var authenticityToken = $('#authenticity-token-incubator input[name=authenticity_token]').val();
    var presumed_server_substitution = term;
    var editor =
          $('<form action="/idioms" accept-charset="UTF-8" method="post">' +
            '  <input name="utf8" value="✓" type="hidden">' +
            '  <input name="idiom_idiom[term]" value="' + term + '" type="hidden">' +
            '  <input name="authenticity_token" value="' + authenticityToken +'" type="hidden">' +
                 term +
            ' → <input name="idiom_idiom[substitution]" type=text class="idiom-substitution"></label> <span class="idiom-ajax-progress"></span></form>');
    $('.idioms-list').append(editor);
    var progress = editor.find('.idiom-ajax-progress');
    editor.find('.idiom-substitution').val(presumed_server_substitution);
    editor.find('.idiom-substitution').on('input', function () {
      if (editor.find('.idiom-substitution').val() !== presumed_server_substitution) {
        progress.empty().append('<span class="glyphicon glyphicon-pencil" aria-label="changed"></span>');
      } else {
        progress.empty().append('<span class="glyphicon glyphicon-ok" aria-label="saved"></span>');
      }
    });
    editor.submit(function(event) {
      event.preventDefault();
      presumed_server_substitution = editor.find('.idiom-substitution').val();
      progress.empty().append('<span class="glyphicon glyphicon-arrow-up" aria-label="saving..."></span>');
      $.post(editor.attr('action'), editor.serialize()).done(function() {
        progress.empty().append('<span class="glyphicon glyphicon-ok" aria-label="saved"></span>');
      });
    });
  });

  // build menus
  dancerMenuForChooser(chooser_dancers).forEach(function(dancer) {
    $('.new-dancers-idiom').append($('<option value="'+dancer+'">'+dancer+'</option>'));
  });
  moves().forEach(function(move) {
    $('.new-move-idiom').append($('<option value="'+move+'">'+move+'</option>'));
  });
});

var buttonSubstitutions = $.map([['gent', 'gents', 'lady', 'ladies'],
                                 ['lark', 'larks', 'raven', 'ravens'],
                                 ['lead', 'leads', 'follow', 'follows']
                                ],
                                function(roleArr) {
                                  var gentlespoon = roleArr[0];
                                  var gentlespoons = roleArr[1];
                                  var ladle = roleArr[2];
                                  var ladles = roleArr[3];
                                  return {'gentlespoons': gentlespoons,
                                          'first gentlespoon': 'first '+gentlespoon,
                                          'second gentlespoon': 'second '+gentlespoon,
                                          'ladles': ladles,
                                          'first ladle': 'first '+ladle,
                                          'second ladle': 'second '+ladle};
                                });

function rebuildIdiomsList(idiom_json_array) {
  var idiomsList = $('.idioms-list');
  idiomsList.empty();
  $.each(idiom_json_array, function(meh, idiom) {
    idiomsList.append("<div>" + idiom.term + " → "+ idiom.substitution + "</div>");
  });
  $.each(buttonSubstitutions, function(meh, bsub) {
    var $form = $('.'+bsub['gentlespoons']+'-'+bsub['ladles']);
    setButtonLight($form, idiomJsonMatchesButtonSubstitution(idiom_json_array, bsub));
  });
}

function setButtonLight($form, bool) {
  $form.find('.btn').addClass(bool ? 'btn-primary' : 'btn-default').removeClass(bool ? 'btn-default' : 'btn-primary');
  $form.find('input[name=lit]').val(!bool);
}

function idiomJsonMatchesButtonSubstitution(idioms, bsub) {
  var matches = 0;
  var bsub_length = 0;
  for (var term in bsub) {
    bsub_length++;
    for (var i=0; i<idioms.length; i++) {
      var idiom = idioms[i];
      if (idiom.term === term) {
        if (idiom.substitution === bsub[term]) {
          matches++;
          break;
        } else {
          return false;
        }
      }
    }
  }
  return matches===bsub_length;
}

$(document).ready(function() {
  if ($('.idioms-list').length <= 0) {return;} // this code is about maintaining .idioms-list

  if ($('#idioms-init').length === 0) {
    throw new Error("Can't initialize page because can't find #idioms-init");
  }
  rebuildIdiomsList(JSON.parse($('#idioms-init').text()));

  // reset the whole dialect
  $('.restore-default-dialect-form').on('ajax:error', function() {
    $('.alert').html('Bummer! Error restoring default dialect.');
  }).on('ajax:success', function() {
    rebuildIdiomsList([]);
    $('.notice').html('Default dialect restored.');
  });<commit_msg>wip on building idiom editors when json comes back<commit_after>$(document).ready(function() {
  if ($('.idioms-list').length <= 0) {return;} // this code is about maintaining .idioms-list

  $('.new-dancers-idiom').change(function(e) {
    console.log("TODO "+$(this).val());
  });

  $('.new-move-idiom').change(function() {
    var term = $(this).val();
    makeIdiomEditor(term);
  });

  function makeIdiomEditor(term, opt_substitution, opt_id) {
    var authenticityToken = $('#authenticity-token-incubator input[name=authenticity_token]').val();
    var presumed_server_substitution = opt_substitution || term;
    var editor =
          $('<form action="/idioms" accept-charset="UTF-8" method="post">' +
            '  <input name="utf8" value="✓" type="hidden">' +
            '  <input name="idiom_idiom[term]" value="' + term + '" type="hidden">' +
            '  <input name="authenticity_token" value="' + authenticityToken +'" type="hidden">' +
            term +
            ' → <input name="idiom_idiom[substitution]" type=text class="idiom-substitution"></label> <span class="idiom-ajax-status"></span></form>');
    $('.idioms-list').append(editor);
    var status = editor.find('.idiom-ajax-status');
    editor.find('.idiom-substitution').val(presumed_server_substitution);
    editor.find('.idiom-substitution').on('input', function () {
      if (editor.find('.idiom-substitution').val() !== presumed_server_substitution) {
        indicateStatus(status, 'glyphicon-pencil', 'unsaved');
      } else {
        indicateStatus(status, 'glyphicon-ok', 'saved');
      }
    });
    editor.submit(function(event) {
      event.preventDefault();
      presumed_server_substitution = editor.find('.idiom-substitution').val();
      indicateStatus(status, 'glyphicon-arrow-up', 'saving');
      $.post(editor.attr('action'), editor.serialize()).done(function() {
        indicateStatus(status, 'glyphicon-ok', 'saved');
      });
    });
  }

  function indicateStatus(status, glyphiconClassName, ariaLabel) {
    status.empty().append('<span class="glyphicon ' + glyphiconClassName + '" aria-label="' + ariaLabel + '"></span>');
  }

  // build menus
  dancerMenuForChooser(chooser_dancers).forEach(function(dancer) {
    $('.new-dancers-idiom').append($('<option value="'+dancer+'">'+dancer+'</option>'));
  });
  moves().forEach(function(move) {
    $('.new-move-idiom').append($('<option value="'+move+'">'+move+'</option>'));
  });

var buttonSubstitutions = $.map([['gent', 'gents', 'lady', 'ladies'],
                                 ['lark', 'larks', 'raven', 'ravens'],
                                 ['lead', 'leads', 'follow', 'follows']
                                ],
                                function(roleArr) {
                                  var gentlespoon = roleArr[0];
                                  var gentlespoons = roleArr[1];
                                  var ladle = roleArr[2];
                                  var ladles = roleArr[3];
                                  return {'gentlespoons': gentlespoons,
                                          'first gentlespoon': 'first '+gentlespoon,
                                          'second gentlespoon': 'second '+gentlespoon,
                                          'ladles': ladles,
                                          'first ladle': 'first '+ladle,
                                          'second ladle': 'second '+ladle};
                                });

function rebuildIdiomsList(idiom_json_array) {
  var idiomsList = $('.idioms-list');
  idiomsList.empty();
  $.each(idiom_json_array, function(meh, idiom) {
    // idiomsList.append("<div>" + idiom.term + " → "+ idiom.substitution + "</div>");
    makeIdiomEditor(idiom.term, idiom.substitution, idiom.id);
  });
  $.each(buttonSubstitutions, function(meh, bsub) {
    var $form = $('.'+bsub['gentlespoons']+'-'+bsub['ladles']);
    setButtonLight($form, idiomJsonMatchesButtonSubstitution(idiom_json_array, bsub));
  });
}

function setButtonLight($form, bool) {
  $form.find('.btn').addClass(bool ? 'btn-primary' : 'btn-default').removeClass(bool ? 'btn-default' : 'btn-primary');
  $form.find('input[name=lit]').val(!bool);
}

function idiomJsonMatchesButtonSubstitution(idioms, bsub) {
  var matches = 0;
  var bsub_length = 0;
  for (var term in bsub) {
    bsub_length++;
    for (var i=0; i<idioms.length; i++) {
      var idiom = idioms[i];
      if (idiom.term === term) {
        if (idiom.substitution === bsub[term]) {
          matches++;
          break;
        } else {
          return false;
        }
      }
    }
  }
  return matches===bsub_length;
}

  if ($('#idioms-init').length === 0) {
    throw new Error("Can't initialize page because can't find #idioms-init");
  }
  rebuildIdiomsList(JSON.parse($('#idioms-init').text()));

  // reset the whole dialect
  $('.restore-default-dialect-form').on('ajax:error', function() {
    $('.alert').html('Bummer! Error restoring default dialect.');
  }).on('ajax:success', function() {
    rebuildIdiomsList([]);
    $('.notice').html('Default dialect restored.');
  });
{'fraction_non_alphanumeric': 0.1839491458085022, 'fraction_numerical': 0.0021851410409217324, 'mean_word_length': 2.521860790486184, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2115518', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2963, 'n_words': 504}