<commit_before>var inaturalist = inaturalist || { };
inaturalist.ICONIC_TAXA = <%= Hash[Taxon::ICONIC_TAXA.map{ |t|
  [ t.id, { name: t.name, icon: asset_path("assets/iconic_taxa/#{ t.name.downcase }-75px.png") } ] }].to_json %>;
inaturalist.UNKNOWN_TAXON_ICON = "<%= asset_path('assets/iconic_taxa/unknown-75px.png') %>";
inaturalist.TAXON_ROOT_URL = "<%= FakeView.taxon_url(1)[0..-2] %>";

var autocompleter = { };

autocompleter.capitalize = function( s ) {
  if( !s ) { return; }
  return s[0].toUpperCase( ) + s.slice( 1 );
};

// return the taxon's photo, or it's iconic taxon icon, or use the unknown icon
autocompleter.defaultPhotoForResult = function( result ) {
  if( result.type == "search_external" || result.type == "message" ) { return; }
  if( result.default_photo_url ) { return result.default_photo_url; }
  if( inaturalist.ICONIC_TAXA[ result.iconic_taxon_id ] ) {
    return inaturalist.ICONIC_TAXA[ result.iconic_taxon_id ].icon;
  } else {
    return inaturalist.UNKNOWN_TAXON_ICON;
  }
};

// return the first (preferred) name in a locale
autocompleter.preferredNameInLocale = function( result, locale ) {
  var nameInLocale;
  if( locale ) { locale = locale.split('-')[0]; }
  _.each( result.names, function( n ) {
    if( nameInLocale ) { return; }
    if( n.locale === locale ) { nameInLocale = n.name; }
  });
  return nameInLocale;
};

// return the best name in the locale, or defaulting to EN
autocompleter.setTitle = function( result ) {
  if( result.title ) { return; }
  var name = autocompleter.preferredNameInLocale( result, I18n.locale );
  if( !name && I18n.locale !== "en" ) {
    name = autocompleter.preferredNameInLocale( result, "en" );
  }
  result.title = name || result.name;
};

autocompleter.taxonTemplate = function( result ) {
  autocompleter.setTitle( result );
  if( result.title && result.name != result.title ) {
    if( result.rank_level <= 10 ) {
      result.subtitle = "<i>" + result.name + "</i>";
    } else {
      result.subtitle = result.name;
    }
  }
  if( result.rank && ( result.rank_level > 10 || !result.subtitle )) {
    result.subtitle =
      autocompleter.capitalize( I18n.t("ranks." + result.rank, { defaultValue: result.rank } ) ) +
      ( result.subtitle ? " " + result.subtitle : "" );
  }

  result.default_photo_url = autocompleter.defaultPhotoForResult( result );
  var wrapperDiv = $( "<div/>" ).addClass( "ac" ).attr( "data-taxon-id", result.id ).
    attr( "data-type", result.type );
  var thumbDiv = $( "<div/>" ).addClass( "ac-thumb" );
  if( result.default_photo_url ) {
    var thumbImage = $( "<img/>" ).attr( "src", result.default_photo_url ).appendTo( thumbDiv );
  }
  var labelDiv = $( "<div/>" ).addClass( "ac-label" );
  labelDiv.append( $( "<span/>" ).addClass( "title" ).append( result.title ));
  labelDiv.append( $( "<span/>" ).addClass( "subtitle" ).
    append( result.subtitle ));
  // anything w/o an ID is some kind of message or non-result UI
  if( !result.id) {
    wrapperDiv.addClass( "ac-message" );
    // JQuery autocomplete need an <a> tag in order to
    // consider the row active and clickable.
    labelDiv.append( $( "<a/>" ).attr( "href", "#" ) );
  }
  if( result.id ) {
    var rightElement = $( "<a/>" ).attr( "target", "_blank" ).
      attr( "href", inaturalist.TAXON_ROOT_URL + result.id );
    var viewDiv = $( "<div/>" ).addClass( "ac-view" ).append( I18n.t("view") ).appendTo( rightElement );
    wrapperDiv.append( rightElement );
  }
  wrapperDiv.append( thumbDiv );
  wrapperDiv.append( labelDiv );
  wrapperDiv.append( rightElement );
  return wrapperDiv;
};

autocompleter.resetTaxonSelection = function( options ) {
  options = options || { };
  options.taxon_id_el = options.taxon_id_el || $("#taxon_id");
  options.taxon_id_el.val( null );
  $(".ac-select-thumb img").attr( "src", inaturalist.UNKNOWN_TAXON_ICON );
  if( options.afterUnselect ) { options.afterUnselect( ); }
};

autocompleter.selectedTaxonID = function( options ) {
  options = options || { };
  options.taxon_id_el = options.taxon_id_el || $("#taxon_id");
  return options.taxon_id_el.val( );
};

