| 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, SelectControl } = 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 |
|
| 11 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 12 |
function renderPyramid( a ) { |
| 13 |
const items = a.items || []; |
| 14 |
const W = a.svgWidth; |
| 15 |
const PT = a.padTop; |
| 16 |
const PL = a.padLeft || 20; |
| 17 |
const PR = a.padRight || 20; |
| 18 |
const PC = a.padCenter || 60; // width reserved for centre labels |
| 19 |
const BH = a.barHeight; |
| 20 |
const BG = a.barGap; |
| 21 |
|
| 22 |
const rowH = BH + BG; |
| 23 |
const H = PT + items.length * rowH + 30; |
| 24 |
const totalVal = items.reduce( ( s, it ) => s + ( it.a || 0 ) + ( it.b || 0 ), 0 ) || 1; |
| 25 |
|
| 26 |
const maxVal = Math.max( ...items.flatMap( it => [ it.a || 0, it.b || 0 ] ), 1 ); |
| 27 |
const halfW = ( W - PL - PR - PC ) / 2; |
| 28 |
const cx = PL + halfW + PC / 2; |
| 29 |
|
| 30 |
function scaleBar( v ) { return ( v / maxVal ) * halfW; } |
| 31 |
function fmt( v, pct ) { |
| 32 |
if ( pct ) return ( ( v / totalVal ) * 100 ).toFixed( 1 ) + '%'; |
| 33 |
return String( parseFloat( v.toFixed( 1 ) ) ); |
| 34 |
} |
| 35 |
|
| 36 |
const svgEls = []; |
| 37 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 38 |
|
| 39 |
// Grid lines (symmetric ticks on both halves) |
| 40 |
if ( a.showGridLines ) { |
| 41 |
const ticks = 4; |
| 42 |
for ( let i = 1; i <= ticks; i++ ) { |
| 43 |
const offset = scaleBar( maxVal * i / ticks ); |
| 44 |
[ cx + offset, cx - offset ].forEach( ( gx, li ) => { |
| 45 |
svgEls.push( el( 'line', { key: `gl${ i }${ li }`, x1: gx, y1: PT - 4, x2: gx, y2: H - 20, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 46 |
} ); |
| 47 |
const tv = parseFloat( ( maxVal * i / ticks ).toFixed( 1 ) ); |
| 48 |
svgEls.push( el( 'text', { key: `gtr${ i }`, x: cx + offset, y: PT - 10, textAnchor: 'middle', fill: '#9ca3af', fontSize: 9, fontFamily: 'inherit' }, String( tv ) ) ); |
| 49 |
svgEls.push( el( 'text', { key: `gtl${ i }`, x: cx - offset, y: PT - 10, textAnchor: 'middle', fill: '#9ca3af', fontSize: 9, fontFamily: 'inherit' }, String( tv ) ) ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Centre axis line |
| 54 |
svgEls.push( el( 'line', { key: 'axL', x1: cx, y1: PT - 4, x2: cx, y2: H - 20, stroke: '#9ca3af', strokeWidth: 1 } ) ); |
| 55 |
|
| 56 |
// Legend labels at top |
| 57 |
svgEls.push( el( 'text', { key: 'legA', x: PL + halfW / 2, y: PT - 20, textAnchor: 'middle', fill: a.colorA || '#3b82f6', fontSize: a.labelFontSize, fontWeight: 700, fontFamily: 'inherit' }, a.labelA || 'Left' ) ); |
| 58 |
svgEls.push( el( 'text', { key: 'legB', x: cx + PC / 2 + halfW / 2, y: PT - 20, textAnchor: 'middle', fill: a.colorB || '#ec4899', fontSize: a.labelFontSize, fontWeight: 700, fontFamily: 'inherit' }, a.labelB || 'Right' ) ); |
| 59 |
|
| 60 |
// Rows |
| 61 |
items.forEach( ( item, i ) => { |
| 62 |
const by = PT + i * rowH; |
| 63 |
const wA = scaleBar( item.a || 0 ); |
| 64 |
const wB = scaleBar( item.b || 0 ); |
| 65 |
|
| 66 |
// Left bar (A) — extends leftward from centre |
| 67 |
svgEls.push( el( 'rect', { key: `bA${ i }`, x: cx - PC / 2 - wA, y: by, width: wA, height: BH, fill: a.colorA || '#3b82f6', rx: 3 } ) ); |
| 68 |
// Right bar (B) — extends rightward from centre |
| 69 |
svgEls.push( el( 'rect', { key: `bB${ i }`, x: cx + PC / 2, y: by, width: wB, height: BH, fill: a.colorB || '#ec4899', rx: 3 } ) ); |
| 70 |
|
| 71 |
// Centre label |
| 72 |
svgEls.push( el( 'text', { key: `cl${ i }`, x: cx, y: by + BH / 2, textAnchor: 'middle', dominantBaseline: 'middle', fill: a.labelColor || '#374151', fontSize: a.labelFontSize, fontFamily: 'inherit' }, item.label || '' ) ); |
| 73 |
|
| 74 |
// Value labels |
| 75 |
if ( a.showValues ) { |
| 76 |
const dispA = fmt( item.a || 0, a.showPercentage ); |
| 77 |
const dispB = fmt( item.b || 0, a.showPercentage ); |
| 78 |
svgEls.push( el( 'text', { key: `vA${ i }`, x: cx - PC / 2 - wA - 3, y: by + BH / 2, textAnchor: 'end', dominantBaseline: 'middle', fill: a.colorA, fontSize: a.valueFontSize, fontFamily: 'inherit' }, dispA ) ); |
| 79 |
svgEls.push( el( 'text', { key: `vB${ i }`, x: cx + PC / 2 + wB + 3, y: by + BH / 2, dominantBaseline: 'middle', fill: a.colorB, fontSize: a.valueFontSize, fontFamily: 'inherit' }, dispB ) ); |
| 80 |
} |
| 81 |
} ); |
| 82 |
|
| 83 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 84 |
} |
| 85 |
|
| 86 |
function updItem( setAttributes, items, idx, field, val ) { |
| 87 |
setAttributes( { items: items.map( ( it, i ) => i === idx ? { ...it, [field]: val } : it ) } ); |
| 88 |
} |
| 89 |
|
| 90 |
registerBlockType( 'blockenberg/population-pyramid', { |
| 91 |
edit: function ( props ) { |
| 92 |
const { attributes: a, setAttributes } = props; |
| 93 |
const blockProps = useBlockProps((function () { |
| 94 |
var _tv = getTypoCssVars(); |
| 95 |
var s = {}; |
| 96 |
Object.assign(s, _tv(a.titleTypo, '--bkbg-pyr-tt-')); |
| 97 |
return { className: 'bkbg-pyramid-wrap', style: s }; |
| 98 |
})()); |
| 99 |
|
| 100 |
return el( 'div', blockProps, |
| 101 |
el( InspectorControls, {}, |
| 102 |
|
| 103 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 104 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 105 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 106 |
el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ), |
| 107 |
el( ToggleControl, { label: __( 'Show as Percentage', 'blockenberg' ), checked: a.showPercentage, onChange: v => setAttributes( { showPercentage: v } ) } ), |
| 108 |
el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ), |
| 109 |
el( TextControl, { label: __( 'Left Group Label (A)', 'blockenberg' ), value: a.labelA, onChange: v => setAttributes( { labelA: v } ) } ), |
| 110 |
el( TextControl, { label: __( 'Right Group Label (B)', 'blockenberg' ), value: a.labelB, onChange: v => setAttributes( { labelB: v } ) } ), |
| 111 |
), |
| 112 |
|
| 113 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 114 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 115 |
el( RangeControl, { label: __( 'Bar Height', 'blockenberg' ), value: a.barHeight, onChange: v => setAttributes( { barHeight: v } ), min: 10, max: 58 } ), |
| 116 |
el( RangeControl, { label: __( 'Bar Gap', 'blockenberg' ), value: a.barGap, onChange: v => setAttributes( { barGap: v } ), min: 0, max: 24 } ), |
| 117 |
el( RangeControl, { label: __( 'Centre Label Width', 'blockenberg' ), value: a.padCenter, onChange: v => setAttributes( { padCenter: v } ), min: 30, max: 180 } ), |
| 118 |
), |
| 119 |
|
| 120 |
|
| 121 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 122 |
(function () { |
| 123 |
var TC = getTypoControl(); |
| 124 |
if (!TC) return el('p', null, 'Loading…'); |
| 125 |
return el(wp.element.Fragment, null, |
| 126 |
el(TC, { label: 'Title Typography', value: a.titleTypo, onChange: function (v) { setAttributes({ titleTypo: v }); } }) |
| 127 |
); |
| 128 |
})(), |
| 129 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 22 } ), |
| 130 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 16 } ) |
| 131 |
), |
| 132 |
el( PanelColorSettings, { |
| 133 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 134 |
colorSettings: [ |
| 135 |
{ label: __( 'Left Bar (A)', 'blockenberg' ), value: a.colorA, onChange: v => setAttributes( { colorA: v || '#3b82f6' } ) }, |
| 136 |
{ label: __( 'Right Bar (B)', 'blockenberg' ), value: a.colorB, onChange: v => setAttributes( { colorB: v || '#ec4899' } ) }, |
| 137 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 138 |
{ label: __( 'Grid Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 139 |
{ label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 140 |
{ label: __( 'Label Color', 'blockenberg' ), value: a.labelColor, onChange: v => setAttributes( { labelColor: v || '#374151' } ) }, |
| 141 |
] |
| 142 |
} ), |
| 143 |
|
| 144 |
el( PanelBody, { title: __( 'Data Rows', 'blockenberg' ), initialOpen: false }, |
| 145 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { items: [ ...a.items, { label: 'New Row', a: 5, b: 5 } ] } ) }, __( '+ Add Row', 'blockenberg' ) ), |
| 146 |
a.items.map( ( item, i ) => |
| 147 |
el( PanelBody, { key: i, title: item.label || `Row ${ i + 1 }`, initialOpen: false }, |
| 148 |
el( TextControl, { label: __( 'Age / Category', 'blockenberg' ), value: item.label, onChange: v => updItem( setAttributes, a.items, i, 'label', v ) } ), |
| 149 |
el( RangeControl, { label: `${ a.labelA || 'A' } value`, value: item.a, onChange: v => updItem( setAttributes, a.items, i, 'a', v ), min: 0, max: 10000, step: 0.1 } ), |
| 150 |
el( RangeControl, { label: `${ a.labelB || 'B' } value`, value: item.b, onChange: v => updItem( setAttributes, a.items, i, 'b', v ), min: 0, max: 10000, step: 0.1 } ), |
| 151 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { items: a.items.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 152 |
) |
| 153 |
), |
| 154 |
), |
| 155 |
), |
| 156 |
|
| 157 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-pyramid-title', style: { color: a.titleColor } }, a.title ), |
| 158 |
el( 'div', { className: 'bkbg-pyramid-svg' }, renderPyramid( a ) ), |
| 159 |
); |
| 160 |
}, |
| 161 |
|
| 162 |
save: function ( { attributes: a } ) { |
| 163 |
var _tv = (typeof window !== 'undefined' && window.bkbgTypoCssVars) || function(){return {};}; |
| 164 |
var s = {}; |
| 165 |
Object.assign(s, _tv(a.titleTypo || {}, '--bkbg-pyr-tt-')); |
| 166 |
const blockProps = useBlockProps.save( { className: 'bkbg-pyramid-wrap', style: s } ); |
| 167 |
return el( 'div', blockProps, |
| 168 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-pyramid-title', style: { color: a.titleColor } }, a.title ) : null, |
| 169 |
el( 'div', { className: 'bkbg-pyramid-svg' }, renderPyramid( a ) ), |
| 170 |
); |
| 171 |
}, |
| 172 |
|
| 173 |
deprecated: [{ |
| 174 |
attributes: {"title":{"type":"string","default":"Age Distribution: Male vs Female"},"showTitle":{"type":"boolean","default":true},"labelA":{"type":"string","default":"Male"},"labelB":{"type":"string","default":"Female"},"svgWidth":{"type":"integer","default":700},"padTop":{"type":"integer","default":36},"padLeft":{"type":"integer","default":20},"padRight":{"type":"integer","default":20},"padCenter":{"type":"integer","default":60},"barHeight":{"type":"integer","default":26},"barGap":{"type":"integer","default":4},"showValues":{"type":"boolean","default":true},"showPercentage":{"type":"boolean","default":false},"showGridLines":{"type":"boolean","default":true},"labelFontSize":{"type":"integer","default":12},"valueFontSize":{"type":"integer","default":10},"colorA":{"type":"string","default":"#3b82f6"},"colorB":{"type":"string","default":"#ec4899"},"bgColor":{"type":"string","default":"#ffffff"},"gridColor":{"type":"string","default":"#f3f4f6"},"titleColor":{"type":"string","default":"#111827"},"labelColor":{"type":"string","default":"#374151"},"items":{"type":"array","default":[{"label":"75+","a":2.1,"b":3.2},{"label":"65\u201374","a":4.8,"b":5.6},{"label":"55\u201364","a":6.9,"b":7.4},{"label":"45\u201354","a":8.2,"b":8.5},{"label":"35\u201344","a":8.7,"b":8.6},{"label":"25\u201334","a":9.1,"b":8.8},{"label":"15\u201324","a":8.4,"b":7.9},{"label":"5\u201314","a":7.6,"b":7.2},{"label":"0\u20134","a":4.3,"b":4.1}]},"labelFontWeight":{"type":"string","default":"400"},"valueFontWeight":{"type":"string","default":"700"}}, |
| 175 |
save: function ( { attributes: a } ) { |
| 176 |
const blockProps = wp.blockEditor.useBlockProps.save( { className: 'bkbg-pyramid-wrap' } ); |
| 177 |
return wp.element.createElement( 'div', blockProps, |
| 178 |
a.showTitle && a.title ? wp.element.createElement( 'h3', { className: 'bkbg-pyramid-title', style: { color: a.titleColor } }, a.title ) : null, |
| 179 |
wp.element.createElement( 'div', { className: 'bkbg-pyramid-svg' }, renderPyramid( a ) ), |
| 180 |
); |
| 181 |
} |
| 182 |
}] |
| 183 |
} ); |
| 184 |
}() ); |
| 185 |
|