← All changes
|
includes/booking-resource-selector/_src/booking-resource-selector.js
+45
-0
11.5
→
11.9
View file →
| @@ -13,8 +13,43 @@ | ||
| 13 | 13 | function get_selected_resource_id( $form ) { |
| 14 | 14 | return Number( $form.find( '[name="wpbc_resource_selector_resource"]:checked' ).first().val() || 0 ); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | + /** | |
| 18 | + * Apply the public text search for one Resource catalog. | |
| 19 | + * | |
| 20 | + * Filtering only hides cards already authorized and rendered by the server; | |
| 21 | + * it cannot add Resource IDs to the signed selection context. | |
| 22 | + * | |
| 23 | + * @param {jQuery} $catalog Resource catalog root. | |
| 24 | + * @return {void} | |
| 25 | + */ | |
| 26 | + function filter_resource_catalog( $catalog ) { | |
| 27 | + var search_term = String( $catalog.find( '[data-wpbc-resource-catalog-search]' ).val() || '' ).toLocaleLowerCase().trim(); | |
| 28 | + var visible_count = 0; | |
| 29 | + | |
| 30 | + $catalog.find( '[data-resource-id]' ).each( function () { | |
| 31 | + var $card = $( this ); | |
| 32 | + var searchable_text = String( $card.attr( 'data-resource-search' ) || '' ).toLocaleLowerCase(); | |
| 33 | + var is_visible = ! search_term || searchable_text.indexOf( search_term ) !== -1; | |
| 34 | + var $resource_input = $card.find( '[name="wpbc_resource_selector_resource"]' ); | |
| 35 | + | |
| 36 | + $card.prop( 'hidden', ! is_visible ); | |
| 37 | + $resource_input.prop( 'disabled', ! is_visible ); | |
| 38 | + if ( is_visible ) { | |
| 39 | + visible_count += 1; | |
| 40 | + } else if ( $resource_input.prop( 'checked' ) ) { | |
| 41 | + $resource_input.prop( 'checked', false ); | |
| 42 | + $card.removeClass( 'is-selected' ); | |
| 43 | + } | |
| 44 | + } ); | |
| 45 | + | |
| 46 | + $catalog.find( '[data-wpbc-resource-catalog-empty]' ).prop( 'hidden', 0 !== visible_count ); | |
| 47 | + $catalog.find( '[data-wpbc-resource-catalog-status]' ).text( | |
| 48 | + String( visible_count ) + ' ' + ( 1 === visible_count ? ( config.resource_found || 'Booking Resource found.' ) : ( config.resources_found || 'Booking Resources found.' ) ) | |
| 49 | + ); | |
| 50 | + } | |
| 51 | + | |
| 17 | 52 | /** Toggle one selector loading state without clearing its current stage. */ |
| 18 | 53 | function set_loading( $root, is_loading ) { |
| 19 | 54 | $root.toggleClass( 'is-loading', is_loading ).attr( 'aria-busy', is_loading ? 'true' : 'false' ); |
| 20 | 55 | $root.find( '> .wpbc_booking_resource_selector__stage' ).attr( 'aria-busy', is_loading ? 'true' : 'false' ); |
| @@ -19,8 +54,13 @@ | ||
| 19 | 54 | $root.toggleClass( 'is-loading', is_loading ).attr( 'aria-busy', is_loading ? 'true' : 'false' ); |
| 20 | 55 | $root.find( '> .wpbc_booking_resource_selector__stage' ).attr( 'aria-busy', is_loading ? 'true' : 'false' ); |
| 21 | 56 | $root.find( '> .wpbc_booking_resource_selector__loading' ).prop( 'hidden', ! is_loading ).attr( 'aria-hidden', is_loading ? 'false' : 'true' ); |
| 22 | 57 | $root.find( '.wpbc_booking_resource_selector__selection_form :input' ).prop( 'disabled', is_loading ); |
| 58 | + if ( ! is_loading ) { | |
| 59 | + $root.find( '[data-wpbc-resource-catalog]' ).each( function () { | |
| 60 | + filter_resource_catalog( $( this ) ); | |
| 61 | + } ); | |
| 62 | + } | |
| 23 | 63 | } |
| 24 | 64 | |
| 25 | 65 | /** Display and focus one controlled AJAX or initialization error. */ |
| 26 | 66 | function show_error( $root, message ) { |
| @@ -344,8 +384,13 @@ | ||
| 344 | 384 | $( document ).on( 'change', '.wpbc_booking_resource_selector__choice > input', function () { |
| 345 | 385 | var $input = $( this ); |
| 346 | 386 | $input.closest( '.wpbc_booking_resource_selector__choices' ).find( '.wpbc_booking_resource_selector__choice' ).removeClass( 'is-selected' ); |
| 347 | 387 | $input.closest( '.wpbc_booking_resource_selector__choice' ).addClass( 'is-selected' ); |
| 388 | + } ); | |
| 389 | + | |
| 390 | + /** Filter cards without changing the server-authorized Resource set. */ | |
| 391 | + $( document ).on( 'input', '.wpbc_booking_resource_catalog [data-wpbc-resource-catalog-search]', function () { | |
| 392 | + filter_resource_catalog( $( this ).closest( '[data-wpbc-resource-catalog]' ) ); | |
| 348 | 393 | } ); |
| 349 | 394 | |
| 350 | 395 | /** Return to Resource selection without reloading the public page. */ |
| 351 | 396 | $( document ).on( 'click', '.wpbc_booking_resource_selector [data-wpbc-resource-selector-action="start-over"]', function ( event ) { |