PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
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.9, at includes/page-bookings/_src/boo_listing__actions.js

1,390 lines 51.1 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 $appointment_entry = $modal.find( '[data-wpbc-add-appointment-entry]' );
731 var current_mode = mode || data.mode || 'add';
732
733 if ( 'edit' === current_mode ) {
734 $resource_control.hide();
735 $allow_past_control.hide();
736 } else {
737 $resource_control.show();
738 $allow_past_control.show();
739 }
740
741 $appointment_entry.toggle( 'add' === current_mode );
742
743 if ( data.resource_id && $resource_select.length ) {
744 $resource_select.val( String( data.resource_id ) );
745 }
746
747 if ( $form_select.length ) {
748 var booking_form = data.booking_form || 'standard';
749 if ( booking_form && ! $form_select.find( 'option' ).filter( function(){
750 return jQuery( this ).val() === booking_form;
751 } ).length ) {
752 $form_select.append( jQuery( '<option />' ).val( booking_form ).text( booking_form ) );
753 }
754 $form_select.val( booking_form );
755 }
756
757 if ( $form_edit_link.length ) {
758 wpbc_boo_listing__sync_add_booking_modal_form_edit_link( $modal );
759 }
760
761 if ( $allow_past_toggle.length ) {
762 $allow_past_toggle.prop( 'checked', !! parseInt( data.allow_past || 0, 10 ) );
763 }
764 }
765
766 /**
767 * Sync Forms Builder edit link with selected custom booking form.
768 *
769 * @param {Object} $modal Modal jQuery object.
770 */
771 function wpbc_boo_listing__sync_add_booking_modal_form_edit_link( $modal ){
772
773 var $form_select = $modal.find( '#wpbc_modal__add_booking__booking_form' );
774 var $form_edit_link = $modal.find( '.wpbc_modal__add_booking__edit_form_link' );
775
776 if ( ! $form_select.length || ! $form_edit_link.length ) {
777 return;
778 }
779
780 var form_name = $form_select.val() || 'standard';
781 var base_url = $form_edit_link.attr( 'data-wpbc-add-booking-form-builder-url' ) || $form_edit_link.attr( 'href' ) || '';
782
783 if ( ! base_url ) {
784 return;
785 }
786
787 var separator = ( -1 === base_url.indexOf( '?' ) ) ? '?' : '&';
788 $form_edit_link.attr( 'href', base_url + separator + 'form_name=' + encodeURIComponent( form_name ) );
789 }
790
791 /**
792 * Init modal footer controls.
793 */
794 function wpbc_boo_listing__init_add_booking_modal_controls(){
795
796 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(
797 'change.wpbc_add_booking_modal',
798 '#wpbc_modal__add_booking__resource_id, #wpbc_modal__add_booking__booking_form, #wpbc_modal__add_booking__allow_past',
799 function(){
800 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
801 var mode = $modal.attr( 'data-wpbc-add-booking-mode' ) || 'add';
802 var changed_id = jQuery( this ).attr( 'id' ) || '';
803 var booking_form = $modal.find( '#wpbc_modal__add_booking__booking_form' ).val() || '';
804
805 if ( 'wpbc_modal__add_booking__resource_id' === changed_id ) {
806 booking_form = '';
807 }
808
809 wpbc_boo_listing__click__add_booking_modal( {
810 mode : mode,
811 booking_id : $modal.attr( 'data-wpbc-add-booking-id' ) || '',
812 resource_id : $modal.find( '#wpbc_modal__add_booking__resource_id' ).val() || '',
813 booking_hash : $modal.attr( 'data-wpbc-add-booking-hash' ) || '',
814 booking_form : booking_form,
815 allow_past : $modal.find( '[data-wpbc-add-booking-allow-past]' ).first().is( ':checked' ) ? 1 : 0,
816 selected_dates_without_calendar : $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar' ) || '',
817 selected_dates : $modal.attr( 'data-wpbc-add-booking-selected-dates' ) || '',
818 selected_date : $modal.attr( 'data-wpbc-add-booking-selected-date' ) || '',
819 selected_time : $modal.attr( 'data-wpbc-add-booking-selected-time' ) || '',
820 time_override_enabled : $modal.find( '[data-wpbc-add-booking-time-override-enabled]' ).first().length
821 ? ( $modal.find( '[data-wpbc-add-booking-time-override-enabled]' ).first().is( ':checked' ) ? 1 : 0 )
822 : ( $modal.attr( 'data-wpbc-add-booking-time-override-enabled' ) || 0 ),
823 time_override_source : $modal.attr( 'data-wpbc-add-booking-time-override-source' ) || '',
824 time_override_start : $modal.attr( 'data-wpbc-add-booking-time-override-start' ) || '',
825 time_override_end : $modal.attr( 'data-wpbc-add-booking-time-override-end' ) || ''
826 } );
827 }
828 );
829
830 jQuery( document ).off( 'change.wpbc_add_booking_modal_time_override', '[data-wpbc-add-booking-time-override-enabled]' ).on(
831 'change.wpbc_add_booking_modal_time_override',
832 '[data-wpbc-add-booking-time-override-enabled]',
833 function(){
834 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
835 var resource_id = parseInt( $modal.attr( 'data-wpbc-add-booking-resource-id' ) || 0, 10 );
836 var $form = jQuery( '#booking_form' + resource_id );
837
838 if ( resource_id && $form.length ) {
839 wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id );
840 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', jQuery( this ).is( ':checked' ) ? '1' : '0' );
841 }
842 }
843 );
844 }
845 jQuery( document ).ready( function(){
846 wpbc_boo_listing__init_add_booking_modal_controls();
847 } );
848
849 /**
850 * Prepare selected timeline interval before Add Booking modal submit.
851 *
852 * @param {number} resource_id Booking resource ID.
853 * @returns {boolean}
854 */
855 function wpbc_boo_listing__prepare_add_booking_modal_time_override( resource_id ){
856
857 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
858 var $form = jQuery( '#booking_form' + resource_id );
859 var $wrap = $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).first();
860 var $enabled = $wrap.find( '[data-wpbc-add-booking-time-override-enabled]' ).first();
861 var $start;
862 var $end;
863
864 if ( ! $modal.is( ':visible' ) || ! $wrap.length || ! $enabled.is( ':checked' ) ) {
865 return true;
866 }
867
868 wpbc_boo_listing__apply_add_booking_modal_time_override_state( $form, resource_id );
869
870 $start = $wrap.find( '[data-wpbc-add-booking-time-override-field="start"]' ).first();
871 $end = $wrap.find( '[data-wpbc-add-booking-time-override-field="end"]' ).first();
872
873 if ( ! $start.val() || ! $end.val() ) {
874 if ( 'function' === typeof wpbc_front_end__show_message__warning ) {
875 wpbc_front_end__show_message__warning( $wrap.get( 0 ), 'Selected timeline interval is not complete.' );
876 }
877 return false;
878 }
879
880 $modal
881 .attr( 'data-wpbc-add-booking-time-override-enabled', '1' )
882 .attr( 'data-wpbc-add-booking-time-override-start', $start.val() )
883 .attr( 'data-wpbc-add-booking-time-override-end', $end.val() );
884
885 return true;
886 }
887
888 /**
889 * Open Add/Edit Booking modal.
890 *
891 * @param {Object} args Modal context.
892 * @returns {boolean|undefined}
893 */
894 function wpbc_boo_listing__click__add_booking_modal( args ){
895
896 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__add_booking__section' ) ) {
897 return false;
898 }
899
900 args = args || {};
901
902 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
903 var $body = jQuery( '#wpbc_modal__add_booking__body' );
904 var nonce = $modal.attr( 'data-wpbc-add-booking-nonce' );
905 var mode = args.mode || ( args.booking_hash ? 'edit' : 'add' );
906 var title = ( 'edit' === mode ) ? 'Edit booking' : 'Add booking';
907 var allow_past = args.allow_past ? 1 : 0;
908
909 if ( ! allow_past && $modal.find( '[data-wpbc-add-booking-allow-past]' ).first().is( ':checked' ) ) {
910 allow_past = 1;
911 }
912
913 if ( 'edit' === mode ) {
914 allow_past = 1;
915 }
916
917 $modal.attr( 'data-wpbc-add-booking-resource-id', '' );
918 $modal.attr( 'data-wpbc-add-booking-hash', args.booking_hash || '' );
919 $modal.attr( 'data-wpbc-add-booking-id', args.booking_id || '' );
920 $modal.attr( 'data-wpbc-add-booking-mode', mode );
921 $modal.attr( 'data-wpbc-add-booking-allow-past', allow_past ? '1' : '0' );
922 $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar', args.selected_dates_without_calendar || '' );
923 $modal.attr( 'data-wpbc-add-booking-selected-dates', args.selected_dates || args.selected_date || '' );
924 $modal.attr( 'data-wpbc-add-booking-selected-date', args.selected_date || '' );
925 $modal.attr( 'data-wpbc-add-booking-selected-time', args.selected_time || '' );
926 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', args.time_override_enabled ? '1' : '0' );
927 $modal.attr( 'data-wpbc-add-booking-time-override-source', args.time_override_source || '' );
928 $modal.attr( 'data-wpbc-add-booking-time-override-start', args.time_override_start || '' );
929 $modal.attr( 'data-wpbc-add-booking-time-override-end', args.time_override_end || '' );
930 $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' );
931 if ( ! args.time_override_enabled ) {
932 $modal.find( '[data-wpbc-add-booking-time-override-panel]' ).remove();
933 }
934 args.allow_past = allow_past;
935 wpbc_boo_listing__sync_add_booking_modal_controls( $modal, args, mode );
936 $modal.find( '.wpbc_modal__add_booking__title' ).text( title );
937 $modal.find( '.wpbc_modal__add_booking__booking_id' ).html( args.booking_id ? ( 'ID: ' + args.booking_id ) : '' );
938 $modal.find( '#wpbc_modal__add_booking__button_send' ).text( ( 'edit' === mode ) ? 'Save booking' : 'Add booking' );
939 $body.html( wpbc_boo_listing__get_add_booking_modal_loading_html() );
940
941 $modal.wpbc_my_modal( 'show' );
942
943 jQuery.post(
944 wpbc_url_ajax,
945 {
946 action : 'WPBC_AJX_ADD_BOOKING_MODAL',
947 nonce : nonce,
948 mode : mode,
949 booking_id : args.booking_id || '',
950 resource_id : args.resource_id || '',
951 booking_hash : args.booking_hash || '',
952 booking_form : args.booking_form || '',
953 allow_past : allow_past,
954 selected_dates_without_calendar : args.selected_dates_without_calendar || '',
955 selected_dates : args.selected_dates || args.selected_date || '',
956 selected_date : args.selected_date || '',
957 selected_time : args.selected_time || '',
958 time_override_enabled : args.time_override_enabled ? 1 : 0,
959 time_override_source : args.time_override_source || '',
960 time_override_start : args.time_override_start || '',
961 time_override_end : args.time_override_end || ''
962 },
963 function( response ){
964
965 if ( ! response || ! response.success ) {
966 var message = ( response && response.data && response.data.message ) ? response.data.message : 'Unable to load booking form.';
967 $body.html( '<div class="wpbc-settings-notice notice-warning" style="text-align:left">' + message + '</div>' );
968 return;
969 }
970
971 $modal.attr( 'data-wpbc-add-booking-resource-id', response.data.resource_id || '' );
972 $modal.attr( 'data-wpbc-add-booking-hash', response.data.booking_hash || '' );
973 $modal.attr( 'data-wpbc-add-booking-id', response.data.booking_id || '' );
974 $modal.attr( 'data-wpbc-add-booking-mode', response.data.mode || mode );
975 $modal.attr( 'data-wpbc-add-booking-allow-past', response.data.allow_past ? '1' : '0' );
976 $modal.attr( 'data-wpbc-add-booking-selected-dates-without-calendar', response.data.selected_dates_without_calendar || '' );
977 $modal.attr( 'data-wpbc-add-booking-selected-dates', response.data.selected_dates || response.data.selected_date || '' );
978 $modal.attr( 'data-wpbc-add-booking-selected-date', response.data.selected_date || '' );
979 $modal.attr( 'data-wpbc-add-booking-selected-time', response.data.selected_time || '' );
980 $modal.attr( 'data-wpbc-add-booking-time-override-enabled', response.data.time_override_enabled ? '1' : '0' );
981 $modal.attr( 'data-wpbc-add-booking-time-override-source', response.data.time_override_source || '' );
982 $modal.attr( 'data-wpbc-add-booking-time-override-start', response.data.time_override_start || '' );
983 $modal.attr( 'data-wpbc-add-booking-time-override-end', response.data.time_override_end || '' );
984 wpbc_boo_listing__sync_add_booking_modal_controls( $modal, response.data, response.data.mode || mode );
985 $modal.find( '.wpbc_modal__add_booking__title' ).text( response.data.title || title );
986 $modal.find( '.wpbc_modal__add_booking__booking_id' ).html( response.data.booking_id ? ( 'ID: ' + response.data.booking_id ) : '' );
987 $modal.find( '#wpbc_modal__add_booking__button_send' ).text( response.data.button_title || ( ( 'edit' === mode ) ? 'Save booking' : 'Add booking' ) );
988 wpbc_boo_listing__set_add_booking_modal_body_html( $body, response.data.html || '' );
989
990 if ( 'function' === typeof wpbc_hook__init_booking_form_wizard_buttons ) {
991 wpbc_hook__init_booking_form_wizard_buttons();
992 }
993
994 if ( 'undefined' !== typeof _wpbc ) {
995 _wpbc.set_other_param( 'this_page_booking_hash', response.data.booking_hash || '' );
996 _wpbc.set_other_param( 'this_page_allow_past', response.data.allow_past ? 1 : 0 );
997 }
998
999 if ( 'function' === typeof wpbc_bs_javascript_tooltips ) {
1000 wpbc_bs_javascript_tooltips();
1001 }
1002 if ( 'function' === typeof wpbc_bs_javascript_popover ) {
1003 wpbc_bs_javascript_popover();
1004 }
1005
1006 wpbc_boo_listing__preload_add_booking_modal_selection( response.data );
1007 }
1008 ).fail( function(){
1009 $body.html( '<div class="wpbc-settings-notice notice-warning" style="text-align:left">Unable to load booking form.</div>' );
1010 } );
1011 }
1012
1013 /**
1014 * Reload Booking Listing after Add/Edit Booking modal saved successfully.
1015 */
1016 function wpbc_boo_listing__reload_after_add_booking_modal_submit(){
1017
1018 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
1019
1020 if ( $modal.length && ( 'function' === typeof $modal.wpbc_my_modal ) ) {
1021 $modal.wpbc_my_modal( 'hide' );
1022 }
1023
1024 if (
1025 ( 'function' === typeof window.wpbc_ajx_booking_send_search_request_with_params )
1026 && ( 'undefined' !== typeof window.wpbc_ajx_booking_listing )
1027 ) {
1028 window.wpbc_ajx_booking_send_search_request_with_params( {} );
1029 return;
1030 }
1031
1032 if ( 'function' === typeof window.wpbc_ajx_booking__actual_listing__show ) {
1033 window.wpbc_ajx_booking__actual_listing__show();
1034 }
1035 }
1036
1037 /**
1038 * Submit Add/Edit Booking modal form.
1039 *
1040 * @returns {boolean|undefined}
1041 */
1042 function wpbc_boo_listing__submit__add_booking_modal(){
1043
1044 var $modal = jQuery( '#wpbc_modal__add_booking__section' );
1045 var $form = $modal.find( 'form.booking_form' ).first();
1046 var resource_id = 0;
1047
1048 if ( $form.length ) {
1049 resource_id = parseInt( ( $form.attr( 'id' ) || '' ).replace( 'booking_form', '' ), 10 );
1050 }
1051
1052 if ( ! resource_id ) {
1053 resource_id = parseInt( $modal.attr( 'data-wpbc-add-booking-resource-id' ), 10 );
1054 }
1055
1056 if ( ! resource_id ) {
1057 return false;
1058 }
1059
1060 var submit_form = $form.length ? $form.get( 0 ) : document.getElementById( 'booking_form' + resource_id );
1061 var locale = ( 'undefined' !== typeof _wpbc ) ? _wpbc.get_other_param( 'locale_active' ) : '';
1062 var submit_result;
1063
1064 if ( ! wpbc_boo_listing__prepare_add_booking_modal_time_override( resource_id ) ) {
1065 return false;
1066 }
1067
1068 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' )
1069 .on( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload', function( event, submitted_resource_id ){
1070
1071 if ( parseInt( submitted_resource_id, 10 ) !== resource_id ) {
1072 return;
1073 }
1074
1075 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' );
1076
1077 if ( ! jQuery( '#wpbc_modal__add_booking__section' ).is( ':visible' ) ) {
1078 return;
1079 }
1080
1081 wpbc_boo_listing__reload_after_add_booking_modal_submit();
1082 } );
1083
1084 submit_result = wpbc_booking_form_submit( submit_form, resource_id, locale );
1085
1086 if ( false === submit_result ) {
1087 jQuery( 'body' ).off( 'wpbc_booking_form_submit_success.wpbc_add_booking_modal_reload' );
1088 }
1089
1090 return submit_result;
1091 }
1092
1093 /**
1094 * Change payment Cost.
1095 *
1096 * @param booking_id - ID of booking.
1097 * @param cost - payment cost.
1098 */
1099 function wpbc_boo_listing__click__set_booking_cost( booking_id, cost ) {
1100
1101 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__booking_cost_edit__section' ) ) {
1102 return false;
1103 }
1104
1105 // Set booking cost.
1106 jQuery( '#wpbc_modal__booking_cost_edit__value' ).val( cost );
1107
1108 // Set booking ID.
1109 jQuery( '#wpbc_modal__booking_cost_edit__booking_id' ).val( booking_id );
1110
1111 // ID title.
1112 jQuery( '.wpbc_modal__booking_cost_edit__booking_id' ).html( 'ID: ' + booking_id );
1113
1114 // Show Modal.
1115 jQuery( '#wpbc_modal__booking_cost_edit__section' ).wpbc_my_modal( 'show' );
1116
1117 // Set focus to input.
1118 jQuery( '#wpbc_modal__booking_cost_edit__value' ).trigger( 'focus' );
1119 }
1120
1121 /**
1122 * Change payment Status.
1123 *
1124 * @param booking_id - ID of booking.
1125 * @param selected_pay_status - payment status.
1126 */
1127 function wpbc_boo_listing__click__set_payment_status( booking_id, selected_pay_status ) {
1128
1129 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__payment_status_edit__section' ) ) {
1130 return false;
1131 }
1132
1133 var jSelect = jQuery( '#wpbc_modal__payment_status_edit__value' );
1134
1135 // Select Status.
1136 if ( ( ! isNaN( parseFloat( selected_pay_status ) )) || ('' === selected_pay_status) ) { // Is it float - then it's unknown.
1137 jSelect.find( 'option[value="1"]' ).prop( 'selected', true ); // Unknown value is '1' in select box.
1138 } else {
1139 jSelect.find( 'option[value="' + selected_pay_status + '"]' ).prop( 'selected', true ); // Otherwise known payment status.
1140 }
1141 // Set booking ID.
1142 jQuery( '#wpbc_modal__payment_status_edit__booking_id' ).val( booking_id );
1143
1144 // ID title.
1145 jQuery( '.wpbc_modal__payment_status_edit__booking_id' ).html( 'ID: ' + booking_id );
1146
1147 // Show Modal.
1148 jQuery( '#wpbc_modal__payment_status_edit__section' ).wpbc_my_modal( 'show' );
1149
1150 // Set focus to input.
1151 jQuery( '#wpbc_modal__payment_status_edit__value' ).trigger( 'focus' );
1152 }
1153
1154 /**
1155 * Send payment request
1156 *
1157 * @param booking_id
1158 * @param visitorbookingpayurl
1159 * @param cost
1160 * @returns {boolean}
1161 */
1162 function wpbc_boo_listing__click__send_payment_request( booking_id, visitorbookingpayurl, cost ){
1163
1164 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__send_payment_request__section' ) ) {
1165 return false;
1166 }
1167
1168 // Set booking cost.
1169 jQuery( '#wpbc_modal__send_payment_request__url' ).val( visitorbookingpayurl );
1170
1171 // Set booking ID.
1172 jQuery( '#wpbc_modal__send_payment_request__booking_id' ).val( booking_id );
1173
1174 // ID title.
1175 jQuery( '.wpbc_modal__send_payment_request__booking_id' ).html( 'ID: ' + booking_id );
1176
1177 // Cost.
1178 jQuery( '.wpbc_modal__send_payment_request__cost' ).html( cost );
1179
1180 // Show Modal.
1181 jQuery( '#wpbc_modal__send_payment_request__section' ).wpbc_my_modal( 'show' );
1182
1183 // Set focus to input.
1184 jQuery( '#wpbc_modal__send_payment_request__value' ).trigger( 'focus' );
1185
1186 }
1187
1188 /**
1189 * Save Notes
1190 *
1191 * @param booking_id
1192 * @param note_text
1193 * @returns {boolean}
1194 */
1195 function wpbc_boo_listing__click__set_booking_note( booking_id, note_text ){
1196
1197 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_booking_note__section' ) ) {
1198 return false;
1199 }
1200
1201 // Set Note.
1202 jQuery( '#wpbc_modal__set_booking_note__value' ).val( note_text );
1203
1204 // Set booking ID.
1205 jQuery( '#wpbc_modal__set_booking_note__booking_id' ).val( booking_id );
1206
1207 // ID title.
1208 jQuery( '.wpbc_modal__set_booking_note__booking_id' ).html( 'ID: ' + booking_id );
1209
1210 // Show Modal.
1211 jQuery( '#wpbc_modal__set_booking_note__section' ).wpbc_my_modal( 'show' );
1212
1213 // Set focus to input. // jQuery( '#wpbc_modal__set_booking_note__value' ).trigger( 'focus' ); .
1214 jQuery( '#wpbc_modal__set_booking_note__value' ).scrollTop( 0 );
1215
1216 }
1217
1218 /**
1219 * Change Resource for Booking
1220 *
1221 * @param booking_id - ID of booking.
1222 * @param resource_id - ID of booking resource.
1223 */
1224 function wpbc_boo_listing__click__change_booking_resource( booking_id, resource_id ){
1225
1226 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__change_booking_resource__section' ) ) {
1227 return false;
1228 }
1229
1230 // Select booking resource that belong to booking.
1231 jQuery( '#wpbc_modal__change_booking_resource__resource_id' ).val( resource_id ).trigger( 'change' );
1232
1233 // Set booking ID.
1234 jQuery( '#wpbc_modal__change_booking_resource__booking_id' ).val( booking_id );
1235 // ID title.
1236 jQuery( '.wpbc_modal__change_booking_resource__booking_id' ).html( 'ID: ' + booking_id );
1237
1238 // Show Modal.
1239 jQuery( '#wpbc_modal__change_booking_resource__section' ).wpbc_my_modal( 'show' );
1240
1241 // Set focus to input.
1242 jQuery( '#wpbc_modal__change_booking_resource__resource_id' ).focus();
1243 }
1244
1245 /**
1246 * Set unavailable times for booking resource and dates.
1247 *
1248 * @param booking_id ID of booking.
1249 * @param resource_id ID of booking resource.
1250 * @param date_start Booking start date.
1251 * @param date_end Booking end date.
1252 */
1253 function wpbc_boo_listing__click__set_unavailable_times( booking_id, resource_id, date_start, date_end ){
1254
1255 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_unavailable_times__section' ) ) {
1256 return false;
1257 }
1258
1259 var $modal = jQuery( '#wpbc_modal__set_unavailable_times__section' );
1260 var $page = $modal.find( '.wpbc_ts_page' ).first();
1261
1262 if ( booking_id ) {
1263 jQuery( '.wpbc_modal__set_unavailable_times__booking_id' ).html( 'ID: ' + booking_id );
1264 } else {
1265 jQuery( '.wpbc_modal__set_unavailable_times__booking_id' ).html( '' );
1266 }
1267
1268 $modal.wpbc_my_modal( 'show' );
1269
1270 if ( 'function' === typeof window.wpbc_availability_timeslots_init ) {
1271 window.wpbc_availability_timeslots_init( $page );
1272 }
1273
1274 if ( 'function' === typeof window.wpbc_availability_timeslots_set_context ) {
1275 window.wpbc_availability_timeslots_set_context(
1276 $page,
1277 {
1278 resource_id: resource_id,
1279 date_start: date_start,
1280 date_end: date_end
1281 }
1282 );
1283 }
1284 }
1285
1286 /**
1287 * Duplicate Booking into another resource.
1288 *
1289 * @param booking_id - ID of booking.
1290 * @param resource_id - ID of booking resource.
1291 */
1292 function wpbc_boo_listing__click__duplicate_booking_to_other_resource( booking_id, resource_id ){
1293
1294 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__duplicate_booking_to_other_resource__section' ) ) {
1295 return false;
1296 }
1297
1298 // Select booking resource that belong to booking.
1299 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__resource_id' ).val( resource_id ).trigger( 'change' );
1300
1301 // Set booking ID.
1302 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__booking_id' ).val( booking_id );
1303 // ID title.
1304 jQuery( '.wpbc_modal__duplicate_booking_to_other_resource__booking_id' ).html( 'ID: ' + booking_id );
1305
1306 // Show Modal.
1307 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__section' ).wpbc_my_modal( 'show' );
1308
1309 // Set focus to input.
1310 jQuery( '#wpbc_modal__duplicate_booking_to_other_resource__resource_id' ).focus();
1311 }
1312
1313 /**
1314 * Change Locale of Booking.
1315 *
1316 * @param booking_id - ID of booking.
1317 * @param resource_id - ID of booking resource.
1318 */
1319 function wpbc_boo_listing__click__set_booking_locale( booking_id, selected_locale_value ){
1320
1321 if ( ! wpbc_is_modal_accessible( '#wpbc_modal__set_booking_locale__section' ) ) {
1322 return false;
1323 }
1324
1325 // Select booking Locale that belong to booking.
1326 jQuery( '#wpbc_modal__set_booking_locale' ).val( selected_locale_value ).trigger( 'change' );
1327
1328 // var jSelect = jQuery( '#set_booking_locale__resource_select' );
1329 // jSelect.find( 'option[value="' + resource_id + '"]' ).prop( 'selected', true ); // Otherwise known payment status.
1330
1331 // Set booking ID.
1332 jQuery( '#wpbc_modal__set_booking_locale__booking_id' ).val( booking_id );
1333 // ID title.
1334 jQuery( '.wpbc_modal__set_booking_locale__booking_id' ).html( 'ID: ' + booking_id );
1335
1336 // Show Modal.
1337 jQuery( '#wpbc_modal__set_booking_locale__section' ).wpbc_my_modal( 'show' );
1338
1339 // Set focus to input.
1340 jQuery( '#wpbc_modal__set_booking_locale' ).focus();
1341 }
1342
1343
1344 // ---------------------------------------------------------------------------------------------------------------------
1345 // == Filter Toolbar ==
1346 // ---------------------------------------------------------------------------------------------------------------------
1347 /**
1348 * == "Sort By" Button ==
1349 * This function update Title in Dropdown menu.
1350 * It executed, after receving Ajax response.
1351 * And based on parameters of this response, we get option title from dropdown list options and show it in toggle title.
1352 */
1353 function wpbc_boo_listing__init_hook__sort_by() {
1354
1355 var el_id = 'wh_sort';
1356
1357 var parameter_value = wpbc_ajx_booking_listing.search_get_param( el_id );
1358
1359 var j_option_link = jQuery( '.ul_dropdown_menu_li__' + el_id + '__' + parameter_value );
1360 if ( j_option_link.length ) {
1361 jQuery( '.ul_dropdown_menu__' + el_id + ' .ul_dropdown_menu_toggle .selected_value' ).html( j_option_link.html() );
1362 } else {
1363 jQuery( '.ul_dropdown_menu__' + el_id + ' .ul_dropdown_menu_toggle .selected_value' ).html( '---' );
1364 }
1365 }
1366
1367 // ---------------------------------------------------------------------------------------------------------------------
1368 // == Listing Header Table ==
1369 // ---------------------------------------------------------------------------------------------------------------------
1370 /**
1371 * == "Expand All Rows" Button ==
1372 */
1373 function wpbc_boo_listing__click__expand_all_rows() {
1374 jQuery( '.wpbc_row_wrap' ).removeClass( 'max_height_a' );
1375 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_less' ).show();
1376 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_more' ).hide();
1377 jQuery( '.wpbc_btn_expand_colapse_all' ).toggle();
1378 }
1379
1380
1381 /**
1382 * == "Colpase All Rows" Button ==
1383 */
1384 function wpbc_boo_listing__click__colapse_all_rows() {
1385 jQuery( '.wpbc_row_wrap' ).addClass( 'max_height_a' );
1386 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_less' ).hide();
1387 jQuery( '.wpbc_row_wrap .wpbc_icn_expand_more' ).show();
1388 jQuery( '.wpbc_btn_expand_colapse_all' ).toggle();
1389 }
1390