PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / waterfall-bar / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/waterfall-bar/index.js

193 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 _tc, _tvf;
9 Object.defineProperty(window, '_tc', { get: function () { return _tc || (_tc = window.bkbgTypographyControl); } });
10 Object.defineProperty(window, '_tvf', { get: function () { return _tvf || (_tvf = window.bkbgTypoCssVars); } });
11 function getTypoControl(label, key, attrs, setA) { return _tc(label, key, attrs, setA); }
12 function getTypoCssVars(attrs) {
13 var v = {};
14 _tvf(v, 'titleTypo', attrs, '--bkwfb-tt-');
15 return v;
16 }
17
18 // ── Renderer ──────────────────────────────────────────────────────────────
19 function renderWaterfall( a ) {
20 const items = a.items || [];
21 const W = a.svgWidth;
22 const H = a.svgHeight;
23 const PT = a.padTop;
24 const PL = a.padLeft;
25 const PR = a.padRight;
26 const PB = a.padBottom;
27 const BW = a.barWidth;
28 const chartW = W - PL - PR;
29 const chartH = H - PT - PB;
30 const n = items.length;
31 if ( ! n ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } );
32
33 // Compute running totals
34 const barData = [];
35 let running = 0;
36 items.forEach( ( it, i ) => {
37 if ( it.isTotal ) {
38 // auto-calc for last total, or use explicit value if non-zero
39 const total = ( it.value !== 0 ) ? it.value : running;
40 barData.push( { ...it, base: 0, end: total, displayVal: total } );
41 running = total;
42 } else {
43 const base = running;
44 const end = running + it.value;
45 barData.push( { ...it, base, end, displayVal: it.value } );
46 running = end;
47 }
48 } );
49
50 // Y range
51 const allVals = barData.flatMap( b => [ b.base, b.end ] );
52 const minV = Math.min( 0, ...allVals );
53 const maxV = Math.max( 0, ...allVals );
54 const range = ( maxV - minV ) || 1;
55 const pad = range * 0.1;
56
57 function yPos( v ) { return PT + chartH - ( ( v - ( minV - pad ) ) / ( range + pad * 2 ) ) * chartH; }
58 const y0 = yPos( 0 );
59
60 // X positions (bar centres)
61 const step = chartW / n;
62 function xCentre( i ) { return PL + i * step + step / 2; }
63
64 const svgEls = [];
65 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
66
67 // Grid lines
68 if ( a.showGridLines ) {
69 const ticks = 5;
70 for ( let t = 0; t <= ticks; t++ ) {
71 const gy = PT + ( t / ticks ) * chartH;
72 svgEls.push( el( 'line', { key: `gl${ t }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
73 const yLbl = Math.round( ( minV - pad ) + ( 1 - t / ticks ) * ( range + pad * 2 ) );
74 svgEls.push( el( 'text', { key: `gy${ t }`, x: PL - 6, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.labelColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, yLbl ) );
75 }
76 }
77
78 // Zero baseline
79 svgEls.push( el( 'line', { key: 'zero', x1: PL, y1: y0, x2: W - PR, y2: y0, stroke: a.labelColor, strokeWidth: 1, strokeOpacity: 0.4 } ) );
80
81 // Bars + connectors
82 barData.forEach( ( b, i ) => {
83 const cx = xCentre( i );
84 const bx = cx - BW / 2;
85 const yTop = Math.min( yPos( b.base ), yPos( b.end ) );
86 const bH = Math.abs( yPos( b.end ) - yPos( b.base ) ) || 2;
87
88 const color = b.isTotal
89 ? ( a.totalColor || '#6366f1' )
90 : b.value >= 0
91 ? ( a.posColor || '#10b981' )
92 : ( a.negColor || '#ef4444' );
93
94 svgEls.push( el( 'rect', { key: `bar${ i }`, x: bx, y: yTop, width: BW, height: bH, fill: color, rx: 3 } ) );
95
96 // Connector to next bar
97 if ( a.showConnectors && i < n - 1 ) {
98 const connY = yPos( b.end );
99 svgEls.push( el( 'line', { key: `con${ i }`, x1: bx + BW, y1: connY, x2: xCentre( i + 1 ) - BW / 2, y2: connY, stroke: a.connectorColor || '#d1d5db', strokeWidth: 1.5, strokeDasharray: '4 3' } ) );
100 }
101
102 // Value label
103 if ( a.showValues ) {
104 const sign = b.displayVal > 0 ? '+' : '';
105 const lbl = b.isTotal ? String( b.displayVal ) : `${ sign }${ b.displayVal }`;
106 svgEls.push( el( 'text', { key: `vl${ i }`, x: cx, y: yTop - 5, textAnchor: 'middle', fill: color, fontSize: a.valueFontSize, fontWeight: 700, fontFamily: 'inherit' }, lbl ) );
107 }
108
109 // X label (rotated if needed)
110 if ( a.labelRotate ) {
111 svgEls.push( el( 'text', { key: `xl${ i }`, x: cx, y: H - PB + 14, textAnchor: 'end', fill: a.labelColor, fontSize: a.labelFontSize, fontFamily: 'inherit', transform: `rotate(-40,${ cx },${ H - PB + 14 })` }, b.label ) );
112 } else {
113 svgEls.push( el( 'text', { key: `xl${ i }`, x: cx, y: H - PB + 16, textAnchor: 'middle', fill: a.labelColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, b.label ) );
114 }
115 } );
116
117 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
118 }
119
120 function updItem( setAttributes, items, ii, field, val ) {
121 setAttributes( { items: items.map( ( it, i ) => i === ii ? { ...it, [field]: val } : it ) } );
122 }
123
124 registerBlockType( 'blockenberg/waterfall-bar', {
125 edit: function ( props ) {
126 const { attributes: a, setAttributes } = props;
127 const blockProps = useBlockProps( { className: 'bkbg-waterfall-wrap', style: getTypoCssVars( a ) } );
128
129 return el( 'div', blockProps,
130 el( InspectorControls, {},
131
132 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
133 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
134 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
135 el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
136 el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ),
137 el( ToggleControl, { label: __( 'Show Connectors', 'blockenberg' ), checked: a.showConnectors, onChange: v => setAttributes( { showConnectors: v } ) } ),
138 el( ToggleControl, { label: __( 'Rotate X Labels', 'blockenberg' ), checked: a.labelRotate, onChange: v => setAttributes( { labelRotate: v } ) } ),
139 ),
140
141 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
142 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
143 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 800 } ),
144 el( RangeControl, { label: __( 'Bar Width', 'blockenberg' ), value: a.barWidth, onChange: v => setAttributes( { barWidth: v } ), min: 10, max: 100 } ),
145 ),
146
147
148 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
149 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } ),
150 el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 18 } ),
151 getTypoControl( __( 'Title', 'blockenberg' ), 'titleTypo', a, setAttributes ),
152 ),
153 el( PanelColorSettings, {
154 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
155 colorSettings: [
156 { label: __( 'Positive bars', 'blockenberg' ), value: a.posColor, onChange: v => setAttributes( { posColor: v || '#10b981' } ) },
157 { label: __( 'Negative bars', 'blockenberg' ), value: a.negColor, onChange: v => setAttributes( { negColor: v || '#ef4444' } ) },
158 { label: __( 'Total bars', 'blockenberg' ), value: a.totalColor, onChange: v => setAttributes( { totalColor: v || '#6366f1' } ) },
159 { label: __( 'Connector lines', 'blockenberg' ), value: a.connectorColor, onChange: v => setAttributes( { connectorColor: v || '#d1d5db' } ) },
160 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
161 { label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
162 { label: __( 'Labels', 'blockenberg' ), value: a.labelColor, onChange: v => setAttributes( { labelColor: v || '#374151' } ) },
163 ]
164 } ),
165
166 el( PanelBody, { title: __( 'Items', 'blockenberg' ), initialOpen: false },
167 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { items: [ ...a.items, { label: 'New Item', value: 100, isTotal: false } ] } ) }, __( '+ Add Item', 'blockenberg' ) ),
168 a.items.map( ( it, ii ) =>
169 el( PanelBody, { key: ii, title: it.label || `Item ${ ii + 1 }`, initialOpen: false },
170 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: it.label, onChange: v => updItem( setAttributes, a.items, ii, 'label', v ) } ),
171 el( TextControl, { label: __( 'Value', 'blockenberg' ), value: String( it.value ), type: 'number', onChange: v => updItem( setAttributes, a.items, ii, 'value', parseFloat( v ) || 0 ) } ),
172 el( ToggleControl, { label: __( 'Is Total bar', 'blockenberg' ), checked: it.isTotal, onChange: v => updItem( setAttributes, a.items, ii, 'isTotal', v ) } ),
173 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { items: a.items.filter( ( _, x ) => x !== ii ) } ) }, __( 'Remove', 'blockenberg' ) ),
174 )
175 ),
176 ),
177 ),
178
179 a.showTitle && a.title && el( 'h3', { className: 'bkbg-waterfall-title', style: { color: a.titleColor } }, a.title ),
180 el( 'div', { className: 'bkbg-waterfall-svg' }, renderWaterfall( a ) ),
181 );
182 },
183
184 save: function ( { attributes: a } ) {
185 const blockProps = useBlockProps.save( { className: 'bkbg-waterfall-wrap', style: getTypoCssVars( a ) } );
186 return el( 'div', blockProps,
187 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-waterfall-title', style: { color: a.titleColor } }, a.title ) : null,
188 el( 'div', { className: 'bkbg-waterfall-svg' }, renderWaterfall( a ) ),
189 );
190 },
191 } );
192 }() );
193