search: simplify generated javascript (#18921)

This commit is contained in:
Frédéric Péters 2019-04-06 22:19:03 +02:00
parent ae6f8d0162
commit aea9d7db5d
1 changed files with 18 additions and 18 deletions

View File

@ -12,45 +12,45 @@
<script>
$(function() {
var combo_search_timeout_{{ cell.pk }};
var combo_search_form_{{ cell.pk }} = $('#combo-search-form-{{ cell.pk }}');
var combo_search_input_{{ cell.pk }} = $('#combo-search-input-{{ cell.pk }}');
var timeout;
var $form = $('#combo-search-form-{{ cell.pk }}');
var $input = $('#combo-search-input-{{ cell.pk }}');
{% for search_service in cell.search_services %}
var combo_search_results_{{ cell.pk }}_{{ forloop.counter }} = $('#combo-search-results-{{ cell.pk }}-{{ forloop.counter }}');
var $results_{{ forloop.counter }} = $('#combo-search-results-{{ cell.pk }}-{{ forloop.counter }}');
var xhr_{{ forloop.counter }} = null;
var url_{{ forloop.counter }} = '{% url 'combo-search-ajax-results' cell_pk=cell.pk service_slug=search_service.slug %}{% if initial_query_string %}?{{ initial_query_string }}{% endif %}';
{% endfor %}
function combo_search_update_{{ cell.pk }}() {
function update() {
{% for search_service in cell.search_services %}
if (xhr_{{ forloop.counter }}) xhr_{{ forloop.counter }}.abort();
xhr_{{ forloop.counter }} = $.get(url_{{ forloop.counter }},
{'q': combo_search_input_{{ cell.pk }}.val()},
{'q': $input.val()},
function (response) {
xhr_{{ forloop.counter }} = null;
combo_search_results_{{ cell.pk }}_{{ forloop.counter }}.html(response);
$results_{{ forloop.counter }}.html(response);
}
);
{% endfor %}
};
combo_search_input_{{ cell.pk }}.on('paste keyup', function() {
clearTimeout(combo_search_timeout_{{ cell.pk }});
combo_search_timeout_{{ cell.pk }} = setTimeout(combo_search_update_{{ cell.pk }}, 300);
$input.on('paste keyup', function() {
clearTimeout(timeout);
timeout = setTimeout(update, 300);
});
combo_search_input_{{ cell.pk }}.on('change', function() {
clearTimeout(combo_search_timeout_{{ cell.pk }});
combo_search_update_{{ cell.pk }}(this);
$input.on('change', function() {
clearTimeout(timeout);
update(this);
});
combo_search_form_{{ cell.pk }}.on('submit', function() {
clearTimeout(combo_search_timeout_{{ cell.pk }});
combo_search_update_{{ cell.pk }}();
$form.on('submit', function() {
clearTimeout(timeout);
update();
return false;
});
{% if initial_q %}
combo_search_input_{{ cell.pk }}.val('{{ initial_q }}');
combo_search_update_{{ cell.pk }}();
$input.val('{{ initial_q }}');
update();
{% endif %}
});
</script>