PluginProbe
Booking Calendar / 11.2.1
Booking Calendar v11.2.1
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / __js / client / cal_ajx_load / wpbc_cal_ajx.js

wpbc_cal_ajx.js in Booking Calendar 11.2.1, at includes/__js/client/cal_ajx_load/wpbc_cal_ajx.js

275 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Probably Error
85 if ( (typeof response_data !== 'object') || (response_data === null) ){
86
87 var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data );
88 var message_type = 'info';
89
90 if ( '' === response_data ){
91 response_data = 'The server responds with an empty string. The server probably stopped working unexpectedly. <br>Please check your <strong>error.log</strong> in your server configuration for relative errors.';
92 message_type = 'warning';
93 }
94
95 // Show Message
96 wpbc_front_end__show_message( response_data , { 'type' : message_type,
97 'show_here': {'jq_node': jq_node, 'where': 'after'},
98 'is_append': true,
99 'style' : 'text-align:left;',
100 'delay' : 0
101 } );
102 return;
103 }
104
105 // Show Calendar
106 wpbc_calendar__loading__stop( response_data[ 'resource_id' ] );
107
108 // -------------------------------------------------------------------------------------------------
109 // Bookings - Dates
110 _wpbc.bookings_in_calendar__set_dates( response_data[ 'resource_id' ], response_data[ 'ajx_data' ]['dates'] );
111
112 // Bookings - Child or only single booking resource in dates
113 _wpbc.booking__set_param_value( response_data[ 'resource_id' ], 'resources_id_arr__in_dates', response_data[ 'ajx_data' ][ 'resources_id_arr__in_dates' ] );
114
115 // Aggregate booking resources, if any ?
116 _wpbc.booking__set_param_value( response_data[ 'resource_id' ], 'aggregate_resource_id_arr', response_data[ 'ajx_data' ][ 'aggregate_resource_id_arr' ] );
117 // -------------------------------------------------------------------------------------------------
118
119 // Update calendar
120 wpbc_calendar__update_look( response_data[ 'resource_id' ] );
121
122 if ( 'function' === typeof (wpbc_hook__init_timeselector) ) {
123 wpbc_hook__init_timeselector();
124 }
125
126 if (
127 ( 'undefined' !== typeof (response_data[ 'ajx_data' ][ 'ajx_after_action_message' ]) )
128 && ( '' != response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ) )
129 ){
130
131 var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data );
132
133 // Show Message
134 wpbc_front_end__show_message( response_data[ 'ajx_data' ][ 'ajx_after_action_message' ].replace( /\n/g, "<br />" ),
135 { 'type' : ( 'undefined' !== typeof( response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] ) )
136 ? response_data[ 'ajx_data' ][ 'ajx_after_action_message_status' ] : 'info',
137 'show_here': {'jq_node': jq_node, 'where': 'after'},
138 'is_append': true,
139 'style' : 'text-align:left;',
140 'delay' : 10000
141 } );
142 }
143
144 if ( 'function' === typeof (wpbc_update_capacity_hint) ) {
145 wpbc_update_capacity_hint( response_data['resource_id'] );
146 }
147
148 // Trigger event that calendar has been // FixIn: 10.0.0.44. // FixIn: 10.14.17.2.
149 if ( (jQuery( '#calendar_booking' + response_data['resource_id'] ).length > 0) || (jQuery( '#date_booking' + response_data['resource_id'] ).length > 0) ) {
150 var target_elm = jQuery( 'body' ).trigger( "wpbc_calendar_ajx__loaded_data", [response_data[ 'resource_id' ]] );
151 //jQuery( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function( event, resource_id ) { ... } );
152 }
153
154 //jQuery( '#ajax_respond' ).html( response_data ); // For ability to show response, add such DIV element to page
155 }
156 ).fail( function ( jqXHR, textStatus, errorThrown ) { if ( window.console && window.console.log ){ console.log( 'Ajax_Error', jqXHR, textStatus, errorThrown ); }
157
158 var ajx_post_data__resource_id = wpbc_get_resource_id__from_ajx_post_data_url( this.data );
159 wpbc_balancer__completed( ajx_post_data__resource_id , 'wpbc_calendar__load_data__ajx' );
160
161 // Get Content of Error Message
162 var error_message = '<strong>' + 'Error!' + '</strong> ' + errorThrown ;
163 if ( jqXHR.status ){
164 error_message += ' (<b>' + jqXHR.status + '</b>)';
165 if (403 == jqXHR.status ){
166 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>.';
167 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>'
168 }
169 }
170 var message_show_delay = 3000;
171 if ( jqXHR.responseText ){
172 error_message += ' ' + jqXHR.responseText;
173 message_show_delay = 10;
174 }
175 error_message = error_message.replace( /\n/g, "<br />" );
176
177 var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data );
178
179 /**
180 * If we make fast clicking on different pages,
181 * then under calendar will show error message with empty text, because ajax was not received.
182 * To not show such warnings we are set delay in 3 seconds. var message_show_delay = 3000;
183 */
184 var closed_timer = setTimeout( function (){
185
186 // Show Message
187 wpbc_front_end__show_message( error_message , { 'type' : 'error',
188 'show_here': {'jq_node': jq_node, 'where': 'after'},
189 'is_append': true,
190 'style' : 'text-align:left;',
191 'css_class':'wpbc_fe_message_alt',
192 'delay' : 0
193 } );
194 } ,
195 parseInt( message_show_delay ) );
196
197 })
198 // .done( function ( data, textStatus, jqXHR ) { if ( window.console && window.console.log ){ console.log( 'second success', data, textStatus, jqXHR ); } })
199 // .always( function ( data_jqXHR, textStatus, jqXHR_errorThrown ) { if ( window.console && window.console.log ){ console.log( 'always finished', data_jqXHR, textStatus, jqXHR_errorThrown ); } })
200 ; // End Ajax
201 }
202
203
204
205 // ---------------------------------------------------------------------------------------------------------------------
206 // Support
207 // ---------------------------------------------------------------------------------------------------------------------
208
209 /**
210 * Get Calendar jQuery node for showing messages during Ajax
211 * This parameter: calendar_request_params[resource_id] parsed from this.data Ajax post data
212 *
213 * @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'
214 * @returns {string} ''#calendar_booking1' | '.booking_form_div' ...
215 *
216 * Example var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data );
217 */
218 function wpbc_get_calendar__jq_node__for_messages( ajx_post_data_url_params ){
219
220 var jq_node = '.booking_form_div';
221
222 var calendar_resource_id = wpbc_get_resource_id__from_ajx_post_data_url( ajx_post_data_url_params );
223
224 if ( calendar_resource_id > 0 ){
225 jq_node = '#calendar_booking' + calendar_resource_id;
226 }
227
228 return jq_node;
229 }
230
231
232 /**
233 * Get resource ID from ajx post data url usually from this.data =
234 * 'action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params'
235 *
236 * @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'
237 * @returns {int} 1 | 0 (if errror then 0)
238 *
239 * Example var jq_node = wpbc_get_calendar__jq_node__for_messages( this.data );
240 */
241 function wpbc_get_resource_id__from_ajx_post_data_url( ajx_post_data_url_params ){
242
243 // 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'
244 var calendar_resource_id = wpbc_get_uri_param_by_name( 'calendar_request_params[resource_id]', ajx_post_data_url_params );
245 if ( (null !== calendar_resource_id) && ('' !== calendar_resource_id) ){
246 calendar_resource_id = parseInt( calendar_resource_id );
247 if ( calendar_resource_id > 0 ){
248 return calendar_resource_id;
249 }
250 }
251 return 0;
252 }
253
254
255 /**
256 * Get parameter from URL - parse URL parameters, like this:
257 * action=WPBC_AJX_CALENDAR_LOAD...&calendar_request_params%5Bresource_id%5D=2&calendar_request_params%5Bbooking_hash%5D=&calendar_request_params
258 * @param name parameter name, like 'calendar_request_params[resource_id]'
259 * @param url 'parameter string URL'
260 * @returns {string|null} parameter value
261 *
262 * Example: wpbc_get_uri_param_by_name( 'calendar_request_params[resource_id]', this.data ); -> '2'
263 */
264 function wpbc_get_uri_param_by_name( name, url ){
265
266 url = decodeURIComponent( url );
267
268 name = name.replace( /[\[\]]/g, '\\$&' );
269 var regex = new RegExp( '[?&]' + name + '(=([^&#]*)|&|#|$)' ),
270 results = regex.exec( url );
271 if ( !results ) return null;
272 if ( !results[ 2 ] ) return '';
273 return decodeURIComponent( results[ 2 ].replace( /\+/g, ' ' ) );
274 }
275