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 / bar-chart / index.js

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

106 lines 6.6 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, PanelColorSettings } = window.wp.blockEditor;
5 const { PanelBody, RangeControl, SelectControl, TextControl, ToggleControl, Button } = window.wp.components;
6 const { __ } = window.wp.i18n;
7
8 function getTypographyControl() {
9 return (typeof window.bkbgTypographyControl !== 'undefined') ? window.bkbgTypographyControl : null;
10 }
11 function getTypoCssVars() {
12 return (typeof window.bkbgTypoCssVars !== 'undefined') ? window.bkbgTypoCssVars : function() { return {}; };
13 }
14
15 function buildWrapStyle(attr) {
16 var _tv = getTypoCssVars();
17 var s = { background: attr.bgColor, borderRadius: attr.borderRadius + 'px', padding: '20px' };
18 Object.assign(s, _tv(attr.titleTypo || {}, '--bkbc-title-'));
19 return s;
20 }
21
22 function DatasetEditor({ ds, index, onChange, onRemove }) {
23 function upd(k,v){ onChange(index, Object.assign({},ds,{[k]:v})); }
24 return el('div',{style:{border:'1px solid #ddd',borderRadius:'8px',padding:'10px',marginBottom:'8px',background:'#fafafa'}},
25 el(TextControl, {label:__('Label'), value:ds.label, onChange:v=>upd('label',v)}),
26 el(TextControl, {label:__('Data (CSV)'), value:ds.data, onChange:v=>upd('data',v), help:'e.g. 10,20,35,18'}),
27 el('label',{style:{fontSize:'11px',fontWeight:600,display:'block',marginBottom:'4px'}},__('Color')),
28 el('input',{type:'color',value:ds.color,onChange:e=>upd('color',e.target.value),style:{width:'100%',height:'34px',border:'1px solid #ccc',borderRadius:'4px',cursor:'pointer'}}),
29 el(Button,{isDestructive:true,isSmall:true,onClick:()=>onRemove(index),style:{marginTop:'8px'}},__('Remove'))
30 );
31 }
32
33 registerBlockType('blockenberg/bar-chart', {
34 edit: function(props) {
35 const {attributes:attr,setAttributes}=props;
36 function updateDs(i,next){const d=attr.datasets.slice();d[i]=next;setAttributes({datasets:d});}
37 function removeDs(i){setAttributes({datasets:attr.datasets.filter((_,x)=>x!==i)});}
38
39 const wrapStyle=buildWrapStyle(attr);
40 const previewStyle={display:'flex',alignItems:'center',justifyContent:'center',height:attr.height+'px',background:'#f5f5f5',borderRadius:'8px',color:'#888',fontSize:'13px',flexDirection:'column',gap:'8px'};
41 var TC = getTypographyControl();
42
43 return el('div',{className:'bkbc-wrap',style:wrapStyle},
44 el(InspectorControls,null,
45 el(PanelBody,{title:__('Chart Title'),initialOpen:true},
46 el(TextControl,{label:__('Title (optional)'),value:attr.title,onChange:v=>setAttributes({title:v})})
47 ),
48 el(PanelBody,{title:__('Labels & Data'),initialOpen:true},
49 el(TextControl,{label:__('Category Labels (CSV)'),value:attr.labels,onChange:v=>setAttributes({labels:v})}),
50 attr.datasets.map((ds,i)=>el(DatasetEditor,{key:i,ds,index:i,onChange:updateDs,onRemove:removeDs})),
51 el(Button,{variant:'secondary',onClick:()=>setAttributes({datasets:[...attr.datasets,{label:'Series '+(attr.datasets.length+1),data:'0,0,0,0',color:'#f5a623'}]}),
52 style:{width:'100%',justifyContent:'center',marginTop:'8px'}},__('+ Add Dataset'))
53 ),
54 el(PanelBody,{title:__('Chart Options'),initialOpen:false},
55 el(SelectControl,{label:__('Orientation'),value:attr.orientation,options:[{label:'Vertical',value:'vertical'},{label:'Horizontal',value:'horizontal'}],onChange:v=>setAttributes({orientation:v})}),
56 el(ToggleControl,{label:__('Stacked'),checked:attr.stacked,onChange:v=>setAttributes({stacked:v}),__nextHasNoMarginBottom:true}),
57 el(RangeControl,{label:__('Bar Border Radius'),value:attr.barRadius,min:0,max:20,onChange:v=>setAttributes({barRadius:v})}),
58 el(RangeControl,{label:__('Height (px)'),value:attr.height,min:160,max:700,onChange:v=>setAttributes({height:v})}),
59 el(ToggleControl,{label:__('Show Grid'),checked:attr.showGrid,onChange:v=>setAttributes({showGrid:v}),__nextHasNoMarginBottom:true}),
60 el(ToggleControl,{label:__('Show Legend'),checked:attr.showLegend,onChange:v=>setAttributes({showLegend:v}),__nextHasNoMarginBottom:true}),
61 el(SelectControl,{label:__('Legend Position'),value:attr.legendPos,options:[{label:'Top',value:'top'},{label:'Bottom',value:'bottom'},{label:'Left',value:'left'},{label:'Right',value:'right'}],onChange:v=>setAttributes({legendPos:v})}),
62 el(RangeControl,{label:__('Card Border Radius (px)'),value:attr.borderRadius,min:0,max:32,onChange:v=>setAttributes({borderRadius:v})})
63 ),
64 el(PanelColorSettings,{title:__('Background'),initialOpen:false,colorSettings:[
65 {label:__('Card Background'),value:attr.bgColor,onChange:v=>setAttributes({bgColor:v||'#ffffff'})},
66 ]}),
67 el(PanelBody,{title:__('Typography'),initialOpen:false},
68 TC && el(TC, { label: __('Title', 'blockenberg'), value: attr.titleTypo || {}, onChange: function(v){ setAttributes({titleTypo: v}); } }),
69 el(RangeControl,{label:__('Legend Font Size (px)'),value:attr.legendFontSize,min:8,max:20,onChange:v=>setAttributes({legendFontSize:v}),__nextHasNoMarginBottom:true}),
70 el(RangeControl,{label:__('Axis Label Font Size (px)'),value:attr.axisLabelFontSize,min:8,max:18,onChange:v=>setAttributes({axisLabelFontSize:v}),__nextHasNoMarginBottom:true})
71 )
72 ),
73 attr.title && el('h3',{className:'bkbc-title'},attr.title),
74 el('div',{style:previewStyle},
75 el('span',{className:'dashicons dashicons-chart-bar',style:{fontSize:'48px',width:'48px',height:'48px',opacity:0.3}}),
76 el('span',null,__('Bar Chart — renders on the frontend')),
77 el('small',null,attr.datasets.map(d=>d.label).join(' · '))
78 )
79 );
80 },
81
82 save: function({attributes:attr}) {
83 const wrapStyle=buildWrapStyle(attr);
84 return el('div',{
85 className:'bkbc-wrap',style:wrapStyle,
86 'data-type':'bar',
87 'data-labels':attr.labels,
88 'data-datasets':JSON.stringify(attr.datasets),
89 'data-orientation':attr.orientation,
90 'data-stacked':attr.stacked?'1':'0',
91 'data-bar-radius':attr.barRadius,
92 'data-height':attr.height,
93 'data-grid':attr.showGrid?'1':'0',
94 'data-legend':attr.showLegend?'1':'0',
95 'data-legend-pos':attr.legendPos,
96 'data-title-size':attr.chartTitleFontSize,
97 'data-legend-size':attr.legendFontSize,
98 'data-axis-label-size':attr.axisLabelFontSize,
99 },
100 attr.title && el('h3',{className:'bkbc-title'},attr.title),
101 el('canvas',{className:'bkbc-canvas',height:attr.height})
102 );
103 }
104 });
105 }() );
106