| 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, ColorPicker, Popover } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
const { useState } = window.wp.element; |
| 8 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 9 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 10 |
|
| 11 |
// ── Arc helpers ─────────────────────────────────────────────────────────── |
| 12 |
function degToRad( d ) { return d * Math.PI / 180; } |
| 13 |
|
| 14 |
function arcPath( cx, cy, r, startDeg, endDeg ) { |
| 15 |
const s = degToRad( startDeg ); |
| 16 |
const e = degToRad( endDeg ); |
| 17 |
const span = endDeg - startDeg; |
| 18 |
const large = span >= 180 ? 1 : 0; |
| 19 |
const x1 = cx + r * Math.cos( s ); |
| 20 |
const y1 = cy + r * Math.sin( s ); |
| 21 |
const x2 = cx + r * Math.cos( e ); |
| 22 |
const y2 = cy + r * Math.sin( e ); |
| 23 |
const f = v => v.toFixed( 3 ); |
| 24 |
if ( Math.abs( span ) >= 359.9 ) { |
| 25 |
// Full circle — draw as two semicircular arcs |
| 26 |
const mx = cx + r * Math.cos( s + Math.PI ); |
| 27 |
const my = cy + r * Math.sin( s + Math.PI ); |
| 28 |
return `M ${ f(x1) } ${ f(y1) } A ${ f(r) } ${ f(r) } 0 1 1 ${ f(mx) } ${ f(my) } A ${ f(r) } ${ f(r) } 0 1 1 ${ f(x1) } ${ f(y1) }`; |
| 29 |
} |
| 30 |
return `M ${ f(x1) } ${ f(y1) } A ${ f(r) } ${ f(r) } 0 ${ large } 1 ${ f(x2) } ${ f(y2) }`; |
| 31 |
} |
| 32 |
|
| 33 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 34 |
function renderRadialBar( a ) { |
| 35 |
const series = a.series || []; |
| 36 |
const S = a.svgSize; |
| 37 |
const cx = S / 2; |
| 38 |
const cy = S / 2; |
| 39 |
const TW = a.trackWidth; |
| 40 |
const TG = a.trackGap; |
| 41 |
const maxVal = a.maxValue || 100; |
| 42 |
const startDeg = a.startAngle || -90; |
| 43 |
const capsType = a.roundedCaps ? 'round' : 'butt'; |
| 44 |
|
| 45 |
// Outermost ring radius, shrinking per ring |
| 46 |
const outerR = Math.min( cx, cy ) - 10; |
| 47 |
const legH = a.showLegend ? Math.ceil( series.length / 2 ) * 22 + 16 : 0; |
| 48 |
const svgH = S + legH; |
| 49 |
|
| 50 |
const svgEls = []; |
| 51 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: S, height: svgH, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 52 |
|
| 53 |
series.forEach( ( ser, i ) => { |
| 54 |
const r = outerR - i * ( TW + TG ); |
| 55 |
if ( r - TW / 2 < 10 ) return; // no room |
| 56 |
const clr = ser.color || '#6366f1'; |
| 57 |
const ratio = Math.min( ( ser.value || 0 ) / maxVal, 1 ); |
| 58 |
const endDeg = startDeg + ratio * 360; |
| 59 |
|
| 60 |
// Track (background circle) |
| 61 |
svgEls.push( el( 'path', { key: `tr${ i }`, d: arcPath( cx, cy, r, startDeg, startDeg + 360 ), fill: 'none', stroke: a.trackBgColor || '#f3f4f6', strokeWidth: TW } ) ); |
| 62 |
|
| 63 |
// Value arc |
| 64 |
if ( ratio > 0 ) { |
| 65 |
svgEls.push( el( 'path', { key: `arc${ i }`, d: arcPath( cx, cy, r, startDeg, endDeg ), fill: 'none', stroke: clr, strokeWidth: TW, strokeLinecap: capsType } ) ); |
| 66 |
} |
| 67 |
|
| 68 |
// Label just before the 12-o'clock start point |
| 69 |
if ( a.showLabels ) { |
| 70 |
const lrad = degToRad( startDeg - 6 ); |
| 71 |
const lr = r + TW / 2 + 2; |
| 72 |
const lx = cx + lr * Math.cos( lrad ); |
| 73 |
const ly = cy + lr * Math.sin( lrad ); |
| 74 |
svgEls.push( el( 'text', { key: `ilbl${ i }`, x: lx, y: ly, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, ser.label || '' ) ); |
| 75 |
} |
| 76 |
} ); |
| 77 |
|
| 78 |
// Centre value display — show the first/only series value prominently |
| 79 |
if ( series.length === 1 && a.showValues ) { |
| 80 |
svgEls.push( el( 'text', { key: 'cv', x: cx, y: cy - 6, textAnchor: 'middle', fill: series[0].color, fontSize: a.valueFontSize || 32, fontWeight: a.valueFontWeight || 700, fontFamily: 'inherit' }, String( series[0].value || 0 ) ) ); |
| 81 |
svgEls.push( el( 'text', { key: 'cl', x: cx, y: cy + 16, textAnchor: 'middle', fill: a.textColor, fontSize: 12, fontFamily: 'inherit' }, `/ ${ a.maxValue }` ) ); |
| 82 |
} |
| 83 |
|
| 84 |
// Legend |
| 85 |
if ( a.showLegend ) { |
| 86 |
const perRow = 2; |
| 87 |
const lBase = S + 12; |
| 88 |
series.forEach( ( ser, i ) => { |
| 89 |
const lx = 10 + ( i % perRow ) * ( S / perRow ); |
| 90 |
const ly = lBase + Math.floor( i / perRow ) * 22; |
| 91 |
svgEls.push( el( 'rect', { key: `lr${ i }`, x: lx, y: ly, width: 12, height: 12, fill: ser.color, rx: 6 } ) ); |
| 92 |
svgEls.push( el( 'text', { key: `lt${ i }`, x: lx + 16, y: ly + 6, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, |
| 93 |
a.showValues ? `${ ser.label } — ${ ser.value }${ a.maxValue === 100 ? '%' : '' }` : ser.label |
| 94 |
) ); |
| 95 |
} ); |
| 96 |
} |
| 97 |
|
| 98 |
return el( 'svg', { viewBox: `0 0 ${ S } ${ svgH }`, width: '100%', style: { display: 'block', maxWidth: S + 'px', margin: '0 auto' } }, ...svgEls ); |
| 99 |
} |
| 100 |
|
| 101 |
function updSer( setAttributes, series, si, field, val ) { |
| 102 |
setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } ); |
| 103 |
} |
| 104 |
|
| 105 |
/* ── colour-swatch + popover ── */ |
| 106 |
function BkbgColorSwatch(p) { |
| 107 |
var st = useState(false), open = st[0], setOpen = st[1]; |
| 108 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 109 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 110 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 111 |
el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); }, |
| 112 |
style:{ width:'28px', height:'28px', borderRadius:'4px', border: open ? '2px solid #007cba' : '2px solid #ddd', cursor:'pointer', padding:0, display:'block', background: p.value||'#ffffff', flexShrink:0 } }), |
| 113 |
open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } }, |
| 114 |
el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } }, |
| 115 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 116 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 117 |
el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } }) |
| 118 |
), |
| 119 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 120 |
) |
| 121 |
) |
| 122 |
) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
registerBlockType( 'blockenberg/radial-bar', { |
| 127 |
edit: function ( props ) { |
| 128 |
const { attributes: a, setAttributes } = props; |
| 129 |
var TC = getTypoControl(); |
| 130 |
var blockProps = useBlockProps((function() { |
| 131 |
var _tvFn = getTypoCssVars(); |
| 132 |
var s = {}; |
| 133 |
if (_tvFn) Object.assign(s, _tvFn(a.titleTypo || {}, '--bkrb-tt-')); |
| 134 |
return { className: 'bkbg-radialbar-wrap', style: s }; |
| 135 |
})()); |
| 136 |
|
| 137 |
return el( 'div', blockProps, |
| 138 |
el( InspectorControls, {}, |
| 139 |
|
| 140 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 141 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 142 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 143 |
el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ), |
| 144 |
el( ToggleControl, { label: __( 'Show Labels', 'blockenberg' ), checked: a.showLabels, onChange: v => setAttributes( { showLabels: v } ) } ), |
| 145 |
el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ), |
| 146 |
el( ToggleControl, { label: __( 'Rounded Caps', 'blockenberg' ), checked: a.roundedCaps, onChange: v => setAttributes( { roundedCaps: v } ) } ), |
| 147 |
el( RangeControl, { label: __( 'Max Value', 'blockenberg' ), value: a.maxValue, onChange: v => setAttributes( { maxValue: v } ), min: 1, max: 10000 } ), |
| 148 |
el( RangeControl, { label: __( 'Start Angle (°)', 'blockenberg' ), value: a.startAngle, onChange: v => setAttributes( { startAngle: v } ), min: -180, max: 180 } ), |
| 149 |
), |
| 150 |
|
| 151 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 152 |
el( RangeControl, { label: __( 'Canvas Size (px)', 'blockenberg' ), value: a.svgSize, onChange: v => setAttributes( { svgSize: v } ), min: 200, max: 800 } ), |
| 153 |
el( RangeControl, { label: __( 'Track Width', 'blockenberg' ), value: a.trackWidth, onChange: v => setAttributes( { trackWidth: v } ), min: 6, max: 60 } ), |
| 154 |
el( RangeControl, { label: __( 'Gap Between Rings', 'blockenberg' ), value: a.trackGap, onChange: v => setAttributes( { trackGap: v } ), min: 0, max: 30 } ), |
| 155 |
), |
| 156 |
|
| 157 |
|
| 158 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 159 |
TC && el( TC, { label: __( 'Title', 'blockenberg' ), value: a.titleTypo || {}, onChange: function(v) { setAttributes({ titleTypo: v }); } }), |
| 160 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize || 32, onChange: v => setAttributes( { valueFontSize: v } ), min: 16, max: 72 } ), |
| 161 |
el( RangeControl, { label: __( 'Value Font Weight', 'blockenberg' ), value: a.valueFontWeight || 700, min: 100, max: 900, step: 100, onChange: v => setAttributes( { valueFontWeight: v } ) } ), |
| 162 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 22 } ), |
| 163 |
el( RangeControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight || 600, min: 100, max: 900, step: 100, onChange: v => setAttributes( { labelFontWeight: v } ) } ) |
| 164 |
), |
| 165 |
el( PanelColorSettings, { |
| 166 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 167 |
colorSettings: [ |
| 168 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 169 |
{ label: __( 'Track Background', 'blockenberg' ), value: a.trackBgColor, onChange: v => setAttributes( { trackBgColor: v || '#f3f4f6' } ) }, |
| 170 |
{ label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 171 |
{ label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 172 |
] |
| 173 |
} ), |
| 174 |
|
| 175 |
el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false }, |
| 176 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Metric', value: 50, color: '#6366f1' } ] } ) }, __( '+ Add Ring', 'blockenberg' ) ), |
| 177 |
a.series.map( ( ser, si ) => |
| 178 |
el( PanelBody, { key: si, title: ser.label || `Ring ${ si + 1 }`, initialOpen: false }, |
| 179 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: ser.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ), |
| 180 |
el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: ser.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ), |
| 181 |
el( RangeControl, { label: __( 'Value', 'blockenberg' ), value: ser.value, onChange: v => updSer( setAttributes, a.series, si, 'value', v ), min: 0, max: a.maxValue || 100 } ), |
| 182 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 183 |
) |
| 184 |
), |
| 185 |
), |
| 186 |
), |
| 187 |
|
| 188 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-radialbar-title', style: { color: a.titleColor } }, a.title ), |
| 189 |
el( 'div', { className: 'bkbg-radialbar-svg' }, renderRadialBar( a ) ), |
| 190 |
); |
| 191 |
}, |
| 192 |
|
| 193 |
save: function ( { attributes: a } ) { |
| 194 |
var _tvFn = window.bkbgTypoCssVars; |
| 195 |
var saveStyle = {}; |
| 196 |
if (_tvFn) Object.assign(saveStyle, _tvFn(a.titleTypo || {}, '--bkrb-tt-')); |
| 197 |
var blockProps = useBlockProps.save( { className: 'bkbg-radialbar-wrap', style: saveStyle } ); |
| 198 |
return el( 'div', blockProps, |
| 199 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-radialbar-title', style: { color: a.titleColor } }, a.title ) : null, |
| 200 |
el( 'div', { className: 'bkbg-radialbar-svg' }, renderRadialBar( a ) ), |
| 201 |
); |
| 202 |
}, |
| 203 |
|
| 204 |
deprecated: [{ |
| 205 |
attributes: { |
| 206 |
"title":{"type":"string","default":"Q4 Performance Metrics"}, |
| 207 |
"showTitle":{"type":"boolean","default":true}, |
| 208 |
"svgSize":{"type":"integer","default":460}, |
| 209 |
"trackWidth":{"type":"integer","default":22}, |
| 210 |
"trackGap":{"type":"integer","default":10}, |
| 211 |
"startAngle":{"type":"integer","default":-90}, |
| 212 |
"maxValue":{"type":"integer","default":100}, |
| 213 |
"showValues":{"type":"boolean","default":true}, |
| 214 |
"showLabels":{"type":"boolean","default":true}, |
| 215 |
"showLegend":{"type":"boolean","default":true}, |
| 216 |
"roundedCaps":{"type":"boolean","default":true}, |
| 217 |
"labelFontSize":{"type":"integer","default":12}, |
| 218 |
"valueFontSize":{"type":"integer","default":11}, |
| 219 |
"bgColor":{"type":"string","default":"#ffffff"}, |
| 220 |
"trackBgColor":{"type":"string","default":"#f3f4f6"}, |
| 221 |
"titleColor":{"type":"string","default":"#111827"}, |
| 222 |
"textColor":{"type":"string","default":"#374151"}, |
| 223 |
"series":{"type":"array","default":[{"label":"Revenue","value":87,"color":"#6366f1"},{"label":"Users","value":73,"color":"#10b981"},{"label":"Conversion","value":61,"color":"#f59e0b"},{"label":"Retention","value":94,"color":"#ef4444"},{"label":"NPS Score","value":56,"color":"#8b5cf6"}]}, |
| 224 |
"labelFontWeight":{"type":"number","default":600}, |
| 225 |
"valueFontWeight":{"type":"number","default":700}, |
| 226 |
"labelLineHeight":{"type":"number","default":1.4} |
| 227 |
}, |
| 228 |
save: function ( { attributes: a } ) { |
| 229 |
var blockProps = useBlockProps.save( { className: 'bkbg-radialbar-wrap' } ); |
| 230 |
return el( 'div', blockProps, |
| 231 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-radialbar-title', style: { color: a.titleColor } }, a.title ) : null, |
| 232 |
el( 'div', { className: 'bkbg-radialbar-svg' }, renderRadialBar( a ) ), |
| 233 |
); |
| 234 |
} |
| 235 |
}], |
| 236 |
} ); |
| 237 |
}() ); |
| 238 |
|