| 1 |
( function ( $ ) { |
| 2 |
$( document ).ready( function () { |
| 3 |
const overlay = $( '.bbpc-search-overlay' ); |
| 4 |
|
| 5 |
/** |
| 6 |
* Highlight matching keyword text in search results. |
| 7 |
* |
| 8 |
* @param {string} keyword - The search keyword to highlight. |
| 9 |
*/ |
| 10 |
const highlightKeyword = function ( keyword ) { |
| 11 |
const isHighlight = $( '#searchInput' ).data( 'highlight' ); |
| 12 |
if ( ! isHighlight || ! keyword ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
// Escape special regex characters. |
| 17 |
const escaped = keyword.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); |
| 18 |
const regex = new RegExp( '(' + escaped + ')', 'gi' ); |
| 19 |
|
| 20 |
$( '#bbpc-search-result .search-result-item a' ).each( function () { |
| 21 |
const text = $( this ).text(); |
| 22 |
const highlighted = text.replace( regex, '<mark class="frmx-search-highlight">$1</mark>' ); |
| 23 |
$( this ).html( highlighted ); |
| 24 |
} ); |
| 25 |
}; |
| 26 |
|
| 27 |
// Show overlay on input or result click |
| 28 |
$( |
| 29 |
'#searchInput, #bbpc-search-result, .bbpc-search-keyword ul li a' |
| 30 |
).on( 'click', function () { |
| 31 |
overlay.css( 'display', 'block' ).addClass( 'active' ); |
| 32 |
} ); |
| 33 |
|
| 34 |
// Hide overlay |
| 35 |
overlay.on( 'click', function () { |
| 36 |
overlay.css( 'display', 'none' ).removeClass( 'active' ); |
| 37 |
} ); |
| 38 |
|
| 39 |
// Focus in search input |
| 40 |
$( '.bbpc_search_form_wrapper' ).on( 'focusin', function () { |
| 41 |
$( '.body_dark #searchInput' ).addClass( 'input_focused' ); |
| 42 |
if ( $( '#bbpc-search-result.ajax-search' ).length > 0 ) { |
| 43 |
$( '.body_dark #searchInput' ).addClass( 'input_focused' ); |
| 44 |
} |
| 45 |
} ); |
| 46 |
|
| 47 |
// Focus out search input |
| 48 |
$( '.bbpc_search_form_wrapper' ).on( 'focusout', function () { |
| 49 |
if ( $( '#bbpc-search-result.ajax-search' ).length > 0 ) { |
| 50 |
$( '.body_dark #searchInput' ).addClass( 'input_focused' ); |
| 51 |
} else { |
| 52 |
$( '.body_dark #searchInput' ).removeClass( 'input_focused' ); |
| 53 |
} |
| 54 |
} ); |
| 55 |
|
| 56 |
$( '#searchInput' ).on( 'keyup', function () { |
| 57 |
$( '.click_capture' ).css( { opacity: '0', visibility: 'hidden' } ); |
| 58 |
} ); |
| 59 |
|
| 60 |
// Debounced AJAX search — only when AJAX search is enabled. |
| 61 |
const isAjaxSearch = $( '#searchInput' ).data( 'ajax-search' ); |
| 62 |
if ( isAjaxSearch ) { |
| 63 |
let debounceTimer; |
| 64 |
$('#searchInput').on('keyup', function () { |
| 65 |
$('.not-found-text').hide(); |
| 66 |
clearTimeout(debounceTimer); |
| 67 |
const searchInput = $(this).val(); |
| 68 |
const postType = $(this).data('post-type') || ''; |
| 69 |
const ajax_url = forumax_localize_script.ajaxurl; |
| 70 |
const nonce = forumax_localize_script.nonce; |
| 71 |
|
| 72 |
debounceTimer = setTimeout(function () { |
| 73 |
if (searchInput !== '') { |
| 74 |
$.ajax({ |
| 75 |
url: ajax_url, |
| 76 |
method: 'POST', |
| 77 |
data: { |
| 78 |
action: 'forumax_search_data_fetch', |
| 79 |
keyword: searchInput, |
| 80 |
post_type: postType, |
| 81 |
nonce: nonce |
| 82 |
}, |
| 83 |
beforeSend: function () { |
| 84 |
$('.spinner').css('display', 'flex'); |
| 85 |
}, |
| 86 |
success: function (data) { |
| 87 |
$('#bbpc-search-result').html(data).addClass('ajax-search'); |
| 88 |
$('.spinner').hide(); |
| 89 |
|
| 90 |
const no_result = $( |
| 91 |
'.tab-item.active.all-active' |
| 92 |
).attr( 'data-noresult' ); |
| 93 |
if ( no_result ) { |
| 94 |
const msg = no_result.replace( /-/g, ' ' ); |
| 95 |
$( '#bbpc-search-result' ).html( |
| 96 |
'<h5 class="bbpc-not-found-text">' + |
| 97 |
msg + |
| 98 |
'</h5>' |
| 99 |
); |
| 100 |
$( '.bbpc-not-found-text' ).show(); |
| 101 |
} else { |
| 102 |
highlightKeyword( searchInput ); |
| 103 |
} |
| 104 |
}, |
| 105 |
} ); |
| 106 |
/* eslint-enable camelCase */ |
| 107 |
} |
| 108 |
}, 1000 ); |
| 109 |
} ); |
| 110 |
} |
| 111 |
|
| 112 |
// Keyword click handler — AJAX only when enabled. |
| 113 |
$('.bbpc-search-keyword ul li a').on('click', function (e) { |
| 114 |
e.preventDefault(); |
| 115 |
const content = $(this).text().trim(); |
| 116 |
const postType = $('#searchInput').data('post-type') || ''; |
| 117 |
const ajax_url = forumax_localize_script.ajaxurl; |
| 118 |
const nonce = forumax_localize_script.nonce; |
| 119 |
$('#searchInput').val(content).focus(); |
| 120 |
|
| 121 |
if ( ! isAjaxSearch ) { |
| 122 |
// Submit the form normally when AJAX search is disabled. |
| 123 |
$( '#searchInput' ).closest( 'form' ).submit(); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if (content !== '') { |
| 128 |
$.ajax({ |
| 129 |
url: ajax_url, |
| 130 |
method: 'POST', |
| 131 |
data: { |
| 132 |
action: 'forumax_search_data_fetch', |
| 133 |
keyword: content, |
| 134 |
post_type: postType, |
| 135 |
nonce: nonce |
| 136 |
}, |
| 137 |
beforeSend: function () { |
| 138 |
$('.spinner').css('display', 'flex'); |
| 139 |
}, |
| 140 |
success: function (data) { |
| 141 |
$('#bbpc-search-result').html(data).addClass('ajax-search'); |
| 142 |
$('.spinner').hide(); |
| 143 |
|
| 144 |
const no_result = $( |
| 145 |
'.tab-item.active.all-active' |
| 146 |
).attr( 'data-noresult' ); |
| 147 |
if ( no_result ) { |
| 148 |
const msg = no_result.replace( /-/g, ' ' ); |
| 149 |
$( '#bbpc-search-result' ).html( |
| 150 |
'<h5 class="bbpc-not-found-text">' + |
| 151 |
msg + |
| 152 |
'</h5>' |
| 153 |
); |
| 154 |
$( '.bbpc-not-found-text' ).show(); |
| 155 |
} else { |
| 156 |
highlightKeyword( content ); |
| 157 |
} |
| 158 |
}, |
| 159 |
} ); |
| 160 |
/* eslint-enable camelcase */ |
| 161 |
} |
| 162 |
} ); |
| 163 |
|
| 164 |
// Clear search input |
| 165 |
$( '#searchInput' ).on( 'input', function () { |
| 166 |
if ( this.value === '' ) { |
| 167 |
$( '#bbpc-search-result' ).removeClass( 'ajax-search' ); |
| 168 |
} |
| 169 |
} ); |
| 170 |
|
| 171 |
// Overlay behavior |
| 172 |
$( '#searchInput' ).on( 'focus', function () { |
| 173 |
$( 'body' ).addClass( 'bbpc-search-overlay' ); |
| 174 |
// Ensure the form is positioned so z-index takes effect |
| 175 |
$( 'form.bbpc_search_form_wrapper' ).css( { |
| 176 |
position: 'relative', |
| 177 |
'z-index': '999', |
| 178 |
} ); |
| 179 |
} ); |
| 180 |
|
| 181 |
$( '.bbpc-search-overlay' ).on( 'click', function () { |
| 182 |
$( 'body' ).removeClass( 'bbpc-search-overlay' ); |
| 183 |
// Revert the temporary positioning and z-index |
| 184 |
$( 'form.bbpc_search_form_wrapper' ).css( { |
| 185 |
'z-index': '', |
| 186 |
position: '', |
| 187 |
} ); |
| 188 |
} ); |
| 189 |
} ); |
| 190 |
} )( jQuery ); /* eslint-disable-line no-undef */ |
| 191 |
|