Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //*****************************************************************************************************
  2. // app.searchsuggestbeta
  3. // Function that is called with SAYT
  4. //*****************************************************************************************************
  5. (function (app, $)
  6. {
  7.     var currentQuery = null, lastQuery = null, runningQuery = null, listTotal = -1, listCurrent = -1, delay = 30, $searchField, $resultsContainer;
  8.  
  9.     function handleArrowKeys(keyCode)
  10.     {
  11.         switch (keyCode)
  12.         {
  13.             case 38:
  14.                 // keyUp
  15.                 listCurrent = (listCurrent <= 0) ? (listTotal - 1) : (listCurrent - 1);
  16.                 break;
  17.             case 40:
  18.                 // keyDown
  19.                 listCurrent = (listCurrent >= listTotal - 1) ? 0 : listCurrent + 1;
  20.                 break;
  21.             default:
  22.                 // reset
  23.                 listCurrent = -1;
  24.                 return false;
  25.         }
  26.         $resultsContainer.children().removeClass('selected').eq(listCurrent).addClass('selected');
  27.         $searchField.val($resultsContainer.find('.selected .suggestionterm').first().text());
  28.         return true;
  29.     }
  30.  
  31.     app.searchsuggestbeta = {
  32.         // Configures parameters and required object instances
  33.         init: function (container, defaultValue)
  34.         {
  35.             var $searchContainer = $(container);
  36.             var $searchForm = $searchContainer.find('form[name="simpleSearch"]');
  37.             $searchField = $searchForm.find('input[name="q"]');
  38.        
  39.             // disable browser auto complete
  40.             $searchField.attr('autocomplete', 'off');
  41.        
  42.             // on focus listener (clear default value)
  43.             $searchField.focus(function ()
  44.             {
  45.                 if (!$resultsContainer)
  46.                 {
  47.                     // create results container if needed
  48.                     $resultsContainer = $('<div/>').attr('id', 'search-suggestions').appendTo($searchContainer);
  49.                 }
  50.                 if ($searchField.val() === defaultValue)
  51.                 {
  52.                     $searchField.val('');
  53.                 }
  54.             });
  55.            
  56.             // on blur listener
  57.             $(document).on('click', function (e) { if (!$searchContainer.is(e.target)) { setTimeout(this.clearResults, 200); }}.bind(this));
  58.            
  59.             // on key up listener
  60.             $searchField.keyup(function (e)
  61.             {
  62.                 // get keyCode (window.event is for IE)
  63.                 var keyCode = e.keyCode || window.event.keyCode;
  64.        
  65.                 // check and treat up and down arrows
  66.                 if (handleArrowKeys(keyCode)) { return;}
  67.                
  68.                 // check for an ENTER or ESC
  69.                 if (keyCode === 13 || keyCode === 27)
  70.                 {
  71.                     this.clearResults();
  72.                     return;
  73.                 }
  74.        
  75.                 currentQuery = $searchField.val().trim();
  76.        
  77.                 // no query currently running, init a update
  78.                 if (runningQuery === null)
  79.                 {
  80.                     runningQuery = currentQuery;
  81.                     setTimeout(this.suggest.bind(this), delay);
  82.                 }
  83.             }.bind(this));
  84.         },
  85.  
  86.         // trigger suggest action
  87.         suggest: function ()
  88.         {
  89.             // check whether query to execute (runningQuery) is still up to date and had not changed in the meanwhile
  90.             if (runningQuery !== currentQuery)
  91.             {
  92.                 // update running query to the most recent search phrase
  93.                 runningQuery = currentQuery;
  94.             }
  95.  
  96.             // if it's empty clear the results box and return
  97.             if (runningQuery.length === 0)
  98.             {
  99.                 this.clearResults();
  100.                 runningQuery = null;
  101.                 return;
  102.             }
  103.  
  104.             // if the current search phrase is the same as for the last suggestion call, just return
  105.             if (lastQuery === runningQuery)
  106.             {
  107.                 runningQuery = null;
  108.                 return;
  109.             }
  110.  
  111.             // build the request url
  112.             var reqUrl = app.util.appendParamToURL(app.urls.searchsuggest, "q", runningQuery);
  113.             reqUrl = app.util.appendParamToURL(reqUrl, 'legacy', 'false');
  114.            
  115.             // execute server call
  116.             $.get(reqUrl,
  117.                     function (data)
  118.                     {
  119.                         var suggestionHTML = data, ansLength = suggestionHTML.trim().length;
  120.                        
  121.                         // if there are results populate the results div
  122.                         if (ansLength === 0)
  123.                         {
  124.                             this.clearResults();
  125.                         }
  126.                         else
  127.                         {
  128.                             // update the results div
  129.                             $resultsContainer.html(suggestionHTML).fadeIn(200);
  130.                         }
  131.  
  132.                         // record the query that has been executed
  133.                         lastQuery = runningQuery;
  134.                
  135.                         // reset currently running query
  136.                         runningQuery = null;
  137.  
  138.                         // check for another required update (if current search phrase is different from just executed call)
  139.                         if (currentQuery !== lastQuery)
  140.                         {
  141.                             // ... and execute immediately if search has changed while this server call was in transit
  142.                             runningQuery = currentQuery;
  143.                             setTimeout(this.suggest.bind(this), delay);
  144.                         }
  145.                         this.hideLeftPanel();
  146.                     }.bind(this));
  147.         },
  148.        
  149.         clearResults: function ()
  150.         {
  151.                 if (!$resultsContainer) { return; }
  152.                 $resultsContainer.fadeOut(200, function () {$resultsContainer.empty();});
  153.         },
  154.        
  155.         hideLeftPanel: function ()
  156.         {
  157.             //hide left panel if there is only a matching suggested custom phrase
  158.             if ($('.search-suggestion-left-panel-hit').length === 1 && $('.search-phrase-suggestion a').text().replace(/(^[\s]+|[\s]+$)/g, '').toUpperCase() === $('.search-suggestion-left-panel-hit a').text().toUpperCase())
  159.             {
  160.                 $('.search-suggestion-left-panel').css('display', 'none');
  161.                 $('.search-suggestion-wrapper-full').addClass('search-suggestion-wrapper');
  162.                 $('.search-suggestion-wrapper').removeClass('search-suggestion-wrapper-full');
  163.             }
  164.         }
  165.     };
  166. }(window.app = window.app || {}, jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement