| 1 |
( function ( $ ) { |
| 2 |
$( document ).ready( function () { |
| 3 |
|
| 4 |
/** |
| 5 |
* Initialise one search widget instance. |
| 6 |
* |
| 7 |
* All DOM queries are scoped to the specific form element so that |
| 8 |
* multiple widgets on the same page never interfere with each other. |
| 9 |
* |
| 10 |
* @param {jQuery} $form - The .bbpc_search_form_wrapper element for this instance. |
| 11 |
*/ |
| 12 |
function initSearchWidget( $form ) { |
| 13 |
var inputId = $form.data( 'input-id' ); |
| 14 |
var resultId = $form.data( 'result-id' ); |
| 15 |
|
| 16 |
var $input = $( '#' + inputId ); |
| 17 |
var $result = $( '#' + resultId ); |
| 18 |
var $overlay = $( '.bbpc-search-overlay[data-widget-id="' + $form.data( 'widget-id' ) + '"]' ); |
| 19 |
|
| 20 |
if ( ! $input.length ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Highlight the matching keyword inside result links. |
| 26 |
* |
| 27 |
* @param {string} keyword - Current search term. |
| 28 |
*/ |
| 29 |
function highlightKeyword( keyword ) { |
| 30 |
var isHighlight = $input.data( 'highlight' ); |
| 31 |
if ( ! isHighlight || ! keyword ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
var escaped = keyword.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); |
| 36 |
var regex = new RegExp( '(' + escaped + ')', 'gi' ); |
| 37 |
|
| 38 |
$result.find( '.search-result-item a' ).each( function () { |
| 39 |
var text = $( this ).text(); |
| 40 |
var highlighted = text.replace( regex, '<mark class="frmx-search-highlight">$1</mark>' ); |
| 41 |
$( this ).html( highlighted ); |
| 42 |
} ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Run an AJAX search request and populate the result container. |
| 47 |
* |
| 48 |
* @param {string} keyword - The search term. |
| 49 |
* @param {string} postType - The post type to search. |
| 50 |
*/ |
| 51 |
function runAjaxSearch( keyword, postType ) { |
| 52 |
var ajaxUrl = forumax_localize_script.ajaxurl; /* eslint-disable-line camelcase */ |
| 53 |
var nonce = forumax_localize_script.nonce; /* eslint-disable-line camelcase */ |
| 54 |
|
| 55 |
$.ajax( { |
| 56 |
url : ajaxUrl, |
| 57 |
method : 'POST', |
| 58 |
data : { |
| 59 |
action : 'forumax_search_data_fetch', |
| 60 |
keyword : keyword, |
| 61 |
post_type : postType, |
| 62 |
nonce : nonce |
| 63 |
}, |
| 64 |
beforeSend: function () { |
| 65 |
$form.find( '.spinner' ).css( 'display', 'inline-flex' ); |
| 66 |
}, |
| 67 |
success: function ( data ) { |
| 68 |
$result.html( data ).addClass( 'ajax-search' ); |
| 69 |
$form.find( '.spinner' ).hide(); |
| 70 |
|
| 71 |
// Check for "no results" marker returned by the server. |
| 72 |
var noResult = $result.find( '.tab-item.active.all-active' ).attr( 'data-noresult' ); |
| 73 |
if ( noResult ) { |
| 74 |
var msg = noResult.replace( /-/g, ' ' ); |
| 75 |
$result.html( '<h5 class="bbpc-not-found-text">' + msg + '</h5>' ); |
| 76 |
} else { |
| 77 |
highlightKeyword( keyword ); |
| 78 |
} |
| 79 |
} |
| 80 |
} ); |
| 81 |
} |
| 82 |
|
| 83 |
// --------------- Overlay ------------------ |
| 84 |
|
| 85 |
$input.add( $result ).add( $form.find( '.bbpc-search-keyword ul li a' ) ).on( 'click', function () { |
| 86 |
$overlay.css( 'display', 'block' ).addClass( 'active' ); |
| 87 |
} ); |
| 88 |
|
| 89 |
$overlay.on( 'click', function () { |
| 90 |
$overlay.css( 'display', 'none' ).removeClass( 'active' ); |
| 91 |
} ); |
| 92 |
|
| 93 |
// --------------- Focus states ------------------ |
| 94 |
|
| 95 |
$form.on( 'focusin', function () { |
| 96 |
if ( $( 'body' ).hasClass( 'body_dark' ) ) { |
| 97 |
$input.addClass( 'input_focused' ); |
| 98 |
} |
| 99 |
} ); |
| 100 |
|
| 101 |
$form.on( 'focusout', function () { |
| 102 |
if ( $( 'body' ).hasClass( 'body_dark' ) ) { |
| 103 |
if ( ! $result.hasClass( 'ajax-search' ) ) { |
| 104 |
$input.removeClass( 'input_focused' ); |
| 105 |
} |
| 106 |
} |
| 107 |
} ); |
| 108 |
|
| 109 |
$input.on( 'focus', function () { |
| 110 |
$( 'body' ).addClass( 'bbpc-search-open' ); |
| 111 |
$form.css( { position: 'relative', 'z-index': '999' } ); |
| 112 |
} ); |
| 113 |
|
| 114 |
$overlay.on( 'click', function () { |
| 115 |
$( 'body' ).removeClass( 'bbpc-search-open' ); |
| 116 |
$form.css( { 'z-index': '', position: '' } ); |
| 117 |
} ); |
| 118 |
|
| 119 |
// --------------- Clear input ------------------ |
| 120 |
|
| 121 |
$input.on( 'input', function () { |
| 122 |
if ( this.value === '' ) { |
| 123 |
$result.removeClass( 'ajax-search' ).empty(); |
| 124 |
} |
| 125 |
} ); |
| 126 |
|
| 127 |
// --------------- AJAX live search ------------------ |
| 128 |
|
| 129 |
var isAjaxSearch = $input.data( 'ajax-search' ); |
| 130 |
|
| 131 |
if ( isAjaxSearch ) { |
| 132 |
var debounceTimer; |
| 133 |
|
| 134 |
$input.on( 'keyup', function () { |
| 135 |
$form.find( '.not-found-text' ).hide(); |
| 136 |
clearTimeout( debounceTimer ); |
| 137 |
|
| 138 |
var keyword = $( this ).val(); |
| 139 |
var postType = $( this ).data( 'post-type' ) || ''; |
| 140 |
|
| 141 |
debounceTimer = setTimeout( function () { |
| 142 |
if ( keyword !== '' ) { |
| 143 |
runAjaxSearch( keyword, postType ); |
| 144 |
} |
| 145 |
}, 1000 ); |
| 146 |
} ); |
| 147 |
} |
| 148 |
|
| 149 |
// --------------- Keyword pill click ------------------ |
| 150 |
|
| 151 |
$form.find( '.bbpc-search-keyword ul li a' ).on( 'click', function ( e ) { |
| 152 |
e.preventDefault(); |
| 153 |
|
| 154 |
var keyword = $( this ).text().trim(); |
| 155 |
var postType = $input.data( 'post-type' ) || ''; |
| 156 |
|
| 157 |
$input.val( keyword ).focus(); |
| 158 |
|
| 159 |
if ( ! isAjaxSearch || keyword === '' ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
runAjaxSearch( keyword, postType ); |
| 164 |
} ); |
| 165 |
} |
| 166 |
|
| 167 |
// --------------- Bootstrap: find every widget instance ------------------ |
| 168 |
|
| 169 |
$( '.bbpc_search_form_wrapper[data-widget-id]' ).each( function () { |
| 170 |
initSearchWidget( $( this ) ); |
| 171 |
} ); |
| 172 |
} ); |
| 173 |
} )( jQuery ); /* eslint-disable-line no-undef */ |
| 174 |
|