From 86bef4df2daefc2fcb30cbfbb51509aade5d24d1 Mon Sep 17 00:00:00 2001 From: ArtemB Date: Fri, 21 Jul 2023 18:33:34 +0300 Subject: [PATCH] 0.0.36 upd autocomlitev2.js --- static/js/autocomlitev2.js | 116 +++++++++------------------- static/js/autocomplete.js | 151 ------------------------------------- 2 files changed, 35 insertions(+), 232 deletions(-) delete mode 100644 static/js/autocomplete.js diff --git a/static/js/autocomlitev2.js b/static/js/autocomlitev2.js index 28fd9de..19d5e9d 100644 --- a/static/js/autocomlitev2.js +++ b/static/js/autocomlitev2.js @@ -1,68 +1,48 @@ function searchTown(el){ - let form = el.form; - let type_transport = form['type_transport'].value; - let search_str = el.value; - let get_address_point = new Object({type_transport, search_str}); - get_address_point['ctrl_name'] = el.name; + let timer = null + if (timer) { + clearTimeout(timer); + } + timer = setTimeout(function(){ + timer = null; + let form = el.form; + let type_transport = form['type_transport'].value; + let search_str = el.value; + let get_address_point = new Object({type_transport, search_str}); + get_address_point['ctrl_name'] = el.name; + $.ajax({ + headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() }, + url: '/ru/reference_data/get_address_point/', + type: "POST", + // async: true, + cache: false, + processData: false, + contentType: false, + // enctype: 'json', + data: JSON.stringify(get_address_point), + success: function(data){ + return insertSearchList(data, el.name + '_list') + }, + error: function (data){ + console.log('Error') - - - - $.ajax({ - headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() }, - url: '/ru/reference_data/get_address_point/', - type: "POST", - // async: true, - cache: false, - processData: false, - contentType: false, - // enctype: 'json', - data: JSON.stringify(get_address_point), - success: function(data){ - return insertSearchList(data, el.name + '_list') - }, - error: function (data){ - console.log('Error') - - } - }); + } + }); + }, 1000); } -function selectItemAddrPoint(id, name, ctrl_name){ - //reset value - // if (ctrl_name.startsWith("from")){ - // let fap_txt_cont = document.querySelector("#id_" + ctrl_name); - // let fap_cont = document.querySelector("#id_" + ctrl_name.slice(0, -4)); - // fap_txt_cont.value = name; - // fap_cont.value = id; - // } - // else if (ctrl_name.startsWith("to")) { + + +function selectItemAddrPoint(id, name, ctrl_name){ + let tap_txt_cont = document.querySelector("#id_" + ctrl_name); tap_txt_cont.value = name; let tap_cont = document.querySelector("#id_" + ctrl_name.slice(0, -4)); tap_cont.value = id; - // } - - - - - - - // // from_address_point - // let fap_txt_cont = document.querySelector("#id_from_address_point_txt"); - // let fap_cont = document.querySelector("#id_from_address_point"); - // fap_txt_cont.value = name; - // fap_cont.value = id; - - //to_address_point - - - - -} + } function insertSearchList(data, ctrl_name) { @@ -71,36 +51,10 @@ function insertSearchList(data, ctrl_name) { let divs = data.res_search_list; if (divs.length > 0) { - // let ac = document.querySelector("#id_from_address_point_txt"); input_list.classList.add("show"); input_list.innerHTML = divs; } else { input_list.classList.remove("show"); } -// -// if (ac.value.length > 2) { -// // -// // let res_data = data.data -// // -// // var divs = res_data.map(function(r, i) { -// // airport_name = "'"+r.iata_code + " - " + r.name+"'"; -// // -// // return '
' -// // + '
'+ r.iata_code +' - '+ r.name +'
' -// // + '
'+ r.city__name +', '+ r.city__country__name +'
' -// // + '
'; -// // }); -// // -// // -// // -// // -// // -// // // .html(divs.join('')) -// // // .attr('data-highlight', selectedIndex); -// // -// } else { -// numResults = 0; -// let divs = []; -// -// } + } \ No newline at end of file diff --git a/static/js/autocomplete.js b/static/js/autocomplete.js deleted file mode 100644 index 027b924..0000000 --- a/static/js/autocomplete.js +++ /dev/null @@ -1,151 +0,0 @@ -// var options = { -// shouldSort: true, -// threshold: 0.4, -// maxPatternLength: 32, -// keys: [{ -// name: 'iata', -// weight: 0.5 -// }, { -// name: 'name', -// weight: 0.3 -// }, { -// name: 'city', -// weight: 0.2 -// }] -// }; -// -// var fuse = new Fuse(airports, options) - -function searchTown(el){ - let form = el.form; - let type_transport = form['type_transport'].value; - let search_str = el.value; - let get_address_point = new Object({type_transport, search_str}) - - - $.ajax({ - headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() }, - url: '/reference_data/get_address_point/', - type: "POST", - // async: true, - cache: false, - processData: false, - contentType: false, - // enctype: 'json', - data: get_address_point, - success: function(data){ - return data; - }, - error: function (data){ - console.log('++') - // document.querySelector(".button_register").innerHTML = data.responseJSON.html - } - }); -} - -var ac = $('#id_from_country') - .on('click', function(e) { - e.stopPropagation(); - }) - .on('keyup', search) - .on('keydown', onKeyDown); - -var wrap = $('
') - .addClass('autocomplete-wrapper') - .insertBefore(ac) - .append(ac); - -var list = $('
') - .addClass('autocomplete-results') - .on('click', '.autocomplete-result', function(e) { - e.preventDefault(); - e.stopPropagation(); - selectIndex($(this).data('index')); - }) - .appendTo(wrap); - -$(document) - .on('mouseover', '.autocomplete-result', function(e) { - var index = parseInt($(this).data('index'), 10); - if (!isNaN(index)) { - list.attr('data-highlight', index); - } - }) - .on('click', clearResults); - -function clearResults() { - results = []; - numResults = 0; - list.empty(); -} - -function selectIndex(index) { - if (results.length >= index + 1) { - ac.val(results[index].iata); - clearResults(); - } -} - -var results = []; -var numResults = 0; -var selectedIndex = -1; - -function search(e) { - if (e.which === 38 || e.which === 13 || e.which === 40) { - return; - } - - let ac = document.querySelector("#id_from_country") - - if (ac.value.length > 0) { - // results = _.take(fuse.search(ac.val()), 7); - // numResults = results.length; - - let results = searchTown(ac); - - var divs = results.map()(function(r, i) { - return '
' - + '
'+ r.iata +' - '+ r.name +'
' - + '
'+ r.city +', '+ r.country +'
' - + '
'; - }); - - selectedIndex = -1; - list.html(divs.join('')) - .attr('data-highlight', selectedIndex); - - } else { - numResults = 0; - list.empty(); - } -} - -function onKeyDown(e) { - switch(e.which) { - case 38: // up - selectedIndex--; - if (selectedIndex <= -1) { - selectedIndex = -1; - } - list.attr('data-highlight', selectedIndex); - break; - case 13: // enter - selectIndex(selectedIndex); - break; - case 9: // enter - selectIndex(selectedIndex); - e.stopPropagation(); - return; - case 40: // down - selectedIndex++; - if (selectedIndex >= numResults) { - selectedIndex = numResults-1; - } - list.attr('data-highlight', selectedIndex); - break; - - default: return; // exit this handler for other keys - } - e.stopPropagation(); - e.preventDefault(); // prevent the default action (scroll / move caret) -} \ No newline at end of file