PluginProbe
Booking Calendar / 11.4.2
Booking Calendar v11.4.2
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 / page-bookings / _src / boo_listing__actions.js

boo_listing__actions.js in Booking Calendar 11.4.2, at includes/page-bookings/_src/boo_listing__actions.js

1,387 lines 51.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Booking Actions in Booking Listing.
3 *
4 * @version 1.0
5 * @package Booking Calendar
6 * @author wpdevelop
7 *
8 * @web-site https://wpbookingcalendar.com/
9 * @email [email protected]
10 * @modified 2025-04-08
11 */
12
13 /**
14 * Check if we can open modal.
15 *
16 * @param html_id ID of modal window, e.g.: '#wpbc_modal__payment_status_edit__section'
17 *
18 * @returns {boolean}
19 */
20 function wpbc_is_modal_accessible( html_id ) {
21 if ( 'function' !== typeof (jQuery( html_id ).wpbc_my_modal) ) {
22 alert( 'Warning! wpbc_my_modal module has not found. Please, recheck about any conflicts by deactivating other plugins.' );
23 return false;
24 }
25 return true;
26 }
27
28
29 // ---------------------------------------------------------------------------------------------------------------------
30 // == Actions, while cliking on option dropdown ==
31 // ---------------------------------------------------------------------------------------------------------------------
32
33 /**
34 * Open Add/Edit Booking modal from Booking Listing row values.
35 *
36 * @param {string|number} booking_id Booking ID.
37 * @param {string|number} resource_id Resource ID.
38 * @param {string} booking_hash Booking hash.
39 * @param {string} booking_form Custom booking form name.
40 * @returns {boolean|undefined}
41 */
42 function wpbc_boo_listing__click__add_booking_modal_from_row( booking_id, resource_id, booking_hash, booking_form ){
43
44 if ( ! booking_hash ) {
45 return false;
46 }
47
48 return wpbc_boo_listing__click__add_booking_modal( {
49 mode : 'edit',
50 booking_id : booking_id || '',
51 resource_id : resource_id || '',
52 booking_hash : booking_hash || '',
53 booking_form : booking_form || ''
54 } );
55 }
56
57 /**
58 * Insert AJAX-rendered Add/Edit Booking HTML and run its inline lifecycle scripts once.
59 *
60 * @param {Object} $body Modal body jQuery object.
61 * @param {string} html Rendered component HTML.
62 */
63 function wpbc_boo_listing__set_add_booking_modal_body_html( $body, html ){
64
65 var $html = jQuery( '<div />' ).append( jQuery.parseHTML( html || '', document, true ) );
66 var $scripts = $html.find( 'script' ).remove();
67
68 $body.html( $html.contents() );
69
70 $scripts.each( function(){
71 var type = ( jQuery( this ).attr( 'type' ) || '' ).toLowerCase();
72 var src = jQuery( this ).attr( 'src' );
73 var code = this.text || this.textContent || this.innerHTML || '';
74
75 if ( type && ! /^(text|application)\/(x-)?javascript$/.test( type ) ) {
76 return;
77 }
78
79 if ( src ) {
80 jQuery.ajax( {
81 url : src,
82 dataType : 'script',
83 cache : true,
84 async : false
85 } );
86 return;
87 }
88
89 if ( code ) {
90 jQuery.globalEval( code );
91 }
92 } );
93 }
94
95 /**
96 * Get modal loading spinner HTML.
97 *
98 * @returns {string}
99 */
100 function wpbc_boo_listing__get_add_booking_modal_loading_html(){
101 return '<div class="wpbc_spins_loading_container">'
102 + '<div class="wpbc_booking_form_spin_loader">'
103 + '<div class="wpbc_spins_loader_wrapper">'
104 + '<div class="wpbc_spin_loader_one_new"></div>'
105 + '</div>'
106 + '</div>'
107 + '<span>Loading...</span>'
108 + '</div>';
109 }
110
111 /**
112 * Normalize time option values for comparison.
113 *
114 * @param {string} value Time or time-range value.
115 * @returns {string}
116 */
117 function wpbc_boo_listing__normalize_time_value( value ){
118 return String( value || '' ).replace( /\s+/g, '' ).toLowerCase();
119 }
120
121 function wpbc_boo_listing__trim_text( value ){
122 return String( value || '' ).replace( /^\s+|\s+$/g, '' );
123 }
124
125 /**
126 * Parse "HH:MM - HH:MM" selected time into start/end values.
127 *
128 * @param {string} selected_time Time range.
129 * @returns {{start_time:string,end_time:string}}
130 */
131 function wpbc_boo_listing__parse_selected_time_range( selected_time ){
132
133 var time_parts = String( selected_time || '' ).split( ' - ' );
134
135 return {
136 start_time: wpbc_boo_listing__trim_text( time_parts[0] ),
137 end_time: wpbc_boo_listing__trim_text( time_parts[1] )
138 };
139 }
140
141 function wpbc_boo_listing__parse_selected_dates( selected_dates ){
142
143 var dates = [];
144 var seen = {};
145
146 if ( Array.isArray( selected_dates ) ) {
147 dates = selected_dates;
148 } else {
149 dates = String( selected_dates || '' ).split( ',' );
150 }
151
152 dates = jQuery.map( dates, function( date ){
153 date = wpbc_boo_listing__trim_text( date );
154 if ( ! date || seen[ date ] ) {
155 return null;
156 }
157 seen[ date ] = true;
158 return date;
159 } );
160
161 return dates;
162 }
163
164 function wpbc_boo_listing__set_modal_days_select_mode_override( resource_id, is_forced ){
165 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
166 var originalValue;
167
168 if (
169 ! is_forced
170 || ! resource_id
171 || 'undefined' === typeof _wpbc
172 || 'function' !== typeof _wpbc.calendar__get_param_value
173 || 'function' !== typeof _wpbc.calendar__set_param_value
174 ) {
175 return;
176 }
177
178 if ( 'undefined' === typeof $modal.attr( 'data-wpbc-add-booking-original-days-select-mode' ) ) {
179 originalValue = _wpbc.calendar__get_param_value( resource_id, 'days_select_mode' );
180 $modal.attr( 'data-wpbc-add-booking-original-days-select-mode', String( originalValue || '' ) );
181 $modal.attr( 'data-wpbc-add-booking-original-days-select-resource-id', String( resource_id ) );
182 }
183
184 $modal.off( 'hidden.wpbc.modal.wpbc_add_booking_days_select_mode hidden.bs.modal.wpbc_add_booking_days_select_mode' ).on(
185 'hidden.wpbc.modal.wpbc_add_booking_days_select_mode hidden.bs.modal.wpbc_add_booking_days_select_mode',
186 function(){
187 var restoreResourceId = parseInt( $modal.attr( 'data-wpbc-add-booking-original-days-select-resource-id' ) || 0, 10 );
188 var restoreValue = $modal.attr( 'data-wpbc-add-booking-original-days-select-mode' );
189
190 if (
191 restoreResourceId
192 && 'undefined' !== typeof restoreValue
193 && 'undefined' !== typeof _wpbc
194 && 'function' === typeof _wpbc.calendar__set_param_value
195 ) {
196 _wpbc.calendar__set_param_value( restoreResourceId, 'days_select_mode', restoreValue || '' );
197 }
198
199 $modal
200 .removeAttr( 'data-wpbc-add-booking-original-days-select-mode' )
201 .removeAttr( 'data-wpbc-add-booking-original-days-select-resource-id' );
202 }
203 );
204 }
205
206 function wpbc_boo_listing__ymd_to_ddmmyyyy( ymd_date ){
207 var dateParts = String( ymd_date || '' ).split( '-' );
208
209 if ( 3 !== dateParts.length ) {
210 return '';
211 }
212
213 return dateParts[2] + '.' + dateParts[1] + '.' + dateParts[0];
214 }
215
216 function wpbc_boo_listing__sync_selected_dates_to_booking_form( resource_id, selected_dates ){
217 var formatted_dates = jQuery.map( selected_dates, function( date ){
218 return wpbc_boo_listing__ymd_to_ddmmyyyy( date );
219 } );
220 var dateValue = formatted_dates.join( ', ' );
221
222 jQuery( '#date_booking' + resource_id ).val( dateValue );
223
224 if ( 'function' === typeof wpbc_disable_time_fields_in_booking_form ) {
225 wpbc_disable_time_fields_in_booking_form( resource_id );
226 }
227
228 jQuery( '.booking_form_div' ).trigger( 'date_selected', [ resource_id, dateValue, selected_dates ] );
229 }
230
231 function wpbc_boo_listing__auto_select_dates_in_calendar( resource_id, selected_dates, is_force_multiple ){
232 var attempts = 0;
233 var maxAttempts = 10;
234 var eventName = 'wpbc_calendar_ajx__loaded_data.wpbc_add_booking_modal_date';
235 var applySelection;
236
237 if (
238 ! selected_dates.length
239 || ( 'function' !== typeof wpbc_auto_select_dates_in_calendar )
240 ) {
241 return;
242 }
243
244 applySelection = function(){
245 var selectedCount;
246
247 attempts++;
248
249 if (
250 is_force_multiple
251 && ( 'undefined' !== typeof _wpbc )
252 && ( 'function' === typeof _wpbc.calendar__get_param_value )
253 && ( 'multiple' !== _wpbc.calendar__get_param_value( resource_id, 'days_select_mode' ) )
254 ) {
255 wpbc_boo_listing__set_modal_days_select_mode_override( resource_id, true );
256 if ( 'function' === typeof _wpbc.calendar__set_param_value ) {
257 _wpbc.calendar__set_param_value( resource_id, 'days_select_mode', 'multiple' );
258 }
259 }
260
261 selectedCount = wpbc_auto_select_dates_in_calendar( resource_id, selected_dates );
262 if ( selectedCount >= selected_dates.length ) {
263 wpbc_boo_listing__sync_selected_dates_to_booking_form( resource_id, selected_dates );
264 jQuery( 'body' ).off( eventName );
265 return true;
266 }
267
268 return false;
269 };
270
271 jQuery( 'body' ).off( eventName ).on( eventName, function( event, loaded_resource_id ){
272 if ( parseInt( loaded_resource_id, 10 ) === resource_id ) {
273 applySelection();
274 }
275 } );
276
277 window.setTimeout( function retrySelection(){
278 if ( applySelection() ) {
279 return;
280 }
281 if ( attempts < maxAttempts ) {
282 window.setTimeout( retrySelection, 300 );
283 }
284 }, 300 );
285 }
286
287 function wpbc_boo_listing__should_force_recurrent_time( data, selected_dates ){
288 return (
289 data
290 && ( selected_dates || [] ).length > 1
291 && !! parseInt( data.time_override_enabled || 0, 10 )
292 && 'times_availability' === String( data.time_override_source || '' )
293 );
294 }
295
296 function wpbc_boo_listing__set_modal_recurrent_time_override( resource_id, is_forced ){
297 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
298 var originalValue;
299
300 $modal.attr( 'data-wpbc-add-booking-force-recurrent-time', is_forced ? '1' : '0' );
301
302 if (
303 ! is_forced
304 || ! resource_id
305 || 'undefined' === typeof _wpbc
306 || 'function' !== typeof _wpbc.calendar__get_param_value
307 || 'function' !== typeof _wpbc.calendar__set_param_value
308 ) {
309 return;
310 }
311
312 if ( 'undefined' === typeof $modal.attr( 'data-wpbc-add-booking-original-recurrent-time' ) ) {
313 originalValue = _wpbc.calendar__get_param_value( resource_id, 'booking_recurrent_time' );
314 $modal.attr( 'data-wpbc-add-booking-original-recurrent-time', String( originalValue || '' ) );
315 $modal.attr( 'data-wpbc-add-booking-original-recurrent-resource-id', String( resource_id ) );
316 }
317
318 _wpbc.calendar__set_param_value( resource_id, 'booking_recurrent_time', 'On' );
319
320 $modal.off( 'hidden.wpbc.modal.wpbc_add_booking_recurrent_time hidden.bs.modal.wpbc_add_booking_recurrent_time' ).on(
321 'hidden.wpbc.modal.wpbc_add_booking_recurrent_time hidden.bs.modal.wpbc_add_booking_recurrent_time',
322 function(){
323 var restoreResourceId = parseInt( $modal.attr( 'data-wpbc-add-booking-original-recurrent-resource-id' ) || 0, 10 );
324 var restoreValue = $modal.attr( 'data-wpbc-add-booking-original-recurrent-time' );
325
326 if (
327 restoreResourceId
328 && 'undefined' !== typeof restoreValue
329 && 'undefined' !== typeof _wpbc
330 && 'function' === typeof _wpbc.calendar__set_param_value
331 ) {
332 _wpbc.calendar__set_param_value( restoreResourceId, 'booking_recurrent_time', restoreValue || 'Off' );
333 }
334
335 $modal
336 .removeAttr( 'data-wpbc-add-booking-original-recurrent-time' )
337 .removeAttr( 'data-wpbc-add-booking-original-recurrent-resource-id' )
338 .attr( 'data-wpbc-add-booking-force-recurrent-time', '0' );
339 }
340 );
341 }
342
343 /**
344 * Get booking form time fields that can conflict with a timeline override.
345 *
346 * @param {Object} $form Booking form jQuery object.
347 * @param {number} resource_id Booking resource ID.
348 * @returns {Object} jQuery collection.
349 */
350 function wpbc_boo_listing__get_add_booking_modal_time_fields( $form, resource_id ){
351
352 var selector = [
353 'select[name="rangetime' + resource_id + '"]',
354 'select[name="rangetime' + resource_id + '[]"]',
355 'select[name="starttime' + resource_id + '"]',
356 'select[name="starttime' + resource_id + '[]"]',
357 'select[name="endtime' + resource_id + '"]',
358 'select[name="endtime' + resource_id + '[]"]',
359 'select[name="durationtime' + resource_id + '"]',
360 'select[name="durationtime' + resource_id + '[]"]',
361 'input[name="starttime' + resource_id + '"]',
362 'input[name="endtime' + resource_id + '"]'
363 ].join( ', ' );
364
365 return $form.find( selector ).filter( function(){
366 var $field = jQuery( this );
367
368 if ( $field.closest( '.wpbc_add_booking_modal__selected_time_fields' ).length ) {
369 return false;
370 }
371
372 if ( 'input' === this.tagName.toLowerCase() && 'hidden' === String( $field.attr( 'type' ) || '' ).toLowerCase() ) {
373 return false;
374 }
375
376 return true;
377 } );
378 }
379
380 /**
381 * Select matching option in a time select without forcing disabled choices.
382 *
383 * @param {Object} $select Select element.
384 * @param {Array} expected_values Acceptable values.
385 * @returns {boolean}
386 */
387 function wpbc_boo_listing__select_time_option( $select, expected_values ){
388
389 var did_select = false;
390 var normalized_expected = [];
391
392 jQuery.each( expected_values, function( index, value ){
393 normalized_expected.push( wpbc_boo_listing__normalize_time_value( value ) );
394 } );
395
396 $select.find( 'option' ).each( function(){
397 var $option = jQuery( this );
398 var option_value = wpbc_boo_listing__normalize_time_value( $option.val() );
399
400 if ( -1 === jQuery.inArray( option_value, normalized_expected ) || $option.prop( 'disabled' ) ) {
401 return true;
402 }
403
404 if ( $select.prop( 'multiple' ) ) {
405 $option.prop( 'selected', true );
406 } else {
407 $select.val( $option.val() );
408 }
409
410 $select.trigger( 'change' );
411 did_select = true;
412 return false;
413 } );
414
415 return did_select;
416 }
417
418 /**
419 * Check whether the rendered booking form already has user-facing time fields.
420 *
421 * @param {Object} $form Booking form jQuery object.
422 * @param {number} resource_id Booking resource ID.
423 * @returns {boolean}
424 */
425 function wpbc_boo_listing__has_add_booking_modal_time_fields( $form, resource_id ){
426
427 return wpbc_boo_listing__get_add_booking_modal_time_fields( $form, resource_id ).length > 0;
428 }
429
430 /**
431 * Add visible read-only start/end time fields when the selected booking form has no time controls.
432 *
433 * @param {Object} $form Booking form jQuery object.
434 * @param {number} resource_id Booking resource ID.
435 * @param {string} start_time Start time in 24h format.
436 * @param {string} end_time End time in 24h format.
437 * @returns {boolean}
438 */
439 function wpbc_boo_listing__ensure_add_booking_modal_selected_time_fields( $form, resource_id, start_time, end_time ){
440
441 var $wrap = $form.find( '.wpbc_add_booking_modal__selected_time_fields' );
442 var html;
443 var $insert_before;
444
445 if ( ! start_time || ! end_time ) {
446 return false;
447 }
448
449 if ( ! $wrap.length ) {
450 html = '<div class="wpbc_add_booking_modal__selected_time_fields" style="margin:12px 0;padding:12px;border:1px solid #dcdcde;background:#f6f7f7;border-radius:4px;">'
451 + '<div style="font-weight:600;margin-bottom:8px;">Selected timeline interval</div>'
452 + '<div style="display:flex;gap:10px;flex-wrap:wrap;align-items:flex-end;">'
453 + '<label style="display:flex;flex-direction:column;gap:4px;min-width:120px;">'
454 + '<span>Start time</span>'
455 + '<input type="text" class="wpbc_ui_control wpbc_ui_text" name="starttime' + resource_id + '" value="" readonly="readonly" />'
456 + '</label>'
457 + '<label style="display:flex;flex-direction:column;gap:4px;min-width:120px;">'
458 + '<span>End time</span>'
459 + '<input type="text" class="wpbc_ui_control wpbc_ui_text" name="endtime' + resource_id + '" value="" readonly="readonly" />'
460 + '</label>'
461 + '</div>'
462 + '</div>';
463
464 $wrap = jQuery( html );
465 $insert_before = $form.find( '#bk_type' + resource_id ).first();
466
467 if ( $insert_before.length ) {
468 $insert_before.before( $wrap );
469 } else {
470 $form.find( '#booking_form_div' + resource_id ).append( $wrap );
471 }
472 }
473
474 $wrap.find( 'input[name="starttime' + resource_id + '"]' ).val( start_time ).trigger( 'input' ).trigger( 'change' );
475 $wrap.find( 'input[name="endtime' + resource_id + '"]' ).val( end_time ).trigger( 'input' ).trigger( 'change' );
476
477 return true;
478 }
479
480 /**
481 * Add/update the explicit timeline interval override panel.
482 *
483 * @param {Object} $form Booking form jQuery object.
484 * @param {number} resource_id Booking resource ID.
485 * @param {Object} data Modal context.
486 * @returns {boolean}
487 */
488 function wpbc_boo_listing__ensure_add_booking_modal_time_override_panel( $form, resource_id, data ){
489
490 var selected_time = ( data && data.selected_time ) ? data.selected_time : '';
491 var parsed_time = wpbc_boo_listing__parse_selected_time_range( selected_time );
492 var start_time = ( data && data.time_override_start ) ? data.time_override_start : parsed_time.start_time;
493 var end_time = ( data && data.time_override_end ) ? data.time_override_end : parsed_time.end_time;
494 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
495 var $footer_slot = $modal.find( '[data-wpbc-add-booking-time-override-footer]' ).first();
496 var toggle_id = 'wpbc_modal__add_booking__time_override_enabled';
497 var $wrap = $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).first();
498 var html;
499
500 if ( ! start_time || ! end_time ) {
501 return false;
502 }
503
504 if ( ! $wrap.length ) {
505 html = '<div class="wpbc_add_booking_modal__selected_time_fields wpbc_add_booking_modal__time_override" data-wpbc-add-booking-time-override-panel="1">'
506 + '<span class="wpbc_ui__toggle wpbc_add_booking_modal__time_override_toggle">'
507 + '<input type="checkbox" id="' + toggle_id + '" value="1" class="wpbc_ui_checkbox" data-wpbc-add-booking-time-override-enabled="1" data-wpbc-booking-submit-ignore="1" checked="checked" autocomplete="off" />'
508 + '<label class="wpbc_ui__toggle_icon tooltip_top" for="' + toggle_id + '" data-original-title="Use selected timeline interval"></label>'
509 + '<label for="' + toggle_id + '" class="wpbc_ui_control_label wpbc_ui__toggle_label">Use selected timeline interval</label>'
510 + '<i class="wpbc_help_tooltip"></i>'
511 + '</span>'
512 + '<div class="wpbc_add_booking_modal__time_override_fields">'
513 + '<label><span>Start time</span><input type="text" class="wpbc_ui_control wpbc_ui_text" name="starttime' + resource_id + '" value="" readonly="readonly" data-wpbc-add-booking-time-override-field="start" /></label>'
514 + '<label><span>End time</span><input type="text" class="wpbc_ui_control wpbc_ui_text" name="endtime' + resource_id + '" value="" readonly="readonly" data-wpbc-add-booking-time-override-field="end" /></label>'
515 + '</div>'
516 + '<div class="wpbc_add_booking_modal__time_override_note">Form time fields are ignored while enabled.</div>'
517 + '</div>';
518
519 $wrap = jQuery( html );
520
521 if ( $footer_slot.length ) {
522 $footer_slot.html( $wrap );
523 } else {
524 $modal.find( '.modal-footer' ).prepend( $wrap );
525 }
526 }
527
528 $wrap.attr( 'data-wpbc-add-booking-time-override-source', ( data && data.time_override_source ) ? data.time_override_source : '' );
529 $wrap.find( '[data-wpbc-add-booking-time-override-field="start"]' ).attr( 'name', 'starttime' + resource_id ).val( start_time ).trigger( 'input' ).trigger( 'change' );
530 $wrap.find( '[data-wpbc-add-booking-time-override-field="end"]' ).attr( 'name', 'endtime' + resource_id ).val( end_time ).trigger( 'input' ).trigger( 'change' );
531 $wrap.find( '[data-wpbc-add-booking-time-override-enabled]' ).prop( 'checked', ! data || ( '0' !== String( data.time_override_enabled || '1' ) ) );
532
533 wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id );
534
535 return true;
536 }
537
538 /**
539 * Enable/disable the timeline interval override and mark conflicting form fields.
540 *
541 * @param {Object} $form Booking form jQuery object.
542 * @param {number} resource_id Booking resource ID.
543 * @returns {boolean}
544 */
545 function wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id ){
546
547 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
548 var $wrap = $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).first();
549 var $enabled = $wrap.find( '[data-wpbc-add-booking-time-override-enabled]' ).first();
550 var is_enabled = $enabled.length ? $enabled.is( ':checked' ) : false;
551 var $override_fields = $wrap.find( '[data-wpbc-add-booking-time-override-field]' );
552 var $form_time_fields = wpbc_boo_listing__get_add_booking_modal_time_fields( $form, resource_id );
553
554 if ( ! $wrap.length ) {
555 return false;
556 }
557
558 $wrap.toggleClass( 'is-enabled', is_enabled );
559 $override_fields.attr( 'data-wpbc-booking-submit-ignore', is_enabled ? '0' : '1' );
560
561 $form_time_fields.each( function(){
562 var $field = jQuery( this );
563
564 if ( is_enabled ) {
565 if ( 'undefined' === typeof $field.attr( 'data-wpbc-add-booking-time-override-original-disabled' ) ) {
566 $field.attr( 'data-wpbc-add-booking-time-override-original-disabled', $field.prop( 'disabled' ) ? '1' : '0' );
567 }
568 $field
569 .attr( 'data-wpbc-booking-submit-ignore', '1' )
570 .prop( 'disabled', true )
571 .addClass( 'wpbc_add_booking_modal__time_field_overridden' );
572 } else {
573 $field
574 .removeAttr( 'data-wpbc-booking-submit-ignore' )
575 .prop( 'disabled', '1' === $field.attr( 'data-wpbc-add-booking-time-override-original-disabled' ) )
576 .removeAttr( 'data-wpbc-add-booking-time-override-original-disabled' )
577 .removeClass( 'wpbc_add_booking_modal__time_field_overridden' );
578 }
579 } );
580
581 return is_enabled;
582 }
583
584 /**
585 * Apply a preselected time range to the rendered Add Booking form.
586 *
587 * @param {number} resource_id Booking resource ID.
588 * @param {string} selected_time Time range, e.g. "09:00 - 11:00".
589 * @returns {boolean}
590 */
591 function wpbc_boo_listing__apply_add_booking_modal_selected_time( resource_id, selected_time ){
592
593 var $form = jQuery( '#booking_form' + resource_id );
594 var time_parts;
595 var start_time;
596 var end_time;
597 var did_select = false;
598 var has_time_fields;
599
600 if ( ! $form.length || ! selected_time ) {
601 return false;
602 }
603
604 time_parts = wpbc_boo_listing__parse_selected_time_range( selected_time );
605 start_time = time_parts.start_time;
606 end_time = time_parts.end_time;
607 has_time_fields = wpbc_boo_listing__has_add_booking_modal_time_fields( $form, resource_id );
608
609 if ( ! has_time_fields ) {
610 return wpbc_boo_listing__ensure_add_booking_modal_selected_time_fields( $form, resource_id, start_time, end_time );
611 }
612
613 $form.find( 'select[name="rangetime' + resource_id + '"], select[name="rangetime' + resource_id + '[]"]' ).each( function(){
614 did_select = wpbc_boo_listing__select_time_option( jQuery( this ), [ selected_time ] ) || did_select;
615 } );
616
617 if ( start_time ) {
618 $form.find( 'select[name="starttime' + resource_id + '"], select[name="starttime' + resource_id + '[]"]' ).each( function(){
619 did_select = wpbc_boo_listing__select_time_option( jQuery( this ), [ start_time ] ) || did_select;
620 } );
621 if ( $form.find( 'input[name="starttime' + resource_id + '"]' ).not( '[type="hidden"]' ).val( start_time ).trigger( 'input' ).trigger( 'change' ).length ) {
622 did_select = true;
623 }
624 }
625
626 if ( end_time ) {
627 $form.find( 'select[name="endtime' + resource_id + '"], select[name="endtime' + resource_id + '[]"]' ).each( function(){
628 did_select = wpbc_boo_listing__select_time_option( jQuery( this ), [ end_time ] ) || did_select;
629 } );
630 if ( $form.find( 'input[name="endtime' + resource_id + '"]' ).not( '[type="hidden"]' ).val( end_time ).trigger( 'input' ).trigger( 'change' ).length ) {
631 did_select = true;
632 }
633 }
634
635 return did_select;
636 }
637
638 /**
639 * Apply Add Booking modal date/time context after AJAX-rendered form lifecycle scripts run.
640 *
641 * @param {Object} data AJAX response data.
642 */
643 function wpbc_boo_listing__preload_add_booking_modal_selection( data ){
644
645 data = data || {};
646
647 var resource_id = parseInt( data.resource_id, 10 );
648 var selected_date = data.selected_date || '';
649 var selected_dates = wpbc_boo_listing__parse_selected_dates( data.selected_dates || selected_date );
650 var selected_time = data.selected_time || '';
651 var selected_dates_without_calendar = data.selected_dates_without_calendar || '';
652 var is_time_override = !! parseInt( data.time_override_enabled || 0, 10 );
653 var is_force_recurrent_time = wpbc_boo_listing__should_force_recurrent_time( data, selected_dates );
654 var apply_time;
655
656 if ( ! resource_id ) {
657 return;
658 }
659
660 wpbc_boo_listing__set_modal_recurrent_time_override( resource_id, is_force_recurrent_time );
661
662 if ( ! is_time_override ) {
663 jQuery( '#wpbc_modal__add_booking__section' ).find( '[data-wpbc-add-booking-time-override-panel]' ).remove();
664 }
665
666 if (
667 selected_dates.length
668 && ! selected_dates_without_calendar
669 ) {
670 wpbc_boo_listing__auto_select_dates_in_calendar( resource_id, selected_dates, is_force_recurrent_time );
671 }
672
673 if ( ! selected_time ) {
674 return;
675 }
676
677 apply_time = function(){
678 var is_calendar_data_loaded = (
679 ! selected_date
680 || ( 'undefined' === typeof _wpbc )
681 || ( 'function' !== typeof _wpbc.bookings_in_calendar__get_for_date )
682 || ( false !== _wpbc.bookings_in_calendar__get_for_date( resource_id, selected_date ) )
683 );
684
685 if ( is_calendar_data_loaded && ( 'function' === typeof wpbc_disable_time_fields_in_booking_form ) ) {
686 wpbc_disable_time_fields_in_booking_form( resource_id );
687 }
688 if ( is_time_override ) {
689 wpbc_boo_listing__ensure_add_booking_modal_time_override_panel( jQuery( '#booking_form' + resource_id ), resource_id, data );
690 return;
691 }
692 wpbc_boo_listing__apply_add_booking_modal_selected_time( resource_id, selected_time );
693 };
694
695 jQuery( '.booking_form_div' ).off( 'wpbc_hook_timeslots_disabled.wpbc_add_booking_modal_time' ).one( 'wpbc_hook_timeslots_disabled.wpbc_add_booking_modal_time', function( event, loaded_resource_id ){
696 if ( parseInt( loaded_resource_id, 10 ) === resource_id ) {
697 window.setTimeout( apply_time, 0 );
698 }
699 } );
700
701 jQuery( 'body' ).off( 'wpbc_calendar_ajx__loaded_data.wpbc_add_booking_modal_time' ).one( 'wpbc_calendar_ajx__loaded_data.wpbc_add_booking_modal_time', function( event, loaded_resource_id ){
702 if ( parseInt( loaded_resource_id, 10 ) === resource_id ) {
703 window.setTimeout( apply_time, 80 );
704 }
705 } );
706
707 window.setTimeout( apply_time, 350 );
708 window.setTimeout( apply_time, 1000 );
709 }
710
711 /**
712 * Refresh modal footer controls after Add/Edit Booking context changes.
713 *
714 * @param {Object} $modal Modal jQuery object.
715 * @param {Object} data AJAX response data.
716 * @param {string} mode Current modal mode.
717 */
718 function wpbc_boo_listing__sync_add_booking_modal_controls( $modal, data, mode ){
719
720 data = data || {};
721
722 $modal.attr( 'data-wpbc-add-booking-mode', mode || data.mode || 'add' );
723
724 var $resource_control = $modal.find( '.wpbc_modal__add_booking__resource_control' );
725 var $resource_select = $modal.find( '#wpbc_modal__add_booking__resource_id' );
726 var $form_select = $modal.find( '#wpbc_modal__add_booking__booking_form' );
727 var $form_edit_link = $modal.find( '.wpbc_modal__add_booking__edit_form_link' );
728 var $allow_past_control = $modal.find( '.wpbc_modal__add_booking__allow_past_control' );
729 var $allow_past_toggle = $modal.find( '[data-wpbc-add-booking-allow-past]' ).first();
730 var current_mode = mode || data.mode || 'add';
731
732 if ( 'edit' === current_mode ) {
733 $resource_control.hide();
734 $allow_past_control.hide();
735 } else {
736 $resource_control.show();
737 $allow_past_control.show();
738 }
739
740 if ( data.resource_id && $resource_select.length ) {
741 $resource_select.val( String( data.resource_id ) );
742 }
743
744 if ( $form_select.length ) {
745 var booking_form = data.booking_form || 'standard';
746 if ( booking_form && ! $form_select.find( 'option' ).filter( function(){
747 return jQuery( this ).val() === booking_form;
748 } ).length ) {
749 $form_select.append( jQuery( '<option />' ).val( booking_form ).text( booking_form ) );
750 }
751 $form_select.val( booking_form );
752 }
753
754 if ( $form_edit_link.length ) {
755 wpbc_boo_listing__sync_add_booking_modal_form_edit_link( $modal );
756 }
757
758 if ( $allow_past_toggle.length ) {
759 $allow_past_toggle.prop( 'checked', !! parseInt( data.allow_past || 0, 10 ) );
760 }
761 }
762
763 /**
764 * Sync Forms Builder edit link with selected custom booking form.
765 *
766 * @param {Object} $modal Modal jQuery object.
767 */
768 function wpbc_boo_listing__sync_add_booking_modal_form_edit_link( $modal ){
769
770 var $form_select = $modal.find( '#wpbc_modal__add_booking__booking_form' );
771 var $form_edit_link = $modal.find( '.wpbc_modal__add_booking__edit_form_link' );
772
773 if ( ! $form_select.length || ! $form_edit_link.length ) {
774 return;
775 }
776
777 var form_name = $form_select.val() || 'standard';
778 var base_url = $form_edit_link.attr( 'data-wpbc-add-booking-form-builder-url' ) || $form_edit_link.attr( 'href' ) || '';
779
780 if ( ! base_url ) {
781 return;
782 }
783
784 var separator = ( -1 === base_url.indexOf( '?' ) ) ? '?' : '&';
785 $form_edit_link.attr( 'href', base_url + separator + 'form_name=' + encodeURIComponent( form_name ) );
786 }
787
788 /**
789 * Init modal footer controls.
790 */
791 function wpbc_boo_listing__init_add_booking_modal_controls(){
792
793 jQuery( document ).off( 'change.wpbc_add_booking_modal', '#wpbc_modal__add_booking__resource_id, #wpbc_modal__add_booking__booking_form, #wpbc_modal__add_booking__allow_past' ).on(
794 'change.wpbc_add_booking_modal',
795 '#wpbc_modal__add_booking__resource_id, #wpbc_modal__add_booking__booking_form, #wpbc_modal__add_booking__allow_past',
796 function(){
797 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
798 var mode = $modal.attr( 'data-wpbc-add-booking-mode' ) || 'add';
799 var changed_id = jQuery( this ).attr( 'id' ) || '';
800 var booking_form = $modal.find( '#wpbc_modal__add_booking__booking_form' ).val() || '';
801
802 if ( 'wpbc_modal__add_booking__resource_id' === changed_id ) {
803 booking_form = '';
804 }
805
806 wpbc_boo_listing__click__add_booking_modal( {
807 mode : mode,
808 booking_id : $modal.attr( 'data-wpbc-add-booking-id' ) || '',
809 resource_id : $modal.find( '#wpbc_modal__add_booking__resource_id' ).val() || '',
810 booking_hash : $modal.attr( 'data-wpbc-add-booking-hash' ) || '',
811 booking_form : booking_form,
812 allow_past : $modal.find( '[data-wpbc-add-booking-allow-past]' ).first().is( ':checked' ) ? 1 : 0,
813 selected_dates_without_calendar : $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar' ) || '',
814 selected_dates : $modal.attr( 'data-wpbc-add-booking-selected-dates' ) || '',
815 selected_date : $modal.attr( 'data-wpbc-add-booking-selected-date' ) || '',
816 selected_time : $modal.attr( 'data-wpbc-add-booking-selected-time' ) || '',
817 time_override_enabled : $modal.find( '[data-wpbc-add-booking-time-override-enabled]' ).first().length
818 ? ( $modal.find( '[data-wpbc-add-booking-time-override-enabled]' ).first().is( ':checked' ) ? 1 : 0 )
819 : ( $modal.attr( 'data-wpbc-add-booking-time-override-enabled' ) || 0 ),
820 time_override_source : $modal.attr( 'data-wpbc-add-booking-time-override-source' ) || '',
821 time_override_start : $modal.attr( 'data-wpbc-add-booking-time-override-start' ) || '',
822 time_override_end : $modal.attr( 'data-wpbc-add-booking-time-override-end' ) || ''
823 } );
824 }
825 );
826
827 jQuery( document ).off( 'change.wpbc_add_booking_modal_time_override', '[data-wpbc-add-booking-time-override-enabled]' ).on(
828 'change.wpbc_add_booking_modal_time_override',
829 '[data-wpbc-add-booking-time-override-enabled]',
830 function(){
831 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
832 var resource_id = parseInt( $modal.attr( 'data-wpbc-add-booking-resource-id' ) || 0, 10 );
833 var $form = jQuery( '#booking_form' + resource_id );
834
835 if ( resource_id && $form.length ) {
836 wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id );
837 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', jQuery( this ).is( ':checked' ) ? '1' : '0' );
838 }
839 }
840 );
841 }
842 jQuery( document ).ready( function(){
843 wpbc_boo_listing__init_add_booking_modal_controls();
844 } );
845
846 /**
847 * Prepare selected timeline interval before Add Booking modal submit.
848 *
849 * @param {number} resource_id Booking resource ID.
850 * @returns {boolean}
851 */
852 function wpbc_boo_listing__prepare_add_booking_modal_time_override( resource_id ){
853
854 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
855 var $form = jQuery( '#booking_form' + resource_id );
856 var $wrap = $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).first();
857 var $enabled = $wrap.find( '[data-wpbc-add-booking-time-override-enabled]' ).first();
858 var $start;
859 var $end;
860
861 if ( ! $modal.is( ':visible' ) || ! $wrap.length || ! $enabled.is( ':checked' ) ) {
862 return true;
863 }
864
865 wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id );
866
867 $start = $wrap.find( '[data-wpbc-add-booking-time-override-field="start"]' ).first();
868 $end = $wrap.find( '[data-wpbc-add-booking-time-override-field="end"]' ).first();
869
870 if ( ! $start.val() || ! $end.val() ) {
871 if ( 'function' === typeof wpbc_front_end__show_message__warning ) {
872 wpbc_front_end__show_message__warning( $wrap.get( 0 ), 'Selected timeline interval is not complete.' );
873 }
874 return false;
875 }
876
877 $modal
878 .attr( 'data-wpbc-add-booking-time-override-enabled', '1' )
879 .attr( 'data-wpbc-add-booking-time-override-start', $start.val() )
880 .attr( 'data-wpbc-add-booking-time-override-end', $end.val() );
881
882 return true;
883 }
884
885 /**
886 * Open Add/Edit Booking modal.
887 *
888 * @param {Object} args Modal context.
889 * @returns {boolean|undefined}
890 */
891 function wpbc_boo_listing__click__add_booking_modal( args ){
892
893 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__add_booking__section' ) ) {
894 return false;
895 }
896
897 args = args || {};
898
899 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
900 var $body = jQuery( '#wpbc_modal__add_booking__body' );
901 var nonce = $modal.attr( 'data-wpbc-add-booking-nonce' );
902 var mode = args.mode || ( args.booking_hash ? 'edit' : 'add' );
903 var title = ( 'edit' === mode ) ? 'Edit booking' : 'Add booking';
904 var allow_past = args.allow_past ? 1 : 0;
905
906 if ( ! allow_past && $modal.find( '[data-wpbc-add-booking-allow-past]' ).first().is( ':checked' ) ) {
907 allow_past = 1;
908 }
909
910 if ( 'edit' === mode ) {
911 allow_past = 1;
912 }
913
914 $modal.attr( 'data-wpbc-add-booking-resource-id', '' );
915 $modal.attr( 'data-wpbc-add-booking-hash', args.booking_hash || '' );
916 $modal.attr( 'data-wpbc-add-booking-id', args.booking_id || '' );
917 $modal.attr( 'data-wpbc-add-booking-mode', mode );
918 $modal.attr( 'data-wpbc-add-booking-allow-past', allow_past ? '1' : '0' );
919 $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar', args.selected_dates_without_calendar || '' );
920 $modal.attr( 'data-wpbc-add-booking-selected-dates', args.selected_dates || args.selected_date || '' );
921 $modal.attr( 'data-wpbc-add-booking-selected-date', args.selected_date || '' );
922 $modal.attr( 'data-wpbc-add-booking-selected-time', args.selected_time || '' );
923 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', args.time_override_enabled ? '1' : '0' );
924 $modal.attr( 'data-wpbc-add-booking-time-override-source', args.time_override_source || '' );
925 $modal.attr( 'data-wpbc-add-booking-time-override-start', args.time_override_start || '' );
926 $modal.attr( 'data-wpbc-add-booking-time-override-end', args.time_override_end || '' );
927 $modal.attr( 'data-wpbc-add-booking-force-recurrent-time', wpbc_boo_listing__should_force_recurrent_time( args, wpbc_boo_listing__parse_selected_dates( args.selected_dates || args.selected_date ) ) ? '1' : '0' );
928 if ( ! args.time_override_enabled ) {
929 $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).remove();
930 }
931 args.allow_past = allow_past;
932 wpbc_boo_listing__sync_add_booking_modal_controls( $modal, args, mode );
933 $modal.find( '.wpbc_modal__add_booking__title' ).text( title );
934 $modal.find( '.wpbc_modal__add_booking__booking_id' ).html( args.booking_id ? ( 'ID: ' + args.booking_id ) : '' );
935 $modal.find( '#wpbc_modal__add_booking__button_send' ).text( ( 'edit' === mode ) ? 'Save booking' : 'Add booking' );
936 $body.html( wpbc_boo_listing__get_add_booking_modal_loading_html() );
937
938 $modal.wpbc_my_modal( 'show' );
939
940 jQuery.post(
941 wpbc_url_ajax,
942 {
943 action : 'WPBC_AJX_ADD_BOOKING_MODAL',
944 nonce : nonce,
945 mode : mode,
946 booking_id : args.booking_id || '',
947 resource_id : args.resource_id || '',
948 booking_hash : args.booking_hash || '',
949 booking_form : args.booking_form || '',
950 allow_past : allow_past,
951 selected_dates_without_calendar : args.selected_dates_without_calendar || '',
952 selected_dates : args.selected_dates || args.selected_date || '',
953 selected_date : args.selected_date || '',
954 selected_time : args.selected_time || '',
955 time_override_enabled : args.time_override_enabled ? 1 : 0,
956 time_override_source : args.time_override_source || '',
957 time_override_start : args.time_override_start || '',
958 time_override_end : args.time_override_end || ''
959 },
960 function( response ){
961
962 if ( ! response || ! response.success ) {
963 var message = ( response && response.data && response.data.message ) ? response.data.message : 'Unable to load booking form.';
964 $body.html( '<div class="wpbc-settings-notice notice-warning" style="text-align:left">' + message + '</div>' );
965 return;
966 }
967
968 $modal.attr( 'data-wpbc-add-booking-resource-id', response.data.resource_id || '' );
969 $modal.attr( 'data-wpbc-add-booking-hash', response.data.booking_hash || '' );
970 $modal.attr( 'data-wpbc-add-booking-id', response.data.booking_id || '' );
971 $modal.attr( 'data-wpbc-add-booking-mode', response.data.mode || mode );
972 $modal.attr( 'data-wpbc-add-booking-allow-past', response.data.allow_past ? '1' : '0' );
973 $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar', response.data.selected_dates_without_calendar || '' );
974 $modal.attr( 'data-wpbc-add-booking-selected-dates', response.data.selected_dates || response.data.selected_date || '' );
975 $modal.attr( 'data-wpbc-add-booking-selected-date', response.data.selected_date || '' );
976 $modal.attr( 'data-wpbc-add-booking-selected-time', response.data.selected_time || '' );
977 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', response.data.time_override_enabled ? '1' : '0' );
978 $modal.attr( 'data-wpbc-add-booking-time-override-source', response.data.time_override_source || '' );
979 $modal.attr( 'data-wpbc-add-booking-time-override-start', response.data.time_override_start || '' );
980 $modal.attr( 'data-wpbc-add-booking-time-override-end', response.data.time_override_end || '' );
981 wpbc_boo_listing__sync_add_booking_modal_controls( $modal, response.data, response.data.mode || mode );
982 $modal.find( '.wpbc_modal__add_booking__title' ).text( response.data.title || title );
983 $modal.find( '.wpbc_modal__add_booking__booking_id' ).html( response.data.booking_id ? ( 'ID: ' + response.data.booking_id ) : '' );
984 $modal.find( '#wpbc_modal__add_booking__button_send' ).text( response.data.button_title || ( ( 'edit' === mode ) ? 'Save booking' : 'Add booking' ) );
985 wpbc_boo_listing__set_add_booking_modal_body_html( $body, response.data.html || '' );
986
987 if ( 'function' === typeof wpbc_hook__init_booking_form_wizard_buttons ) {
988 wpbc_hook__init_booking_form_wizard_buttons();
989 }
990
991 if ( 'undefined' !== typeof _wpbc ) {
992 _wpbc.set_other_param( 'this_page_booking_hash', response.data.booking_hash || '' );
993 _wpbc.set_other_param( 'this_page_allow_past', response.data.allow_past ? 1 : 0 );
994 }
995
996 if ( 'function' === typeof wpbc_bs_javascript_tooltips ) {
997 wpbc_bs_javascript_tooltips();
998 }
999 if ( 'function' === typeof wpbc_bs_javascript_popover ) {
1000 wpbc_bs_javascript_popover();
1001 }
1002
1003 wpbc_boo_listing__preload_add_booking_modal_selection( response.data );
1004 }
1005 ).fail( function(){
1006 $body.html( '<div class="wpbc-settings-notice notice-warning" style="text-align:left">Unable to load booking form.</div>' );
1007 } );
1008 }
1009
1010 /**
1011 * Reload Booking Listing after Add/Edit Booking modal saved successfully.
1012 */
1013 function wpbc_boo_listing__reload_after_add_booking_modal_submit(){
1014
1015 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
1016
1017 if ( $modal.length && ( 'function' === typeof $modal.wpbc_my_modal ) ) {
1018 $modal.wpbc_my_modal( 'hide' );
1019 }
1020
1021 if (
1022 ( 'function' === typeof window.wpbc_ajx_booking_send_search_request_with_params )
1023 && ( 'undefined' !== typeof window.wpbc_ajx_booking_listing )
1024 ) {
1025 window.wpbc_ajx_booking_send_search_request_with_params( {} );
1026 return;
1027 }
1028
1029 if ( 'function' === typeof window.wpbc_ajx_booking__actual_listing__show ) {
1030 window.wpbc_ajx_booking__actual_listing__show();
1031 }
1032 }
1033
1034 /**
1035 * Submit Add/Edit Booking modal form.
1036 *
1037 * @returns {boolean|undefined}
1038 */
1039 function wpbc_boo_listing__submit__add_booking_modal(){
1040
1041 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
1042 var $form = $modal.find( 'form.booking_form' ).first();
1043 var resource_id = 0;
1044
1045 if ( $form.length ) {
1046 resource_id = parseInt( ( $form.attr( 'id' ) || '' ).replace( 'booking_form', '' ), 10 );
1047 }
1048
1049 if ( ! resource_id ) {
1050 resource_id = parseInt( $modal.attr( 'data-wpbc-add-booking-resource-id' ), 10 );
1051 }
1052
1053 if ( ! resource_id ) {
1054 return false;
1055 }
1056
1057 var submit_form = $form.length ? $form.get( 0 ) : document.getElementById( 'booking_form' + resource_id );
1058 var locale = ( 'undefined' !== typeof _wpbc ) ? _wpbc.get_other_param( 'locale_active' ) : '';
1059 var submit_result;
1060
1061 if ( ! wpbc_boo_listing__prepare_add_booking_modal_time_override( resource_id ) ) {
1062 return false;
1063 }
1064
1065 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' )
1066 .on( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload', function( event, submitted_resource_id ){
1067
1068 if ( parseInt( submitted_resource_id, 10 ) !== resource_id ) {
1069 return;
1070 }
1071
1072 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' );
1073
1074 if ( ! jQuery( '#wpbc_modal__add_booking__section' ).is( ':visible' ) ) {
1075 return;
1076 }
1077
1078 wpbc_boo_listing__reload_after_add_booking_modal_submit();
1079 } );
1080
1081 submit_result = wpbc_booking_form_submit( submit_form, resource_id, locale );
1082
1083 if ( false === submit_result ) {
1084 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' );
1085 }
1086
1087 return submit_result;
1088 }
1089
1090 /**
1091 * Change payment Cost.
1092 *
1093 * @param booking_id - ID of booking.
1094 * @param cost - payment cost.
1095 */
1096 function wpbc_boo_listing__click__set_booking_cost( booking_id, cost ) {
1097
1098 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__booking_cost_edit__section' ) ) {
1099 return false;
1100 }
1101
1102 // Set booking cost.
1103 jQuery( '#wpbc_modal__booking_cost_edit__value' ).val( cost );
1104
1105 // Set booking ID.
1106 jQuery( '#wpbc_modal__booking_cost_edit__booking_id' ).val( booking_id );
1107
1108 // ID title.
1109 jQuery( '.wpbc_modal__booking_cost_edit__booking_id' ).html( 'ID: ' + booking_id );
1110
1111 // Show Modal.
1112 jQuery( '#wpbc_modal__booking_cost_edit__section' ).wpbc_my_modal( 'show' );
1113
1114 // Set focus to input.
1115 jQuery( '#wpbc_modal__booking_cost_edit__value' ).trigger( 'focus' );
1116 }
1117
1118 /**
1119 * Change payment Status.
1120 *
1121 * @param booking_id - ID of booking.
1122 * @param selected_pay_status - payment status.
1123 */
1124 function wpbc_boo_listing__click__set_payment_status( booking_id, selected_pay_status ) {
1125
1126 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__payment_status_edit__section' ) ) {
1127 return false;
1128 }
1129
1130 var jSelect = jQuery( '#wpbc_modal__payment_status_edit__value' );
1131
1132 // Select Status.
1133 if ( ( ! isNaN( parseFloat( selected_pay_status ) )) || ('' === selected_pay_status) ) { // Is it float - then it's unknown.
1134 jSelect.find( 'option[value="1"]' ).prop( 'selected', true ); // Unknown value is '1' in select box.
1135 } else {
1136 jSelect.find( 'option[value="' + selected_pay_status + '"]' ).prop( 'selected', true ); // Otherwise known payment status.
1137 }
1138 // Set booking ID.
1139 jQuery( '#wpbc_modal__payment_status_edit__booking_id' ).val( booking_id );
1140
1141 // ID title.
1142 jQuery( '.wpbc_modal__payment_status_edit__booking_id' ).html( 'ID: ' + booking_id );
1143
1144 // Show Modal.
1145 jQuery( '#wpbc_modal__payment_status_edit__section' ).wpbc_my_modal( 'show' );
1146
1147 // Set focus to input.
1148 jQuery( '#wpbc_modal__payment_status_edit__value' ).trigger( 'focus' );
1149 }
1150
1151 /**
1152 * Send payment request
1153 *
1154 * @param booking_id
1155 * @param visitorbookingpayurl
1156 * @param cost
1157 * @returns {boolean}
1158 */
1159 function wpbc_boo_listing__click__send_payment_request( booking_id, visitorbookingpayurl, cost ){
1160
1161 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__send_payment_request__section' ) ) {
1162 return false;
1163 }
1164
1165 // Set booking cost.
1166 jQuery( '#wpbc_modal__send_payment_request__url' ).val( visitorbookingpayurl );
1167
1168 // Set booking ID.
1169 jQuery( '#wpbc_modal__send_payment_request__booking_id' ).val( booking_id );
1170
1171 // ID title.
1172 jQuery( '.wpbc_modal__send_payment_request__booking_id' ).html( 'ID: ' + booking_id );
1173
1174 // Cost.
1175 jQuery( '.wpbc_modal__send_payment_request__cost' ).html( cost );
1176
1177 // Show Modal.
1178 jQuery( '#wpbc_modal__send_payment_request__section' ).wpbc_my_modal( 'show' );
1179
1180 // Set focus to input.
1181 jQuery( '#wpbc_modal__send_payment_request__value' ).trigger( 'focus' );
1182
1183 }
1184
1185 /**
1186 * Save Notes
1187 *
1188 * @param booking_id
1189 * @param note_text
1190 * @returns {boolean}
1191 */
1192 function wpbc_boo_listing__click__set_booking_note( booking_id, note_text ){
1193
1194 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_booking_note__section' ) ) {
1195 return false;
1196 }
1197
1198 // Set Note.
1199 jQuery( '#wpbc_modal__set_booking_note__value' ).val( note_text );
1200
1201 // Set booking ID.
1202 jQuery( '#wpbc_modal__set_booking_note__booking_id' ).val( booking_id );
1203
1204 // ID title.
1205 jQuery( '.wpbc_modal__set_booking_note__booking_id' ).html( 'ID: ' + booking_id );
1206
1207 // Show Modal.
1208 jQuery( '#wpbc_modal__set_booking_note__section' ).wpbc_my_modal( 'show' );
1209
1210 // Set focus to input. // jQuery( '#wpbc_modal__set_booking_note__value' ).trigger( 'focus' ); .
1211 jQuery( '#wpbc_modal__set_booking_note__value' ).scrollTop( 0 );
1212
1213 }
1214
1215 /**
1216 * Change Resource for Booking
1217 *
1218 * @param booking_id - ID of booking.
1219 * @param resource_id - ID of booking resource.
1220 */
1221 function wpbc_boo_listing__click__change_booking_resource( booking_id, resource_id ){
1222
1223 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__change_booking_resource__section' ) ) {
1224 return false;
1225 }
1226
1227 // Select booking resource that belong to booking.
1228 jQuery( '#wpbc_modal__change_booking_resource__resource_id' ).val( resource_id ).trigger( 'change' );
1229
1230 // Set booking ID.
1231 jQuery( '#wpbc_modal__change_booking_resource__booking_id' ).val( booking_id );
1232 // ID title.
1233 jQuery( '.wpbc_modal__change_booking_resource__booking_id' ).html( 'ID: ' + booking_id );
1234
1235 // Show Modal.
1236 jQuery( '#wpbc_modal__change_booking_resource__section' ).wpbc_my_modal( 'show' );
1237
1238 // Set focus to input.
1239 jQuery( '#wpbc_modal__change_booking_resource__resource_id' ).focus();
1240 }
1241
1242 /**
1243 * Set unavailable times for booking resource and dates.
1244 *
1245 * @param booking_id ID of booking.
1246 * @param resource_id ID of booking resource.
1247 * @param date_start Booking start date.
1248 * @param date_end Booking end date.
1249 */
1250 function wpbc_boo_listing__click__set_unavailable_times( booking_id, resource_id, date_start, date_end ){
1251
1252 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_unavailable_times__section' ) ) {
1253 return false;
1254 }
1255
1256 var $modal = jQuery( '#wpbc_modal__set_unavailable_times__section' );
1257 var $page = $modal.find( '.wpbc_ts_page' ).first();
1258
1259 if ( booking_id ) {
1260 jQuery( '.wpbc_modal__set_unavailable_times__booking_id' ).html( 'ID: ' + booking_id );
1261 } else {
1262 jQuery( '.wpbc_modal__set_unavailable_times__booking_id' ).html( '' );
1263 }
1264
1265 $modal.wpbc_my_modal( 'show' );
1266
1267 if ( 'function' === typeof window.wpbc_availability_timeslots_init ) {
1268 window.wpbc_availability_timeslots_init( $page );
1269 }
1270
1271 if ( 'function' === typeof window.wpbc_availability_timeslots_set_context ) {
1272 window.wpbc_availability_timeslots_set_context(
1273 $page,
1274 {
1275 resource_id: resource_id,
1276 date_start: date_start,
1277 date_end: date_end
1278 }
1279 );
1280 }
1281 }
1282
1283 /**
1284 * Duplicate Booking into another resource.
1285 *
1286 * @param booking_id - ID of booking.
1287 * @param resource_id - ID of booking resource.
1288 */
1289 function wpbc_boo_listing__click__duplicate_booking_to_other_resource( booking_id, resource_id ){
1290
1291 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__duplicate_booking_to_other_resource__section' ) ) {
1292 return false;
1293 }
1294
1295 // Select booking resource that belong to booking.
1296 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__resource_id' ).val( resource_id ).trigger( 'change' );
1297
1298 // Set booking ID.
1299 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__booking_id' ).val( booking_id );
1300 // ID title.
1301 jQuery( '.wpbc_modal__duplicate_booking_to_other_resource__booking_id' ).html( 'ID: ' + booking_id );
1302
1303 // Show Modal.
1304 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__section' ).wpbc_my_modal( 'show' );
1305
1306 // Set focus to input.
1307 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__resource_id' ).focus();
1308 }
1309
1310 /**
1311 * Change Locale of Booking.
1312 *
1313 * @param booking_id - ID of booking.
1314 * @param resource_id - ID of booking resource.
1315 */
1316 function wpbc_boo_listing__click__set_booking_locale( booking_id, selected_locale_value ){
1317
1318 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_booking_locale__section' ) ) {
1319 return false;
1320 }
1321
1322 // Select booking Locale that belong to booking.
1323 jQuery( '#wpbc_modal__set_booking_locale' ).val( selected_locale_value ).trigger( 'change' );
1324
1325 // var jSelect = jQuery( '#set_booking_locale__resource_select' );
1326 // jSelect.find( 'option[value="' + resource_id + '"]' ).prop( 'selected', true ); // Otherwise known payment status.
1327
1328 // Set booking ID.
1329 jQuery( '#wpbc_modal__set_booking_locale__booking_id' ).val( booking_id );
1330 // ID title.
1331 jQuery( '.wpbc_modal__set_booking_locale__booking_id' ).html( 'ID: ' + booking_id );
1332
1333 // Show Modal.
1334 jQuery( '#wpbc_modal__set_booking_locale__section' ).wpbc_my_modal( 'show' );
1335
1336 // Set focus to input.
1337 jQuery( '#wpbc_modal__set_booking_locale' ).focus();
1338 }
1339
1340
1341 // ---------------------------------------------------------------------------------------------------------------------
1342 // == Filter Toolbar ==
1343 // ---------------------------------------------------------------------------------------------------------------------
1344 /**
1345 * == "Sort By" Button ==
1346 * This function update Title in Dropdown menu.
1347 * It executed, after receving Ajax response.
1348 * And based on parameters of this response, we get option title from dropdown list options and show it in toggle title.
1349 */
1350 function wpbc_boo_listing__init_hook__sort_by() {
1351
1352 var el_id = 'wh_sort';
1353
1354 var parameter_value = wpbc_ajx_booking_listing.search_get_param( el_id );
1355
1356 var j_option_link = jQuery( '.ul_dropdown_menu_li__' + el_id + '__' + parameter_value );
1357 if ( j_option_link.length ) {
1358 jQuery( '.ul_dropdown_menu__' + el_id + ' .ul_dropdown_menu_toggle .selected_value' ).html( j_option_link.html() );
1359 } else {
1360 jQuery( '.ul_dropdown_menu__' + el_id + ' .ul_dropdown_menu_toggle .selected_value' ).html( '---' );
1361 }
1362 }
1363
1364 // ---------------------------------------------------------------------------------------------------------------------
1365 // == Listing Header Table ==
1366 // ---------------------------------------------------------------------------------------------------------------------
1367 /**
1368 * == "Expand All Rows" Button ==
1369 */
1370 function wpbc_boo_listing__click__expand_all_rows() {
1371 jQuery( '.wpbc_row_wrap' ).removeClass( 'max_height_a' );
1372 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_less' ).show();
1373 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_more' ).hide();
1374 jQuery( '.wpbc_btn_expand_colapse_all' ).toggle();
1375 }
1376
1377
1378 /**
1379 * == "Colpase All Rows" Button ==
1380 */
1381 function wpbc_boo_listing__click__colapse_all_rows() {
1382 jQuery( '.wpbc_row_wrap' ).addClass( 'max_height_a' );
1383 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_less' ).hide();
1384 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_more' ).show();
1385 jQuery( '.wpbc_btn_expand_colapse_all' ).toggle();
1386 }
1387