| 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, RangeControl, ToggleControl, TextControl, Button } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
|
| 8 |
var _divbarTC, _divbarTV; |
| 9 |
function _tc() { return _divbarTC || (_divbarTC = window.bkbgTypographyControl); } |
| 10 |
function _tv(t, p) { return (_divbarTV || (_divbarTV = window.bkbgTypoCssVars)) ? _divbarTV(t, p) : {}; } |
| 11 |
|
| 12 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 13 |
function renderDivergingBar( a ) { |
| 14 |
const items = a.items || []; |
| 15 |
const W = a.svgWidth; |
| 16 |
const RH = a.rowHeight; |
| 17 |
const PT = a.padTop; |
| 18 |
const PL = a.padLeft; |
| 19 |
const PR = a.padRight; |
| 20 |
const BH = a.barHeight; |
| 21 |
|
| 22 |
const maxPos = Math.max( ...items.map( it => it.positive || 0 ), 1 ); |
| 23 |
const maxNeg = Math.max( ...items.map( it => it.negative || 0 ), 1 ); |
| 24 |
const maxVal = Math.max( maxPos, maxNeg ); |
| 25 |
|
| 26 |
const H = PT + items.length * RH + 20; |
| 27 |
const halfW = ( W - PL - PR ) / 2; |
| 28 |
const cx = PL + halfW; // x of zero baseline |
| 29 |
|
| 30 |
function scalePos( v ) { return ( v / maxVal ) * halfW; } |
| 31 |
|
| 32 |
const svgEls = []; |
| 33 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 34 |
|
| 35 |
// Grid lines (symmetric) |
| 36 |
if ( a.showGridLines ) { |
| 37 |
const ticks = 4; |
| 38 |
for ( let i = 1; i <= ticks; i++ ) { |
| 39 |
const offset = scalePos( maxVal * i / ticks ); |
| 40 |
[ cx + offset, cx - offset ].forEach( ( gx, li ) => { |
| 41 |
svgEls.push( el( 'line', { key: `gl${ i }${ li }`, x1: gx, y1: PT - 4, x2: gx, y2: H - 10, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 42 |
} ); |
| 43 |
// tick labels at top |
| 44 |
const tv = Math.round( maxVal * i / ticks ); |
| 45 |
svgEls.push( el( 'text', { key: `gtr${ i }`, x: cx + offset, y: PT - 8, textAnchor: 'middle', fill: '#9ca3af', fontSize: 10, fontFamily: 'inherit' }, `+${ tv }` ) ); |
| 46 |
svgEls.push( el( 'text', { key: `gtl${ i }`, x: cx - offset, y: PT - 8, textAnchor: 'middle', fill: '#9ca3af', fontSize: 10, fontFamily: 'inherit' }, `-${ tv }` ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Zero line |
| 51 |
if ( a.showZeroLine ) { |
| 52 |
svgEls.push( el( 'line', { key: 'zero', x1: cx, y1: PT - 8, x2: cx, y2: H - 10, stroke: '#6b7280', strokeWidth: 2 } ) ); |
| 53 |
} |
| 54 |
|
| 55 |
// Rows |
| 56 |
items.forEach( ( item, i ) => { |
| 57 |
const cy = PT + i * RH + RH / 2; |
| 58 |
const posW = scalePos( item.positive || 0 ); |
| 59 |
const negW = scalePos( item.negative || 0 ); |
| 60 |
|
| 61 |
// Positive bar (right of centre) |
| 62 |
svgEls.push( el( 'rect', { key: `bp${ i }`, x: cx, y: cy - BH / 2, width: posW, height: BH, fill: a.posColor || '#4f46e5', rx: 3 } ) ); |
| 63 |
// Negative bar (left of centre) |
| 64 |
svgEls.push( el( 'rect', { key: `bn${ i }`, x: cx - negW, y: cy - BH / 2, width: negW, height: BH, fill: a.negColor || '#dc2626', rx: 3 } ) ); |
| 65 |
|
| 66 |
// Value labels |
| 67 |
if ( a.showValues ) { |
| 68 |
if ( item.positive > 0 ) { |
| 69 |
svgEls.push( el( 'text', { key: `vp${ i }`, x: cx + posW + 4, y: cy, dominantBaseline: 'middle', fill: a.posColor, fontSize: a.valueFontSize, fontFamily: 'inherit' }, String( item.positive ) ) ); |
| 70 |
} |
| 71 |
if ( item.negative > 0 ) { |
| 72 |
svgEls.push( el( 'text', { key: `vn${ i }`, x: cx - negW - 4, y: cy, dominantBaseline: 'middle', textAnchor: 'end', fill: a.negColor, fontSize: a.valueFontSize, fontFamily: 'inherit' }, String( item.negative ) ) ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Row label |
| 77 |
svgEls.push( el( 'text', { key: `lbl${ i }`, x: PL - 10, y: cy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.labelColor || '#374151', fontSize: a.labelFontSize, fontWeight: a.labelFontWeight || '400', fontFamily: 'inherit' }, item.label || '' ) ); |
| 78 |
} ); |
| 79 |
|
| 80 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 81 |
} |
| 82 |
|
| 83 |
function updItem( setAttributes, items, idx, field, val ) { |
| 84 |
setAttributes( { items: items.map( ( it, i ) => i === idx ? { ...it, [field]: val } : it ) } ); |
| 85 |
} |
| 86 |
|
| 87 |
registerBlockType( 'blockenberg/diverging-bar', { |
| 88 |
edit: function ( props ) { |
| 89 |
const { attributes: a, setAttributes } = props; |
| 90 |
const blockProps = useBlockProps( { className: 'bkbg-divbar-wrap', style: Object.assign( |
| 91 |
{ '--bkbg-divbar-ttl-fs': (a.titleFontSize || 18) + 'px' }, |
| 92 |
_tv(a.typoTitle || {}, '--bkbg-divbar-ttl-') |
| 93 |
) } ); |
| 94 |
|
| 95 |
return el( 'div', blockProps, |
| 96 |
el( InspectorControls, {}, |
| 97 |
|
| 98 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 99 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 100 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 101 |
el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ), |
| 102 |
el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ), |
| 103 |
el( ToggleControl, { label: __( 'Show Zero Line', 'blockenberg' ), checked: a.showZeroLine, onChange: v => setAttributes( { showZeroLine: v } ) } ), |
| 104 |
), |
| 105 |
|
| 106 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 107 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 108 |
el( RangeControl, { label: __( 'Row Height', 'blockenberg' ), value: a.rowHeight, onChange: v => setAttributes( { rowHeight: v } ), min: 24, max: 90 } ), |
| 109 |
el( RangeControl, { label: __( 'Bar Height', 'blockenberg' ), value: a.barHeight, onChange: v => setAttributes( { barHeight: v } ), min: 8, max: 56 } ), |
| 110 |
el( RangeControl, { label: __( 'Label Column Width', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 60, max: 300 } ), |
| 111 |
), |
| 112 |
|
| 113 |
|
| 114 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 115 |
_tc() && el(_tc(), { label: __('Title'), typo: a.typoTitle || {}, onChange: v => setAttributes( { typoTitle: v } ), defaultSize: a.titleFontSize || 18 }), |
| 116 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 9, max: 22 } ), |
| 117 |
el( wp.components.SelectControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight || '400', |
| 118 |
options: [{label:'Normal (400)',value:'400'},{label:'Medium (500)',value:'500'},{label:'Bold (700)',value:'700'}], |
| 119 |
onChange: v => setAttributes( { labelFontWeight: v } ) } ), |
| 120 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 16 } ), |
| 121 |
el( wp.components.SelectControl, { label: __( 'Value Font Weight', 'blockenberg' ), value: a.valueFontWeight || '400', |
| 122 |
options: [{label:'Normal (400)',value:'400'},{label:'Bold (700)',value:'700'}], |
| 123 |
onChange: v => setAttributes( { valueFontWeight: v } ) } ) |
| 124 |
), |
| 125 |
el( PanelColorSettings, { |
| 126 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 127 |
colorSettings: [ |
| 128 |
{ label: __( 'Positive Bar', 'blockenberg' ), value: a.posColor, onChange: v => setAttributes( { posColor: v || '#4f46e5' } ) }, |
| 129 |
{ label: __( 'Negative Bar', 'blockenberg' ), value: a.negColor, onChange: v => setAttributes( { negColor: v || '#dc2626' } ) }, |
| 130 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 131 |
{ label: __( 'Grid Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 132 |
{ label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 133 |
{ label: __( 'Label Color', 'blockenberg' ), value: a.labelColor, onChange: v => setAttributes( { labelColor: v || '#374151' } ) }, |
| 134 |
] |
| 135 |
} ), |
| 136 |
|
| 137 |
el( PanelBody, { title: __( 'Data', 'blockenberg' ), initialOpen: false }, |
| 138 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { items: [ ...a.items, { label: 'New Row', positive: 20, negative: 10 } ] } ) }, __( '+ Add Row', 'blockenberg' ) ), |
| 139 |
a.items.map( ( item, i ) => |
| 140 |
el( PanelBody, { key: i, title: item.label || `Row ${ i + 1 }`, initialOpen: false }, |
| 141 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: item.label, onChange: v => updItem( setAttributes, a.items, i, 'label', v ) } ), |
| 142 |
el( RangeControl, { label: __( 'Positive', 'blockenberg' ), value: item.positive, onChange: v => updItem( setAttributes, a.items, i, 'positive', v ), min: 0, max: 500 } ), |
| 143 |
el( RangeControl, { label: __( 'Negative', 'blockenberg' ), value: item.negative, onChange: v => updItem( setAttributes, a.items, i, 'negative', v ), min: 0, max: 500 } ), |
| 144 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { items: a.items.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 145 |
) |
| 146 |
), |
| 147 |
), |
| 148 |
), |
| 149 |
|
| 150 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-divbar-title', style: { color: a.titleColor } }, a.title ), |
| 151 |
el( 'div', { className: 'bkbg-divbar-svg' }, renderDivergingBar( a ) ), |
| 152 |
); |
| 153 |
}, |
| 154 |
|
| 155 |
save: function ( { attributes: a } ) { |
| 156 |
const blockProps = useBlockProps.save( { className: 'bkbg-divbar-wrap', style: Object.assign( |
| 157 |
{ '--bkbg-divbar-ttl-fs': (a.titleFontSize || 18) + 'px' }, |
| 158 |
_tv(a.typoTitle || {}, '--bkbg-divbar-ttl-') |
| 159 |
) } ); |
| 160 |
return el( 'div', blockProps, |
| 161 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-divbar-title', style: { color: a.titleColor } }, a.title ) : null, |
| 162 |
el( 'div', { className: 'bkbg-divbar-svg' }, renderDivergingBar( a ) ), |
| 163 |
); |
| 164 |
}, |
| 165 |
} ); |
| 166 |
}() ); |
| 167 |
|