Guest User

Untitled

a guest
Oct 8th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   jQuery(function() {
  2.     var hacked_do_ajax_call = function(that) {
  3.       //var a = jQuery('#td-header-search').val()
  4.       var a = jQuery(that).val()
  5.       '' === a || ( a.length < 3 )
  6.         ? tdAjaxSearch.td_aj_search_input_focus()
  7.         : tdLocalCache.exist(a)
  8.           ? tdAjaxSearch.process_ajax_response(tdLocalCache.get(a))
  9.           : jQuery.ajax({
  10.               type: 'GET',
  11.               cache: false,
  12.               url: td_ajax_url,
  13.               data: {
  14.                 action: 'td_ajax_search',
  15.                 td_string: a,
  16.               },
  17.               success: function(b, c, d) {
  18.                 tdLocalCache.set(a, b)
  19.                 tdAjaxSearch.process_ajax_response(b)
  20.               },
  21.               error: function(a, c, d) {},
  22.             })
  23.     }
  24.     debounce_ajax_call = debounce(hacked_do_ajax_call, 600)
  25.  
  26.     var hacked_do_ajax_call_mob = function(that) {
  27.       //var a = jQuery('#td-header-search-mob').val()
  28.       var a = jQuery(that).val();
  29.       "" !== a && (tdLocalCache.exist(a) ? tdAjaxSearch.process_ajax_response_mob(tdLocalCache.get(a)) : jQuery.ajax({
  30.         type: "GET",
  31.         cache: false,
  32.         url: td_ajax_url,
  33.         data: {
  34.           action: "td_ajax_search",
  35.           td_string: a
  36.         },
  37.         success: function(b, c, d) {
  38.           tdLocalCache.set(a, b);
  39.           tdAjaxSearch.process_ajax_response_mob(b)
  40.         },
  41.         error: function(a, c, d) {}
  42.       }))
  43.     }
  44.     debounce_ajax_call_mob = debounce(hacked_do_ajax_call_mob, 600)
  45.  
  46.    
  47.     jQuery('#td-header-search-mob').on('focusin', function(e) {
  48.  
  49.       var $this = jQuery(this);
  50.      
  51.       if (!this.isHacked) {
  52.         this.isHacked = true
  53.         /*******************************************************************
  54.          * here comes the hack
  55.          */
  56.        
  57.         console.log('taking over keydown', this)
  58.         $this.off('keydown')
  59.         $this.keydown(function(a) {
  60.           if (a.which && 13 === a.which || a.keyCode && 13 === a.keyCode)
  61.             return a = jQuery(".td-aj-cur-element"),
  62.               0 < a.length ? (a = a.find(".entry-title a").attr("href"),
  63.                               window.location = a) : jQuery(this).parent().parent().submit(),
  64.               !1;
  65.           (a.which && 8 === a.which || a.keyCode && 8 === a.keyCode) && 1 === jQuery(this).val().length && jQuery("#td-aj-search-mob").empty();
  66.          
  67.           debounce_ajax_call_mob(this)
  68.          
  69.           return !0
  70.         })
  71.       } // this.isHacked
  72.     })
  73.    
  74.     jQuery('#td-header-search').on('focusin', function(e) {
  75.       //jQuery(this).select();
  76.      
  77.       // put cursor at end
  78.       var $this = jQuery(this);
  79.       $this.focus();
  80.       var fldLength = $this.val().length;
  81.       // fieldInput.focus();
  82.       $this[0].setSelectionRange(fldLength, fldLength);
  83.      
  84.       if (!this.isHacked) {
  85.         this.isHacked = true
  86.         /*******************************************************************
  87.          * here comes the hack
  88.          */
  89.        
  90.         tdAjaxSearch.td_aj_search_input_focus()
  91.  
  92.         console.log('taking over keydown', this)
  93.         $this.off('keydown')
  94.         $this.keydown(function(a) {
  95.           console.log('keydown')
  96.           if (
  97.             (a.which && 39 === a.which) ||
  98.             (a.keyCode && 39 === a.keyCode) ||
  99.             (a.which && 37 === a.which) ||
  100.             (a.keyCode && 37 === a.keyCode)
  101.           )
  102.             tdAjaxSearch.td_aj_search_input_focus()
  103.           else {
  104.             if ((a.which && 13 === a.which) || (a.keyCode && 13 === a.keyCode))
  105.               return (
  106.                 (a = jQuery('.td-aj-cur-element')),
  107.                 0 < a.length
  108.                   ? ((a = a.find('.entry-title a').attr('href')),
  109.                     (window.location = a))
  110.                   : jQuery(this).parent().parent().submit(),
  111.                 !1
  112.               )
  113.             if ((a.which && 40 === a.which) || (a.keyCode && 40 === a.keyCode))
  114.               return tdAjaxSearch.move_prompt_down(), !1
  115.             if ((a.which && 38 === a.which) || (a.keyCode && 38 === a.keyCode))
  116.               return tdAjaxSearch.move_prompt_up(), !1
  117.             ;((a.which && 8 === a.which) || (a.keyCode && 8 === a.keyCode)) &&
  118.               1 === jQuery(this).val().length &&
  119.               jQuery('#td-aj-search').empty()
  120.             tdAjaxSearch.td_aj_search_input_focus()
  121.  
  122.             debounce_ajax_call(this)
  123.             return !0
  124.           }
  125.         })
  126.         /*******************
  127.          * end
  128.          */
  129.       } // /isHacked
  130.     })
  131.   })
  132.  
  133.  
  134.   // https://davidwalsh.name/javascript-debounce-function
  135.   function debounce(func, wait, immediate) {
  136.     var timeout;
  137.     return function() {
  138.       var context = this, args = arguments;
  139.       var later = function() {
  140.         timeout = null;
  141.         if (!immediate) func.apply(context, args);
  142.       };
  143.       var callNow = immediate && !timeout;
  144.       clearTimeout(timeout);
  145.       timeout = setTimeout(later, wait);
  146.       if (callNow) func.apply(context, args);
  147.     };
  148.   };
Advertisement
Add Comment
Please, Sign In to add comment