<commit_before>import $ from 'jquery';

window.SNIPPET_CHOOSER_MODAL_ONLOAD_HANDLERS = {
  choose: function (modal) {
    function ajaxifyLinks(context) {
      $('a.snippet-choice', modal.body).on('click', function () {
        modal.loadUrl(this.href);
        return false;
      });

      $('.pagination a', context).on('click', function () {
        loadResults(this.href);
        return false;
      });
    }

    var searchForm$ = $('form.snippet-search', modal.body);
    var searchUrl = searchForm$.attr('action');
    var request;

    function search() {
      loadResults(searchUrl, searchForm$.serialize());
      return false;
    }

    function loadResults(url, data) {
      var opts = {
        url: url,
        success: function (resultsData, status) {
          request = null;
          $('#search-results').html(resultsData);
          ajaxifyLinks($('#search-results'));
        },
        error: function () {
          request = null;
        },
      };
      if (data) {
        opts.data = data;
      }
      request = $.ajax(opts);
    }

    $('form.snippet-search', modal.body).on('submit', search);
    $('#snippet-chooser-locale', modal.body).on('change', search);

    $('#id_q').on('input', function () {
      if (request) {
        request.abort();
      }
      clearTimeout($.data(this, 'timer'));
      var wait = setTimeout(search, 200);
      $(this).data('timer', wait);
    });

    ajaxifyLinks(modal.body);
  },
  chosen: function (modal, jsonData) {
    modal.respond('snippetChosen', jsonData.result);
    modal.close();
  },
};<commit_msg>Use SearchController on snippet chooser modal<commit_after>import $ from 'jquery';
import { SearchController } from '../../includes/chooserModal';

window.SNIPPET_CHOOSER_MODAL_ONLOAD_HANDLERS = {
  choose: function (modal) {
    function ajaxifyLinks(context) {
      $('a.snippet-choice', modal.body).on('click', function () {
        modal.loadUrl(this.href);
        return false;
      });

      $('.pagination a', context).on('click', function () {
        searchController.fetchResults(this.href);
        return false;
      });
    }

    const searchController = new SearchController({
      form: $('form.snippet-search', modal.body),
      resultsContainerSelector: '#search-results',
      onLoadResults: (context) => {
        ajaxifyLinks(context);
      },
    });
    searchController.attachSearchInput('#id_q');
    searchController.attachSearchFilter('#snippet-chooser-locale');

    ajaxifyLinks(modal.body);
  },
  chosen: function (modal, jsonData) {
    modal.respond('snippetChosen', jsonData.result);
    modal.close();
  },
};
{'fraction_non_alphanumeric': 0.16250944822373395, 'fraction_numerical': 0.0011337868480725624, 'mean_word_length': 2.717696629213483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1689433', 'n_tokens_mistral': 805, 'n_tokens_neox': 786, 'n_words': 142}