Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery(function() {
- var hacked_do_ajax_call = function(that) {
- //var a = jQuery('#td-header-search').val()
- var a = jQuery(that).val()
- '' === a || ( a.length < 3 )
- ? tdAjaxSearch.td_aj_search_input_focus()
- : tdLocalCache.exist(a)
- ? tdAjaxSearch.process_ajax_response(tdLocalCache.get(a))
- : jQuery.ajax({
- type: 'GET',
- cache: false,
- url: td_ajax_url,
- data: {
- action: 'td_ajax_search',
- td_string: a,
- },
- success: function(b, c, d) {
- tdLocalCache.set(a, b)
- tdAjaxSearch.process_ajax_response(b)
- },
- error: function(a, c, d) {},
- })
- }
- debounce_ajax_call = debounce(hacked_do_ajax_call, 600)
- var hacked_do_ajax_call_mob = function(that) {
- //var a = jQuery('#td-header-search-mob').val()
- var a = jQuery(that).val();
- "" !== a && (tdLocalCache.exist(a) ? tdAjaxSearch.process_ajax_response_mob(tdLocalCache.get(a)) : jQuery.ajax({
- type: "GET",
- cache: false,
- url: td_ajax_url,
- data: {
- action: "td_ajax_search",
- td_string: a
- },
- success: function(b, c, d) {
- tdLocalCache.set(a, b);
- tdAjaxSearch.process_ajax_response_mob(b)
- },
- error: function(a, c, d) {}
- }))
- }
- debounce_ajax_call_mob = debounce(hacked_do_ajax_call_mob, 600)
- jQuery('#td-header-search-mob').on('focusin', function(e) {
- var $this = jQuery(this);
- if (!this.isHacked) {
- this.isHacked = true
- /*******************************************************************
- * here comes the hack
- */
- console.log('taking over keydown', this)
- $this.off('keydown')
- $this.keydown(function(a) {
- if (a.which && 13 === a.which || a.keyCode && 13 === a.keyCode)
- return a = jQuery(".td-aj-cur-element"),
- 0 < a.length ? (a = a.find(".entry-title a").attr("href"),
- window.location = a) : jQuery(this).parent().parent().submit(),
- !1;
- (a.which && 8 === a.which || a.keyCode && 8 === a.keyCode) && 1 === jQuery(this).val().length && jQuery("#td-aj-search-mob").empty();
- debounce_ajax_call_mob(this)
- return !0
- })
- } // this.isHacked
- })
- jQuery('#td-header-search').on('focusin', function(e) {
- //jQuery(this).select();
- // put cursor at end
- var $this = jQuery(this);
- $this.focus();
- var fldLength = $this.val().length;
- // fieldInput.focus();
- $this[0].setSelectionRange(fldLength, fldLength);
- if (!this.isHacked) {
- this.isHacked = true
- /*******************************************************************
- * here comes the hack
- */
- tdAjaxSearch.td_aj_search_input_focus()
- console.log('taking over keydown', this)
- $this.off('keydown')
- $this.keydown(function(a) {
- console.log('keydown')
- if (
- (a.which && 39 === a.which) ||
- (a.keyCode && 39 === a.keyCode) ||
- (a.which && 37 === a.which) ||
- (a.keyCode && 37 === a.keyCode)
- )
- tdAjaxSearch.td_aj_search_input_focus()
- else {
- if ((a.which && 13 === a.which) || (a.keyCode && 13 === a.keyCode))
- return (
- (a = jQuery('.td-aj-cur-element')),
- 0 < a.length
- ? ((a = a.find('.entry-title a').attr('href')),
- (window.location = a))
- : jQuery(this).parent().parent().submit(),
- !1
- )
- if ((a.which && 40 === a.which) || (a.keyCode && 40 === a.keyCode))
- return tdAjaxSearch.move_prompt_down(), !1
- if ((a.which && 38 === a.which) || (a.keyCode && 38 === a.keyCode))
- return tdAjaxSearch.move_prompt_up(), !1
- ;((a.which && 8 === a.which) || (a.keyCode && 8 === a.keyCode)) &&
- 1 === jQuery(this).val().length &&
- jQuery('#td-aj-search').empty()
- tdAjaxSearch.td_aj_search_input_focus()
- debounce_ajax_call(this)
- return !0
- }
- })
- /*******************
- * end
- */
- } // /isHacked
- })
- })
- // https://davidwalsh.name/javascript-debounce-function
- function debounce(func, wait, immediate) {
- var timeout;
- return function() {
- var context = this, args = arguments;
- var later = function() {
- timeout = null;
- if (!immediate) func.apply(context, args);
- };
- var callNow = immediate && !timeout;
- clearTimeout(timeout);
- timeout = setTimeout(later, wait);
- if (callNow) func.apply(context, args);
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment