| 1 |
( function () { |
| 2 |
const el = window.wp.element.createElement; |
| 3 |
const { registerBlockType } = window.wp.blocks; |
| 4 |
const { InspectorControls, useBlockProps, PanelColorSettings } = window.wp.blockEditor; |
| 5 |
const { PanelBody, TextControl, ToggleControl } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
|
| 8 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 9 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 10 |
var _tvf = function (typo, prefix) { var fn = getTypoCssVars(); return fn ? fn(typo, prefix) : {}; }; |
| 11 |
const IP = function () { return window.bkbgIconPicker; }; |
| 12 |
|
| 13 |
function renderIconPart(type, charVal, dashicon, imageUrl, dashiconColor, forSave) { |
| 14 |
if (!type || type === 'custom-char') return charVal; |
| 15 |
var fn = forSave ? IP().buildSaveIcon : IP().buildEditorIcon; |
| 16 |
return fn(type, charVal, dashicon, imageUrl, dashiconColor); |
| 17 |
} |
| 18 |
|
| 19 |
// ── Barcode generator (deterministic from ticket number string) ─────────── |
| 20 |
function makeBarcode( str ) { |
| 21 |
const src = ( str || 'TICKET' ).replace( /\W/g, '' ).toUpperCase().padEnd( 20, '0' ); |
| 22 |
const bars = []; |
| 23 |
for ( let i = 0; i < 32; i++ ) { |
| 24 |
const code = src.charCodeAt( i % src.length ); |
| 25 |
bars.push( { |
| 26 |
w: ( ( code * 7 + i * 13 ) % 3 ) + 1, // width 1–3 |
| 27 |
dark: ( ( code + i * 5 ) % 4 ) !== 0, // ~75% dark |
| 28 |
} ); |
| 29 |
} |
| 30 |
return bars; |
| 31 |
} |
| 32 |
|
| 33 |
// ── Render ticket ───────────────────────────────────────────────────────── |
| 34 |
function renderTicket( a, forSave ) { |
| 35 |
const barcode = makeBarcode( a.ticketNumber ); |
| 36 |
|
| 37 |
const detailItem = ( label, value ) => |
| 38 |
el( 'div', { className: 'bkbg-ts-detail' }, |
| 39 |
el( 'span', { className: 'bkbg-ts-detail-label' }, label ), |
| 40 |
el( 'span', { className: 'bkbg-ts-detail-value' }, value ), |
| 41 |
); |
| 42 |
|
| 43 |
const stubRow = ( label, value ) => |
| 44 |
el( 'div', { className: 'bkbg-ts-stub-row' }, |
| 45 |
el( 'span', { className: 'bkbg-ts-stub-label' }, label ), |
| 46 |
el( 'span', { className: 'bkbg-ts-stub-value' }, value ), |
| 47 |
); |
| 48 |
|
| 49 |
return el( 'div', { className: 'bkbg-ticket', style: { background: a.bgColor, color: a.textColor } }, |
| 50 |
|
| 51 |
// ── Main body ──────────────────────────────────────────────────── |
| 52 |
el( 'div', { className: 'bkbg-ts-main' }, |
| 53 |
|
| 54 |
// Header: emoji logo + event name |
| 55 |
el( 'div', { className: 'bkbg-ts-header' }, |
| 56 |
el( 'div', { className: 'bkbg-ts-logo', style: { background: a.accentColor } }, renderIconPart(a.logoEmojiType, a.logoEmoji || '🎫', a.logoEmojiDashicon, a.logoEmojiImageUrl, a.logoEmojiDashiconColor, forSave) ), |
| 57 |
el( 'div', { className: 'bkbg-ts-event' }, |
| 58 |
el( 'h2', { className: 'bkbg-ts-event-name' }, a.eventName || '' ), |
| 59 |
a.tagline && el( 'p', { className: 'bkbg-ts-tagline', style: { color: a.accentColor } }, a.tagline ), |
| 60 |
), |
| 61 |
), |
| 62 |
|
| 63 |
// Accent stripe |
| 64 |
el( 'div', { className: 'bkbg-ts-stripe', style: { background: `linear-gradient(90deg, ${ a.accentColor } 0%, transparent 100%)` } } ), |
| 65 |
|
| 66 |
// Details grid |
| 67 |
el( 'div', { className: 'bkbg-ts-details' }, |
| 68 |
detailItem( __( 'DATE', 'blockenberg' ), a.eventDate ), |
| 69 |
detailItem( __( 'TIME', 'blockenberg' ), a.eventTime ), |
| 70 |
detailItem( __( 'VENUE', 'blockenberg' ), a.venue ), |
| 71 |
a.showPrice && detailItem( __( 'PRICE', 'blockenberg' ), a.price ), |
| 72 |
), |
| 73 |
), |
| 74 |
|
| 75 |
// ── Perforated divider ─────────────────────────────────────────── |
| 76 |
el( 'div', { className: 'bkbg-ts-perf', style: { borderColor: a.accentColor } }, |
| 77 |
el( 'div', { className: 'bkbg-ts-notch bkbg-ts-notch-top', style: { background: 'inherit' } } ), |
| 78 |
el( 'div', { className: 'bkbg-ts-notch bkbg-ts-notch-bottom', style: { background: 'inherit' } } ), |
| 79 |
), |
| 80 |
|
| 81 |
// ── Stub ───────────────────────────────────────────────────────── |
| 82 |
el( 'div', { className: 'bkbg-ts-stub', style: { background: a.stubBg } }, |
| 83 |
|
| 84 |
// Seat info |
| 85 |
el( 'div', { className: 'bkbg-ts-stub-info' }, |
| 86 |
a.showSection && stubRow( __( 'SEC', 'blockenberg' ), a.seatSection ), |
| 87 |
stubRow( __( 'ROW', 'blockenberg' ), a.seatRow ), |
| 88 |
stubRow( __( 'SEAT', 'blockenberg' ), a.seatNumber ), |
| 89 |
), |
| 90 |
|
| 91 |
// Barcode |
| 92 |
a.showBarcode && el( 'div', { className: 'bkbg-ts-barcode' }, |
| 93 |
barcode.map( ( bar, i ) => |
| 94 |
el( 'div', { |
| 95 |
key: i, |
| 96 |
className: 'bkbg-ts-bar', |
| 97 |
style: { |
| 98 |
width: bar.w + 'px', |
| 99 |
background: bar.dark ? a.textColor : 'transparent', |
| 100 |
} |
| 101 |
} ) |
| 102 |
) |
| 103 |
), |
| 104 |
|
| 105 |
// Ticket number |
| 106 |
el( 'div', { className: 'bkbg-ts-num', style: { color: a.accentColor } }, a.ticketNumber ), |
| 107 |
), |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// ── Block registration ──────────────────────────────────────────────────── |
| 112 |
registerBlockType( 'blockenberg/ticket-stub', { |
| 113 |
edit: function ( props ) { |
| 114 |
const { attributes: a, setAttributes } = props; |
| 115 |
const blockProps = useBlockProps( (function () { |
| 116 |
var tv = Object.assign({}, _tvf(a.titleTypo, '--bktks-tt-'), _tvf(a.bodyTypo, '--bktks-bt-')); |
| 117 |
return { className: 'bkbg-ticket-stub-wrap', style: tv }; |
| 118 |
})() ); |
| 119 |
|
| 120 |
return el( 'div', blockProps, |
| 121 |
el( InspectorControls, {}, |
| 122 |
|
| 123 |
el( PanelBody, { title: __( 'Event Info', 'blockenberg' ), initialOpen: true }, |
| 124 |
el( TextControl, { label: __( 'Event Name', 'blockenberg' ), value: a.eventName, onChange: v => setAttributes( { eventName: v } ) } ), |
| 125 |
el( TextControl, { label: __( 'Tagline', 'blockenberg' ), value: a.tagline, onChange: v => setAttributes( { tagline: v } ) } ), |
| 126 |
el( TextControl, { label: __( 'Date', 'blockenberg' ), value: a.eventDate, onChange: v => setAttributes( { eventDate: v } ) } ), |
| 127 |
el( TextControl, { label: __( 'Time', 'blockenberg' ), value: a.eventTime, onChange: v => setAttributes( { eventTime: v } ) } ), |
| 128 |
el( TextControl, { label: __( 'Venue', 'blockenberg' ), value: a.venue, onChange: v => setAttributes( { venue: v } ) } ), |
| 129 |
el( TextControl, { label: __( 'Price', 'blockenberg' ), value: a.price, onChange: v => setAttributes( { price: v } ) } ), |
| 130 |
el( IP().IconPickerControl, IP().iconPickerProps( a, setAttributes, { charAttr: 'logoEmoji', typeAttr: 'logoEmojiType', dashiconAttr: 'logoEmojiDashicon', imageUrlAttr: 'logoEmojiImageUrl', colorAttr: 'logoEmojiDashiconColor' } ) ), |
| 131 |
), |
| 132 |
|
| 133 |
el( PanelBody, { title: __( 'Stub / Seat', 'blockenberg' ), initialOpen: false }, |
| 134 |
el( TextControl, { label: __( 'Section', 'blockenberg' ), value: a.seatSection, onChange: v => setAttributes( { seatSection: v } ) } ), |
| 135 |
el( TextControl, { label: __( 'Row', 'blockenberg' ), value: a.seatRow, onChange: v => setAttributes( { seatRow: v } ) } ), |
| 136 |
el( TextControl, { label: __( 'Seat', 'blockenberg' ), value: a.seatNumber, onChange: v => setAttributes( { seatNumber: v } ) } ), |
| 137 |
el( TextControl, { label: __( 'Ticket Number', 'blockenberg' ), value: a.ticketNumber, onChange: v => setAttributes( { ticketNumber: v } ) } ), |
| 138 |
el( ToggleControl, { label: __( 'Show Price', 'blockenberg' ), checked: a.showPrice, onChange: v => setAttributes( { showPrice: v } ) } ), |
| 139 |
el( ToggleControl, { label: __( 'Show Section', 'blockenberg' ), checked: a.showSection, onChange: v => setAttributes( { showSection: v } ) } ), |
| 140 |
el( ToggleControl, { label: __( 'Show Barcode', 'blockenberg' ), checked: a.showBarcode, onChange: v => setAttributes( { showBarcode: v } ) } ), |
| 141 |
), |
| 142 |
|
| 143 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 144 |
getTypoControl() && el(getTypoControl(), { |
| 145 |
label: __('Event Name Typography', 'blockenberg'), |
| 146 |
value: a.titleTypo || {}, |
| 147 |
onChange: function (v) { setAttributes({ titleTypo: v }); } |
| 148 |
}), |
| 149 |
getTypoControl() && el(getTypoControl(), { |
| 150 |
label: __('Body Typography', 'blockenberg'), |
| 151 |
value: a.bodyTypo || {}, |
| 152 |
onChange: function (v) { setAttributes({ bodyTypo: v }); } |
| 153 |
}) |
| 154 |
), |
| 155 |
|
| 156 |
el( PanelColorSettings, { |
| 157 |
title: __( 'Colors', 'blockenberg' ), |
| 158 |
initialOpen: false, |
| 159 |
colorSettings: [ |
| 160 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#1e1b4b' } ) }, |
| 161 |
{ label: __( 'Stub Background', 'blockenberg' ), value: a.stubBg, onChange: v => setAttributes( { stubBg: v || '#4c1d95' } ) }, |
| 162 |
{ label: __( 'Accent Color', 'blockenberg' ), value: a.accentColor, onChange: v => setAttributes( { accentColor: v || '#6d28d9' } ) }, |
| 163 |
{ label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#ffffff' } ) }, |
| 164 |
] |
| 165 |
} ), |
| 166 |
), |
| 167 |
|
| 168 |
renderTicket( a ), |
| 169 |
); |
| 170 |
}, |
| 171 |
|
| 172 |
save: function ( { attributes: a } ) { |
| 173 |
const blockProps = useBlockProps.save( (function () { |
| 174 |
var tv = Object.assign({}, _tvf(a.titleTypo, '--bktks-tt-'), _tvf(a.bodyTypo, '--bktks-bt-')); |
| 175 |
return { className: 'bkbg-ticket-stub-wrap', style: tv }; |
| 176 |
})() ); |
| 177 |
return el( 'div', blockProps, renderTicket( a, true ) ); |
| 178 |
}, |
| 179 |
} ); |
| 180 |
}() ); |
| 181 |
|