PluginProbe
Booking Calendar / 11.4.3
Booking Calendar v11.4.3
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 10.11 All 204 releases
booking / includes / page-form-builder / form-settings / _src / settings_effects.js

settings_effects.js in Booking Calendar 11.4.3, at includes/page-form-builder/form-settings/_src/settings_effects.js

1,154 lines 43.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Applies effects in Canvas, after changing some settings in the right sidebar in BFB.
3 *
4 * @file ../includes/page-form-builder/form-settings/_src/settings_effects.js
5 */
6 (function (w, d) {
7 'use strict';
8
9 const Effects = (w.WPBC_BFB_Settings_Effects = w.WPBC_BFB_Settings_Effects || {});
10 const map = (Effects.map = Effects.map || Object.create( null ));
11
12 Effects.register = function (key, fn) {
13 if ( key && typeof fn === 'function' ) {
14 map[String( key )] = fn;
15 }
16 };
17
18 function get_canvas_root() {
19 return (
20 d.querySelector( '#wpbc_bfb__pages_container' ) ||
21 d.querySelector( '.wpbc_bfb__panel--preview' ) ||
22 d.getElementById( 'wpbc_bfb__preview' ) ||
23 d.body || d.documentElement
24 );
25 }
26
27 Effects.apply_one = function (key, value, ctx) {
28 const fn = map[String( key )];
29 if ( ! fn ) {
30 return;
31 }
32 try {
33 fn( value, Object.assign( { key, value, canvas: get_canvas_root() }, ctx || {} ) );
34 } catch ( e ) {
35 // keep silent in production if you prefer
36 console.error( 'WPBC Effects error:', key, e );
37 }
38 };
39
40 Effects.apply_all = function (options, ctx) {
41 if ( ! options || typeof options !== 'object' ) {
42 return;
43 }
44 Object.keys( options ).forEach( function (k) {
45 Effects.apply_one( k, options[k], Object.assign( { options: options }, ctx || {} ) );
46 } );
47 };
48
49 /**
50 * Normalize settings pack to the minimum required shape:
51 * { options: {}, css_vars: {} }
52 *
53 * @param {*} pack
54 * @return {{options:Object, css_vars:Object, bfb_options?:Object}|null}
55 */
56 Effects.normalize_pack = function (pack) {
57
58 if ( pack === null || typeof pack === 'undefined' || pack === '' ) {
59 return null;
60 }
61
62 // Parse JSON string if needed.
63 if ( typeof pack === 'string' ) {
64 try {
65 pack = JSON.parse( pack );
66 } catch ( _e ) {
67 return null;
68 }
69 }
70
71 if ( ! pack || typeof pack !== 'object' ) {
72 return null;
73 }
74
75 // If user passed just {key:value} options map, wrap it.
76 const has_shape =
77 Object.prototype.hasOwnProperty.call( pack, 'options' ) ||
78 Object.prototype.hasOwnProperty.call( pack, 'css_vars' ) ||
79 Object.prototype.hasOwnProperty.call( pack, 'bfb_options' );
80
81 if ( ! has_shape ) {
82 pack = { options: pack, css_vars: {} };
83 }
84
85 if ( ! pack.options || typeof pack.options !== 'object' ) {
86 pack.options = {};
87 }
88 if ( ! pack.css_vars || typeof pack.css_vars !== 'object' ) {
89 pack.css_vars = {};
90 }
91
92 // bfb_options is optional; keep if valid.
93 if ( pack.bfb_options && typeof pack.bfb_options !== 'object' ) {
94 delete pack.bfb_options;
95 }
96
97 return pack;
98 };
99
100 /**
101 * Re-apply settings effects after a canvas rebuild / structure load.
102 *
103 * This is needed because structure loading can replace DOM nodes that effects target.
104 *
105 * @param {*} settings_pack string|object settings_json pack (or plain options map)
106 * @param {Object} [ctx]
107 */
108 Effects.reapply_after_canvas = function (settings_pack, ctx) {
109
110 const pack = Effects.normalize_pack( settings_pack );
111 if ( ! pack ) {
112 return;
113 }
114
115 // Apply immediately (best effort).
116 Effects.apply_all( pack.options, Object.assign( { source: 'reapply_after_canvas' }, ctx || {} ) );
117 wpbc_bfb_global_form_style__apply( null, Object.assign( { source: 'reapply_after_canvas' }, ctx || {} ) );
118
119 // Some modules/hydration may run shortly after; do one more pass.
120 setTimeout( function () {
121 Effects.apply_all( pack.options, Object.assign( { source: 'reapply_after_canvas_delayed' }, ctx || {} ) );
122 wpbc_bfb_global_form_style__apply( null, Object.assign( { source: 'reapply_after_canvas_delayed' }, ctx || {} ) );
123 }, 60 );
124 };
125
126 // 1) Apply from AJAX load.
127 d.addEventListener( 'wpbc:bfb:form_settings:apply', function (e) {
128 const pack = e && e.detail ? e.detail.settings : null;
129 if ( pack && pack.options ) {
130 Effects.apply_all( pack.options, { source: 'apply' } );
131 }
132 wpbc_bfb_global_form_style__apply( null, { source: 'apply-global-style' } );
133 } );
134
135 // 2) Apply live from UI change (delegated).
136 function css_escape(value) {
137 const v = String( value == null ? '' : value );
138 if ( w.CSS && typeof w.CSS.escape === 'function' ) {
139 return w.CSS.escape( v );
140 }
141 return v.replace( /[^a-zA-Z0-9_\-]/g, '\\$&' );
142 }
143
144 function find_fs_root(el) {
145 if ( ! el || ! el.closest ) {
146 return null;
147 }
148
149 // 1) Direct: element or ancestor carries FS key (input/select/textarea writer, radio wrapper, etc.)
150 const direct = el.closest( '[data-wpbc-bfb-fs-key]' );
151 if ( direct ) {
152 return direct;
153 }
154
155 // 2) Length: event came from number/unit/range inside .wpbc_slider_len_group
156 const len_group = el.closest( '.wpbc_slider_len_group' );
157 if ( len_group ) {
158 return (
159 len_group.querySelector( 'input[data-wpbc_slider_len_writer][data-wpbc-bfb-fs-key]' ) ||
160 len_group.querySelector( 'input[data-wpbc-bfb-fs-type="length"][data-wpbc-bfb-fs-key]' ) ||
161 null
162 );
163 }
164
165 // 3) Spacing: event came from vertical/horizontal number inside .wpbc_spacing_group.
166 const spacing_group = el.closest( '.wpbc_spacing_group' );
167 if ( spacing_group ) {
168 return (
169 spacing_group.querySelector( 'input[data-wpbc_spacing_writer][data-wpbc-bfb-fs-key]' ) ||
170 spacing_group.querySelector( 'input[data-wpbc-bfb-fs-type="spacing"][data-wpbc-bfb-fs-key]' ) ||
171 null
172 );
173 }
174
175 // 4) Range: event came from range input inside .wpbc_slider_range_group
176 const range_group = el.closest( '.wpbc_slider_range_group' );
177 if ( range_group ) {
178 return (
179 range_group.querySelector( 'input[data-wpbc_slider_range_writer][data-wpbc-bfb-fs-key]' ) ||
180 range_group.querySelector( 'input[data-wpbc_slider_range_writer]' ) ||
181 null
182 );
183 }
184
185 return null;
186 }
187
188 function read_value_from_fs_root(fs_root, original_target) {
189 if ( ! fs_root ) {
190 return '';
191 }
192
193 const fs_type = String( fs_root.getAttribute( 'data-wpbc-bfb-fs-type' ) || '' );
194
195 // RADIO: read checked within wrapper.
196 if ( fs_type === 'radio' ) {
197 const control_id = fs_root.getAttribute( 'data-wpbc-bfb-fs-controlid' ) || '';
198 const selector = control_id
199 ? 'input[type="radio"][name="' + css_escape( control_id ) + '"]:checked'
200 : 'input[type="radio"]:checked';
201
202 const checked = fs_root.querySelector( selector );
203 return checked ? String( checked.value || '' ) : '';
204 }
205
206 if ( fs_type === 'spacing' ) {
207 const group = ( original_target && original_target.closest ) ? original_target.closest( '.wpbc_spacing_group' ) : fs_root.closest( '.wpbc_spacing_group' );
208 const vertical_input = group ? group.querySelector( 'input[data-wpbc_spacing_vertical]' ) : null;
209 const horizontal_input = group ? group.querySelector( 'input[data-wpbc_spacing_horizontal]' ) : null;
210 const writer = group ? group.querySelector( 'input[data-wpbc_spacing_writer]' ) : null;
211 const vertical = vertical_input ? String( vertical_input.value || '0' ) : '0';
212 const horizontal = horizontal_input ? String( horizontal_input.value || vertical ) : vertical;
213 const combined = wpbc_bfb_form_appearance__normalize_spacing_numbers( vertical, horizontal );
214
215 if ( writer ) {
216 writer.value = combined;
217 }
218
219 return combined;
220 }
221
222 // CHECKBOX / TOGGLE
223 if ( (original_target && original_target.type === 'checkbox') || fs_root.type === 'checkbox' ) {
224 const cb = (original_target && original_target.type === 'checkbox') ? original_target : fs_root;
225 return cb.checked ? 'On' : 'Off';
226 }
227
228 // DEFAULT: writer/input/textarea/select
229 if ( fs_root.value != null ) {
230 return String( fs_root.value );
231 }
232 if ( original_target && original_target.value != null ) {
233 return String( original_target.value );
234 }
235
236 return '';
237 }
238
239 function apply_change_from_target(target, event_type, event_source) {
240 if ( ! target ) { return; }
241
242 const fs_root = find_fs_root( target );
243 if ( ! fs_root ) { return; }
244
245
246 // Normalize events so each control produces exactly one effect call.
247 // - toggle/select/radio/checkbox => "change" only.
248 // - everything else => "input" only.
249 const fs_type = String( fs_root.getAttribute( 'data-wpbc-bfb-fs-type' ) || '' );
250 const tag = String( target.tagName || '' ).toLowerCase();
251 const type = String( target.type || '' ).toLowerCase();
252
253 const use_change = (fs_type === 'toggle') || (fs_type === 'select') || (fs_type === 'radio') || (type === 'checkbox') || (type === 'radio') || (tag === 'select');
254
255 if ( use_change && event_type !== 'change' ) { return; }
256 if ( ! use_change && event_type !== 'input' ) { return; }
257 // -------------------------------------------------------------------------------------------
258
259 const key = fs_root.getAttribute( 'data-wpbc-bfb-fs-key' );
260 if ( ! key ) { return; }
261
262 const scope = fs_root.getAttribute( 'data-wpbc-bfb-fs-scope' ) || '';
263 const value = read_value_from_fs_root( fs_root, target );
264
265 Effects.apply_one( key, value, { source: event_source || 'ui', scope: scope, control: target, fs_root: fs_root } );
266 }
267
268 function is_coloris_control(target) {
269 if ( ! target || ! target.matches ) {
270 return false;
271 }
272
273 return target.matches( '[data-wpbc-bfb-fs-type="color"], [data-inspector-type="color"], .wpbc_bfb_coloris' );
274 }
275
276 function on_change(ev) {
277 // Ignore generic synthetic events dispatched by code (apply/reapply, slider sync, etc.).
278 // Coloris dispatches synthetic input events while the user picks a color, so allow those color controls through.
279 if ( ev && ev.isTrusted === false && ! is_coloris_control( ev.target ) ) { return; }
280 apply_change_from_target( ev && ev.target, ev && ev.type, ( ev && ev.isTrusted === false ) ? 'coloris' : 'ui' );
281 }
282
283 d.addEventListener( 'input', on_change, false );
284 d.addEventListener( 'change', on_change, false );
285 function on_coloris_pick(ev) {
286 const detail = ev && ev.detail ? ev.detail : {};
287 const target = detail.currentEl || detail.el || detail.input || ev.target || null;
288
289 if ( ! is_coloris_control( target ) ) {
290 return;
291 }
292
293 if ( detail.color && target.value !== detail.color ) {
294 target.value = detail.color;
295 }
296
297 apply_change_from_target( target, 'input', 'coloris' );
298 }
299 d.addEventListener( 'coloris:pick', on_coloris_pick, false );
300 d.addEventListener( 'wpbc:bfb:coloris:change', on_coloris_pick, false );
301
302 })( window, document );
303
304 function wpbc_bfb_form_appearance__get_presets() {
305 return {
306 bordered: {
307 background : '#ffffff',
308 borderColor: '#cccccc',
309 borderWidth: '1px',
310 radius : '2px',
311 padding : '10px 30px',
312 shadow : 'rgba(0, 0, 0, 0.05) 0px 2px 6px 0px'
313 },
314 none : {
315 background : 'transparent',
316 borderColor: 'transparent',
317 borderWidth: '0px',
318 radius : '0px',
319 padding : '0px',
320 shadow : 'none'
321 },
322 soft : {
323 background : '#f9f9fa',
324 borderColor: '#fff',
325 borderWidth: '3px',
326 radius : '8px',
327 padding : '20px',
328 shadow : 'rgba(15, 23, 42, 0.06) 0px 4px 16px 0px'
329 }
330 };
331 }
332
333 function wpbc_bfb_form_appearance__is_dark_theme(options) {
334 options = options && typeof options === 'object' ? options : {};
335 return 'wpbc_theme_dark_1' === String( options.booking_form_theme || '' );
336 }
337
338 function wpbc_bfb_form_appearance__get_preset_for_options(style, options) {
339 const presets = wpbc_bfb_form_appearance__get_presets();
340
341 if ( ! wpbc_bfb_form_appearance__is_dark_theme( options ) ) {
342 return presets[style] || presets.bordered;
343 }
344
345 if ( 'soft' === style ) {
346 return {
347 background : '#1f2937',
348 borderColor: '#334155',
349 borderWidth: '3px',
350 radius : '8px',
351 padding : '20px',
352 shadow : 'rgba(0, 0, 0, 0.24) 0px 4px 16px 0px'
353 };
354 }
355
356 return presets[style] || presets.bordered;
357 }
358
359 function wpbc_bfb_form_appearance__sanitize_color(value, fallback) {
360 const v = String( value == null ? '' : value ).trim();
361 if ( /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i.test( v ) ) {
362 return v;
363 }
364 if ( v === 'transparent' ) {
365 return v;
366 }
367 return fallback;
368 }
369
370 function wpbc_bfb_form_appearance__sanitize_optional_color(value) {
371 return wpbc_bfb_form_appearance__sanitize_color( value, '' );
372 }
373
374 function wpbc_bfb_form_appearance__sanitize_length(value, fallback) {
375 const v = String( value == null ? '' : value ).trim();
376 if ( /^-?\d+(?:\.\d+)?(?:px|rem|em|%)$/i.test( v ) ) {
377 return v;
378 }
379 return fallback;
380 }
381
382 function wpbc_bfb_form_appearance__sanitize_spacing(value, fallback) {
383 const v = String( value == null ? '' : value ).trim().replace( /\s+/g, ' ' );
384 const parts = v ? v.split( ' ' ) : [];
385 if ( parts.length < 1 || parts.length > 4 ) {
386 return fallback;
387 }
388 for ( let i = 0; i < parts.length; i++ ) {
389 if ( ! /^-?\d+(?:\.\d+)?(?:px|rem|em|%)$/i.test( parts[i] ) ) {
390 return fallback;
391 }
392 }
393 return parts.join( ' ' );
394 }
395
396 function wpbc_bfb_form_appearance__normalize_spacing_numbers(vertical, horizontal) {
397 const v = String( vertical == null ? '' : vertical ).trim();
398 const h = String( horizontal == null ? '' : horizontal ).trim();
399 const vertical_num = /^-?\d+(?:\.\d+)?$/.test( v ) ? v : '0';
400 const horizontal_num = /^-?\d+(?:\.\d+)?$/.test( h ) ? h : vertical_num;
401
402 return vertical_num + 'px ' + horizontal_num + 'px';
403 }
404
405 function wpbc_bfb_form_appearance__collect_options(ctx, key, value) {
406 let options = (ctx && ctx.options && typeof ctx.options === 'object') ? Object.assign( {}, ctx.options ) : {};
407
408 if ( window.WPBC_BFB_FormSettings && typeof window.WPBC_BFB_FormSettings.collect === 'function' ) {
409 options = Object.assign( window.WPBC_BFB_FormSettings.collect( 'form' ) || {}, options );
410 }
411
412 if ( key ) {
413 options[String( key )] = value;
414 }
415
416 if ( wpbc_bfb_form_appearance__is_custom_control_key( key ) ) {
417 options.booking_form_container_style = 'custom';
418 wpbc_bfb_form_appearance__set_container_style_control( 'custom' );
419 }
420
421 return options;
422 }
423
424 function wpbc_bfb_form_appearance__get_custom_control_keys() {
425 return [
426 'booking_form_background_color',
427 'booking_form_border_color',
428 'booking_form_border_width',
429 'booking_form_border_radius',
430 'booking_form_padding',
431 'booking_form_text_color',
432 'booking_form_field_background_color',
433 'booking_form_field_text_color',
434 'booking_form_field_border_color'
435 ];
436 }
437
438 function wpbc_bfb_form_appearance__is_custom_control_key(key) {
439 return wpbc_bfb_form_appearance__get_custom_control_keys().indexOf( String( key || '' ) ) !== -1;
440 }
441
442 function wpbc_bfb_form_appearance__set_container_style_control(value) {
443 const control = document.querySelector( '[data-wpbc-bfb-fs-key="booking_form_container_style"]' );
444 if ( ! control || control.value === value ) {
445 return;
446 }
447
448 control.value = value;
449 }
450
451 function wpbc_bfb_form_appearance__set_radio_control(key, value) {
452 const row = document.querySelector( '.wpbc_bfb__form_setting[data-key="' + key + '"]' );
453 if ( ! row ) {
454 return;
455 }
456
457 const wrap = row.querySelector( '.wpbc_bfb__form_setting_radio[data-wpbc-bfb-fs-controlid]' );
458 const control_id = wrap ? String( wrap.getAttribute( 'data-wpbc-bfb-fs-controlid' ) || '' ) : '';
459 const radios = control_id
460 ? row.querySelectorAll( 'input[type="radio"][name="' + control_id + '"]' )
461 : row.querySelectorAll( 'input[type="radio"]' );
462
463 radios.forEach( function (radio) {
464 const should_check = ( String( radio.value ) === String( value == null ? '' : value ) );
465 radio.checked = should_check;
466
467 const choice = radio.closest ? radio.closest( '.wpbc_theme_choice' ) : null;
468 if ( choice ) {
469 choice.classList.toggle( 'is-selected', should_check );
470 }
471 } );
472 }
473
474 function wpbc_bfb_form_appearance__set_select_control(key, value) {
475 const control = document.querySelector( '[data-wpbc-bfb-fs-key="' + key + '"]' );
476 if ( control ) {
477 control.value = String( value == null ? '' : value );
478 }
479 }
480
481 function wpbc_bfb_form_appearance__get_current_options() {
482 return window.WPBC_BFB_FormSettings && typeof window.WPBC_BFB_FormSettings.collect === 'function'
483 ? window.WPBC_BFB_FormSettings.collect( 'form' ) || {}
484 : {};
485 }
486
487 function wpbc_bfb_form_appearance__get_style_value_from_options(options) {
488 options = options && typeof options === 'object' ? options : {};
489
490 const theme = String( options.booking_form_theme || '' );
491 const style = String( options.booking_form_container_style || 'inherit' );
492
493 if ( 'custom' === style ) {
494 return 'custom';
495 }
496 if ( 'inherit' === style || '' === style ) {
497 return 'inherit';
498 }
499
500 const prefix = ( 'wpbc_theme_dark_1' === theme ) ? 'dark' : 'light';
501 if ( [ 'bordered', 'none', 'soft' ].indexOf( style ) === -1 ) {
502 return prefix + '_bordered';
503 }
504
505 return prefix + '_' + style;
506 }
507
508 function wpbc_bfb_form_appearance__sync_form_style_control(options) {
509 wpbc_bfb_form_appearance__set_radio_control( 'booking_form_style', wpbc_bfb_form_appearance__get_style_value_from_options( options ) );
510 }
511
512 function wpbc_bfb_form_appearance__resolve_form_style_choice(value) {
513 const current_options = wpbc_bfb_form_appearance__get_current_options();
514 const current_theme = String( current_options.booking_form_theme || '' );
515 const choice = String( value || 'inherit' );
516
517 if ( 'custom' === choice ) {
518 return {
519 booking_form_theme : current_theme,
520 booking_form_container_style: 'custom'
521 };
522 }
523 if ( 'inherit' === choice || '' === choice ) {
524 return {
525 booking_form_theme : '',
526 booking_form_container_style: 'inherit'
527 };
528 }
529
530 const parts = choice.split( '_' );
531 const theme = ( 'dark' === parts[0] ) ? 'wpbc_theme_dark_1' : '';
532 const style = parts[1] || 'bordered';
533
534 return {
535 booking_form_theme : theme,
536 booking_form_container_style: ( [ 'bordered', 'none', 'soft' ].indexOf( style ) === -1 ) ? 'bordered' : style
537 };
538 }
539
540 function wpbc_bfb_form_appearance__is_user_theme_switch(ctx) {
541 const source = String( ctx && ctx.source ? ctx.source : '' );
542 return [ 'ui', 'coloris' ].indexOf( source ) !== -1;
543 }
544
545 function wpbc_bfb_form_appearance__is_custom_style(options) {
546 return String( options && options.booking_form_container_style ? options.booking_form_container_style : 'inherit' ) === 'custom';
547 }
548
549 function wpbc_bfb_form_appearance__sync_custom_controls(options) {
550 const is_custom = wpbc_bfb_form_appearance__is_custom_style( options );
551 const reset_row = document.querySelector( '[data-wpbc-bfb-custom-appearance-reset-row]' );
552 const base_theme_row = document.querySelector( '.wpbc_bfb__form_setting[data-key="booking_form_theme"]' );
553
554 wpbc_bfb_form_appearance__get_custom_control_keys().forEach( function (key) {
555 const row = document.querySelector( '.wpbc_bfb__form_setting[data-key="' + key + '"]' );
556 if ( ! row ) {
557 return;
558 }
559 row.hidden = ! is_custom;
560 row.setAttribute( 'aria-hidden', is_custom ? 'false' : 'true' );
561 row.classList.toggle( 'is-hidden', ! is_custom );
562 } );
563
564 if ( reset_row ) {
565 reset_row.hidden = ! is_custom;
566 reset_row.setAttribute( 'aria-hidden', is_custom ? 'false' : 'true' );
567 reset_row.classList.toggle( 'is-hidden', ! is_custom );
568 }
569
570 if ( base_theme_row ) {
571 base_theme_row.hidden = ! is_custom;
572 base_theme_row.setAttribute( 'aria-hidden', is_custom ? 'false' : 'true' );
573 base_theme_row.classList.toggle( 'is-hidden', ! is_custom );
574 }
575 }
576
577 function wpbc_bfb_form_appearance__resolve(options) {
578 let style = String( options.booking_form_container_style || 'inherit' );
579
580 if ( style === 'inherit' ) {
581 const global_options = window.wpbc_bfb_settings_vars && window.wpbc_bfb_settings_vars.global_appearance
582 ? window.wpbc_bfb_settings_vars.global_appearance
583 : {};
584 options = Object.assign( {}, global_options || {} );
585 style = String( options.booking_form_container_style || 'bordered' );
586 }
587
588 if ( style === 'bordered' ) {
589 return null;
590 }
591
592 if ( style !== 'custom' ) {
593 return wpbc_bfb_form_appearance__get_preset_for_options( style, options );
594 }
595
596 return {
597 background : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_background_color, '#ffffff' ),
598 borderColor: wpbc_bfb_form_appearance__sanitize_color( options.booking_form_border_color, '#cccccc' ),
599 borderWidth: wpbc_bfb_form_appearance__sanitize_length( options.booking_form_border_width, '1px' ),
600 radius : wpbc_bfb_form_appearance__sanitize_length( options.booking_form_border_radius, '2px' ),
601 padding : wpbc_bfb_form_appearance__sanitize_spacing( options.booking_form_padding, '10px 30px' ),
602 shadow : 'rgba(0, 0, 0, 0.05) 0px 2px 6px 0px'
603 };
604 }
605
606 function wpbc_bfb_form_appearance__resolve_design_colors(options) {
607 options = options && typeof options === 'object' ? options : {};
608
609 if ( ! wpbc_bfb_form_appearance__is_custom_style( options ) ) {
610 if (
611 wpbc_bfb_form_appearance__is_dark_theme( options ) &&
612 'none' === String( options.booking_form_container_style || '' )
613 ) {
614 return {
615 textColor : '#1d2327',
616 fieldBackground: '',
617 fieldText : '',
618 fieldBorder : ''
619 };
620 }
621 return {
622 textColor : '',
623 fieldBackground: '',
624 fieldText : '',
625 fieldBorder : ''
626 };
627 }
628
629 return {
630 textColor : wpbc_bfb_form_appearance__sanitize_optional_color( options.booking_form_text_color ),
631 fieldBackground: wpbc_bfb_form_appearance__sanitize_optional_color( options.booking_form_field_background_color ),
632 fieldText : wpbc_bfb_form_appearance__sanitize_optional_color( options.booking_form_field_text_color ),
633 fieldBorder : wpbc_bfb_form_appearance__sanitize_optional_color( options.booking_form_field_border_color )
634 };
635 }
636
637 function wpbc_bfb_form_appearance__apply_vars(value, ctx) {
638 const options = wpbc_bfb_form_appearance__collect_options( ctx, ctx && ctx.key, value );
639 const resolved = wpbc_bfb_form_appearance__resolve( options );
640 const design = wpbc_bfb_form_appearance__resolve_design_colors( options );
641 const is_custom = wpbc_bfb_form_appearance__is_custom_style( options );
642 const root = ctx && ctx.canvas;
643
644 wpbc_bfb_form_appearance__sync_form_style_control( options );
645 wpbc_bfb_form_appearance__sync_custom_controls( options );
646
647 if ( ! root || ! root.querySelectorAll ) {
648 return;
649 }
650
651 const wraps = root.querySelectorAll( '.wpbc_bfb__form_preview_section_container, .wpbc_bfb_form' );
652 if ( ! wraps.length ) {
653 return;
654 }
655
656 wraps.forEach( function (wrap) {
657 if ( ! wrap || ! wrap.style ) {
658 return;
659 }
660 wrap.classList.toggle( 'wpbc_bfb_form_appearance_custom', is_custom );
661 if ( ! resolved ) {
662 wrap.style.removeProperty( '--wpbc-bfb-form-background' );
663 wrap.style.removeProperty( '--wpbc-bfb-form-border-color' );
664 wrap.style.removeProperty( '--wpbc-bfb-form-border-width' );
665 wrap.style.removeProperty( '--wpbc-bfb-form-border-radius' );
666 wrap.style.removeProperty( '--wpbc-bfb-form-padding' );
667 wrap.style.removeProperty( '--wpbc-bfb-form-box-shadow' );
668 } else {
669 wrap.style.setProperty( '--wpbc-bfb-form-background', resolved.background );
670 wrap.style.setProperty( '--wpbc-bfb-form-border-color', resolved.borderColor );
671 wrap.style.setProperty( '--wpbc-bfb-form-border-width', resolved.borderWidth );
672 wrap.style.setProperty( '--wpbc-bfb-form-border-radius', resolved.radius );
673 wrap.style.setProperty( '--wpbc-bfb-form-padding', resolved.padding );
674 wrap.style.setProperty( '--wpbc-bfb-form-box-shadow', resolved.shadow );
675 }
676
677 if ( design.textColor ) {
678 wrap.style.setProperty( '--wpbc_form-label-color', design.textColor );
679 wrap.style.setProperty( '--wpbc_form-label-sublabel-color', design.textColor );
680 } else {
681 wrap.style.removeProperty( '--wpbc_form-label-color' );
682 wrap.style.removeProperty( '--wpbc_form-label-sublabel-color' );
683 }
684
685 if ( design.fieldBackground ) {
686 wrap.style.setProperty( '--wpbc_form-field-background-color', design.fieldBackground );
687 wrap.style.setProperty( '--wpbc_form-field-menu-color', design.fieldBackground );
688 } else {
689 wrap.style.removeProperty( '--wpbc_form-field-background-color' );
690 wrap.style.removeProperty( '--wpbc_form-field-menu-color' );
691 }
692
693 if ( design.fieldText ) {
694 wrap.style.setProperty( '--wpbc_form-field-text-color', design.fieldText );
695 } else {
696 wrap.style.removeProperty( '--wpbc_form-field-text-color' );
697 }
698
699 if ( design.fieldBorder ) {
700 wrap.style.setProperty( '--wpbc_form-field-border-color', design.fieldBorder );
701 wrap.style.setProperty( '--wpbc_form-field-border-color-spare', design.fieldBorder );
702 } else {
703 wrap.style.removeProperty( '--wpbc_form-field-border-color' );
704 wrap.style.removeProperty( '--wpbc_form-field-border-color-spare' );
705 }
706 } );
707 }
708
709 function wpbc_bfb_global_form_style__get_vars() {
710 return window.wpbc_bfb_settings_vars || {};
711 }
712
713 function wpbc_bfb_global_form_style__get_presets() {
714 const vars = wpbc_bfb_global_form_style__get_vars();
715 return vars.form_style_presets && typeof vars.form_style_presets === 'object' ? vars.form_style_presets : {};
716 }
717
718 function wpbc_bfb_global_form_style__get_custom_keys() {
719 return [
720 'booking_form_custom_background_color',
721 'booking_form_custom_border_color',
722 'booking_form_custom_border_width',
723 'booking_form_custom_border_radius',
724 'booking_form_custom_padding_vertical',
725 'booking_form_custom_padding_horizontal',
726 'booking_form_custom_text_color',
727 'booking_form_custom_field_background_color',
728 'booking_form_custom_field_text_color',
729 'booking_form_custom_field_border_color',
730 'booking_form_custom_button_background_color',
731 'booking_form_custom_button_text_color',
732 'booking_form_custom_button_border_color',
733 'booking_form_custom_button_hover_background_color',
734 'booking_form_custom_button_hover_text_color',
735 'booking_form_custom_button_hover_border_color',
736 'booking_form_custom_secondary_button_background_color',
737 'booking_form_custom_secondary_button_text_color',
738 'booking_form_custom_secondary_button_border_color',
739 'booking_form_custom_secondary_button_hover_background_color',
740 'booking_form_custom_secondary_button_hover_text_color',
741 'booking_form_custom_secondary_button_hover_border_color'
742 ];
743 }
744
745 function wpbc_bfb_global_form_style__get_custom_defaults() {
746 const vars = wpbc_bfb_global_form_style__get_vars();
747 const localized = vars.custom_form_style_defaults && typeof vars.custom_form_style_defaults === 'object'
748 ? vars.custom_form_style_defaults
749 : {};
750
751 return Object.assign( {
752 booking_form_custom_background_color : '#ffffff',
753 booking_form_custom_border_color : '#cccccc',
754 booking_form_custom_border_width : '1px',
755 booking_form_custom_border_radius : '2px',
756 booking_form_custom_padding_vertical : '10px',
757 booking_form_custom_padding_horizontal : '30px',
758 booking_form_custom_text_color : '#1d2327',
759 booking_form_custom_field_background_color : '#ffffff',
760 booking_form_custom_field_text_color : '#3c434a',
761 booking_form_custom_field_border_color : '#cccccc',
762 booking_form_custom_button_background_color: '#066aab',
763 booking_form_custom_button_text_color : '#ffffff',
764 booking_form_custom_button_border_color : '#066aab',
765 booking_form_custom_button_hover_background_color: '#055589',
766 booking_form_custom_button_hover_text_color: '#ffffff',
767 booking_form_custom_button_hover_border_color: '#055589',
768 booking_form_custom_secondary_button_background_color: '#fdfdfd',
769 booking_form_custom_secondary_button_text_color: '#444444',
770 booking_form_custom_secondary_button_border_color: '#eeeeee',
771 booking_form_custom_secondary_button_hover_background_color: '#fdfdfd',
772 booking_form_custom_secondary_button_hover_text_color: '#444444',
773 booking_form_custom_secondary_button_hover_border_color: '#4d91cd'
774 }, localized );
775 }
776
777 function wpbc_bfb_global_form_style__get_current_options(ctx, key, value) {
778 const vars = wpbc_bfb_global_form_style__get_vars();
779 let options = vars.global_form_style && typeof vars.global_form_style === 'object'
780 ? Object.assign( {}, vars.global_form_style )
781 : {};
782
783 if ( window.WPBC_BFB_FormSettings && typeof window.WPBC_BFB_FormSettings.collect === 'function' ) {
784 options = Object.assign( options, window.WPBC_BFB_FormSettings.collect( 'global' ) || {} );
785 }
786
787 if ( ctx && ctx.options && typeof ctx.options === 'object' ) {
788 options = Object.assign( options, ctx.options );
789 }
790 if ( key ) {
791 options[String( key )] = value;
792 }
793 if ( ! options.booking_form_style ) {
794 options.booking_form_style = 'light_bordered';
795 }
796
797 return options;
798 }
799
800 function wpbc_bfb_global_form_style__resolve_css_vars(options) {
801 options = options && typeof options === 'object' ? options : {};
802 const style = String( options.booking_form_style || 'light_bordered' );
803 const presets = wpbc_bfb_global_form_style__get_presets();
804 const preset = presets[style] || presets.light_bordered || {};
805 const defaults = wpbc_bfb_global_form_style__get_custom_defaults();
806
807 if ( 'custom' !== style ) {
808 return preset.css_vars && typeof preset.css_vars === 'object' ? Object.assign( {}, preset.css_vars ) : {};
809 }
810
811 return {
812 '--wpbc-bfb-form-background' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_background_color, defaults.booking_form_custom_background_color ),
813 '--wpbc-bfb-form-border-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_border_color, defaults.booking_form_custom_border_color ),
814 '--wpbc-bfb-form-border-width' : wpbc_bfb_form_appearance__sanitize_length( options.booking_form_custom_border_width, defaults.booking_form_custom_border_width ),
815 '--wpbc-bfb-form-border-radius' : wpbc_bfb_form_appearance__sanitize_length( options.booking_form_custom_border_radius, defaults.booking_form_custom_border_radius ),
816 '--wpbc-bfb-form-padding' : wpbc_bfb_form_appearance__sanitize_length( options.booking_form_custom_padding_vertical, defaults.booking_form_custom_padding_vertical ) + ' ' + wpbc_bfb_form_appearance__sanitize_length( options.booking_form_custom_padding_horizontal, defaults.booking_form_custom_padding_horizontal ),
817 '--wpbc-bfb-form-box-shadow' : 'rgba(0, 0, 0, 0.05) 0px 2px 6px 0px',
818 '--wpbc_form-label-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_text_color, defaults.booking_form_custom_text_color ),
819 '--wpbc_form-label-sublabel-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_text_color, defaults.booking_form_custom_text_color ),
820 '--wpbc_form-label-error-color' : '#d63637',
821 '--wpbc_form-field-background-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_field_background_color, defaults.booking_form_custom_field_background_color ),
822 '--wpbc_form-field-menu-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_field_background_color, defaults.booking_form_custom_field_background_color ),
823 '--wpbc_form-field-text-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_field_text_color, defaults.booking_form_custom_field_text_color ),
824 '--wpbc_form-field-border-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_field_border_color, defaults.booking_form_custom_field_border_color ),
825 '--wpbc_form-field-border-color-spare': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_field_border_color, defaults.booking_form_custom_field_border_color ),
826 '--wpbc_form-field-focus-border-color': '#066aab',
827 '--wpbc_form-field-focus-shadow-color': '#066aab',
828 '--wpbc_form-field-disabled-color' : 'rgba(0, 0, 0, 0.2)',
829 '--wpbc_form-button-background-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_background_color, defaults.booking_form_custom_button_background_color ),
830 '--wpbc_form-button-background-color-alt': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_background_color, defaults.booking_form_custom_button_background_color ),
831 '--wpbc_form-button-border-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_border_color, defaults.booking_form_custom_button_border_color ),
832 '--wpbc_form-button-text-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_text_color, defaults.booking_form_custom_button_text_color ),
833 '--wpbc_form-button-text-color-alt' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_text_color, defaults.booking_form_custom_button_text_color ),
834 '--wpbc_form-button-hover-background-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_hover_background_color, defaults.booking_form_custom_button_hover_background_color ),
835 '--wpbc_form-button-hover-border-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_hover_border_color, defaults.booking_form_custom_button_hover_border_color ),
836 '--wpbc_form-button-hover-text-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_hover_text_color, defaults.booking_form_custom_button_hover_text_color ),
837 '--wpbc_form-choice-checked-border-color': '#066aab',
838 '--wpbc_form-choice-checked-color' : '#066aab',
839 '--wpbc_form-choice-focus-color' : '#066aab',
840 '--wpbc_form-button-light-background-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_background_color, defaults.booking_form_custom_secondary_button_background_color ),
841 '--wpbc_form-button-light-border-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_border_color, defaults.booking_form_custom_secondary_button_border_color ),
842 '--wpbc_form-button-light-text-color' : wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_text_color, defaults.booking_form_custom_secondary_button_text_color ),
843 '--wpbc_form-button-light-box-shadow' : '0 2px 10px 2px #ffffff54',
844 '--wpbc_form-button-light-hover-background-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_hover_background_color, defaults.booking_form_custom_secondary_button_hover_background_color ),
845 '--wpbc_form-button-light-hover-border-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_hover_border_color, defaults.booking_form_custom_secondary_button_hover_border_color ),
846 '--wpbc_form-button-light-hover-text-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_secondary_button_hover_text_color, defaults.booking_form_custom_secondary_button_hover_text_color ),
847 '--wpbc_form-button-light-hover-box-shadow': '0 2px 10px 2px #ffffff54',
848 '--wpbc_form-button-primary-hover-border-color': wpbc_bfb_form_appearance__sanitize_color( options.booking_form_custom_button_hover_border_color, defaults.booking_form_custom_button_hover_border_color ),
849 '--wpbc_form-page-break-color' : '#066aab'
850 };
851 }
852
853 function wpbc_bfb_global_form_style__get_css_var_keys(options) {
854 const vars = wpbc_bfb_global_form_style__get_vars();
855 const localized = Array.isArray( vars.form_style_css_var_names ) ? vars.form_style_css_var_names : [];
856 const keys = [];
857 const presets = wpbc_bfb_global_form_style__get_presets();
858
859 if ( localized.length ) {
860 return localized;
861 }
862
863 Object.keys( presets ).forEach( function (preset_key) {
864 const preset = presets[preset_key] || {};
865 const css_vars = preset.css_vars && typeof preset.css_vars === 'object' ? preset.css_vars : {};
866
867 Object.keys( css_vars ).forEach( function (var_name) {
868 if ( keys.indexOf( var_name ) === -1 ) {
869 keys.push( var_name );
870 }
871 } );
872 } );
873
874 Object.keys( wpbc_bfb_global_form_style__resolve_css_vars( Object.assign( {}, options || {}, { booking_form_style: 'custom' } ) ) ).forEach( function (var_name) {
875 if ( keys.indexOf( var_name ) === -1 ) {
876 keys.push( var_name );
877 }
878 } );
879
880 return keys;
881 }
882
883 function wpbc_bfb_global_form_style__sync_controls(options) {
884 const is_custom = 'custom' === String( options && options.booking_form_style ? options.booking_form_style : '' );
885 const reset_row = document.querySelector( '[data-wpbc-bfb-custom-appearance-reset-row]' );
886
887 wpbc_bfb_form_appearance__set_radio_control( 'booking_form_style', options.booking_form_style || 'light_bordered' );
888
889 document.querySelectorAll( '.wpbc_bfb__form_setting_global_custom_style' ).forEach( function (row) {
890 row.hidden = ! is_custom;
891 row.setAttribute( 'aria-hidden', is_custom ? 'false' : 'true' );
892 row.classList.toggle( 'is-hidden', ! is_custom );
893 } );
894
895 if ( reset_row ) {
896 reset_row.hidden = ! is_custom;
897 reset_row.setAttribute( 'aria-hidden', is_custom ? 'false' : 'true' );
898 reset_row.classList.toggle( 'is-hidden', ! is_custom );
899 }
900 }
901
902 function wpbc_bfb_global_form_style__apply(value, ctx) {
903 const options = wpbc_bfb_global_form_style__get_current_options( ctx, ctx && ctx.key, value );
904 const style = String( options.booking_form_style || 'light_bordered' );
905 const presets = wpbc_bfb_global_form_style__get_presets();
906 const preset = presets[style] || presets.light_bordered || {};
907 const css_vars = wpbc_bfb_global_form_style__resolve_css_vars( options );
908 const root = ( ctx && ctx.canvas ) || document.getElementById( 'wpbc_bfb__theme_scope' ) || document;
909 const theme_classes = [];
910 const css_var_keys = wpbc_bfb_global_form_style__get_css_var_keys( options );
911
912 wpbc_bfb_global_form_style__sync_controls( options );
913
914 Object.keys( presets ).forEach( function (preset_key) {
915 const class_name = presets[preset_key] && presets[preset_key].theme_class ? String( presets[preset_key].theme_class ) : '';
916 if ( class_name && theme_classes.indexOf( class_name ) === -1 ) {
917 theme_classes.push( class_name );
918 }
919 } );
920
921 if ( ! root || ! root.querySelectorAll ) {
922 return;
923 }
924
925 const selector = '.wpbc_container.wpbc_form, .wpbc_bfb_form, .wpbc_bfb__pages_panel, .wpbc_bfb__form_preview_section_container';
926 const wraps = [];
927 if ( root.matches && root.matches( selector ) ) {
928 wraps.push( root );
929 }
930 root.querySelectorAll( selector ).forEach( function (wrap) {
931 wraps.push( wrap );
932 } );
933
934 wraps.forEach( function (wrap) {
935 if ( ! wrap || ! wrap.style ) {
936 return;
937 }
938 theme_classes.forEach( function (class_name) {
939 wrap.classList.remove( class_name );
940 } );
941 if ( preset.theme_class ) {
942 wrap.classList.add( String( preset.theme_class ) );
943 }
944 wrap.classList.toggle( 'wpbc_bfb_form_appearance_custom', 'custom' === style );
945
946 css_var_keys.forEach( function (css_key) {
947 wrap.style.removeProperty( css_key );
948 } );
949 Object.keys( css_vars ).forEach( function (css_key) {
950 wrap.style.setProperty( css_key, css_vars[css_key] );
951 } );
952 } );
953 }
954
955 [
956 'booking_form_style',
957 'booking_form_custom_background_color',
958 'booking_form_custom_border_color',
959 'booking_form_custom_border_width',
960 'booking_form_custom_border_radius',
961 'booking_form_custom_padding_vertical',
962 'booking_form_custom_padding_horizontal',
963 'booking_form_custom_text_color',
964 'booking_form_custom_field_background_color',
965 'booking_form_custom_field_text_color',
966 'booking_form_custom_field_border_color',
967 'booking_form_custom_button_background_color',
968 'booking_form_custom_button_text_color',
969 'booking_form_custom_button_border_color',
970 'booking_form_custom_button_hover_background_color',
971 'booking_form_custom_button_hover_text_color',
972 'booking_form_custom_button_hover_border_color',
973 'booking_form_custom_secondary_button_background_color',
974 'booking_form_custom_secondary_button_text_color',
975 'booking_form_custom_secondary_button_border_color',
976 'booking_form_custom_secondary_button_hover_background_color',
977 'booking_form_custom_secondary_button_hover_text_color',
978 'booking_form_custom_secondary_button_hover_border_color'
979 ].forEach( function (key) {
980 WPBC_BFB_Settings_Effects.register( key, function (value, ctx) {
981 wpbc_bfb_global_form_style__apply( value, Object.assign( {}, ctx || {}, { key: key } ) );
982 } );
983 } );
984
985 function wpbc_bfb_form_appearance__sync_custom_controls_from_ui() {
986 const options = wpbc_bfb_global_form_style__get_current_options();
987 wpbc_bfb_global_form_style__sync_controls( options );
988 wpbc_bfb_global_form_style__apply( null, { source: 'sync-global-style' } );
989 }
990
991 wpbc_bfb_form_appearance__sync_custom_controls_from_ui();
992 document.addEventListener( 'DOMContentLoaded', wpbc_bfb_form_appearance__sync_custom_controls_from_ui );
993
994
995 // BOOKING_FORM_THEME.
996 WPBC_BFB_Settings_Effects.register( 'booking_form_theme', function (value, ctx) {
997 const root = (ctx && ctx.canvas) || document.getElementById( 'wpbc_bfb__theme_scope' ) || document;
998 if ( ! root || ! root.querySelectorAll ) {
999 return;
1000 }
1001
1002 if ( wpbc_bfb_form_appearance__is_user_theme_switch( ctx ) ) {
1003 const current_options = wpbc_bfb_form_appearance__get_current_options();
1004 if ( 'custom' === String( current_options.booking_form_container_style || '' ) ) {
1005 current_options.booking_form_theme = value;
1006 wpbc_bfb_form_appearance__sync_form_style_control( current_options );
1007 wpbc_bfb_form_appearance__apply_vars( 'custom', Object.assign( {}, ctx || {}, {
1008 key : 'booking_form_container_style',
1009 source : 'theme-base-custom',
1010 options: current_options
1011 } ) );
1012 } else {
1013 wpbc_bfb_form_appearance__set_container_style_control( 'bordered' );
1014 wpbc_bfb_form_appearance__apply_vars( 'bordered', Object.assign( {}, ctx || {}, { key: 'booking_form_container_style' } ) );
1015 }
1016 } else if ( ctx && ctx.options ) {
1017 wpbc_bfb_form_appearance__sync_form_style_control( ctx.options );
1018 }
1019
1020 const theme_selector = '.wpbc_container.wpbc_form, .wpbc_bfb_form, .wpbc_bfb__pages_panel';
1021 const wraps = [];
1022 if ( root.matches && root.matches( theme_selector ) ) {
1023 wraps.push( root );
1024 }
1025 root.querySelectorAll( theme_selector ).forEach( function (wrap) {
1026 wraps.push( wrap );
1027 } );
1028 if ( ! wraps.length ) {
1029 return;
1030 }
1031
1032 wraps.forEach( function (wrap) {
1033 // remove any previous theme classes (simple + future-proof).
1034 Array.from( wrap.classList ).forEach( function (cls) {
1035 if ( /^wpbc_theme_/.test( cls ) ) {
1036 wrap.classList.remove( cls );
1037 }
1038 } );
1039
1040 if ( value ) {
1041 wrap.classList.add( String( value ) );
1042 }
1043 } );
1044 } );
1045
1046
1047 // BOOKING_FORM_LAYOUT_WIDTH — Form width: applies combined "100%" / "600px" / "40rem" to the booking form containers.
1048 WPBC_BFB_Settings_Effects.register( 'booking_form_layout_width', function (value, ctx) {
1049 const root = ctx && ctx.canvas;
1050 if ( ! root || ! root.querySelectorAll ) {
1051 return;
1052 }
1053
1054 const wraps = root.querySelectorAll( '.wpbc_bfb__form_preview_section_container' );
1055 if ( ! wraps.length ) {
1056 return;
1057 }
1058
1059 const v = String( value == null ? '' : value ).trim();
1060
1061 // allow only "number + unit".
1062 if ( v && ! /^-?\d+(?:\.\d+)?(?:%|px|rem|em|vw|vh)$/.test( v ) ) {
1063 return;
1064 }
1065
1066 wraps.forEach(
1067 function (wrap) {
1068 if ( ! wrap || ! wrap.style ) {
1069 return;
1070 }
1071
1072 if ( ! v ) {
1073 wrap.style.removeProperty( '--wpbc-bfb-booking_form_layout_width' );
1074 } else {
1075 wrap.style.setProperty( '--wpbc-bfb-booking_form_layout_width', v );
1076 }
1077 }
1078 );
1079 } );
1080
1081
1082 // Debug Preview Mode.
1083 WPBC_BFB_Settings_Effects.register( 'booking_bfb_preview_mode', function (value, ctx) {
1084 const root = ctx.canvas;
1085 if ( ! root || ! root.querySelectorAll ) {
1086 return;
1087 }
1088
1089 const wraps = root.querySelectorAll( '.wpbc_container.wpbc_form' );
1090 if ( ! wraps.length ) {
1091 return;
1092 }
1093
1094 // Get builder async.
1095 wpbc_bfb_api.with_builder(
1096 function (Builder) {
1097
1098 /**
1099 * Capture active right sidebar tab and return restore handle.
1100 *
1101 * @return {{restore:function():void}|null}
1102 */
1103 function capture_right_sidebar_active_tab_restore_handle() {
1104
1105 var tablist_el = document.querySelector( '.wpbc_bfb__rightbar_tabs[role="tablist"]' );
1106 if ( ! tablist_el ) {
1107 return null;
1108 }
1109
1110 var active_tab_el = tablist_el.querySelector( '[role="tab"][aria-selected="true"]' );
1111
1112 if ( ! active_tab_el ) {
1113 active_tab_el = tablist_el.querySelector( '[role="tab"][aria-controls="wpbc_bfb__palette_add_new"]' );
1114 }
1115
1116 if ( ! active_tab_el || typeof active_tab_el.click !== 'function' ) {
1117 return null;
1118 }
1119
1120 return {
1121 restore: function () {
1122 try { active_tab_el.click(); } catch ( _e ) {}
1123 }
1124 };
1125 }
1126
1127 var tab_restore_handle = capture_right_sidebar_active_tab_restore_handle();
1128
1129 let restored = false;
1130 var EVS = window.WPBC_BFB_Core && window.WPBC_BFB_Core.WPBC_BFB_Events ? window.WPBC_BFB_Core.WPBC_BFB_Events : {};
1131 var EV_DONE = EVS.STRUCTURE_LOADED || EVS.CANVAS_REFRESHED || 'wpbc:bfb:structure-loaded';
1132
1133
1134 function do_restore() {
1135 if ( restored ) { return; }
1136 restored = true;
1137 try { Builder?.bus?.off?.( EV_DONE, do_restore ); } catch ( _ ) {}
1138 requestAnimationFrame( function () {
1139 if ( ! tab_restore_handle ) { return; }
1140 tab_restore_handle.restore();
1141 } );
1142 }
1143
1144 // Listen once (best), plus a fallback in case event isn't fired.
1145 try { Builder?.bus?.on?.( EV_DONE, do_restore ); } catch ( _ ) {}
1146
1147
1148 var enabled = ('On' === value);
1149 Builder.set_preview_mode( enabled, { rebuild: true, reinit: true, source: 'settings-effects' } );
1150 }
1151 );
1152
1153 } );
1154