| 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 |
var _tc, _tv; |
| 9 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 10 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 11 |
|
| 12 |
function DatasetEditor({ ds, index, onChange, onRemove }) { |
| 13 |
function upd(k,v){ onChange(index, Object.assign({},ds,{[k]:v})); } |
| 14 |
return el('div',{style:{border:'1px solid #ddd',borderRadius:'8px',padding:'10px',marginBottom:'8px',background:'#fafafa'}}, |
| 15 |
el(TextControl, {label:__('Label'), value:ds.label, onChange:v=>upd('label',v)}), |
| 16 |
el(TextControl, {label:__('Data (CSV)'), value:ds.data, onChange:v=>upd('data',v), help:'e.g. 10,20,35,18'}), |
| 17 |
el('label',{style:{fontSize:'11px',fontWeight:600,display:'block',marginBottom:'4px'}},__('Color')), |
| 18 |
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'}}), |
| 19 |
el(ToggleControl, {label:__('Fill area under line'), checked:ds.fill, onChange:v=>upd('fill',v), __nextHasNoMarginBottom:true}), |
| 20 |
el(Button,{isDestructive:true,isSmall:true,onClick:()=>onRemove(index),style:{marginTop:'8px'}},__('Remove Dataset')) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
registerBlockType('blockenberg/line-chart', { |
| 25 |
edit: function(props) { |
| 26 |
const {attributes:attr,setAttributes}=props; |
| 27 |
|
| 28 |
function updateDs(i,next){ const d=attr.datasets.slice();d[i]=next;setAttributes({datasets:d}); } |
| 29 |
function removeDs(i){ setAttributes({datasets:attr.datasets.filter((_,x)=>x!==i)}); } |
| 30 |
|
| 31 |
const wrapStyle = (function () { |
| 32 |
var _tvFn = getTypoCssVars(); |
| 33 |
var s = { |
| 34 |
background: attr.bgColor, |
| 35 |
borderRadius: attr.borderRadius+'px', |
| 36 |
padding: '20px', |
| 37 |
}; |
| 38 |
if (_tvFn) Object.assign(s, _tvFn(attr.titleTypo, '--bklc-tt-')); |
| 39 |
return s; |
| 40 |
})(); |
| 41 |
|
| 42 |
const previewStyle = { |
| 43 |
display:'flex',alignItems:'center',justifyContent:'center', |
| 44 |
height: attr.height+'px', |
| 45 |
background:'#f5f5f5',borderRadius:'8px', |
| 46 |
color:'#888',fontSize:'13px',flexDirection:'column',gap:'8px' |
| 47 |
}; |
| 48 |
|
| 49 |
return el('div',{className:'bklc-wrap',style:wrapStyle}, |
| 50 |
|
| 51 |
el(InspectorControls,null, |
| 52 |
el(PanelBody,{title:__('Chart Title'),initialOpen:true}, |
| 53 |
el(TextControl,{label:__('Title (optional)'),value:attr.title,onChange:v=>setAttributes({title:v})}) |
| 54 |
), |
| 55 |
el(PanelBody,{title:__('Labels & Data'),initialOpen:true}, |
| 56 |
el(TextControl,{label:__('X-Axis Labels (CSV)'),value:attr.labels,onChange:v=>setAttributes({labels:v}),help:'e.g. Jan,Feb,Mar'}), |
| 57 |
attr.datasets.map((ds,i)=>el(DatasetEditor,{key:i,ds,index:i,onChange:updateDs,onRemove:removeDs})), |
| 58 |
el(Button,{variant:'secondary',onClick:()=>setAttributes({datasets:[...attr.datasets,{label:'Series '+(attr.datasets.length+1),data:'0,0,0,0,0,0',color:'#f5a623',fill:false,tension:0.4}]}), |
| 59 |
style:{width:'100%',justifyContent:'center',marginTop:'8px'}},__('+ Add Dataset')) |
| 60 |
), |
| 61 |
el(PanelBody,{title:__('Chart Options'),initialOpen:false}, |
| 62 |
el(RangeControl,{label:__('Height (px)'),value:attr.height,min:160,max:700,onChange:v=>setAttributes({height:v})}), |
| 63 |
el(ToggleControl,{label:__('Show Grid'),checked:attr.showGrid,onChange:v=>setAttributes({showGrid:v}),__nextHasNoMarginBottom:true}), |
| 64 |
el(ToggleControl,{label:__('Show Legend'),checked:attr.showLegend,onChange:v=>setAttributes({showLegend:v}),__nextHasNoMarginBottom:true}), |
| 65 |
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})}), |
| 66 |
el(SelectControl,{label:__('Point Style'),value:attr.pointStyle,options:[{label:'Circle',value:'circle'},{label:'Square',value:'rect'},{label:'Diamond',value:'rectRot'},{label:'None',value:'false'}],onChange:v=>setAttributes({pointStyle:v})}), |
| 67 |
el(RangeControl,{label:__('Border Radius (px)'),value:attr.borderRadius,min:0,max:32,onChange:v=>setAttributes({borderRadius:v})}) |
| 68 |
), |
| 69 |
el(PanelColorSettings,{title:__('Background'),initialOpen:false,colorSettings:[ |
| 70 |
{label:__('Card Background'),value:attr.bgColor,onChange:v=>setAttributes({bgColor:v||'#ffffff'})}, |
| 71 |
]}), |
| 72 |
el(PanelBody, { title: 'Typography', initialOpen: false }, |
| 73 |
getTypoControl() && el(getTypoControl(), { label: __('Title'), value: attr.titleTypo || {}, onChange: function (v) { setAttributes({ titleTypo: v }); } }) |
| 74 |
), |
| 75 |
|
| 76 |
), |
| 77 |
|
| 78 |
attr.title && el('h3',{className:'bklc-title'},attr.title), |
| 79 |
el('div',{style:previewStyle}, |
| 80 |
el('span',{className:'dashicons dashicons-chart-line',style:{fontSize:'48px',width:'48px',height:'48px',opacity:0.3}}), |
| 81 |
el('span',null,__('Line Chart — renders on the frontend')), |
| 82 |
el('small',null,attr.datasets.map(d=>d.label).join(' · ')) |
| 83 |
) |
| 84 |
); |
| 85 |
}, |
| 86 |
|
| 87 |
save: function({attributes:attr}) { |
| 88 |
const wrapStyle = (function () { |
| 89 |
var _tvFn = getTypoCssVars(); |
| 90 |
var s = { background: attr.bgColor, borderRadius: attr.borderRadius+'px', padding: '20px' }; |
| 91 |
if (_tvFn) Object.assign(s, _tvFn(attr.titleTypo, '--bklc-tt-')); |
| 92 |
return s; |
| 93 |
})(); |
| 94 |
return el('div',{ |
| 95 |
className:'bklc-wrap',style:wrapStyle, |
| 96 |
'data-type':'line', |
| 97 |
'data-labels':attr.labels, |
| 98 |
'data-datasets':JSON.stringify(attr.datasets), |
| 99 |
'data-height':attr.height, |
| 100 |
'data-grid':attr.showGrid?'1':'0', |
| 101 |
'data-legend':attr.showLegend?'1':'0', |
| 102 |
'data-legend-pos':attr.legendPos, |
| 103 |
'data-point-style':attr.pointStyle, |
| 104 |
}, |
| 105 |
attr.title && el('h3',{className:'bklc-title'},attr.title), |
| 106 |
el('canvas',{className:'bklc-canvas',height:attr.height}) |
| 107 |
); |
| 108 |
} |
| 109 |
}); |
| 110 |
}() ); |
| 111 |
|