| 1 |
/** |
| 2 |
* ==================================================================================================================== |
| 3 |
* includes/__js/cal_ajx_load/wpbc_cal_ajx.js |
| 4 |
* ==================================================================================================================== |
| 5 |
*/ |
| 6 |
|
| 7 |
// --------------------------------------------------------------------------------------------------------------------- |
| 8 |
// A j a x L o a d C a l e n d a r D a t a |
| 9 |
// --------------------------------------------------------------------------------------------------------------------- |
| 10 |
|
| 11 |
function wpbc_calendar__load_data__ajx( params ){ |
| 12 |
|
| 13 |
// FixIn: 9.8.6.2. |
| 14 |
wpbc_calendar__loading__start( params['resource_id'] ); |
| 15 |
|
| 16 |
// Trigger event for calendar before loading Booking data, but after showing Calendar. |
| 17 |
if ( jQuery( '#calendar_booking' + params['resource_id'] ).length > 0 ){ |
| 18 |
var target_elm = jQuery( 'body' ).trigger( "wpbc_calendar_ajx__before_loaded_data", [params['resource_id']] ); |
| 19 |
//jQuery( 'body' ).on( 'wpbc_calendar_ajx__before_loaded_data', function( event, resource_id ) { ... } ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( wpbc_balancer__is_wait( params , 'wpbc_calendar__load_data__ajx' ) ){ |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
// FixIn: 9.8.6.2. |
| 27 |
wpbc_calendar__blur__stop( params['resource_id'] ); |
| 28 |
|
| 29 |
// ----------------------------------------------------------------------------------------------------------------- |
| 30 |
// == Get start / end dates from the Booking Calendar shortcode. == |
| 31 |
// Example: [booking calendar_dates_start='2026-01-01' calendar_dates_end='2026-12-31' resource_id=1] // FixIn: 10.13.1.4. |
| 32 |
// ----------------------------------------------------------------------------------------------------------------- |
| 33 |
if ( false !== wpbc_calendar__get_dates_start( params['resource_id'] ) ) { |
| 34 |
if ( ! params['dates_to_check'] ) { params['dates_to_check'] = []; } |
| 35 |
var dates_start = wpbc_calendar__get_dates_start( params['resource_id'] ); // E.g. - local__min_date = new Date( 2025, 0, 1 ); |
| 36 |
if ( false !== dates_start ){ |
| 37 |
params['dates_to_check'][0] = wpbc__get__sql_class_date( dates_start ); |
| 38 |
} |
| 39 |
} |
| 40 |
if ( false !== wpbc_calendar__get_dates_end( params['resource_id'] ) ) { |
| 41 |
if ( !params['dates_to_check'] ) { params['dates_to_check'] = []; } |
| 42 |
var dates_end = wpbc_calendar__get_dates_end( params['resource_id'] ); // E.g. - local__min_date = new Date( 2025, 0, 1 ); |
| 43 |
if ( false !== dates_end ) { |
| 44 |
params['dates_to_check'][1] = wpbc__get__sql_class_date( dates_end ); |
| 45 |
if ( !params['dates_to_check'][0] ) { |
| 46 |
params['dates_to_check'][0] = wpbc__get__sql_class_date( new Date() ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
// ----------------------------------------------------------------------------------------------------------------- |
| 51 |
|
| 52 |
// console.groupEnd(); console.time('resource_id_' + params['resource_id']); |
| 53 |
console.groupCollapsed( 'WPBC_AJX_CALENDAR_LOAD' ); console.log( ' == Before Ajax Send - calendars_all__get() == ' , _wpbc.calendars_all__get() ); |
| 54 |
if ( 'function' === typeof (wpbc_hook__init_timeselector) ) { |
| 55 |
wpbc_hook__init_timeselector(); |
| 56 |
} |
| 57 |
|
| 58 |
// Start Ajax |
| 59 |
jQuery.post( wpbc_url_ajax, |
| 60 |
{ |
| 61 |
action : 'WPBC_AJX_CALENDAR_LOAD', |
| 62 |
wpbc_ajx_user_id: _wpbc.get_secure_param( 'user_id' ), |
| 63 |
nonce : _wpbc.get_secure_param( 'nonce' ), |
| 64 |
wpbc_ajx_locale : _wpbc.get_secure_param( 'locale' ), |
| 65 |
|
| 66 |
calendar_request_params : params // Usually like: { 'resource_id': 1, 'max_days_count': 365 } |
| 67 |
}, |
| 68 |
|
| 69 |
/** |
| 70 |
* S u c c e s s |
| 71 |
* |
| 72 |
* @param response_data - its object returned from Ajax - class-live-search.php |
| 73 |
* @param textStatus - 'success' |
| 74 |
* @param jqXHR - Object |
| 75 |
*/ |
| 76 |
function ( response_data, textStatus, jqXHR ) { |
| 77 |
// console.timeEnd('resource_id_' + response_data['resource_id']); |
| 78 |
console.log( ' == Response WPBC_AJX_CALENDAR_LOAD == ', response_data ); console.groupEnd(); |
| 79 |
|
| 80 |
// FixIn: 9.8.6.2. |
| 81 |
var ajx_post_data__resource_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); |
| 82 |
wpbc_balancer__completed( ajx_post_data__resource_id , 'wpbc_calendar__load_data__ajx' ); |
| 83 |
|
| 84 |
var is_valid_response = ( |
| 85 |
(typeof response_data === 'object') |
| 86 |
&& (response_data !== null) |
| 87 |
&& (! Array.isArray( response_data )) |
| 88 |
&& Object.prototype.hasOwnProperty.call( response_data, 'resource_id' ) |
| 89 |
&& (typeof response_data[ 'ajx_data' ] === 'object') |
| 90 |
&& (response_data[ 'ajx_data' ] !== null) |
| 91 |
&& (! Array.isArray( response_data[ 'ajx_data' ] )) |
| 92 |
&& Object.prototype.hasOwnProperty.call( response_data[ 'ajx_data' ], 'dates' ) |
| 93 |
&& Object.prototype.hasOwnProperty.call( response_data[ 'ajx_data' ], 'resources_id_arr__in_dates' ) |
| 94 |
&& Object.prototype.hasOwnProperty.call( response_data[ 'ajx_data' ], 'aggregate_resource_id_arr' ) |
| 95 |
); |
| 96 |
|
| 97 |
if ( ! is_valid_response ) { |
| 98 |
var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data ); |
| 99 |
var response_error_message = _wpbc.get_message( 'message_unexpected_server_response' ) || 'The server returned an unexpected response. Please reload the page and try again.'; |
| 100 |
|
| 101 |
wpbc_front_end__log_unexpected_ajax_response( 'WPBC_AJX_CALENDAR_LOAD', response_data, jqXHR, textStatus, 'Invalid response structure' ); |
| 102 |
wpbc_calendar__loading__stop( ajx_post_data__resource_id ); |
| 103 |
|
| 104 |
wpbc_front_end__show_message( response_error_message, { 'type' : 'error', |
| 105 |
'show_here' : {'jq_node': jq_node, 'where': 'after'}, |
| 106 |
'is_append' : true, |
| 107 |
'style' : 'text-align:left;', |
| 108 |
'content_mode': 'text', |
| 109 |
'delay' : 0 |
| 110 |
} ); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
// Show Calendar |
| 115 |
wpbc_calendar__loading__stop( response_data[ 'resource_id' ] ); |
| 116 |
|
| 117 |
// ------------------------------------------------------------------------------------------------- |
| 118 |
// Bookings - Dates |
| 119 |
_wpbc.bookings_in_calendar__set_dates( response_data[ 'resource_id' ], response_data[ 'ajx_data' ]['dates'] ); |
| 120 |
|
| 121 |
// Bookings - Child or only single booking resource in dates |
| 122 |
_wpbc.booking__set_param_value( response_data[ 'resource_id' ], 'resources_id_arr__in_dates', response_data[ 'ajx_data' ][ 'resources_id_arr__in_dates' ] ); |
| 123 |
|
| 124 |
// Aggregate booking resources, if any ? |
| 125 |
_wpbc.booking__set_param_value( response_data[ 'resource_id' ], 'aggregate_resource_id_arr', response_data[ 'ajx_data' ][ 'aggregate_resource_id_arr' ] ); |
| 126 |
// ------------------------------------------------------------------------------------------------- |
| 127 |
|
| 128 |
// Update calendar |
| 129 |
wpbc_calendar__update_look( response_data[ 'resource_id' ] ); |
| 130 |
|
| 131 |
if ( 'function' === typeof (wpbc_hook__init_timeselector) ) { |
| 132 |
wpbc_hook__init_timeselector(); |
| 133 |
} |
| 134 |
|
| 135 |
if ( |
| 136 |
( 'undefined' !== typeof (response_data[ 'ajx_data' ][ 'ajx_after_action_message' ]) ) |
| 137 |
&& ( '' != response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ) ) |
| 138 |
){ |
| 139 |
|
| 140 |
var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data ); |
| 141 |
|
| 142 |
// Show Message |
| 143 |
wpbc_front_end__show_message( response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ), |
| 144 |
{ 'type' : ( 'undefined' !== typeof( response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] ) ) |
| 145 |
? response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] : 'info', |
| 146 |
'show_here': {'jq_node': jq_node, 'where': 'after'}, |
| 147 |
'is_append': true, |
| 148 |
'style' : 'text-align:left;', |
| 149 |
'delay' : 10000 |
| 150 |
} ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( 'function' === typeof (wpbc_update_capacity_hint) ) { |
| 154 |
wpbc_update_capacity_hint( response_data['resource_id'] ); |
| 155 |
} |
| 156 |
|
| 157 |
// Trigger event that calendar has been // FixIn: 10.0.0.44. // FixIn: 10.14.17.2. |
| 158 |
if ( (jQuery( '#calendar_booking' + response_data['resource_id'] ).length > 0) || (jQuery( '#date_booking' + response_data['resource_id'] ).length > 0) ) { |
| 159 |
var target_elm = jQuery( 'body' ).trigger( "wpbc_calendar_ajx__loaded_data", [response_data[ 'resource_id' ]] ); |
| 160 |
//jQuery( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function( event, resource_id ) { ... } ); |
| 161 |
} |
| 162 |
|
| 163 |
//jQuery( '#ajax_respond' ).html( response_data ); // For ability to show response, add such DIV element to page |
| 164 |
} |
| 165 |
).fail( function ( jqXHR, textStatus, errorThrown ) { |
| 166 |
wpbc_front_end__log_unexpected_ajax_response( 'WPBC_AJX_CALENDAR_LOAD', jqXHR.responseText || '', jqXHR, textStatus, errorThrown ); |
| 167 |
|
| 168 |
var ajx_post_data__resource_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data ); |
| 169 |
wpbc_balancer__completed( ajx_post_data__resource_id , 'wpbc_calendar__load_data__ajx' ); |
| 170 |
wpbc_calendar__loading__stop( ajx_post_data__resource_id ); |
| 171 |
|
| 172 |
var error_message = _wpbc.get_message( 'message_unexpected_server_response' ) || 'The server returned an unexpected response. Please reload the page and try again.'; |
| 173 |
if ( jqXHR.status ){ |
| 174 |
error_message += ' (' + parseInt( jqXHR.status, 10 ) + ')'; |
| 175 |
} |
| 176 |
var message_show_delay = 3000; |
| 177 |
|
| 178 |
var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data ); |
| 179 |
|
| 180 |
/** |
| 181 |
* If we make fast clicking on different pages, |
| 182 |
* then under calendar will show error message with empty text, because ajax was not received. |
| 183 |
* To not show such warnings we are set delay in 3 seconds. var message_show_delay = 3000; |
| 184 |
*/ |
| 185 |
var closed_timer = setTimeout( function (){ |
| 186 |
|
| 187 |
// Show Message |
| 188 |
wpbc_front_end__show_message( error_message , { 'type' : 'error', |
| 189 |
'show_here': {'jq_node': jq_node, 'where': 'after'}, |
| 190 |
'is_append': true, |
| 191 |
'style' : 'text-align:left;', |
| 192 |
'css_class':'wpbc_fe_message_alt', |
| 193 |
'content_mode': 'text', |
| 194 |
'delay' : 0 |
| 195 |
} ); |
| 196 |
} , |
| 197 |
parseInt( message_show_delay ) ); |
| 198 |
|
| 199 |
}) |
| 200 |
// .done( function ( data, textStatus, jqXHR ) { if ( window.console && window.console.log ){ console.log( 'second success', data, textStatus, jqXHR ); } }) |
| 201 |
// .always( function ( data_jqXHR, textStatus, jqXHR_errorThrown ) { if ( window.console && window.console.log ){ console.log( 'always finished', data_jqXHR, textStatus, jqXHR_errorThrown ); } }) |
| 202 |
; // End Ajax |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
// --------------------------------------------------------------------------------------------------------------------- |
| 208 |
// Support |
| 209 |
// --------------------------------------------------------------------------------------------------------------------- |
| 210 |
|
| 211 |
/** |
| 212 |
* Get Calendar jQuery node for showing messages during Ajax |
| 213 |
* This parameter: calendar_request_params[resource_id] parsed from this.data Ajax post data |
| 214 |
* |
| 215 |
* @param ajx_post_data_url_params 'action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params' |
| 216 |
* @returns {string} ''#calendar_booking1' | '.booking_form_div' ... |
| 217 |
* |
| 218 |
* Example var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data ); |
| 219 |
*/ |
| 220 |
function wpbc_get_calendar__jq_node__for_messages( ajx_post_data_url_params ){ |
| 221 |
|
| 222 |
var jq_node = '.booking_form_div'; |
| 223 |
|
| 224 |
var calendar_resource_id = wpbc_get_resource_id__from_ajx_post_data_url( ajx_post_data_url_params ); |
| 225 |
|
| 226 |
if ( calendar_resource_id > 0 ){ |
| 227 |
jq_node = '#calendar_booking' + calendar_resource_id; |
| 228 |
} |
| 229 |
|
| 230 |
return jq_node; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Get resource ID from ajx post data url usually from this.data = |
| 236 |
* 'action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params' |
| 237 |
* |
| 238 |
* @param ajx_post_data_url_params 'action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params' |
| 239 |
* @returns {int} 1 | 0 (if errror then 0) |
| 240 |
* |
| 241 |
* Example var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data ); |
| 242 |
*/ |
| 243 |
function wpbc_get_resource_id__from_ajx_post_data_url( ajx_post_data_url_params ){ |
| 244 |
|
| 245 |
// Get booking resource ID from Ajax Post Request -> this.data = 'action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params' |
| 246 |
var calendar_resource_id = wpbc_get_uri_param_by_name( 'calendar_request_params[resource_id]', ajx_post_data_url_params ); |
| 247 |
if ( (null !== calendar_resource_id) && ('' !== calendar_resource_id) ){ |
| 248 |
calendar_resource_id = parseInt( calendar_resource_id ); |
| 249 |
if ( calendar_resource_id > 0 ){ |
| 250 |
return calendar_resource_id; |
| 251 |
} |
| 252 |
} |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* Get parameter from URL - parse URL parameters, like this: |
| 259 |
* action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params |
| 260 |
* @param name parameter name, like 'calendar_request_params[resource_id]' |
| 261 |
* @param url 'parameter string URL' |
| 262 |
* @returns {string|null} parameter value |
| 263 |
* |
| 264 |
* Example: wpbc_get_uri_param_by_name( 'calendar_request_params[resource_id]', this.data ); -> '2' |
| 265 |
*/ |
| 266 |
function wpbc_get_uri_param_by_name( name, url ){ |
| 267 |
|
| 268 |
url = decodeURIComponent( url ); |
| 269 |
|
| 270 |
name = name.replace( /[\[\]]/g, '\\$&' ); |
| 271 |
var regex = new RegExp( '[?&]' + name + '(=([^&#]*)|&|#|$)' ), |
| 272 |
results = regex.exec( url ); |
| 273 |
if ( !results ) return null; |
| 274 |
if ( !results[ 2 ] ) return ''; |
| 275 |
return decodeURIComponent( results[ 2 ].replace( /\+/g, ' ' ) ); |
| 276 |
} |
| 277 |
|