| 1 |
// File: /includes/page-form-builder/field-packs/submit/_out/submit.js |
| 2 |
(function ( w ) { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
var Core = w.WPBC_BFB_Core || {}; |
| 6 |
var registry = Core.WPBC_BFB_Field_Renderer_Registry; |
| 7 |
var Base = Core.WPBC_BFB_Field_Base; |
| 8 |
|
| 9 |
if ( ! registry || typeof registry.register !== 'function' || ! Base ) { |
| 10 |
_wpbc?.dev?.error?.( 'WPBC_BFB_Field_Submit', 'Core registry/base missing' ); |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* WPBC BFB: Field Renderer for "submit" (Schema-driven, template-literal render) |
| 16 |
* - Inspector is rendered by Factory (from PHP schema). |
| 17 |
* - No wp.template usage for preview. |
| 18 |
*/ |
| 19 |
class WPBC_BFB_Field_Submit extends Base { |
| 20 |
|
| 21 |
/** |
| 22 |
* Return default props for "submit" field. |
| 23 |
* Must stay in sync with PHP schema defaults. |
| 24 |
* |
| 25 |
* @returns {Object} |
| 26 |
*/ |
| 27 |
static get_defaults() { |
| 28 |
return { |
| 29 |
type : 'submit', |
| 30 |
label : 'Send', |
| 31 |
name : '', |
| 32 |
html_id : '', |
| 33 |
cssclass : 'wpbc_bfb__btn wpbc_bfb__btn--primary', |
| 34 |
help : '', |
| 35 |
usage_key : 'submit' |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Render the preview markup into the field element. |
| 41 |
* |
| 42 |
* @param {HTMLElement} el Field root element inside the canvas. |
| 43 |
* @param {Object} data Field props (already normalized by schema). |
| 44 |
* @param {Object} ctx Context: { builder, sanit, ... } |
| 45 |
* @returns {void} |
| 46 |
*/ |
| 47 |
static render( el, data, ctx ) { |
| 48 |
|
| 49 |
if ( ! el ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Normalize against defaults first. |
| 54 |
const d = this.normalize_data( data ); |
| 55 |
|
| 56 |
// ----- Core sanitize helpers (static) ----- |
| 57 |
const eh = ( v ) => Core.WPBC_BFB_Sanitize.escape_html( v ); |
| 58 |
const sid = ( v ) => Core.WPBC_BFB_Sanitize.sanitize_html_id( v ); |
| 59 |
const sname = ( v ) => Core.WPBC_BFB_Sanitize.sanitize_html_name( v ); |
| 60 |
const sclass = ( v ) => Core.WPBC_BFB_Sanitize.sanitize_css_classlist( v ); |
| 61 |
const truthy = ( v ) => Core.WPBC_BFB_Sanitize.is_truthy( v ); |
| 62 |
const one_of = ( v, list, def ) => ( list.indexOf( v ) !== -1 ? v : def ); |
| 63 |
|
| 64 |
// Public attributes. |
| 65 |
const html_id = d.html_id ? sid( String( d.html_id ) ) : ''; |
| 66 |
const name_val = d.name ? sname( String( d.name ) ) : ''; |
| 67 |
const css_next = sclass( String( d.cssclass || '' ) ); |
| 68 |
|
| 69 |
|
| 70 |
// Persist core-related props on dataset (do not mutate wrapper classes directly). |
| 71 |
if ( 'cssclass' in d && el.dataset.cssclass !== css_next ) { |
| 72 |
el.dataset.cssclass = css_next; |
| 73 |
} |
| 74 |
if ( 'html_id' in d && el.dataset.html_id !== html_id ) { |
| 75 |
el.dataset.html_id = html_id; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
// Attribute fragments. |
| 80 |
const id_attr = html_id ? ` id="${eh( html_id )}"` : ''; |
| 81 |
const name_attr = name_val ? ` name="${eh( name_val )}"` : ''; |
| 82 |
const cls_attr = ` class="wpbc_button wpbc_button_primary ${css_next ? eh( css_next ) : ''}"`; |
| 83 |
|
| 84 |
// Optional help text below the button. |
| 85 |
const help_html = d.help ? `<div class="wpbc_bfb__help">${eh( d.help )}</div>` : ''; |
| 86 |
|
| 87 |
// Render preview HTML (no actions; prevent real submit via type="button"). |
| 88 |
el.innerHTML = ` |
| 89 |
<span class="wpbc_bfb__noaction wpbc_bfb__no-drag-zone" inert=""> |
| 90 |
<div class="wpbc_bfb__field-preview"> |
| 91 |
<button type="button"${cls_attr}${id_attr}${name_attr}> |
| 92 |
${eh( d.label || 'Send' )} |
| 93 |
</button> |
| 94 |
</div> |
| 95 |
${help_html} |
| 96 |
</span> |
| 97 |
`; |
| 98 |
|
| 99 |
// Overlay (handles/toolbars). |
| 100 |
Core.UI?.WPBC_BFB_Overlay?.ensure?.( ctx?.builder, el ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Optional hook executed after field is dropped from the palette. |
| 106 |
* |
| 107 |
* @param {Object} data |
| 108 |
* @param {HTMLElement} el |
| 109 |
* @param {Object} ctx |
| 110 |
* @returns {void} |
| 111 |
*/ |
| 112 |
static on_field_drop( data, el, ctx ) { |
| 113 |
// Keep base behavior (auto-name from label, etc.). |
| 114 |
super.on_field_drop?.( data, el, ctx ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
try { |
| 119 |
registry.register( 'submit', WPBC_BFB_Field_Submit ); |
| 120 |
} catch ( e ) { _wpbc?.dev?.error?.( 'WPBC_BFB_Field_Submit.register', e ); } |
| 121 |
|
| 122 |
|
| 123 |
// ----------------------------------------------------------------------------------------------------------------- |
| 124 |
// Export for "Booking Form" (Advanced Form shortcode) |
| 125 |
// ----------------------------------------------------------------------------------------------------------------- |
| 126 |
/** |
| 127 |
* Register Booking Form exporter callback (Advanced Form) for "submit". |
| 128 |
* |
| 129 |
* This exporter: |
| 130 |
* - Emits the shortcode: |
| 131 |
* [submit "Label"] |
| 132 |
* where "Label" defaults to "Send" when empty. |
| 133 |
* - Behavior is compatible with the previous centralized exporter: |
| 134 |
* • uses Core.WPBC_BFB_Sanitize.escape_for_shortcode() when available, |
| 135 |
* • does not wrap the shortcode in extra HTML, |
| 136 |
* • leaves help text handling to WPBC_BFB_Exporter.render_field_node(). |
| 137 |
*/ |
| 138 |
function register_submit_booking_form_exporter() { |
| 139 |
|
| 140 |
var Exp = w.WPBC_BFB_Exporter; |
| 141 |
if ( ! Exp || typeof Exp.register !== 'function' ) { return; } |
| 142 |
if ( typeof Exp.has_exporter === 'function' && Exp.has_exporter( 'submit' ) ) { return; } |
| 143 |
|
| 144 |
var S = Core.WPBC_BFB_Sanitize || {}; |
| 145 |
var esc_html = S.escape_html || function( v ){ return String( v ); }; |
| 146 |
var sid = S.sanitize_html_id || function( v ){ return String( v ).trim(); }; |
| 147 |
var scls = S.sanitize_css_classlist || function( v ){ return String( v ).trim(); }; |
| 148 |
var esc_sc = S.escape_for_shortcode || function( v ){ return String( v ); }; |
| 149 |
|
| 150 |
/** |
| 151 |
* @type {WPBC_BFB_ExporterCallback} |
| 152 |
* @param {Object} field |
| 153 |
* @param {function(string):void} emit |
| 154 |
* @param {Object} [extras] |
| 155 |
*/ |
| 156 |
var exporter_callback = function( field, emit, extras ) { |
| 157 |
extras = extras || {}; |
| 158 |
|
| 159 |
// Merge with defaults to ensure all props (label, html_id, cssclass, …) are present. |
| 160 |
var defs = WPBC_BFB_Field_Submit.get_defaults(); |
| 161 |
var d = Object.assign( {}, defs, field || {} ); |
| 162 |
|
| 163 |
// Label (shortcode argument). |
| 164 |
var raw_label = ( d && typeof d.label === 'string' && d.label.length ) ? d.label : 'Send'; |
| 165 |
var label = '"' + esc_sc( String( raw_label ) ) + '"'; |
| 166 |
|
| 167 |
// ID / class wrapper around the shortcode (layout-only, no behavior). |
| 168 |
var html_id = d.html_id ? sid( String( d.html_id ) ) : ''; |
| 169 |
var cls_raw = String( d.cssclass_extra || d.cssclass || d.class || '' ); |
| 170 |
var cls_val = scls( cls_raw ); |
| 171 |
|
| 172 |
// Ensure unique id across the export tree (shared Set from the export context). |
| 173 |
var used_ids = extras && extras.ctx && extras.ctx.usedIds; |
| 174 |
if ( html_id && used_ids instanceof Set ) { |
| 175 |
var unique = html_id, i = 2; |
| 176 |
while ( used_ids.has( unique ) ) { |
| 177 |
unique = html_id + '_' + ( i++ ); |
| 178 |
} |
| 179 |
used_ids.add( unique ); |
| 180 |
html_id = unique; |
| 181 |
} |
| 182 |
|
| 183 |
var id_attr = html_id ? ' id="' + esc_html( html_id ) + '"' : ''; |
| 184 |
var cls_attr = cls_val ? ' class="' + esc_html( cls_val ) + '"' : ''; |
| 185 |
|
| 186 |
// Only wrap in <span ... style="flex:1;"> if id or class exists (matches other packs). |
| 187 |
var has_wrapper = !! ( id_attr || cls_attr ); |
| 188 |
var open = has_wrapper ? ( '<span' + id_attr + cls_attr + ' style="flex:1;">' ) : ''; |
| 189 |
var close = has_wrapper ? '</span>' : ''; |
| 190 |
|
| 191 |
// Shortcode itself remains legacy-compatible: - [submit "Send"] //. |
| 192 |
emit( open + '[submit ' + label + ']' + close ); |
| 193 |
}; |
| 194 |
|
| 195 |
Exp.register( 'submit', exporter_callback ); |
| 196 |
} |
| 197 |
|
| 198 |
if ( w.WPBC_BFB_Exporter && typeof w.WPBC_BFB_Exporter.register === 'function' ) { |
| 199 |
register_submit_booking_form_exporter(); |
| 200 |
} else if ( typeof document !== 'undefined' ) { |
| 201 |
document.addEventListener( 'wpbc:bfb:exporter-ready', register_submit_booking_form_exporter, { once: true } ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
// ----------------------------------------------------------------------------------------------------------------- |
| 206 |
// Export for "Booking Data" (Content of booking fields data) |
| 207 |
// ----------------------------------------------------------------------------------------------------------------- |
| 208 |
/** |
| 209 |
* Register Booking Data exporter callback ("Content of booking fields data") for "submit". |
| 210 |
* |
| 211 |
* Submit is a control element and does not carry user-entered values, |
| 212 |
* so it is intentionally omitted from the "Content of booking fields data" output. |
| 213 |
*/ |
| 214 |
function register_submit_booking_data_exporter() { |
| 215 |
|
| 216 |
var C = w.WPBC_BFB_ContentExporter; |
| 217 |
if ( ! C || typeof C.register !== 'function' ) { return; } |
| 218 |
if ( typeof C.has_exporter === 'function' && C.has_exporter( 'submit' ) ) { return; } |
| 219 |
|
| 220 |
/** |
| 221 |
* @param {Object} field |
| 222 |
* @param {function(string):void} emit |
| 223 |
* @param {Object} [extras] |
| 224 |
* @returns {void} |
| 225 |
*/ |
| 226 |
var exporter_callback = function( field, emit, extras ) { |
| 227 |
// Intentionally empty: submit has no data token/value. |
| 228 |
return; |
| 229 |
}; |
| 230 |
|
| 231 |
C.register( 'submit', exporter_callback ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( w.WPBC_BFB_ContentExporter && typeof w.WPBC_BFB_ContentExporter.register === 'function' ) { |
| 235 |
register_submit_booking_data_exporter(); |
| 236 |
} else if ( typeof document !== 'undefined' ) { |
| 237 |
document.addEventListener( 'wpbc:bfb:content-exporter-ready', register_submit_booking_data_exporter, { once: true } ); |
| 238 |
} |
| 239 |
|
| 240 |
})( window ); |
| 241 |
|