| 1 |
/** |
| 2 |
* First-install Setup Wizard invitation adapter. |
| 3 |
* |
| 4 |
* @package Booking Calendar |
| 5 |
*/ |
| 6 |
( function( window, document, $ ) { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
var config = window.wpbc_setup_wizard_first_run_launcher_vars || {}; |
| 10 |
var request = null; |
| 11 |
var previous_focus = null; |
| 12 |
var redirect_started = false; |
| 13 |
|
| 14 |
/** |
| 15 |
* Restore focus to the pre-dialog control or a stable Booking Listing control. |
| 16 |
* |
| 17 |
* @return {void} |
| 18 |
*/ |
| 19 |
function restore_focus() { |
| 20 |
var fallback_focus; |
| 21 |
|
| 22 |
if ( previous_focus && document.body !== previous_focus && document.contains( previous_focus ) ) { |
| 23 |
previous_focus.focus(); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
fallback_focus = document.querySelector( '#wpbc_booking_listing_add_booking_button, .wpbc_ui_el__top_nav a, #wpbody-content h1' ); |
| 28 |
if ( fallback_focus && 'function' === typeof fallback_focus.focus ) { |
| 29 |
fallback_focus.focus(); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Persist one validated launcher choice once per request. |
| 35 |
* |
| 36 |
* @param {string} choice Choice identifier. |
| 37 |
* @return {jqXHR|null} Request, or null when configuration is incomplete. |
| 38 |
*/ |
| 39 |
function persist_choice( choice ) { |
| 40 |
if ( request ) { |
| 41 |
return request; |
| 42 |
} |
| 43 |
if ( ! config.ajax_url || ! config.action || ! config.nonce ) { |
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
request = $.post( config.ajax_url, { |
| 48 |
action: config.action, |
| 49 |
nonce: config.nonce, |
| 50 |
choice: choice |
| 51 |
} ); |
| 52 |
|
| 53 |
return request; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Render and open the invitation when all runtime dependencies exist. |
| 58 |
* |
| 59 |
* @return {void} |
| 60 |
*/ |
| 61 |
function show_dialog() { |
| 62 |
var template; |
| 63 |
var $modal; |
| 64 |
var choice_started = false; |
| 65 |
var selected_choice = ''; |
| 66 |
var request_finished = false; |
| 67 |
var request_saved = false; |
| 68 |
var modal_hidden = false; |
| 69 |
|
| 70 |
/** |
| 71 |
* Continue after both persistence and modal removal finish. |
| 72 |
* |
| 73 |
* @return {void} |
| 74 |
*/ |
| 75 |
function finish_dismissal() { |
| 76 |
if ( ! request_finished || ! modal_hidden ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( request_saved && config.show_booking_pages_after_dismiss ) { |
| 81 |
$( document ).trigger( 'wpbc:setup-booking-pages-open', [ { |
| 82 |
consume_prompt: true, |
| 83 |
return_focus: previous_focus |
| 84 |
} ] ); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
restore_focus(); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! config.setup_url || ! window.wp || 'function' !== typeof window.wp.template || 'function' !== typeof $.fn.wpbc_my_modal || ! document.getElementById( 'tmpl-wpbc-setup-wizard-first-run-popup' ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
previous_focus = document.activeElement; |
| 96 |
template = window.wp.template( 'wpbc-setup-wizard-first-run-popup' ); |
| 97 |
$( document.body ).append( template( {} ) ); |
| 98 |
$modal = $( '#wpbc_setup_wizard_first_run_popup' ); |
| 99 |
|
| 100 |
$modal.on( 'shown.wpbc.modal.wpbcSetupFirstRun', function() { |
| 101 |
$modal.attr( 'aria-hidden', 'false' ); |
| 102 |
$( '.modal-backdrop' ).last().addClass( 'wpbc_setup_wizard_first_run_popup__backdrop' ); |
| 103 |
$modal.find( '[data-wpbc-setup-first-run-start]' ).trigger( 'focus' ); |
| 104 |
} ); |
| 105 |
|
| 106 |
$modal.on( 'hide.wpbc.modal.wpbcSetupFirstRun', function() { |
| 107 |
var dismissal_request; |
| 108 |
|
| 109 |
if ( choice_started ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
choice_started = true; |
| 113 |
selected_choice = 'dismiss'; |
| 114 |
dismissal_request = persist_choice( selected_choice ); |
| 115 |
if ( ! dismissal_request ) { |
| 116 |
request_finished = true; |
| 117 |
return; |
| 118 |
} |
| 119 |
dismissal_request.done( function( response ) { |
| 120 |
request_saved = !! ( response && true === response.success ); |
| 121 |
} ).always( function() { |
| 122 |
request_finished = true; |
| 123 |
finish_dismissal(); |
| 124 |
} ); |
| 125 |
} ); |
| 126 |
|
| 127 |
$modal.on( 'hidden.wpbc.modal.wpbcSetupFirstRun', function() { |
| 128 |
$modal.attr( 'aria-hidden', 'true' ).remove(); |
| 129 |
modal_hidden = true; |
| 130 |
if ( 'dismiss' === selected_choice ) { |
| 131 |
finish_dismissal(); |
| 132 |
} |
| 133 |
} ); |
| 134 |
|
| 135 |
$modal.on( 'click.wpbcSetupFirstRun', '[data-wpbc-setup-first-run-dismiss]', function( event ) { |
| 136 |
event.preventDefault(); |
| 137 |
$modal.wpbc_my_modal( 'hide' ); |
| 138 |
} ); |
| 139 |
|
| 140 |
$modal.on( 'click.wpbcSetupFirstRun', '[data-wpbc-setup-first-run-start]', function( event ) { |
| 141 |
var start_request; |
| 142 |
|
| 143 |
event.preventDefault(); |
| 144 |
choice_started = true; |
| 145 |
selected_choice = 'start'; |
| 146 |
start_request = persist_choice( selected_choice ); |
| 147 |
if ( start_request ) { |
| 148 |
start_request.always( function() { |
| 149 |
if ( ! redirect_started ) { |
| 150 |
redirect_started = true; |
| 151 |
window.location.assign( config.setup_url ); |
| 152 |
} |
| 153 |
} ); |
| 154 |
} else if ( ! redirect_started ) { |
| 155 |
redirect_started = true; |
| 156 |
window.location.assign( config.setup_url ); |
| 157 |
} |
| 158 |
$modal.wpbc_my_modal( 'hide' ); |
| 159 |
} ); |
| 160 |
|
| 161 |
$modal.wpbc_my_modal( { backdrop: 'static', keyboard: true, show: true } ); |
| 162 |
} |
| 163 |
|
| 164 |
$( function() { |
| 165 |
if ( config.show ) { |
| 166 |
show_dialog(); |
| 167 |
} |
| 168 |
} ); |
| 169 |
}( window, document, window.jQuery ) ); |
| 170 |
|