Guest User

Untitled

a guest
Dec 11th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. /* JavaScript responsible for making the autocomplete search box work. */
  2.  
  3. function autocomplete_search(search_box, search_form, search_URL, lookup_URL){
  4. /* Customized autocomplete search box for use with GWM's search system.
  5. *
  6. * This autocomplete search box works with the search.py view to provide
  7. * a list of search suggestions as the user types. It must be provided the
  8. * following parameters:
  9. *
  10. * `search_box`: The search input box as a jQuery object
  11. * e.g. $('#search_box')
  12. * `search_form`: The form that contains the search box as a jQuery
  13. * object, e.g. $('#search_form')
  14. * `search_URL`: The url responsible for returning the search
  15. * suggestions as a JSON object, e.g.
  16. * '/search-suggestions.json'
  17. * `lookup_URL`: The url responsible for returning the absolute URL
  18. * to the details page for a GWM object
  19. *
  20. * Example markup:
  21. * <form id='search_form' action='/search' method='GET'>
  22. * <input id='search_box' type='text' name='q'>
  23. * </form>
  24. *
  25. * Requires:
  26. * jQuery and jquery-ui (with the autocomplete widget)
  27. */
  28.  
  29. // keycode for the enter/return key
  30. var ENTER_KEYCODE = 13;
  31.  
  32. // Submit search on return/enter keypress as long as no suggestion selected
  33. search_box.keydown(function(event){
  34. if(event.keyCode == ENTER_KEYCODE){
  35. if(!$('.ui-autocomplete #ui-active-menuitem').is(":visible")){
  36. search_form.submit();
  37. }
  38. }
  39. })
  40.  
  41. // Initialize the autocomplete widget on the search box. This must go
  42. // *after* the keydown binding (above) in the chain so the autocomplete
  43. // selection event takes precedence, i.e., when something is selected, go
  44. // to its details page over simply searching for the keyword, and if
  45. // nothing's selected, search for the keyword.
  46. .autocomplete({
  47. source: search_URL, // The search results as a JSON file
  48. minLength: 2, // Only AJAX search if there are >= 2 chars entered
  49.  
  50. // Custom focus function to handle our custom results format
  51. focus: function(event, ui) {
  52. search_box.val(ui.item.value);
  53. return false;
  54. },
  55.  
  56. // When an item is selected, bypass the search results and go directly
  57. // to the item's detail page. We do this by looking up the item's URL
  58. // and setting the window location appropriately.
  59. select: function(event, ui){
  60. window.location = lookup_URL + "?type=" + ui.item.type +
  61. "&hostname=" + ui.item.value;
  62. }
  63. })
  64.  
  65. // Create custom result item rendering for out custom format
  66. .data("autocomplete")._renderItem = function(ul, item){
  67. return $("<li></li>")
  68. .data("item.autocomplete", item)
  69. .append("<a><b>" + item.type + ": </b>" + item.value + "</a>")
  70. .appendTo(ul);
  71. };
  72.  
  73. // Remove the widget-content class b/c it is customized for tab use in
  74. // other parts of the site. We'll customize the the autocomplete box
  75. // in the styles above.
  76. $('.ui-autocomplete').removeClass('ui-widget-content');
  77. }
Add Comment
Please, Sign In to add comment