| 1 |
// == WPBC BFB Shared Pack Helper: Generic Hint Shortcode |
| 2 |
(function ( w, d ) { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Register one shortcode-backed hint field. |
| 7 |
* |
| 8 |
* @param {Object} cfg Hint field configuration. |
| 9 |
* @param {boolean} cfg.export_to_booking_data Whether the Content exporter should include this hint. |
| 10 |
* @return {void} |
| 11 |
*/ |
| 12 |
w.WPBC_BFB_RegisterShortcodeHintPack = function ( cfg ) { |
| 13 |
cfg = cfg || {}; |
| 14 |
|
| 15 |
var T = String( cfg.token || '' ); |
| 16 |
var S = String( cfg.shortcode || T ); |
| 17 |
var P = String( cfg.prefix || '' ); |
| 18 |
var L = String( cfg.label || T ); |
| 19 |
var B = String( cfg.boot || '' ); |
| 20 |
var F = String( cfg.fallback || '' ); |
| 21 |
var export_to_booking_data = true === cfg.export_to_booking_data; |
| 22 |
var Core = w.WPBC_BFB_Core || {}; |
| 23 |
var Registry = Core.WPBC_BFB_Field_Renderer_Registry; |
| 24 |
var Base = Core.WPBC_BFB_Field_Base; |
| 25 |
|
| 26 |
if ( ! T ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! Registry || typeof Registry.register !== 'function' || ! Base ) { |
| 31 |
w._wpbc?.dev?.error?.( 'wpbc_bfb_field_' + T, 'Core registry/base missing' ); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
class ShortcodeHintField extends Base { |
| 36 |
static template_id = 'wpbc-bfb-field-' + T; |
| 37 |
|
| 38 |
static get_defaults() { |
| 39 |
return { type: T, prefix_text: P, html_id: '', cssclass: '', help: '' }; |
| 40 |
} |
| 41 |
|
| 42 |
static get_boot() { |
| 43 |
return B ? ( w[ B ] || {} ) : {}; |
| 44 |
} |
| 45 |
|
| 46 |
static render( el, data, ctx ) { |
| 47 |
if ( ! el ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
var d_normalized = this.normalize_data( data ); |
| 52 |
var s = Core.WPBC_BFB_Sanitize || {}; |
| 53 |
var boot = this.get_boot(); |
| 54 |
var html_id = d_normalized.html_id ? ( s.sanitize_html_id ? s.sanitize_html_id( String( d_normalized.html_id ) ) : String( d_normalized.html_id ) ) : ''; |
| 55 |
var cssclass = d_normalized.cssclass ? ( s.sanitize_css_classlist ? s.sanitize_css_classlist( String( d_normalized.cssclass ) ) : String( d_normalized.cssclass ) ) : ''; |
| 56 |
var tpl = this.get_template( this.template_id ); |
| 57 |
var is_supported = String( boot.is_supported || '0' ) === '1'; |
| 58 |
|
| 59 |
if ( typeof tpl !== 'function' ) { |
| 60 |
el.innerHTML = '<div class="wpbc_bfb__error" role="alert">Template not found: ' + this.template_id + '.</div>'; |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
try { |
| 65 |
el.innerHTML = tpl( Object.assign( {}, d_normalized, { |
| 66 |
html_id: html_id, |
| 67 |
cssclass: cssclass, |
| 68 |
is_supported: is_supported, |
| 69 |
upgrade_text: String( boot.upgrade_text || '' ), |
| 70 |
preview_value: String( boot.preview_value || F ) |
| 71 |
} ) ); |
| 72 |
} catch ( er ) { |
| 73 |
w._wpbc?.dev?.error?.( T + '_wptpl.tpl.render', er ); |
| 74 |
el.innerHTML = '<div class="wpbc_bfb__error" role="alert">Error rendering ' + L + ' preview.</div>'; |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
el.dataset.type = T; |
| 79 |
el.dataset.prefix_text = d_normalized.prefix_text != null ? String( d_normalized.prefix_text ) : ''; |
| 80 |
el.dataset.html_id = html_id; |
| 81 |
el.dataset.cssclass = cssclass; |
| 82 |
el.dataset.help = d_normalized.help != null ? String( d_normalized.help ) : ''; |
| 83 |
el.setAttribute( 'data-label', d_normalized.prefix_text != null ? String( d_normalized.prefix_text ) : '' ); |
| 84 |
Core.UI?.WPBC_BFB_Overlay?.ensure?.( ctx?.builder, el ); |
| 85 |
} |
| 86 |
|
| 87 |
static on_field_drop( data, el, ctx ) { |
| 88 |
super.on_field_drop?.( data, el, ctx ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
try { |
| 93 |
Registry.register( T, ShortcodeHintField ); |
| 94 |
} catch ( er ) { |
| 95 |
w._wpbc?.dev?.error?.( 'wpbc_bfb_field_' + T + '.register', er ); |
| 96 |
} |
| 97 |
|
| 98 |
function register_booking_form_exporter() { |
| 99 |
var exporter = w.WPBC_BFB_Exporter; |
| 100 |
if ( ! exporter || typeof exporter.register !== 'function' ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
if ( typeof exporter.has_exporter === 'function' && exporter.has_exporter( T ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
var s = Core.WPBC_BFB_Sanitize || {}; |
| 108 |
var esc_html = s.escape_html || function ( value ) { |
| 109 |
return String( value ).replace( /&/g, '&' ).replace( /</g, '<' ).replace( />/g, '>' ).replace( /"/g, '"' ).replace( /'/g, ''' ); |
| 110 |
}; |
| 111 |
var sanitize_html_id = s.sanitize_html_id || function ( value ) { return String( value || '' ); }; |
| 112 |
var sanitize_css_classlist = s.sanitize_css_classlist || function ( value ) { return String( value || '' ); }; |
| 113 |
|
| 114 |
exporter.register( T, function ( field, emit ) { |
| 115 |
var prefix_text = ( field && typeof field.prefix_text === 'string' ) ? field.prefix_text.trim() : P; |
| 116 |
var html_id = field && field.html_id ? sanitize_html_id( field.html_id ) : ''; |
| 117 |
var cssclass = field && field.cssclass ? sanitize_css_classlist( field.cssclass ) : ''; |
| 118 |
var body_html = ''; |
| 119 |
var attrs = ''; |
| 120 |
|
| 121 |
if ( prefix_text ) { |
| 122 |
body_html += esc_html( prefix_text ) + ' '; |
| 123 |
} |
| 124 |
body_html += '<strong>[' + S + ']</strong>'; |
| 125 |
if ( html_id ) { |
| 126 |
attrs += ' id="' + esc_html( html_id ) + '"'; |
| 127 |
} |
| 128 |
if ( cssclass ) { |
| 129 |
attrs += ' class="' + esc_html( cssclass ) + '"'; |
| 130 |
} |
| 131 |
if ( attrs ) { |
| 132 |
body_html = '<span' + attrs + '>' + body_html + '</span>'; |
| 133 |
} |
| 134 |
emit( body_html ); |
| 135 |
} ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( w.WPBC_BFB_Exporter && typeof w.WPBC_BFB_Exporter.register === 'function' ) { |
| 139 |
register_booking_form_exporter(); |
| 140 |
} else { |
| 141 |
d.addEventListener( 'wpbc:bfb:exporter-ready', register_booking_form_exporter, { once: true } ); |
| 142 |
} |
| 143 |
|
| 144 |
function register_booking_data_exporter() { |
| 145 |
var content_exporter = w.WPBC_BFB_ContentExporter; |
| 146 |
if ( ! content_exporter || typeof content_exporter.register !== 'function' ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
if ( typeof content_exporter.has_exporter === 'function' && content_exporter.has_exporter( T ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
content_exporter.register( T, function ( field, emit, extras ) { |
| 154 |
extras = extras || {}; |
| 155 |
var cfg_export = extras.cfg || {}; |
| 156 |
var has_prefix_text = field && typeof field.prefix_text === 'string'; |
| 157 |
var raw_label = has_prefix_text ? field.prefix_text.trim() : ''; |
| 158 |
var label = has_prefix_text ? raw_label.replace( /\s*:\s*$/, '' ) : L; |
| 159 |
|
| 160 |
if ( ! export_to_booking_data ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if ( typeof content_exporter.emit_line_bold_field === 'function' ) { |
| 165 |
content_exporter.emit_line_bold_field( emit, label, S, cfg_export ); |
| 166 |
return; |
| 167 |
} |
| 168 |
emit( ( label ? '<b>' + label + '</b>: ' : '' ) + '<f>[' + S + ']</f><br>' ); |
| 169 |
} ); |
| 170 |
} |
| 171 |
|
| 172 |
if ( w.WPBC_BFB_ContentExporter && typeof w.WPBC_BFB_ContentExporter.register === 'function' ) { |
| 173 |
register_booking_data_exporter(); |
| 174 |
} else { |
| 175 |
d.addEventListener( 'wpbc:bfb:content-exporter-ready', register_booking_data_exporter, { once: true } ); |
| 176 |
} |
| 177 |
}; |
| 178 |
})( window, document ); |
| 179 |
|