| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | "use strict"; |
| 2 | 2 | |
| 3 | 3 | // --------------------------------------------------------------------------------------------------------------------- |
| 4 | 4 | // == A j a x A d d N e w B o o k i n g == |
| @@ -65,11 +65,39 @@ | ||
| 65 | 65 | * @param response_data - its object returned from Ajax - class-live-searcg.php |
| 66 | 66 | * @param textStatus - 'success' |
| 67 | 67 | * @param jqXHR - Object |
| 68 | 68 | */ |
| 69 | - function ( response_data, textStatus, jqXHR ) { | |
| 70 | -console.log( ' == Response WPBC_AJX_BOOKING__CREATE == ' ); | |
| 71 | -for ( var obj_key in response_data ){ | |
| 69 | + function ( response_data, textStatus, jqXHR ) { | |
| 70 | + var is_valid_response = ( | |
| 71 | + (typeof response_data === 'object') | |
| 72 | + && (response_data !== null) | |
| 73 | + && (! Array.isArray( response_data )) | |
| 74 | + && Object.prototype.hasOwnProperty.call( response_data, 'resource_id' ) | |
| 75 | + && (typeof response_data[ 'ajx_data' ] === 'object') | |
| 76 | + && (response_data[ 'ajx_data' ] !== null) | |
| 77 | + && (! Array.isArray( response_data[ 'ajx_data' ] )) | |
| 78 | + && (typeof response_data[ 'ajx_data' ][ 'status' ] === 'string') | |
| 79 | + ); | |
| 80 | + | |
| 81 | + if ( ! is_valid_response ) { | |
| 82 | + var calendar_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); | |
| 83 | + var jq_node = '#booking_form' + calendar_id; | |
| 84 | + var response_error_message = _wpbc.get_message( 'message_unexpected_server_response' ) || 'The server returned an unexpected response. Please reload the page and try again.'; | |
| 85 | + | |
| 86 | + wpbc_front_end__log_unexpected_ajax_response( 'WPBC_AJX_BOOKING__CREATE', response_data, jqXHR, textStatus, 'Invalid response structure' ); | |
| 87 | + wpbc_front_end__show_message( response_error_message, { 'type' : 'error', | |
| 88 | + 'show_here' : {'jq_node': jq_node, 'where': 'after'}, | |
| 89 | + 'is_append' : true, | |
| 90 | + 'style' : 'text-align:left;', | |
| 91 | + 'content_mode': 'text', | |
| 92 | + 'delay' : 0 | |
| 93 | + } ); | |
| 94 | + wpbc_booking_form__on_response__ui_elements_enable( calendar_id ); | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + | |
| 98 | +console.log( ' == Response WPBC_AJX_BOOKING__CREATE == ' ); | |
| 99 | +for ( var obj_key in response_data ){ | |
| 72 | 100 | console.groupCollapsed( '==' + obj_key + '==' ); |
| 73 | 101 | console.log( ' : ' + obj_key + ' : ', response_data[ obj_key ] ); |
| 74 | 102 | console.groupEnd(); |
| 75 | 103 | } |
| @@ -75,34 +103,8 @@ | ||
| 75 | 103 | } |
| 76 | 104 | console.groupEnd(); |
| 77 | 105 | |
| 78 | 106 | |
| 79 | - // <editor-fold defaultstate="collapsed" desc=" = Error Message! Server response with String. -> E_X_I_T " > | |
| 80 | - // ------------------------------------------------------------------------------------------------- | |
| 81 | - // This section execute, when server response with String instead of Object -- Usually it's because of mistake in code ! | |
| 82 | - // ------------------------------------------------------------------------------------------------- | |
| 83 | - if ( (typeof response_data !== 'object') || (response_data === null) ){ | |
| 84 | - | |
| 85 | - var calendar_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); | |
| 86 | - var jq_node = '#booking_form' + calendar_id; | |
| 87 | - | |
| 88 | - if ( '' == response_data ){ | |
| 89 | - response_data = '<strong>' + 'Error! Server respond with empty string!' + '</strong> ' ; | |
| 90 | - } | |
| 91 | - // Show Message | |
| 92 | - wpbc_front_end__show_message( response_data , { 'type' : 'error', | |
| 93 | - 'show_here': {'jq_node': jq_node, 'where': 'after'}, | |
| 94 | - 'is_append': true, | |
| 95 | - 'style' : 'text-align:left;', | |
| 96 | - 'delay' : 0 | |
| 97 | - } ); | |
| 98 | - // Enable Submit | Hide spin loader | |
| 99 | - wpbc_booking_form__on_response__ui_elements_enable( calendar_id ); | |
| 100 | - return; | |
| 101 | - } | |
| 102 | - // </editor-fold> | |
| 103 | - | |
| 104 | - | |
| 105 | 107 | // <editor-fold defaultstate="collapsed" desc=" == This section execute, when we have KNOWN errors from Booking Calendar. -> E_X_I_T " > |
| 106 | 108 | // ------------------------------------------------------------------------------------------------- |
| 107 | 109 | // This section execute, when we have KNOWN errors from Booking Calendar |
| 108 | 110 | // ------------------------------------------------------------------------------------------------- |
| @@ -115,9 +117,9 @@ | ||
| 115 | 117 | wpbc_captcha__simple__update( { |
| 116 | 118 | 'resource_id': response_data[ 'resource_id' ], |
| 117 | 119 | 'url' : response_data[ 'ajx_data' ][ 'captcha__simple' ][ 'url' ], |
| 118 | 120 | 'challenge' : response_data[ 'ajx_data' ][ 'captcha__simple' ][ 'challenge' ], |
| 119 | - 'message' : response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ) | |
| 121 | + 'message' : response_data[ 'ajx_data' ][ 'ajx_after_action_message' ] | |
| 120 | 122 | } |
| 121 | 123 | ); |
| 122 | 124 | break; |
| 123 | 125 | |
| @@ -157,25 +159,21 @@ | ||
| 157 | 159 | |
| 158 | 160 | var calendar_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); |
| 159 | 161 | var jq_node = '#booking_form' + calendar_id; |
| 160 | 162 | |
| 161 | - var ajx_after_booking_message = response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ); | |
| 162 | - | |
| 163 | - console.log( ajx_after_booking_message ); | |
| 164 | - | |
| 165 | - /** | |
| 166 | - * // Show Message | |
| 167 | - var ajx_after_action_message_id = wpbc_front_end__show_message( ajx_after_booking_message, | |
| 168 | - { | |
| 169 | - 'type' : ('undefined' !== typeof (response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ])) | |
| 170 | - ? response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] : 'info', | |
| 171 | - 'delay' : 10000, | |
| 172 | - 'show_here': { | |
| 173 | - 'jq_node': jq_node, | |
| 174 | - 'where' : 'after' | |
| 175 | - } | |
| 176 | - } ); | |
| 177 | - */ | |
| 163 | + var ajx_after_booking_message = response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ); | |
| 164 | + | |
| 165 | + console.log( ajx_after_booking_message ); | |
| 166 | + wpbc_front_end__show_message( ajx_after_booking_message, | |
| 167 | + { | |
| 168 | + 'type' : ('undefined' !== typeof (response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ])) | |
| 169 | + ? response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] : 'warning', | |
| 170 | + 'delay' : 0, | |
| 171 | + 'show_here': { | |
| 172 | + 'jq_node': jq_node, | |
| 173 | + 'where' : 'after' | |
| 174 | + } | |
| 175 | + } ); | |
| 178 | 176 | } |
| 179 | 177 | // </editor-fold> |
| 180 | 178 | } |
| 181 | 179 | |
| @@ -236,8 +234,11 @@ | ||
| 236 | 234 | |
| 237 | 235 | // Show Confirmation | Payment section |
| 238 | 236 | wpbc_show_thank_you_message_after_booking( response_data ); |
| 239 | 237 | |
| 238 | + // Trigger hook after a booking was created or updated successfully. | |
| 239 | + jQuery( 'body' ).trigger( 'wpbc_booking_form_submit_success', [ response_data[ 'resource_id' ], response_data, params ] ); | |
| 240 | + | |
| 240 | 241 | setTimeout( function (){ |
| 241 | 242 | wpbc_do_scroll( '#wpbc_scroll_point_' + response_data[ 'resource_id' ], 10 ); |
| 242 | 243 | }, 500 ); |
| 243 | 244 | |
| @@ -245,44 +246,31 @@ | ||
| 245 | 246 | |
| 246 | 247 | } |
| 247 | 248 | ).fail( |
| 248 | 249 | // <editor-fold defaultstate="collapsed" desc=" = This section execute, when NONCE field was not passed or some error happened at server! = " > |
| 249 | - function ( jqXHR, textStatus, errorThrown ) { if ( window.console && window.console.log ){ console.log( 'Ajax_Error', jqXHR, textStatus, errorThrown ); } | |
| 250 | + function ( jqXHR, textStatus, errorThrown ) { | |
| 251 | + wpbc_front_end__log_unexpected_ajax_response( 'WPBC_AJX_BOOKING__CREATE', jqXHR.responseText || '', jqXHR, textStatus, errorThrown ); | |
| 250 | 252 | |
| 251 | 253 | // ------------------------------------------------------------------------------------------------- |
| 252 | 254 | // This section execute, when NONCE field was not passed or some error happened at server! |
| 253 | 255 | // ------------------------------------------------------------------------------------------------- |
| 254 | 256 | |
| 255 | - // Get Content of Error Message | |
| 256 | - var error_message = '<strong>' + 'Error!' + '</strong> ' + errorThrown ; | |
| 257 | - if ( jqXHR.status ){ | |
| 258 | - error_message += ' (<b>' + jqXHR.status + '</b>)'; | |
| 259 | - if (403 == jqXHR.status ){ | |
| 260 | - error_message += '<br> Probably nonce for this page has been expired. Please <a href="javascript:void(0)" onclick="javascript:location.reload();">reload the page</a>.'; | |
| 261 | - error_message += '<br> Otherwise, please check this <a style="font-weight: 600;" href="https://wpbookingcalendar.com/faq/request-do-not-pass-security-check/?after_update=10.1.1">troubleshooting instruction</a>.<br>' | |
| 262 | - } | |
| 263 | - } | |
| 264 | - if ( jqXHR.responseText ){ | |
| 265 | - // Escape tags in Error message | |
| 266 | - error_message += '<br><strong>Response</strong><div style="padding: 0 10px;margin: 0 0 10px;border-radius:3px; box-shadow:0px 0px 1px #a3a3a3;">' + jqXHR.responseText.replace(/&/g, "&") | |
| 267 | - .replace(/</g, "<") | |
| 268 | - .replace(/>/g, ">") | |
| 269 | - .replace(/"/g, """) | |
| 270 | - .replace(/'/g, "'") | |
| 271 | - +'</div>'; | |
| 272 | - } | |
| 273 | - error_message = error_message.replace( /\n/g, "<br />" ); | |
| 257 | + var error_message = _wpbc.get_message( 'message_unexpected_server_response' ) || 'The server returned an unexpected response. Please reload the page and try again.'; | |
| 258 | + if ( jqXHR.status ){ | |
| 259 | + error_message += ' (' + parseInt( jqXHR.status, 10 ) + ')'; | |
| 260 | + } | |
| 274 | 261 | |
| 275 | 262 | var calendar_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); |
| 276 | 263 | var jq_node = '#booking_form' + calendar_id; |
| 277 | 264 | |
| 278 | 265 | // Show Message |
| 279 | - wpbc_front_end__show_message( error_message , { 'type' : 'error', | |
| 280 | - 'show_here': {'jq_node': jq_node, 'where': 'after'}, | |
| 281 | - 'is_append': true, | |
| 282 | - 'style' : 'text-align:left;', | |
| 283 | - 'delay' : 0 | |
| 284 | - } ); | |
| 266 | + wpbc_front_end__show_message( error_message , { 'type' : 'error', | |
| 267 | + 'show_here' : {'jq_node': jq_node, 'where': 'after'}, | |
| 268 | + 'is_append' : true, | |
| 269 | + 'style' : 'text-align:left;', | |
| 270 | + 'content_mode': 'text', | |
| 271 | + 'delay' : 0 | |
| 272 | + } ); | |
| 285 | 273 | // Enable Submit | Hide spin loader |
| 286 | 274 | wpbc_booking_form__on_response__ui_elements_enable( calendar_id ); |
| 287 | 275 | } |
| 288 | 276 | // </editor-fold> |
| @@ -315,9 +303,9 @@ | ||
| 315 | 303 | document.getElementById( 'captcha_img' + params[ 'resource_id' ] ).src = params[ 'url' ]; |
| 316 | 304 | document.getElementById( 'wpdev_captcha_challenge_' + params[ 'resource_id' ] ).value = params[ 'challenge' ]; |
| 317 | 305 | |
| 318 | 306 | // Show warning After CAPTCHA Img |
| 319 | - var message_id = wpbc_front_end__show_message__warning( '#captcha_input' + params[ 'resource_id' ] + ' + img', params[ 'message' ] ); | |
| 307 | + var message_id = wpbc_front_end__show_message__warning( '#captcha_input' + params[ 'resource_id' ] + ' + img', params[ 'message' ], 'text' ); | |
| 320 | 308 | |
| 321 | 309 | // Animate |
| 322 | 310 | jQuery( '#' + message_id + ', ' + '#captcha_input' + params[ 'resource_id' ] ).fadeOut( 350 ).fadeIn( 300 ).fadeOut( 350 ).fadeIn( 400 ).animate( {opacity: 1}, 4000 ); |
| 323 | 311 | // Focus text field |
| @@ -429,11 +417,32 @@ | ||
| 429 | 417 | * @param resource_id |
| 430 | 418 | */ |
| 431 | 419 | function wpbc_booking_form__spin_loader__show( resource_id ){ |
| 432 | 420 | |
| 433 | - // Show Spin Loader | |
| 421 | + var $form_container = jQuery( '#booking_form_div' + resource_id ); | |
| 422 | + var loader_text = 'Saving'; | |
| 423 | + if ( 'undefined' !== typeof _wpbc && _wpbc.get_message ) { | |
| 424 | + loader_text = _wpbc.get_message( 'message_booking_saving' ) || loader_text; | |
| 425 | + } | |
| 426 | + var loader_html = '<div id="wpbc_booking_form_spin_loader' + resource_id + '" class="wpbc_booking_form_spin_loader wpbc_booking_form_submit_overlay" aria-live="polite" aria-hidden="false">' | |
| 427 | + + '<div class="wpbc_spins_loading_container">' | |
| 428 | + + '<div class="wpbc_booking_form_spin_loader"><div class="wpbc_spins_loader_wrapper"><div class="wpbc_spin_loader_one_new"></div></div></div>' | |
| 429 | + + '<span>' + loader_text + '...</span>' | |
| 430 | + + '</div>' | |
| 431 | + + '</div>'; | |
| 432 | + | |
| 433 | + // Remove any previous inline loader before adding the submit overlay. | |
| 434 | + jQuery( '#wpbc_booking_form_spin_loader' + resource_id ).remove(); | |
| 435 | + | |
| 436 | + if ( $form_container.length && $form_container.is( ':visible' ) ) { | |
| 437 | + $form_container.addClass( 'wpbc_booking_form_is_submitting' ); | |
| 438 | + $form_container.prepend( loader_html ); | |
| 439 | + return; | |
| 440 | + } | |
| 441 | + | |
| 442 | + // Fallback for any unexpected legacy markup without #booking_form_div{resource_id}. | |
| 434 | 443 | jQuery( '#booking_form' + resource_id ).after( |
| 435 | - '<div id="wpbc_booking_form_spin_loader' + resource_id + '" class="wpbc_booking_form_spin_loader" style="position: relative;"><div class="wpbc_spins_loader_wrapper"><div class="wpbc_spins_loader_mini"></div></div></div>' | |
| 444 | + '<div id="wpbc_booking_form_spin_loader' + resource_id + '" class="wpbc_booking_form_spin_loader" style="position: relative;"><div class="wpbc_spins_loader_wrapper"><div class="wpbc_spin_loader_one_new"></div></div></div>' | |
| 436 | 445 | ); |
| 437 | 446 | } |
| 438 | 447 | |
| 439 | 448 | /** |
| @@ -443,8 +452,9 @@ | ||
| 443 | 452 | function wpbc_booking_form__spin_loader__hide( resource_id ){ |
| 444 | 453 | |
| 445 | 454 | // Remove Spin Loader |
| 446 | 455 | jQuery( '#wpbc_booking_form_spin_loader' + resource_id ).remove(); |
| 456 | + jQuery( '#booking_form_div' + resource_id ).removeClass( 'wpbc_booking_form_is_submitting' ); | |
| 447 | 457 | } |
| 448 | 458 | |
| 449 | 459 | |
| 450 | 460 | /** |
| @@ -453,38 +463,9 @@ | ||
| 453 | 463 | * @param resource_id |
| 454 | 464 | */ |
| 455 | 465 | function wpbc_booking_form__animated__hide( resource_id ){ |
| 456 | 466 | |
| 457 | - // jQuery( '#booking_form' + resource_id ).slideUp( 1000 | |
| 458 | - // , function (){ | |
| 459 | - // | |
| 460 | - // // if ( document.getElementById( 'gateway_payment_forms' + response_data[ 'resource_id' ] ) != null ){ | |
| 461 | - // // wpbc_do_scroll( '#submiting' + resource_id ); | |
| 462 | - // // } else | |
| 463 | - // if ( jQuery( '#booking_form' + resource_id ).parent().find( '.submiting_content' ).length > 0 ){ | |
| 464 | - // //wpbc_do_scroll( '#booking_form' + resource_id + ' + .submiting_content' ); | |
| 465 | - // | |
| 466 | - // var hideTimeout = setTimeout(function () { | |
| 467 | - // wpbc_do_scroll( jQuery( '#booking_form' + resource_id ).parent().find( '.submiting_content' ).get( 0 ) ); | |
| 468 | - // }, 100); | |
| 469 | - // | |
| 470 | - // } | |
| 471 | - // } | |
| 472 | - // ); | |
| 473 | - | |
| 474 | 467 | jQuery( '#booking_form' + resource_id ).hide(); |
| 475 | - | |
| 476 | - // var hideTimeout = setTimeout( function (){ | |
| 477 | - // | |
| 478 | - // if ( jQuery( '#booking_form' + resource_id ).parent().find( '.submiting_content' ).length > 0 ){ | |
| 479 | - // var random_id = Math.floor( (Math.random() * 10000) + 1 ); | |
| 480 | - // jQuery( '#booking_form' + resource_id ).parent().before( '<div id="scroll_to' + random_id + '"></div>' ); | |
| 481 | - // console.log( jQuery( '#scroll_to' + random_id ) ); | |
| 482 | - // | |
| 483 | - // wpbc_do_scroll( '#scroll_to' + random_id ); | |
| 484 | - // //wpbc_do_scroll( jQuery( '#booking_form' + resource_id ).parent().get( 0 ) ); | |
| 485 | - // } | |
| 486 | - // }, 500 ); | |
| 487 | 468 | } |
| 488 | 469 | // </editor-fold> |
| 489 | 470 | |
| 490 | 471 | |
| @@ -535,9 +516,9 @@ | ||
| 535 | 516 | 'jq_node': '', // any jQuery node definition |
| 536 | 517 | 'where' : 'after' // 'inside' | 'before' | 'after' | 'right' | 'left' |
| 537 | 518 | }, |
| 538 | 519 | 'style' : 'position: relative;min-height: 2.8rem;', |
| 539 | - 'class' : 'wpbc_one_spin_loader_mini 0wpbc_spins_loader_mini' | |
| 520 | + 'class' : 'wpbc_one_spin_loader_mini 0wpbc_spin_loader_one_new' | |
| 540 | 521 | }; |
| 541 | 522 | for ( var p_key in params ){ |
| 542 | 523 | params_default[ p_key ] = params[ p_key ]; |
| 543 | 524 | } |