autocompleter.createWrappingDiv = function( field, options ) {
  if( !field.parent().hasClass( "ac-chooser" ) ) {
    var wrappingDiv = $( "<div/>" ).addClass( "ac-chooser" );
    field.wrap( wrappingDiv );
    if( options.thumbnail !== false ) {
      // render the initial thumbnail of existing result, or the unknown icon
      var photo_url = autocompleter.defaultPhotoForResult({
        default_photo_url: field.data( "default-photo-url" ),
        iconic_taxon_id: field.data( "iconic-taxon-id" ) });<commit_msg>small changes so autocomplete JS is recompiled<commit_after>var inaturalist = inaturalist || { };
inaturalist.ICONIC_TAXA = <%= Hash[Taxon::ICONIC_TAXA.map{ |t|
  [ t.id, { name: t.name, icon: asset_path("assets/iconic_taxa/#{ t.name.downcase }-75px.png") } ] }].to_json %>;
inaturalist.UNKNOWN_TAXON_ICON = "<%= asset_path('assets/iconic_taxa/unknown-75px.png') %>";
inaturalist.TAXON_ROOT_URL = "<%= FakeView.taxon_url(1)[0..-2] %>";

var autocompleter = { };

autocompleter.capitalize = function( s ) {
  if( !s ) { return; }
  return s[0].toUpperCase( ) + s.slice( 1 );
};

// return the taxon's photo, or it's iconic taxon icon, or use the unknown icon
autocompleter.defaultPhotoForResult = function( result ) {
  if( result.type === "search_external" || result.type === "message" ) { return; }
  if( result.default_photo_url ) { return result.default_photo_url; }
  if( inaturalist.ICONIC_TAXA[ result.iconic_taxon_id ] ) {
    return inaturalist.ICONIC_TAXA[ result.iconic_taxon_id ].icon;
  } else {
    return inaturalist.UNKNOWN_TAXON_ICON;
  }
};

// return the first (preferred) name in a locale
autocompleter.preferredNameInLocale = function( result, locale ) {
  var nameInLocale;
  if( locale ) { locale = locale.split('-')[0]; }
  _.each( result.names, function( n ) {
    if( nameInLocale ) { return; }
    if( n.locale === locale ) { nameInLocale = n.name; }
  });
  return nameInLocale;
};

// return the best name in the locale, or defaulting to EN
autocompleter.setTitle = function( result ) {
  if( result.title ) { return; }
  var name = autocompleter.preferredNameInLocale( result, I18n.locale );
  if( !name && I18n.locale !== "en" ) {
    name = autocompleter.preferredNameInLocale( result, "en" );
  }
  result.title = name || result.name;
};

autocompleter.taxonTemplate = function( result ) {
  autocompleter.setTitle( result );
  if( result.title && result.name != result.title ) {
    if( result.rank_level <= 10 ) {
      result.subtitle = "<i>" + result.name + "</i>";
    } else {
      result.subtitle = result.name;
    }
  }
  if( result.rank && ( result.rank_level > 10 || !result.subtitle )) {
    result.subtitle =
      autocompleter.capitalize( I18n.t("ranks." + result.rank, { defaultValue: result.rank } ) ) +
      ( result.subtitle ? " " + result.subtitle : "" );
  }

  result.default_photo_url = autocompleter.defaultPhotoForResult( result );
  var wrapperDiv = $( "<div/>" ).addClass( "ac" ).attr( "data-taxon-id", result.id ).
    attr( "data-type", result.type );
  var thumbDiv = $( "<div/>" ).addClass( "ac-thumb" );
  if( result.default_photo_url ) {
    $( "<img/>" ).attr( "src", result.default_photo_url ).appendTo( thumbDiv );
  }
  var labelDiv = $( "<div/>" ).addClass( "ac-label" );
  labelDiv.append( $( "<span/>" ).addClass( "title" ).append( result.title ));
  labelDiv.append( $( "<span/>" ).addClass( "subtitle" ).
    append( result.subtitle ));
  // anything w/o an ID is some kind of message or non-result UI
  if( !result.id) {
    wrapperDiv.addClass( "ac-message" );
    // JQuery autocomplete need an <a> tag in order to
    // consider the row active and clickable.
    labelDiv.append( $( "<a/>" ).attr( "href", "#" ) );
  }
  if( result.id ) {
    var rightElement = $( "<a/>" ).attr( "target", "_blank" ).
      attr( "href", inaturalist.TAXON_ROOT_URL + result.id );
    $( "<div/>" ).addClass( "ac-view" ).append( I18n.t("view") ).appendTo( rightElement );
    wrapperDiv.append( rightElement );
  }
  wrapperDiv.append( thumbDiv );
  wrapperDiv.append( labelDiv );
  wrapperDiv.append( rightElement );
  return wrapperDiv;
};

autocompleter.resetTaxonSelection = function( options ) {
  options = options || { };
  options.taxon_id_el = options.taxon_id_el || $("#taxon_id");
  options.taxon_id_el.val( null );
  $(".ac-select-thumb img").attr( "src", inaturalist.UNKNOWN_TAXON_ICON );
  if( options.afterUnselect ) { options.afterUnselect( ); }
};

autocompleter.selectedTaxonID = function( options ) {
  options = options || { };
  options.taxon_id_el = options.taxon_id_el || $("#taxon_id");
  return options.taxon_id_el.val( );
};

autocompleter.createWrappingDiv = function( field, options ) {
  if( !field.parent().hasClass( "ac-chooser" ) ) {
    var wrappingDiv = $( "<div/>" ).addClass( "ac-chooser" );
    field.wrap( wrappingDiv );
    if( options.thumbnail !== false ) {
      // render the initial thumbnail of existing result, or the unknown icon
      var photo_url = autocompleter.defaultPhotoForResult({
        default_photo_url: field.data( "default-photo-url" ),
        iconic_taxon_id: field.data( "iconic-taxon-id" ) });
{'fraction_non_alphanumeric': 0.16966024669984853, 'fraction_numerical': 0.004760874269638607, 'mean_word_length': 3.6517362858580773, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '3110004', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3224, 'n_words': 682}