← All changes
|
includes/page-add-appointment/_src/add_appointment_page.js
+81
-0
11.5
→
11.9
View file →
| @@ -13,8 +13,9 @@ | ||
| 13 | 13 | |
| 14 | 14 | window._wpbc.set_other_param( 'this_page_booking_hash', '' ); |
| 15 | 15 | window._wpbc.set_other_param( 'this_page_allow_past', labels.allowPast ? 1 : 0 ); |
| 16 | 16 | window._wpbc.set_other_param( 'this_page_allow_past_arr', labels.allowPastDateArr || [] ); |
| 17 | + window._wpbc.set_other_param( 'this_page_admin_booking_nonce', String( labels.adminBookingNonce || '' ) ); | |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | 20 | /** Switch one shared right-sidebar panel. */ |
| 20 | 21 | function switch_right_panel( $tab ) { |
| @@ -226,8 +227,81 @@ | ||
| 226 | 227 | |
| 227 | 228 | schedule_summary_refresh(); |
| 228 | 229 | } |
| 229 | 230 | |
| 231 | + /** Copy the authoritative Appointment correction into its convenience slider. */ | |
| 232 | + function synchronize_cost_correction_range() { | |
| 233 | + var $number_field = $( '#wpbc_add_appointment_cost_correction' ); | |
| 234 | + var $range = $( '[data-wpbc-admin-cost-correction-range="1"]' ).first(); | |
| 235 | + var number_value; | |
| 236 | + | |
| 237 | + if ( ! $number_field.length || ! $range.length ) { | |
| 238 | + return; | |
| 239 | + } | |
| 240 | + | |
| 241 | + number_value = Number( $number_field.val() ); | |
| 242 | + if ( '' === String( $number_field.val() || '' ).trim() || ! isFinite( number_value ) ) { | |
| 243 | + $range.val( '0' ); | |
| 244 | + return; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $range.val( String( number_value ) ); | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** Copy the convenience slider into the authoritative Appointment number input. */ | |
| 251 | + function synchronize_cost_correction_number() { | |
| 252 | + var $number_field = $( '#wpbc_add_appointment_cost_correction' ); | |
| 253 | + var $range = $( '[data-wpbc-admin-cost-correction-range="1"]' ).first(); | |
| 254 | + | |
| 255 | + if ( ! $number_field.length || ! $range.length ) { | |
| 256 | + return; | |
| 257 | + } | |
| 258 | + | |
| 259 | + $number_field.val( $range.val() ).trigger( 'input' ); | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** Clear an unsaved or successfully submitted Appointment correction draft. */ | |
| 263 | + function clear_cost_correction() { | |
| 264 | + var $number_field = $( '#wpbc_add_appointment_cost_correction' ); | |
| 265 | + | |
| 266 | + if ( ! $number_field.length ) { | |
| 267 | + return; | |
| 268 | + } | |
| 269 | + | |
| 270 | + $number_field.val( '' ); | |
| 271 | + synchronize_cost_correction_range(); | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * Add an explicitly entered correction to the active Appointment request. | |
| 276 | + * | |
| 277 | + * The inspector is outside the AJAX-inserted native Booking Form. Requiring | |
| 278 | + * the submitted Provider to match the active Appointment form prevents this | |
| 279 | + * page-only draft from affecting an unrelated booking-create event. | |
| 280 | + * | |
| 281 | + * @param {Event} event jQuery event. | |
| 282 | + * @param {number} resource_id Submitted Provider resource ID. | |
| 283 | + * @param {Object} params Mutable booking-create request parameters. | |
| 284 | + * @return {void} | |
| 285 | + */ | |
| 286 | + function add_cost_correction_to_appointment_request( event, resource_id, params ) { | |
| 287 | + var number_field = document.getElementById( 'wpbc_add_appointment_cost_correction' ); | |
| 288 | + var $native_form = $( '.wpbc_add_appointment__canvas .wpbc_booking_appointment__native_form[data-provider-id="' + Number( resource_id || 0 ) + '"]' ).first(); | |
| 289 | + var raw_cost; | |
| 290 | + | |
| 291 | + if ( ! params || ! number_field || ! $native_form.length ) { | |
| 292 | + return; | |
| 293 | + } | |
| 294 | + | |
| 295 | + delete params.wpbc_admin_cost_correction; | |
| 296 | + raw_cost = String( number_field.value || '' ).trim(); | |
| 297 | + if ( '' === raw_cost || ! number_field.checkValidity() ) { | |
| 298 | + return; | |
| 299 | + } | |
| 300 | + | |
| 301 | + params.wpbc_admin_cost_correction = raw_cost; | |
| 302 | + } | |
| 303 | + | |
| 230 | 304 | $( document ).on( 'click', '.wpbc_add_appointment__rightbar_tabs [role="tab"]', function ( event ) { |
| 231 | 305 | event.preventDefault(); |
| 232 | 306 | switch_right_panel( $( this ) ); |
| 233 | 307 | } ); |
| @@ -232,8 +306,9 @@ | ||
| 232 | 306 | switch_right_panel( $( this ) ); |
| 233 | 307 | } ); |
| 234 | 308 | |
| 235 | 309 | $( document ).on( 'click', '[data-wpbc-add-appointment-start-over]', function () { |
| 310 | + clear_cost_correction(); | |
| 236 | 311 | var $action = $( '.wpbc_add_appointment__canvas [data-wpbc-appointment-action="start-over"]' ).first(); |
| 237 | 312 | if ( ! $action.length ) { |
| 238 | 313 | $action = $( '.wpbc_add_appointment__canvas [data-appointment-back="service"]' ).first(); |
| 239 | 314 | } |
| @@ -240,8 +315,9 @@ | ||
| 240 | 315 | if ( $action.length ) { |
| 241 | 316 | $action.trigger( 'click' ); |
| 242 | 317 | } |
| 243 | 318 | } ); |
| 319 | + $( document ).on( 'click', '.wpbc_add_appointment__canvas [data-wpbc-appointment-action="start-over"]', clear_cost_correction ); | |
| 244 | 320 | |
| 245 | 321 | $( document ).on( 'click', '[data-wpbc-add-appointment-open-group]', function ( event ) { |
| 246 | 322 | event.preventDefault(); |
| 247 | 323 | open_inspector_group( $( this ).attr( 'data-wpbc-add-appointment-open-group' ), $( this ).attr( 'data-wpbc-add-appointment-focus' ) || '' ); |
| @@ -248,13 +324,18 @@ | ||
| 248 | 324 | } ); |
| 249 | 325 | |
| 250 | 326 | $( document ).on( 'click', '[data-wpbc-add-appointment-autofill]', auto_fill_booking_form ); |
| 251 | 327 | $( document ).on( 'input change wpbc_booking_date_or_option_selected', '.wpbc_add_appointment__canvas, #is_send_email_for_pending', schedule_summary_refresh ); |
| 328 | + $( document ).on( 'input', '#wpbc_add_appointment_cost_correction', synchronize_cost_correction_range ); | |
| 329 | + $( document ).on( 'input', '.wpbc_add_appointment__cost_correction [data-wpbc-admin-cost-correction-range="1"]', synchronize_cost_correction_number ); | |
| 330 | + $( 'body' ).on( 'wpbc_before_booking_create.wpbc_add_appointment_cost_correction', add_cost_correction_to_appointment_request ); | |
| 331 | + $( 'body' ).on( 'wpbc_booking_form_submit_success.wpbc_add_appointment_cost_correction', clear_cost_correction ); | |
| 252 | 332 | |
| 253 | 333 | $( function () { |
| 254 | 334 | var stage = document.querySelector( '.wpbc_add_appointment__canvas .wpbc_booking_appointment__stage' ); |
| 255 | 335 | apply_booking_context(); |
| 256 | 336 | refresh_summary(); |
| 337 | + synchronize_cost_correction_range(); | |
| 257 | 338 | if ( stage && window.MutationObserver ) { |
| 258 | 339 | new MutationObserver( schedule_summary_refresh ).observe( stage, { childList: true, subtree: true } ); |
| 259 | 340 | } |
| 260 | 341 | } ); |