← All changes
|
includes/page-availability-general/_src/availability_general_page.js
+285
-2
11.0
→
11.8.4
View file →
| @@ -36,8 +36,105 @@ | ||
| 36 | 36 | $button.attr( 'aria-expanded', is_open ? 'false' : 'true' ); |
| 37 | 37 | $fields.prop( 'hidden', is_open ).attr( 'aria-hidden', is_open ? 'true' : 'false' ); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | + function close_group( $group ) { | |
| 41 | + var $button = $group.find( '> .group__header' ); | |
| 42 | + var $fields = $group.find( '> .group__fields' ); | |
| 43 | + | |
| 44 | + $group.removeClass( 'is-open' ); | |
| 45 | + $button.attr( 'aria-expanded', 'false' ); | |
| 46 | + $fields.prop( 'hidden', true ).attr( 'aria-hidden', 'true' ); | |
| 47 | + } | |
| 48 | + | |
| 49 | + function open_group( group_name, is_exclusive ) { | |
| 50 | + var $group = $( '.wpbc_ui__collapsible_group[data-group="' + group_name + '"]' ); | |
| 51 | + var $button = $group.find( '> .group__header' ); | |
| 52 | + var $fields = $group.find( '> .group__fields' ); | |
| 53 | + | |
| 54 | + if ( ! $group.length ) { | |
| 55 | + return; | |
| 56 | + } | |
| 57 | + | |
| 58 | + if ( is_exclusive ) { | |
| 59 | + $group.siblings( '.wpbc_ui__collapsible_group' ).each( function () { | |
| 60 | + close_group( $( this ) ); | |
| 61 | + } ); | |
| 62 | + } | |
| 63 | + | |
| 64 | + $group.addClass( 'is-open' ); | |
| 65 | + $button.attr( 'aria-expanded', 'true' ); | |
| 66 | + $fields.prop( 'hidden', false ).attr( 'aria-hidden', 'false' ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + function scroll_to_group( group_name ) { | |
| 70 | + var $group = $( '.wpbc_ui__collapsible_group[data-group="' + group_name + '"]' ); | |
| 71 | + var $scroll_parent = $(); | |
| 72 | + var scroll_parent_el; | |
| 73 | + var group_el; | |
| 74 | + var scroll_top; | |
| 75 | + var parent_rect; | |
| 76 | + var group_rect; | |
| 77 | + | |
| 78 | + if ( ! $group.length ) { | |
| 79 | + return; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $group.parents().each( function () { | |
| 83 | + var $candidate = $( this ); | |
| 84 | + var overflow_y = $candidate.css( 'overflow-y' ); | |
| 85 | + | |
| 86 | + if ( | |
| 87 | + $candidate.hasClass( 'simplebar-content-wrapper' ) | |
| 88 | + || ( | |
| 89 | + /(auto|scroll)/.test( overflow_y ) | |
| 90 | + && this.scrollHeight > this.clientHeight | |
| 91 | + ) | |
| 92 | + ) { | |
| 93 | + $scroll_parent = $candidate; | |
| 94 | + return false; | |
| 95 | + } | |
| 96 | + } ); | |
| 97 | + | |
| 98 | + if ( ! $scroll_parent.length ) { | |
| 99 | + $scroll_parent = $group.closest( '.wpbc_ui_el__vert_right_bar__content' ).find( '.simplebar-content-wrapper' ).first(); | |
| 100 | + } | |
| 101 | + | |
| 102 | + if ( ! $scroll_parent.length ) { | |
| 103 | + $group.get( 0 ).scrollIntoView( { block: 'nearest' } ); | |
| 104 | + return; | |
| 105 | + } | |
| 106 | + | |
| 107 | + scroll_parent_el = $scroll_parent.get( 0 ); | |
| 108 | + group_el = $group.get( 0 ); | |
| 109 | + parent_rect = scroll_parent_el.getBoundingClientRect(); | |
| 110 | + group_rect = group_el.getBoundingClientRect(); | |
| 111 | + scroll_top = $scroll_parent.scrollTop() + group_rect.top - parent_rect.top - 10; | |
| 112 | + | |
| 113 | + $scroll_parent.stop().animate( { scrollTop: Math.max( 0, scroll_top ) }, 180 ); | |
| 114 | + } | |
| 115 | + | |
| 116 | + function apply_open_section_from_url() { | |
| 117 | + var section_groups = { | |
| 118 | + weekdays: 'general-availability-weekdays', | |
| 119 | + from_today: 'general-availability-from-today', | |
| 120 | + buffer: 'general-availability-buffer', | |
| 121 | + working_time: 'general-availability-working-time' | |
| 122 | + }; | |
| 123 | + var group_name = section_groups[ cfg.open_section ] || ''; | |
| 124 | + | |
| 125 | + if ( '' === group_name ) { | |
| 126 | + return; | |
| 127 | + } | |
| 128 | + | |
| 129 | + open_group( group_name, true ); | |
| 130 | + $.each( [ 120, 650 ], function ( index, delay ) { | |
| 131 | + setTimeout( function () { | |
| 132 | + scroll_to_group( group_name ); | |
| 133 | + }, delay ); | |
| 134 | + } ); | |
| 135 | + } | |
| 136 | + | |
| 40 | 137 | function refresh_buffer_fields() { |
| 41 | 138 | var value = $( 'input[name="booking_unavailable_extra_in_out"]:checked' ).val() || ''; |
| 42 | 139 | |
| 43 | 140 | $( '.wpbc_ag_buffer_fields' ).each( function () { |
| @@ -132,9 +229,10 @@ | ||
| 132 | 229 | booking_unavailable_extra_in_out: $form.find( '[name="booking_unavailable_extra_in_out"]:checked' ).val() || '', |
| 133 | 230 | booking_unavailable_extra_minutes_in: $form.find( '[name="booking_unavailable_extra_minutes_in"]' ).val() || '', |
| 134 | 231 | booking_unavailable_extra_minutes_out: $form.find( '[name="booking_unavailable_extra_minutes_out"]' ).val() || '', |
| 135 | 232 | booking_unavailable_extra_days_in: $form.find( '[name="booking_unavailable_extra_days_in"]' ).val() || '', |
| 136 | - booking_unavailable_extra_days_out: $form.find( '[name="booking_unavailable_extra_days_out"]' ).val() || '' | |
| 233 | + booking_unavailable_extra_days_out: $form.find( '[name="booking_unavailable_extra_days_out"]' ).val() || '', | |
| 234 | + working_time: get_working_time_settings_from_form() | |
| 137 | 235 | }; |
| 138 | 236 | } |
| 139 | 237 | |
| 140 | 238 | function set_select_value( name, value ) { |
| @@ -171,8 +269,9 @@ | ||
| 171 | 269 | set_select_value( 'booking_unavailable_extra_days_out', settings.booking_unavailable_extra_days_out || '' ); |
| 172 | 270 | |
| 173 | 271 | $form.find( 'input[name="booking_unavailable_extra_in_out"]' ).prop( 'checked', false ); |
| 174 | 272 | $form.find( 'input[name="booking_unavailable_extra_in_out"][value="' + ( settings.booking_unavailable_extra_in_out || '' ) + '"]' ).prop( 'checked', true ); |
| 273 | + apply_working_time_settings_to_form( settings.working_time || {}, $( '#wpbc_ag_resource_id' ).val() ); | |
| 175 | 274 | |
| 176 | 275 | refresh_buffer_fields(); |
| 177 | 276 | sync_all_ranges(); |
| 178 | 277 | schedule_preview_refresh(); |
| @@ -272,8 +371,169 @@ | ||
| 272 | 371 | } |
| 273 | 372 | return parseInt( value, 10 ) || 0; |
| 274 | 373 | } |
| 275 | 374 | |
| 375 | + function time_to_seconds( time ) { | |
| 376 | + var parts = String( time || '' ).split( ':' ); | |
| 377 | + var hours; | |
| 378 | + var mins; | |
| 379 | + | |
| 380 | + if ( parts.length < 2 ) { | |
| 381 | + return 0; | |
| 382 | + } | |
| 383 | + | |
| 384 | + hours = Math.max( 0, Math.min( 24, parseInt( parts[0], 10 ) || 0 ) ); | |
| 385 | + mins = hours === 24 ? 0 : Math.max( 0, Math.min( 59, parseInt( parts[1], 10 ) || 0 ) ); | |
| 386 | + | |
| 387 | + return Math.min( 86400, ( hours * 3600 ) + ( mins * 60 ) ); | |
| 388 | + } | |
| 389 | + | |
| 390 | + function seconds_to_time( seconds ) { | |
| 391 | + var hours; | |
| 392 | + var mins; | |
| 393 | + | |
| 394 | + seconds = Math.max( 0, Math.min( 86400, parseInt( seconds, 10 ) || 0 ) ); | |
| 395 | + hours = Math.floor( seconds / 3600 ); | |
| 396 | + mins = Math.floor( ( seconds % 3600 ) / 60 ); | |
| 397 | + | |
| 398 | + return ( hours < 10 ? '0' : '' ) + hours + ':' + ( mins < 10 ? '0' : '' ) + mins; | |
| 399 | + } | |
| 400 | + | |
| 401 | + function collect_working_time_weekdays( prefix ) { | |
| 402 | + var $form = get_form(); | |
| 403 | + var weekdays = {}; | |
| 404 | + | |
| 405 | + $form.find( '[data-wpbc-working-time-weekdays="' + prefix + '"] .wpbc_ag_working_time_row' ).each( function () { | |
| 406 | + var $row = $( this ); | |
| 407 | + var day = parseInt( $row.find( 'input[type="checkbox"]' ).val(), 10 ); | |
| 408 | + | |
| 409 | + weekdays[ day ] = []; | |
| 410 | + if ( ! $row.find( 'input[type="checkbox"]' ).prop( 'checked' ) ) { | |
| 411 | + return; | |
| 412 | + } | |
| 413 | + | |
| 414 | + weekdays[ day ].push( { | |
| 415 | + start_second: time_to_seconds( $row.find( '.wpbc_ag_working_time_start' ).val() ), | |
| 416 | + end_second: time_to_seconds( $row.find( '.wpbc_ag_working_time_end' ).val() ) | |
| 417 | + } ); | |
| 418 | + } ); | |
| 419 | + | |
| 420 | + return weekdays; | |
| 421 | + } | |
| 422 | + | |
| 423 | + function set_working_time_weekdays( prefix, weekdays ) { | |
| 424 | + var $wrap = get_form().find( '[data-wpbc-working-time-weekdays="' + prefix + '"]' ); | |
| 425 | + | |
| 426 | + $wrap.find( '.wpbc_ag_working_time_row' ).each( function () { | |
| 427 | + var $row = $( this ); | |
| 428 | + var day = parseInt( $row.find( 'input[type="checkbox"]' ).val(), 10 ); | |
| 429 | + var intervals = weekdays && weekdays[ day ] ? weekdays[ day ] : []; | |
| 430 | + var interval = intervals.length ? intervals[0] : { start_second: 32400, end_second: 64800 }; | |
| 431 | + | |
| 432 | + $row.find( 'input[type="checkbox"]' ).prop( 'checked', intervals.length > 0 ); | |
| 433 | + $row.find( '.wpbc_ag_working_time_start' ).val( seconds_to_time( interval.start_second ) ); | |
| 434 | + $row.find( '.wpbc_ag_working_time_end' ).val( seconds_to_time( interval.end_second ) ); | |
| 435 | + } ); | |
| 436 | + } | |
| 437 | + | |
| 438 | + function refresh_working_time_panels() { | |
| 439 | + var is_enabled = get_form().find( 'input[name="booking_working_time_enabled"]' ).prop( 'checked' ); | |
| 440 | + var mode = get_form().find( 'input[name="booking_working_time_resource_mode"]:checked' ).val() || 'inherit'; | |
| 441 | + | |
| 442 | + get_form() | |
| 443 | + .find( '.wpbc_ag_working_time_block' ) | |
| 444 | + .toggleClass( 'is-disabled', ! is_enabled ) | |
| 445 | + .find( 'input, select, button' ) | |
| 446 | + .prop( 'disabled', ! is_enabled ); | |
| 447 | + | |
| 448 | + get_form().find( '[data-wpbc-working-time-resource-custom]' ).toggleClass( 'is-visible', mode === 'custom' ); | |
| 449 | + } | |
| 450 | + | |
| 451 | + function get_working_time_settings_from_form() { | |
| 452 | + var $form = get_form(); | |
| 453 | + var resourceId = parseInt( $form.find( '[data-wpbc-working-time-resource-id]' ).val(), 10 ) || parseInt( $( '#wpbc_ag_resource_id' ).val(), 10 ) || 0; | |
| 454 | + var workingTime = $.extend( true, {}, cfg.settings && cfg.settings.working_time ? cfg.settings.working_time : ( cfg.default_settings && cfg.default_settings.working_time ? cfg.default_settings.working_time : {} ) ); | |
| 455 | + | |
| 456 | + if ( ! workingTime.default ) { | |
| 457 | + workingTime.default = {}; | |
| 458 | + } | |
| 459 | + if ( ! workingTime.resources ) { | |
| 460 | + workingTime.resources = {}; | |
| 461 | + } | |
| 462 | + | |
| 463 | + workingTime.enabled = $form.find( 'input[name="booking_working_time_enabled"]' ).prop( 'checked' ) ? 'On' : 'Off'; | |
| 464 | + workingTime.default.weekdays = collect_working_time_weekdays( 'booking_working_time_default' ); | |
| 465 | + | |
| 466 | + if ( resourceId > 0 ) { | |
| 467 | + workingTime.resources[ resourceId ] = { | |
| 468 | + mode: $form.find( 'input[name="booking_working_time_resource_mode"]:checked' ).val() || 'inherit', | |
| 469 | + weekdays: collect_working_time_weekdays( 'booking_working_time_resource' ) | |
| 470 | + }; | |
| 471 | + } | |
| 472 | + | |
| 473 | + return workingTime; | |
| 474 | + } | |
| 475 | + | |
| 476 | + function apply_working_time_settings_to_form( workingTime, resourceId ) { | |
| 477 | + var resourceSettings; | |
| 478 | + | |
| 479 | + workingTime = workingTime || {}; | |
| 480 | + resourceId = parseInt( resourceId, 10 ) || parseInt( $( '#wpbc_ag_resource_id' ).val(), 10 ) || 0; | |
| 481 | + resourceSettings = workingTime.resources && workingTime.resources[ resourceId ] ? workingTime.resources[ resourceId ] : { | |
| 482 | + mode: 'inherit', | |
| 483 | + weekdays: workingTime.default && workingTime.default.weekdays ? workingTime.default.weekdays : {} | |
| 484 | + }; | |
| 485 | + | |
| 486 | + get_form().find( 'input[name="booking_working_time_enabled"]' ).prop( 'checked', workingTime.enabled === 'On' ); | |
| 487 | + get_form().find( '[data-wpbc-working-time-resource-id]' ).val( resourceId ); | |
| 488 | + set_working_time_weekdays( 'booking_working_time_default', workingTime.default && workingTime.default.weekdays ? workingTime.default.weekdays : {} ); | |
| 489 | + get_form().find( 'input[name="booking_working_time_resource_mode"][value="' + ( resourceSettings.mode || 'inherit' ) + '"]' ).prop( 'checked', true ); | |
| 490 | + set_working_time_weekdays( 'booking_working_time_resource', resourceSettings.weekdays || ( workingTime.default ? workingTime.default.weekdays : {} ) ); | |
| 491 | + refresh_working_time_panels(); | |
| 492 | + } | |
| 493 | + | |
| 494 | + function append_working_time_payload( payload ) { | |
| 495 | + var $form = get_form(); | |
| 496 | + var defaultDays = []; | |
| 497 | + var resourceDays = []; | |
| 498 | + var defaultStart = {}; | |
| 499 | + var defaultEnd = {}; | |
| 500 | + var resourceStart = {}; | |
| 501 | + var resourceEnd = {}; | |
| 502 | + | |
| 503 | + $form.find( '[data-wpbc-working-time-weekdays="booking_working_time_default"] .wpbc_ag_working_time_row' ).each( function () { | |
| 504 | + var $row = $( this ); | |
| 505 | + var day = parseInt( $row.find( 'input[type="checkbox"]' ).val(), 10 ); | |
| 506 | + | |
| 507 | + defaultStart[ day ] = $row.find( '.wpbc_ag_working_time_start' ).val() || '09:00'; | |
| 508 | + defaultEnd[ day ] = $row.find( '.wpbc_ag_working_time_end' ).val() || '18:00'; | |
| 509 | + if ( $row.find( 'input[type="checkbox"]' ).prop( 'checked' ) ) { | |
| 510 | + defaultDays.push( day ); | |
| 511 | + } | |
| 512 | + } ); | |
| 513 | + | |
| 514 | + $form.find( '[data-wpbc-working-time-weekdays="booking_working_time_resource"] .wpbc_ag_working_time_row' ).each( function () { | |
| 515 | + var $row = $( this ); | |
| 516 | + var day = parseInt( $row.find( 'input[type="checkbox"]' ).val(), 10 ); | |
| 517 | + | |
| 518 | + resourceStart[ day ] = $row.find( '.wpbc_ag_working_time_start' ).val() || '09:00'; | |
| 519 | + resourceEnd[ day ] = $row.find( '.wpbc_ag_working_time_end' ).val() || '18:00'; | |
| 520 | + if ( $row.find( 'input[type="checkbox"]' ).prop( 'checked' ) ) { | |
| 521 | + resourceDays.push( day ); | |
| 522 | + } | |
| 523 | + } ); | |
| 524 | + | |
| 525 | + payload.booking_working_time_enabled = $form.find( 'input[name="booking_working_time_enabled"]' ).prop( 'checked' ) ? 'On' : ''; | |
| 526 | + payload.booking_working_time_resource_id = $form.find( '[data-wpbc-working-time-resource-id]' ).val() || $( '#wpbc_ag_resource_id' ).val() || ''; | |
| 527 | + payload.booking_working_time_resource_mode = $form.find( 'input[name="booking_working_time_resource_mode"]:checked' ).val() || 'inherit'; | |
| 528 | + payload.booking_working_time_default_days = defaultDays; | |
| 529 | + payload.booking_working_time_default_start = defaultStart; | |
| 530 | + payload.booking_working_time_default_end = defaultEnd; | |
| 531 | + payload.booking_working_time_resource_days = resourceDays; | |
| 532 | + payload.booking_working_time_resource_start = resourceStart; | |
| 533 | + payload.booking_working_time_resource_end = resourceEnd; | |
| 534 | + } | |
| 535 | + | |
| 276 | 536 | function update_wpbc_preview_params( settings ) { |
| 277 | 537 | if ( ! w._wpbc || typeof w._wpbc.set_other_param !== 'function' ) { |
| 278 | 538 | return; |
| 279 | 539 | } |
| @@ -670,8 +930,10 @@ | ||
| 670 | 930 | nonce: cfg.nonce || '', |
| 671 | 931 | booking_unavailable_days: settings.weekdays |
| 672 | 932 | } ); |
| 673 | 933 | |
| 934 | + append_working_time_payload( payload ); | |
| 935 | + | |
| 674 | 936 | if ( ! cfg.ajax_url ) { |
| 675 | 937 | show_message( ( cfg.i18n && cfg.i18n.save_failed ) || 'Unable to save general availability settings.', 'error', 10000 ); |
| 676 | 938 | return; |
| 677 | 939 | } |
| @@ -691,10 +953,18 @@ | ||
| 691 | 953 | |
| 692 | 954 | if ( response.data && response.data.settings && response.data.settings.hints ) { |
| 693 | 955 | update_hints( response.data.settings.hints ); |
| 694 | 956 | } |
| 957 | + if ( response.data && response.data.settings ) { | |
| 958 | + cfg.settings = response.data.settings; | |
| 959 | + } | |
| 695 | 960 | |
| 696 | 961 | load_calendar_preview(); |
| 962 | + document.dispatchEvent( new CustomEvent( 'wpbc:availability-general:settings-saved', { | |
| 963 | + detail: { | |
| 964 | + settings: response.data && response.data.settings ? response.data.settings : {} | |
| 965 | + } | |
| 966 | + } ) ); | |
| 697 | 967 | show_message( ( response.data && response.data.message ) || ( cfg.i18n && cfg.i18n.saved ) || 'General availability settings updated.', 'success', 2000 ); |
| 698 | 968 | } ).fail( function () { |
| 699 | 969 | show_message( ( cfg.i18n && cfg.i18n.save_failed ) || 'Unable to save general availability settings.', 'error', 10000 ); |
| 700 | 970 | } ).always( function () { |
| @@ -711,9 +981,10 @@ | ||
| 711 | 981 | booking_unavailable_extra_in_out: '', |
| 712 | 982 | booking_unavailable_extra_minutes_in: '', |
| 713 | 983 | booking_unavailable_extra_minutes_out: '', |
| 714 | 984 | booking_unavailable_extra_days_in: '', |
| 715 | - booking_unavailable_extra_days_out: '' | |
| 985 | + booking_unavailable_extra_days_out: '', | |
| 986 | + working_time: {} | |
| 716 | 987 | }; |
| 717 | 988 | |
| 718 | 989 | if ( ! w.confirm( confirm_message ) ) { |
| 719 | 990 | return; |
| @@ -756,8 +1027,14 @@ | ||
| 756 | 1027 | } ); |
| 757 | 1028 | |
| 758 | 1029 | $( document ).on( 'change', '#wpbc_ag_resource_id, #wpbc_ag_months_count', load_calendar_preview ); |
| 759 | 1030 | |
| 1031 | + $( document ).on( 'change', '#wpbc_ag_resource_id', function () { | |
| 1032 | + cfg.settings = cfg.settings || {}; | |
| 1033 | + cfg.settings.working_time = get_working_time_settings_from_form(); | |
| 1034 | + apply_working_time_settings_to_form( cfg.settings.working_time, $( this ).val() ); | |
| 1035 | + } ); | |
| 1036 | + | |
| 760 | 1037 | $( document ).on( 'input change', '[data-wpbc-ag-range-for]', function () { |
| 761 | 1038 | sync_select_from_range( this ); |
| 762 | 1039 | schedule_preview_refresh(); |
| 763 | 1040 | } ); |
| @@ -774,8 +1051,12 @@ | ||
| 774 | 1051 | refresh_buffer_fields(); |
| 775 | 1052 | schedule_preview_refresh(); |
| 776 | 1053 | } ); |
| 777 | 1054 | |
| 1055 | + $( document ).on( 'change', 'input[name="booking_working_time_enabled"]', refresh_working_time_panels ); | |
| 1056 | + | |
| 1057 | + $( document ).on( 'change', 'input[name="booking_working_time_resource_mode"]', refresh_working_time_panels ); | |
| 1058 | + | |
| 778 | 1059 | $( document ).on( 'change', '[data-wpbc-ag-settings-form="1"] input, [data-wpbc-ag-settings-form="1"] select', schedule_preview_refresh ); |
| 779 | 1060 | |
| 780 | 1061 | $( document ).on( 'submit', '[data-wpbc-ag-settings-form="1"]', function ( event ) { |
| 781 | 1062 | event.preventDefault(); |
| @@ -790,8 +1071,10 @@ | ||
| 790 | 1071 | reset_settings( this ); |
| 791 | 1072 | } ); |
| 792 | 1073 | |
| 793 | 1074 | $( document ).ready( function () { |
| 1075 | + apply_working_time_settings_to_form( ( cfg.settings && cfg.settings.working_time ) || ( cfg.default_settings && cfg.default_settings.working_time ) || {}, $( '#wpbc_ag_resource_id' ).val() ); | |
| 1076 | + apply_open_section_from_url(); | |
| 794 | 1077 | refresh_buffer_fields(); |
| 795 | 1078 | sync_all_ranges(); |
| 796 | 1079 | observe_calendar_changes(); |
| 797 | 1080 | schedule_preview_refresh(); |