| 1 |
/** |
| 2 |
* BFB Publish modal controller. |
| 3 |
* |
| 4 |
* Responsibilities: |
| 5 |
* - Open Publish modal from toolbar button. |
| 6 |
* - Support legacy inline call: wpbc_modal_dialog__show__resource_publish( resource_id ). |
| 7 |
* - Keep shortcode in sync with current form name. |
| 8 |
* - Ask user to save current form before publishing. |
| 9 |
* - Publish shortcode into existing/new page via AJAX. |
| 10 |
* - Show success/error message without page reload. |
| 11 |
* |
| 12 |
* @package Booking Calendar |
| 13 |
* @subpackage Booking Form Builder |
| 14 |
* @since 11.0.0 |
| 15 |
* @file ../includes/page-form-builder/publish/_out/bfb-publish.js |
| 16 |
*/ |
| 17 |
(function ( $, window, document ) { |
| 18 |
'use strict'; |
| 19 |
|
| 20 |
var publish_api = { |
| 21 |
|
| 22 |
/** |
| 23 |
* Init module. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
init: function() { |
| 28 |
this.bind_events(); |
| 29 |
this.bind_form_ajax_loaded_events(); |
| 30 |
this.register_global_helpers(); |
| 31 |
this.refresh_publish_button_shortcodes( this.get_current_form_name() ); |
| 32 |
}, |
| 33 |
|
| 34 |
/** |
| 35 |
* Get localized vars. |
| 36 |
* |
| 37 |
* @return {Object} |
| 38 |
*/ |
| 39 |
get_vars: function() { |
| 40 |
return window.wpbc_bfb_publish_vars || {}; |
| 41 |
}, |
| 42 |
|
| 43 |
/** |
| 44 |
* Get i18n vars. |
| 45 |
* |
| 46 |
* @return {Object} |
| 47 |
*/ |
| 48 |
get_i18n: function() { |
| 49 |
var vars = this.get_vars(); |
| 50 |
|
| 51 |
return vars.i18n || {}; |
| 52 |
}, |
| 53 |
|
| 54 |
/** |
| 55 |
* Get modal jQuery object. |
| 56 |
* |
| 57 |
* @return {jQuery} |
| 58 |
*/ |
| 59 |
get_modal: function() { |
| 60 |
var vars = this.get_vars(); |
| 61 |
var selector = vars.modal_selector || '#wpbc_bfb_modal__publish'; |
| 62 |
|
| 63 |
return $( selector ); |
| 64 |
}, |
| 65 |
|
| 66 |
/** |
| 67 |
* Sanitize form name similar to WP sanitize_key(). |
| 68 |
* |
| 69 |
* @param {string} form_name Form name. |
| 70 |
* |
| 71 |
* @return {string} |
| 72 |
*/ |
| 73 |
sanitize_form_name: function( form_name ) { |
| 74 |
form_name = String( form_name || '' ).toLowerCase(); |
| 75 |
form_name = form_name.replace( /[^a-z0-9_-]/g, '' ); |
| 76 |
|
| 77 |
if ( ! form_name ) { |
| 78 |
form_name = this.get_vars().default_form_name || 'standard'; |
| 79 |
} |
| 80 |
|
| 81 |
return form_name; |
| 82 |
}, |
| 83 |
|
| 84 |
/** |
| 85 |
* Normalize resource ID. |
| 86 |
* |
| 87 |
* @param {number|string} resource_id Resource ID. |
| 88 |
* |
| 89 |
* @return {number} |
| 90 |
*/ |
| 91 |
normalize_resource_id: function( resource_id ) { |
| 92 |
resource_id = parseInt( resource_id, 10 ); |
| 93 |
|
| 94 |
if ( ! resource_id || resource_id < 1 ) { |
| 95 |
resource_id = parseInt( this.get_vars().default_resource_id || 1, 10 ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! resource_id || resource_id < 1 ) { |
| 99 |
resource_id = 1; |
| 100 |
} |
| 101 |
|
| 102 |
return resource_id; |
| 103 |
}, |
| 104 |
|
| 105 |
/** |
| 106 |
* Get current BFB form name. |
| 107 |
* |
| 108 |
* @return {string} |
| 109 |
*/ |
| 110 |
get_current_form_name: function() { |
| 111 |
var vars = this.get_vars(); |
| 112 |
var form_name = ''; |
| 113 |
|
| 114 |
if ( window.WPBC_BFB_Ajax && window.WPBC_BFB_Ajax.form_name ) { |
| 115 |
form_name = window.WPBC_BFB_Ajax.form_name; |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! form_name && vars.default_form_name ) { |
| 119 |
form_name = vars.default_form_name; |
| 120 |
} |
| 121 |
|
| 122 |
return this.sanitize_form_name( form_name || 'standard' ); |
| 123 |
}, |
| 124 |
|
| 125 |
/** |
| 126 |
* Build default shortcode raw. |
| 127 |
* |
| 128 |
* @param {number|string} resource_id Resource ID. |
| 129 |
* @param {string} form_name Form name. |
| 130 |
* |
| 131 |
* @return {string} |
| 132 |
*/ |
| 133 |
build_default_shortcode_raw: function( resource_id, form_name ) { |
| 134 |
resource_id = this.normalize_resource_id( resource_id ); |
| 135 |
form_name = this.sanitize_form_name( form_name ); |
| 136 |
|
| 137 |
return "[booking resource_id=" + resource_id + " form_type='" + form_name + "']"; |
| 138 |
}, |
| 139 |
|
| 140 |
/** |
| 141 |
* Upsert one shortcode attribute. |
| 142 |
* |
| 143 |
* @param {string} shortcode_raw Shortcode. |
| 144 |
* @param {string} attr_name Attribute name. |
| 145 |
* @param {string} attr_value Attribute value. |
| 146 |
* @param {string} quote_char Quote char. |
| 147 |
* |
| 148 |
* @return {string} |
| 149 |
*/ |
| 150 |
upsert_shortcode_attr: function( shortcode_raw, attr_name, attr_value, quote_char ) { |
| 151 |
var escaped_attr_name, pattern, replacement_value, replacement; |
| 152 |
|
| 153 |
shortcode_raw = trim_string( shortcode_raw ); |
| 154 |
attr_name = String( attr_name || '' ); |
| 155 |
attr_value = String( attr_value || '' ); |
| 156 |
quote_char = String( quote_char || '' ); |
| 157 |
|
| 158 |
if ( ! attr_name ) { |
| 159 |
return shortcode_raw; |
| 160 |
} |
| 161 |
|
| 162 |
replacement_value = attr_value; |
| 163 |
if ( quote_char ) { |
| 164 |
replacement_value = quote_char + attr_value + quote_char; |
| 165 |
} |
| 166 |
|
| 167 |
replacement = attr_name + '=' + replacement_value; |
| 168 |
escaped_attr_name = attr_name.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' ); |
| 169 |
pattern = new RegExp( "\\b" + escaped_attr_name + "\\s*=\\s*(?:\"[^\"]*\"|'[^']*'|[^\\s\\]]+)", 'i' ); |
| 170 |
|
| 171 |
if ( pattern.test( shortcode_raw ) ) { |
| 172 |
return shortcode_raw.replace( pattern, replacement ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( /\]$/.test( shortcode_raw ) ) { |
| 176 |
return shortcode_raw.replace( /\]$/, ' ' + replacement + ']' ); |
| 177 |
} |
| 178 |
|
| 179 |
return shortcode_raw + ' ' + replacement; |
| 180 |
}, |
| 181 |
|
| 182 |
/** |
| 183 |
* Normalize shortcode to current resource + form type. |
| 184 |
* |
| 185 |
* @param {string} shortcode_raw Shortcode. |
| 186 |
* @param {number|string} resource_id Resource ID. |
| 187 |
* @param {string} form_name Form name. |
| 188 |
* |
| 189 |
* @return {string} |
| 190 |
*/ |
| 191 |
normalize_shortcode_raw: function( shortcode_raw, resource_id, form_name ) { |
| 192 |
resource_id = this.normalize_resource_id( resource_id ); |
| 193 |
form_name = this.sanitize_form_name( form_name ); |
| 194 |
shortcode_raw = trim_string( shortcode_raw ); |
| 195 |
|
| 196 |
if ( ( ! shortcode_raw || 0 !== shortcode_raw.indexOf( '[booking' ) ) ) { |
| 197 |
return this.build_default_shortcode_raw( resource_id, form_name ); |
| 198 |
} |
| 199 |
|
| 200 |
shortcode_raw = this.upsert_shortcode_attr( shortcode_raw, 'resource_id', String( resource_id ), '' ); |
| 201 |
shortcode_raw = this.upsert_shortcode_attr( shortcode_raw, 'form_type', form_name, '\'' ); |
| 202 |
|
| 203 |
return trim_string( shortcode_raw ); |
| 204 |
}, |
| 205 |
|
| 206 |
/** |
| 207 |
* Refresh toolbar button shortcode attributes. |
| 208 |
* |
| 209 |
* @param {string} form_name Form name. |
| 210 |
* |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
refresh_publish_button_shortcodes: function( form_name ) { |
| 214 |
var self = this; |
| 215 |
var vars = this.get_vars(); |
| 216 |
var $buttons = $( '[data-wpbc-bfb-top-publish-btn="1"]' ); |
| 217 |
|
| 218 |
form_name = this.sanitize_form_name( form_name ); |
| 219 |
|
| 220 |
$buttons.each( |
| 221 |
function() { |
| 222 |
var $button = $( this ); |
| 223 |
var resource_id = self.normalize_resource_id( $button.attr( 'data-wpbc-bfb-resource-id' ) || vars.default_resource_id || 1 ); |
| 224 |
var shortcode_raw = $button.attr( 'data-wpbc-bfb-shortcode-raw' ) || vars.default_shortcode_raw || ''; |
| 225 |
|
| 226 |
shortcode_raw = self.normalize_shortcode_raw( shortcode_raw, resource_id, form_name ); |
| 227 |
|
| 228 |
$button.attr( 'data-wpbc-bfb-shortcode-raw', shortcode_raw ); |
| 229 |
$button.attr( 'data-wpbc-bfb-form-name', form_name ); |
| 230 |
} |
| 231 |
); |
| 232 |
}, |
| 233 |
|
| 234 |
/** |
| 235 |
* Keep publish shortcode in sync after AJAX form load. |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
bind_form_ajax_loaded_events: function() { |
| 240 |
var self = this; |
| 241 |
|
| 242 |
if ( window.__wpbc_bfb_publish__form_ajax_loaded_bound === '1' ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
window.__wpbc_bfb_publish__form_ajax_loaded_bound = '1'; |
| 246 |
|
| 247 |
document.addEventListener( |
| 248 |
'wpbc:bfb:form:ajax_loaded', |
| 249 |
function( ev ) { |
| 250 |
var detail = ( ev && ev.detail ) ? ev.detail : {}; |
| 251 |
var form_name = self.sanitize_form_name( detail.form_name || self.get_current_form_name() ); |
| 252 |
var $modal = self.get_modal(); |
| 253 |
var resource_id, shortcode_raw; |
| 254 |
|
| 255 |
window.WPBC_BFB_Ajax = window.WPBC_BFB_Ajax || {}; |
| 256 |
window.WPBC_BFB_Ajax.form_name = form_name; |
| 257 |
|
| 258 |
self.refresh_publish_button_shortcodes( form_name ); |
| 259 |
|
| 260 |
if ( $modal.length ) { |
| 261 |
resource_id = $modal.find( '#wpbc_bfb_publish__resource_id' ).val() || self.get_vars().default_resource_id || 1; |
| 262 |
shortcode_raw = $modal.find( '#wpbc_bfb_publish__shortcode_raw' ).val() || self.get_vars().default_shortcode_raw || ''; |
| 263 |
|
| 264 |
$modal.find( '#wpbc_bfb_publish__form_name' ).val( form_name ); |
| 265 |
$modal.find( '#wpbc_bfb_publish__shortcode_raw' ).val( |
| 266 |
self.normalize_shortcode_raw( shortcode_raw, resource_id, form_name ) |
| 267 |
); |
| 268 |
} |
| 269 |
}, |
| 270 |
{ passive: true } |
| 271 |
); |
| 272 |
}, |
| 273 |
|
| 274 |
/** |
| 275 |
* Bind DOM events. |
| 276 |
* |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
bind_events: function() { |
| 280 |
var self = this; |
| 281 |
|
| 282 |
$( document ).on( |
| 283 |
'click', |
| 284 |
'[data-wpbc-bfb-top-publish-btn="1"]', |
| 285 |
function( event ) { |
| 286 |
self.open_from_button( event, $( this ) ); |
| 287 |
} |
| 288 |
); |
| 289 |
|
| 290 |
$( document ).on( |
| 291 |
'click', |
| 292 |
'[data-wpbc-bfb-publish-save-step="save"]', |
| 293 |
function( event ) { |
| 294 |
self.start_save_and_continue( event, $( this ) ); |
| 295 |
} |
| 296 |
); |
| 297 |
|
| 298 |
$( document ).on( |
| 299 |
'click', |
| 300 |
'[data-wpbc-bfb-publish-save-step="skip"]', |
| 301 |
function( event ) { |
| 302 |
event.preventDefault(); |
| 303 |
self.show_chooser_step(); |
| 304 |
} |
| 305 |
); |
| 306 |
|
| 307 |
$( document ).on( |
| 308 |
'click', |
| 309 |
'[data-wpbc-bfb-publish-mode]', |
| 310 |
function( event ) { |
| 311 |
self.open_mode_panel( event, $( this ).attr( 'data-wpbc-bfb-publish-mode' ) ); |
| 312 |
} |
| 313 |
); |
| 314 |
|
| 315 |
$( document ).on( |
| 316 |
'click', |
| 317 |
'[data-wpbc-bfb-publish-submit]', |
| 318 |
function( event ) { |
| 319 |
self.submit_publish( event, $( this ).attr( 'data-wpbc-bfb-publish-submit' ) ); |
| 320 |
} |
| 321 |
); |
| 322 |
|
| 323 |
$( document ).on( |
| 324 |
'click', |
| 325 |
'[data-wpbc-bfb-publish-back="1"]', |
| 326 |
function( event ) { |
| 327 |
self.go_back( event ); |
| 328 |
} |
| 329 |
); |
| 330 |
}, |
| 331 |
|
| 332 |
/** |
| 333 |
* Register global helpers for backward compatibility. |
| 334 |
* |
| 335 |
* @return void |
| 336 |
*/ |
| 337 |
register_global_helpers: function() { |
| 338 |
var self = this; |
| 339 |
|
| 340 |
window.wpbc_bfb_publish__open = function( resource_id, shortcode_raw ) { |
| 341 |
self.open_modal( resource_id, shortcode_raw ); |
| 342 |
}; |
| 343 |
|
| 344 |
window.wpbc_modal_dialog__show__resource_publish = function( resource_id ) { |
| 345 |
self.open_modal( resource_id, '' ); |
| 346 |
}; |
| 347 |
}, |
| 348 |
|
| 349 |
/** |
| 350 |
* Reset modal to default state. |
| 351 |
* |
| 352 |
* @return void |
| 353 |
*/ |
| 354 |
reset_modal: function() { |
| 355 |
var $modal = this.get_modal(); |
| 356 |
|
| 357 |
if ( ! $modal.length ) { |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
$modal.find( '.wpbc_bfb_publish__notice' ).html( '' ); |
| 362 |
$modal.find( '.wpbc_bfb_publish__save_step' ).hide(); |
| 363 |
$modal.find( '.wpbc_bfb_publish__chooser' ).hide(); |
| 364 |
$modal.find( '.wpbc_bfb_publish__panel' ).hide(); |
| 365 |
$modal.find( '.modal-footer' ).hide(); |
| 366 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 367 |
|
| 368 |
$modal.find( '[data-wpbc-bfb-publish-open-page="1"]' ).hide().attr( 'href', '#' ); |
| 369 |
$modal.find( '[data-wpbc-bfb-publish-edit-page="1"]' ).hide().attr( 'href', '#' ); |
| 370 |
}, |
| 371 |
|
| 372 |
/** |
| 373 |
* Show initial save step. |
| 374 |
* |
| 375 |
* @return void |
| 376 |
*/ |
| 377 |
show_save_step: function() { |
| 378 |
var $modal = this.get_modal(); |
| 379 |
|
| 380 |
if ( ! $modal.length ) { |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
$modal.find( '.wpbc_bfb_publish__chooser' ).hide(); |
| 385 |
$modal.find( '.wpbc_bfb_publish__panel' ).hide(); |
| 386 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 387 |
$modal.find( '.wpbc_bfb_publish__save_step' ).show(); |
| 388 |
$modal.find( '.modal-footer' ).hide(); |
| 389 |
}, |
| 390 |
|
| 391 |
/** |
| 392 |
* Show chooser step. |
| 393 |
* |
| 394 |
* @return void |
| 395 |
*/ |
| 396 |
show_chooser_step: function() { |
| 397 |
var $modal = this.get_modal(); |
| 398 |
|
| 399 |
if ( ! $modal.length ) { |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
$modal.find( '.wpbc_bfb_publish__notice' ).html( '' ); |
| 404 |
$modal.find( '.wpbc_bfb_publish__save_step' ).hide(); |
| 405 |
$modal.find( '.wpbc_bfb_publish__panel' ).hide(); |
| 406 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 407 |
$modal.find( '.wpbc_bfb_publish__chooser' ).show(); |
| 408 |
$modal.find( '.modal-footer' ).show(); |
| 409 |
}, |
| 410 |
|
| 411 |
/** |
| 412 |
* Open modal from toolbar button. |
| 413 |
* |
| 414 |
* @param {Object} event Click event. |
| 415 |
* @param {jQuery} $button Button. |
| 416 |
* |
| 417 |
* @return void |
| 418 |
*/ |
| 419 |
open_from_button: function( event, $button ) { |
| 420 |
var vars = this.get_vars(); |
| 421 |
var resource_id; |
| 422 |
var shortcode_raw; |
| 423 |
var form_name; |
| 424 |
|
| 425 |
event.preventDefault(); |
| 426 |
|
| 427 |
resource_id = $button.attr( 'data-wpbc-bfb-resource-id' ) || vars.default_resource_id || 1; |
| 428 |
form_name = this.get_current_form_name(); |
| 429 |
shortcode_raw = $button.attr( 'data-wpbc-bfb-shortcode-raw' ) || vars.default_shortcode_raw || ''; |
| 430 |
|
| 431 |
shortcode_raw = this.normalize_shortcode_raw( shortcode_raw, resource_id, form_name ); |
| 432 |
|
| 433 |
$button.attr( 'data-wpbc-bfb-shortcode-raw', shortcode_raw ); |
| 434 |
$button.attr( 'data-wpbc-bfb-form-name', form_name ); |
| 435 |
|
| 436 |
this.open_modal( resource_id, shortcode_raw ); |
| 437 |
}, |
| 438 |
|
| 439 |
/** |
| 440 |
* Open modal. |
| 441 |
* |
| 442 |
* @param {number|string} resource_id Resource ID. |
| 443 |
* @param {string} shortcode_raw Shortcode. |
| 444 |
* |
| 445 |
* @return void |
| 446 |
*/ |
| 447 |
open_modal: function( resource_id, shortcode_raw ) { |
| 448 |
var $modal = this.get_modal(); |
| 449 |
var vars = this.get_vars(); |
| 450 |
var form_name; |
| 451 |
|
| 452 |
if ( ! $modal.length ) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
resource_id = this.normalize_resource_id( resource_id || vars.default_resource_id || 1 ); |
| 457 |
form_name = this.get_current_form_name(); |
| 458 |
shortcode_raw = this.normalize_shortcode_raw( shortcode_raw || vars.default_shortcode_raw || '', resource_id, form_name ); |
| 459 |
|
| 460 |
this.reset_modal(); |
| 461 |
|
| 462 |
$modal.find( '#wpbc_bfb_publish__resource_id' ).val( resource_id ); |
| 463 |
$modal.find( '#wpbc_bfb_publish__form_name' ).val( form_name ); |
| 464 |
$modal.find( '#wpbc_bfb_publish__shortcode_raw' ).val( shortcode_raw ); |
| 465 |
$modal.find( '#wpbc_bfb_publish_page_title' ).val( '' ); |
| 466 |
$modal.find( '#wpbc_bfb_publish_page_id' ).val( '0' ); |
| 467 |
|
| 468 |
if ( typeof $modal.wpbc_my_modal === 'function' ) { |
| 469 |
$modal.wpbc_my_modal( 'show' ); |
| 470 |
} else { |
| 471 |
$modal.show(); |
| 472 |
} |
| 473 |
|
| 474 |
if ( parseInt( vars.is_demo || 0, 10 ) !== 1 ) { |
| 475 |
this.show_save_step(); |
| 476 |
} |
| 477 |
}, |
| 478 |
|
| 479 |
/** |
| 480 |
* Open selected mode panel. |
| 481 |
* |
| 482 |
* @param {Object} event Click event. |
| 483 |
* @param {string} mode Mode. |
| 484 |
* |
| 485 |
* @return void |
| 486 |
*/ |
| 487 |
open_mode_panel: function( event, mode ) { |
| 488 |
var $modal = this.get_modal(); |
| 489 |
|
| 490 |
event.preventDefault(); |
| 491 |
|
| 492 |
if ( ! $modal.length ) { |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
$modal.find( '.wpbc_bfb_publish__notice' ).html( '' ); |
| 497 |
$modal.find( '.wpbc_bfb_publish__save_step' ).hide(); |
| 498 |
$modal.find( '.wpbc_bfb_publish__chooser' ).hide(); |
| 499 |
$modal.find( '.wpbc_bfb_publish__panel' ).hide(); |
| 500 |
$modal.find( '.wpbc_bfb_publish__panel--' + mode ).show(); |
| 501 |
$modal.find( '.modal-footer' ).show(); |
| 502 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 503 |
}, |
| 504 |
|
| 505 |
/** |
| 506 |
* Return from current step. |
| 507 |
* |
| 508 |
* @param {Object} event Click event. |
| 509 |
* |
| 510 |
* @return void |
| 511 |
*/ |
| 512 |
go_back: function( event ) { |
| 513 |
var $modal = this.get_modal(); |
| 514 |
|
| 515 |
event.preventDefault(); |
| 516 |
|
| 517 |
if ( ! $modal.length ) { |
| 518 |
return; |
| 519 |
} |
| 520 |
|
| 521 |
$modal.find( '.wpbc_bfb_publish__notice' ).html( '' ); |
| 522 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 523 |
|
| 524 |
if ( $modal.find( '.wpbc_bfb_publish__panel:visible' ).length ) { |
| 525 |
this.show_chooser_step(); |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
if ( $modal.find( '.wpbc_bfb_publish__chooser:visible' ).length ) { |
| 530 |
this.show_save_step(); |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
this.show_save_step(); |
| 535 |
}, |
| 536 |
|
| 537 |
/** |
| 538 |
* Render notice inside modal. |
| 539 |
* |
| 540 |
* @param {string} type success | error | info |
| 541 |
* @param {string} message Message HTML. |
| 542 |
* |
| 543 |
* @return void |
| 544 |
*/ |
| 545 |
render_notice: function( type, message ) { |
| 546 |
var $modal = this.get_modal(); |
| 547 |
var css_class = 'notice-info'; |
| 548 |
|
| 549 |
if ( ! $modal.length ) { |
| 550 |
return; |
| 551 |
} |
| 552 |
|
| 553 |
if ( 'success' === type ) { |
| 554 |
css_class = 'notice-success'; |
| 555 |
} else if ( 'error' === type ) { |
| 556 |
css_class = 'notice-error'; |
| 557 |
} |
| 558 |
|
| 559 |
$modal.find( '.wpbc_bfb_publish__notice' ).html( |
| 560 |
'<div class="wpbc-settings-notice notice ' + css_class + '" style="text-align:left;font-size:1rem;margin-top:0;">' + |
| 561 |
message + |
| 562 |
'</div>' |
| 563 |
); |
| 564 |
}, |
| 565 |
|
| 566 |
/** |
| 567 |
* Start save flow and continue to chooser after save. |
| 568 |
* |
| 569 |
* @param {Object} event Click event. |
| 570 |
* @param {jQuery} $save_button Save button. |
| 571 |
* |
| 572 |
* @return void |
| 573 |
*/ |
| 574 |
start_save_and_continue: function( event, $save_button ) { |
| 575 |
var self = this; |
| 576 |
var vars = this.get_vars(); |
| 577 |
var i18n = this.get_i18n(); |
| 578 |
var save_fn = window.wpbc_bfb__ajax_save_current_form; |
| 579 |
var save_result = null; |
| 580 |
var has_async_handle = false; |
| 581 |
var finished = false; |
| 582 |
var busy_seen = false; |
| 583 |
var poll_id = null; |
| 584 |
var quick_id = null; |
| 585 |
var timeout_id = null; |
| 586 |
var event_names, event_handlers; |
| 587 |
|
| 588 |
event.preventDefault(); |
| 589 |
|
| 590 |
if ( parseInt( vars.is_demo || 0, 10 ) === 1 ) { |
| 591 |
this.render_notice( 'error', i18n.demo_error || 'This operation is restricted in the demo version.' ); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
if ( 'function' !== typeof save_fn ) { |
| 596 |
this.render_notice( 'error', i18n.save_fn_missing || 'Save function is not available. You can use Skip to continue without saving.' ); |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
this.render_notice( 'info', i18n.saving_form || 'Saving booking form...' ); |
| 601 |
$save_button.prop( 'disabled', true ).addClass( 'disabled' ); |
| 602 |
|
| 603 |
event_names = array_or_list_to_array( [ |
| 604 |
'wpbc:bfb:form:ajax_saved', |
| 605 |
'wpbc:bfb:form:saved', |
| 606 |
'wpbc:bfb:save:done', |
| 607 |
'wpbc:bfb:ajax_saved' |
| 608 |
] ); |
| 609 |
event_handlers = []; |
| 610 |
|
| 611 |
function cleanup() { |
| 612 |
var i; |
| 613 |
|
| 614 |
if ( poll_id ) { |
| 615 |
window.clearInterval( poll_id ); |
| 616 |
} |
| 617 |
if ( quick_id ) { |
| 618 |
window.clearTimeout( quick_id ); |
| 619 |
} |
| 620 |
if ( timeout_id ) { |
| 621 |
window.clearTimeout( timeout_id ); |
| 622 |
} |
| 623 |
|
| 624 |
for ( i = 0; i < event_names.length; i++ ) { |
| 625 |
if ( event_handlers[i] ) { |
| 626 |
document.removeEventListener( event_names[i], event_handlers[i] ); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
$save_button.prop( 'disabled', false ).removeClass( 'disabled' ); |
| 631 |
} |
| 632 |
|
| 633 |
function finish_success() { |
| 634 |
if ( finished ) { |
| 635 |
return; |
| 636 |
} |
| 637 |
finished = true; |
| 638 |
|
| 639 |
cleanup(); |
| 640 |
|
| 641 |
self.refresh_publish_button_shortcodes( self.get_current_form_name() ); |
| 642 |
self.render_notice( 'success', i18n.save_success || 'Booking form has been saved. Continue with publishing.' ); |
| 643 |
self.show_chooser_step(); |
| 644 |
} |
| 645 |
|
| 646 |
function finish_error( message ) { |
| 647 |
if ( finished ) { |
| 648 |
return; |
| 649 |
} |
| 650 |
finished = true; |
| 651 |
|
| 652 |
cleanup(); |
| 653 |
|
| 654 |
self.render_notice( 'error', message || i18n.save_failed || 'Unable to confirm that the booking form was saved.' ); |
| 655 |
} |
| 656 |
|
| 657 |
try { |
| 658 |
save_result = save_fn( $save_button.get( 0 ) ); |
| 659 |
} catch ( err ) { |
| 660 |
finish_error( i18n.save_failed || 'Unable to confirm that the booking form was saved.' ); |
| 661 |
return; |
| 662 |
} |
| 663 |
|
| 664 |
if ( true === save_result ) { |
| 665 |
finish_success(); |
| 666 |
return; |
| 667 |
} |
| 668 |
|
| 669 |
if ( false === save_result ) { |
| 670 |
finish_error( i18n.save_failed || 'Unable to confirm that the booking form was saved.' ); |
| 671 |
return; |
| 672 |
} |
| 673 |
|
| 674 |
if ( save_result && 'function' === typeof save_result.then ) { |
| 675 |
has_async_handle = true; |
| 676 |
save_result.then( |
| 677 |
function() { |
| 678 |
finish_success(); |
| 679 |
}, |
| 680 |
function() { |
| 681 |
finish_error( i18n.save_failed || 'Unable to confirm that the booking form was saved.' ); |
| 682 |
} |
| 683 |
); |
| 684 |
} else if ( save_result && 'function' === typeof save_result.done && 'function' === typeof save_result.fail ) { |
| 685 |
has_async_handle = true; |
| 686 |
save_result |
| 687 |
.done( |
| 688 |
function() { |
| 689 |
finish_success(); |
| 690 |
} |
| 691 |
) |
| 692 |
.fail( |
| 693 |
function() { |
| 694 |
finish_error( i18n.save_failed || 'Unable to confirm that the booking form was saved.' ); |
| 695 |
} |
| 696 |
); |
| 697 |
} |
| 698 |
|
| 699 |
event_names.forEach( |
| 700 |
function( event_name, index ) { |
| 701 |
event_handlers[index] = function() { |
| 702 |
finish_success(); |
| 703 |
}; |
| 704 |
document.addEventListener( event_name, event_handlers[index] ); |
| 705 |
} |
| 706 |
); |
| 707 |
|
| 708 |
poll_id = window.setInterval( |
| 709 |
function() { |
| 710 |
if ( finished ) { |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
if ( $save_button.hasClass( 'wpbc-is-busy' ) ) { |
| 715 |
busy_seen = true; |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
if ( busy_seen ) { |
| 720 |
finish_success(); |
| 721 |
} |
| 722 |
}, |
| 723 |
250 |
| 724 |
); |
| 725 |
|
| 726 |
quick_id = window.setTimeout( |
| 727 |
function() { |
| 728 |
if ( finished ) { |
| 729 |
return; |
| 730 |
} |
| 731 |
|
| 732 |
if ( ! has_async_handle && ! busy_seen ) { |
| 733 |
finish_success(); |
| 734 |
} |
| 735 |
}, |
| 736 |
1000 |
| 737 |
); |
| 738 |
|
| 739 |
timeout_id = window.setTimeout( |
| 740 |
function() { |
| 741 |
if ( finished ) { |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
finish_error( i18n.save_timeout || 'Saving is taking longer than expected. You can wait a little longer or use Skip to continue without waiting.' ); |
| 746 |
}, |
| 747 |
20000 |
| 748 |
); |
| 749 |
}, |
| 750 |
|
| 751 |
/** |
| 752 |
* Show success action buttons. |
| 753 |
* |
| 754 |
* @param {Object} response_data AJAX success data. |
| 755 |
* |
| 756 |
* @return void |
| 757 |
*/ |
| 758 |
show_result_actions: function( response_data ) { |
| 759 |
var i18n = this.get_i18n(); |
| 760 |
var $modal = this.get_modal(); |
| 761 |
var $wrap = $modal.find( '.wpbc_bfb_publish__result_actions' ); |
| 762 |
var $view = $modal.find( '[data-wpbc-bfb-publish-open-page="1"]' ); |
| 763 |
var $edit = $modal.find( '[data-wpbc-bfb-publish-edit-page="1"]' ); |
| 764 |
|
| 765 |
if ( ! $modal.length ) { |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
$view.hide(); |
| 770 |
$edit.hide(); |
| 771 |
|
| 772 |
if ( response_data.view_url ) { |
| 773 |
$view.attr( 'href', response_data.view_url ).text( i18n.view_page || 'Open Page' ).show(); |
| 774 |
} |
| 775 |
|
| 776 |
if ( response_data.edit_url ) { |
| 777 |
$edit.attr( 'href', response_data.edit_url ).text( i18n.edit_page || 'Edit Page' ).show(); |
| 778 |
} |
| 779 |
|
| 780 |
if ( response_data.view_url || response_data.edit_url ) { |
| 781 |
$wrap.css( 'display', 'flex' ); |
| 782 |
} else { |
| 783 |
$wrap.hide(); |
| 784 |
} |
| 785 |
}, |
| 786 |
|
| 787 |
/** |
| 788 |
* Submit publish request. |
| 789 |
* |
| 790 |
* @param {Object} event Click event. |
| 791 |
* @param {string} mode Mode. |
| 792 |
* |
| 793 |
* @return void |
| 794 |
*/ |
| 795 |
submit_publish: function( event, mode ) { |
| 796 |
var self = this; |
| 797 |
var vars = this.get_vars(); |
| 798 |
var i18n = this.get_i18n(); |
| 799 |
var $modal = this.get_modal(); |
| 800 |
var resource_id; |
| 801 |
var form_name; |
| 802 |
var shortcode_raw; |
| 803 |
var page_id; |
| 804 |
var page_title; |
| 805 |
var $submit_button; |
| 806 |
|
| 807 |
event.preventDefault(); |
| 808 |
|
| 809 |
if ( ! $modal.length ) { |
| 810 |
return; |
| 811 |
} |
| 812 |
|
| 813 |
if ( parseInt( vars.is_demo || 0, 10 ) === 1 ) { |
| 814 |
this.render_notice( 'error', i18n.demo_error || 'This operation is restricted in the demo version.' ); |
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
resource_id = this.normalize_resource_id( $modal.find( '#wpbc_bfb_publish__resource_id' ).val() || vars.default_resource_id || 1 ); |
| 819 |
form_name = this.sanitize_form_name( $modal.find( '#wpbc_bfb_publish__form_name' ).val() || this.get_current_form_name() ); |
| 820 |
shortcode_raw = this.normalize_shortcode_raw( |
| 821 |
$modal.find( '#wpbc_bfb_publish__shortcode_raw' ).val() || vars.default_shortcode_raw || '', |
| 822 |
resource_id, |
| 823 |
form_name |
| 824 |
); |
| 825 |
page_id = $modal.find( '#wpbc_bfb_publish_page_id' ).val() || ''; |
| 826 |
page_title = trim_string( $modal.find( '#wpbc_bfb_publish_page_title' ).val() || '' ); |
| 827 |
$submit_button = $modal.find( '[data-wpbc-bfb-publish-submit="' + mode + '"]' ); |
| 828 |
|
| 829 |
$modal.find( '#wpbc_bfb_publish__form_name' ).val( form_name ); |
| 830 |
$modal.find( '#wpbc_bfb_publish__shortcode_raw' ).val( shortcode_raw ); |
| 831 |
|
| 832 |
if ( 'edit' === mode && ( ! page_id || '0' === page_id ) ) { |
| 833 |
this.render_notice( 'error', i18n.select_page || 'Please select an existing page.' ); |
| 834 |
return; |
| 835 |
} |
| 836 |
|
| 837 |
if ( 'create' === mode && ! page_title ) { |
| 838 |
this.render_notice( 'error', i18n.enter_page_title || 'Please enter a page title.' ); |
| 839 |
return; |
| 840 |
} |
| 841 |
|
| 842 |
this.render_notice( 'info', i18n.loading || 'Publishing booking form...' ); |
| 843 |
$modal.find( '.wpbc_bfb_publish__result_actions' ).hide(); |
| 844 |
|
| 845 |
$submit_button.prop( 'disabled', true ).addClass( 'disabled' ); |
| 846 |
|
| 847 |
$.ajax( |
| 848 |
{ |
| 849 |
url: vars.ajax_url, |
| 850 |
type: 'POST', |
| 851 |
dataType: 'json', |
| 852 |
data: { |
| 853 |
action: vars.action, |
| 854 |
nonce: vars.nonce, |
| 855 |
publish_mode: mode, |
| 856 |
resource_id: resource_id, |
| 857 |
form_name: form_name, |
| 858 |
shortcode_raw: shortcode_raw, |
| 859 |
page_id: page_id, |
| 860 |
page_title: page_title |
| 861 |
} |
| 862 |
} |
| 863 |
) |
| 864 |
.done( |
| 865 |
function( response ) { |
| 866 |
if ( response && response.success && response.data ) { |
| 867 |
self.render_notice( 'success', response.data.message || '' ); |
| 868 |
self.show_result_actions( response.data ); |
| 869 |
|
| 870 |
if ( response.data.form_name ) { |
| 871 |
window.WPBC_BFB_Ajax = window.WPBC_BFB_Ajax || {}; |
| 872 |
window.WPBC_BFB_Ajax.form_name = self.sanitize_form_name( response.data.form_name ); |
| 873 |
self.refresh_publish_button_shortcodes( response.data.form_name ); |
| 874 |
} |
| 875 |
} else if ( response && response.data && response.data.message ) { |
| 876 |
self.render_notice( 'error', response.data.message ); |
| 877 |
} else { |
| 878 |
self.render_notice( 'error', i18n.generic_error || 'An unexpected error occurred while publishing the booking form.' ); |
| 879 |
} |
| 880 |
} |
| 881 |
) |
| 882 |
.fail( |
| 883 |
function() { |
| 884 |
self.render_notice( 'error', i18n.generic_error || 'An unexpected error occurred while publishing the booking form.' ); |
| 885 |
} |
| 886 |
) |
| 887 |
.always( |
| 888 |
function() { |
| 889 |
$submit_button.prop( 'disabled', false ).removeClass( 'disabled' ); |
| 890 |
} |
| 891 |
); |
| 892 |
} |
| 893 |
}; |
| 894 |
|
| 895 |
/** |
| 896 |
* Trim string without using jQuery.trim(). |
| 897 |
* |
| 898 |
* @param {*} value Raw value. |
| 899 |
* |
| 900 |
* @return {string} |
| 901 |
*/ |
| 902 |
function trim_string( value ) { |
| 903 |
return String( value == null ? '' : value ).trim(); |
| 904 |
} |
| 905 |
|
| 906 |
/** |
| 907 |
* Normalize list helper. |
| 908 |
* |
| 909 |
* @param {*} input List-like input. |
| 910 |
* |
| 911 |
* @return {Array} |
| 912 |
*/ |
| 913 |
function array_or_list_to_array( input ) { |
| 914 |
if ( Array.isArray( input ) ) { |
| 915 |
return input; |
| 916 |
} |
| 917 |
|
| 918 |
return []; |
| 919 |
} |
| 920 |
|
| 921 |
$( function() { |
| 922 |
publish_api.init(); |
| 923 |
} ); |
| 924 |
|
| 925 |
})( jQuery, window, document ); |