Guest User

Untitled

a guest
Jan 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. (function( $ ) {
  2. $(function() {
  3. // Custom autocomplete instance.
  4. $.widget( "app.autocomplete", $.ui.autocomplete, {
  5.  
  6. // Which class get's applied to matched text in the menu items.
  7. options: {
  8. highlightClass: "ui-state-highlight"
  9. },
  10.  
  11. _renderItem: function( ul, item ) {
  12.  
  13. // Replace the matched text with a custom span. This
  14. // span uses the class found in the "highlightClass" option.
  15. var re = new RegExp( "(" + this.term + ")", "gi" ),
  16. cls = this.options.highlightClass,
  17. template = "<span class='" + cls + "'>$1</span>",
  18. label = item.label.replace( re, template ),
  19. $li = $( "<li/>" ).appendTo( ul );
  20.  
  21. // Create and return the custom menu item content.
  22. $( "<a/>" ).attr( "href", "#" )
  23. .html( label )
  24. .appendTo( $li );
  25.  
  26. return $li;
  27.  
  28. }
  29.  
  30. });
  31. var autocompleteTextbox = '<div class="autocomplete-search autocomplete-search-open">'
  32. +'<a href="#" class="st-btn01 autocomplete-search-icon"><i class="search-icon" aria-hidden="true"></i></a>'
  33. +'<div class="autocomplete-search-sub1">'
  34. +'<form role="search" method="get" action="<?php echo esc_url( home_url( '+"/"+' ) ); ?>">'
  35. +'<input type="text" name="s" class="form01 autocomplete-search-input" id="autocomplete-search-input" placeholder="Type something" autocomplete="off">'
  36. +'<a href="#" class="st-btn02 autocomplete-search-icon"><i class="searchx-icon" aria-hidden="true"></i></a>'
  37. +'</form>'
  38. +'</div>'
  39. +'</div>';
  40.  
  41. var appendElement = function(){
  42. $('.st-navbar-collapse').append(autocompleteTextbox);
  43. $(this).unbind('click');
  44. $(".st-search .st-btn01 .search-icon").addClass('text-visibility');
  45. };
  46. $(".two-brokers").parent('a').on('click',appendElement);
  47.  
  48. var removeElement = function(){
  49. $(this).parents('.autocomplete-search').remove();
  50. $(".two-brokers").parent('a').bind('click',appendElement);
  51. $(".st-search .st-btn01 .search-icon").removeClass('text-visibility');
  52. };
  53. $("body").on("click",".st-btn02.autocomplete-search-icon",removeElement);
  54.  
  55. var searchRequest,timeoutRequest;
  56. $("body").on("keyup",".autocomplete-search-input",function(){
  57. clearTimeout(timeoutRequest);
  58. var _this = $(this);
  59. timeoutRequest = setTimeout(function(){
  60. // create an jq-ui autocomplete on your selected element
  61. _this.autocomplete({
  62. minChars: 2,
  63. highlightClass: "bold-text",
  64. // use a function for its source, which will return ajax response
  65. source: function(request, response){
  66. try { searchRequest.abort(); } catch(e){}
  67. // well use postautocomplete_search.ajax_url which we enqueued with WP
  68. $.post(postautocomplete_search, {
  69. action: 'get_test_pages', // our action is called search
  70. term: request.term // and we get the term form jq-ui
  71. }, function(data) {
  72. if(!data.length){
  73. var result = [{
  74. label: 'No matches found',
  75. value: response.term
  76. }];
  77. response(result);
  78. }else{
  79. response(data);
  80. }
  81. }, 'json');
  82. },
  83. select: function( evt, ui ) {
  84. evt.preventDefault();
  85. window.location = ui.item.link;
  86. }
  87. });
  88. },100);
  89. });
  90. });
  91. })( jQuery );
Add Comment
Please, Sign In to add comment