| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var useState = wp.element.useState; |
| 4 |
var Fragment = wp.element.Fragment; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var __ = wp.i18n.__; |
| 7 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
var TextareaControl = wp.components.TextareaControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
var SelectControl = wp.components.SelectControl; |
| 16 |
|
| 17 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 18 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 19 |
|
| 20 |
registerBlockType('blockenberg/statistics-calculator', { |
| 21 |
edit: function (props) { |
| 22 |
var attributes = props.attributes; |
| 23 |
var setAttributes = props.setAttributes; |
| 24 |
|
| 25 |
var tv = getTypoCssVars(); |
| 26 |
var blockProps = useBlockProps({ style: (function () { |
| 27 |
var s = { |
| 28 |
background: attributes.sectionBg, |
| 29 |
paddingTop: attributes.paddingTop + 'px', |
| 30 |
paddingBottom: attributes.paddingBottom + 'px', |
| 31 |
'--bkstc-tt-sz': attributes.titleSize + 'px', |
| 32 |
'--bkstc-tt-wt': attributes.titleFontWeight || '800', |
| 33 |
'--bkstc-tt-lh': attributes.titleLineHeight, |
| 34 |
'--bkstc-st-sz': (attributes.subtitleFontSize || 14) + 'px', |
| 35 |
'--bkstc-st-lh': attributes.subtitleLineHeight |
| 36 |
}; |
| 37 |
Object.assign(s, tv(attributes.titleTypo, '--bkstc-tt-')); |
| 38 |
Object.assign(s, tv(attributes.subtitleTypo, '--bkstc-st-')); |
| 39 |
return s; |
| 40 |
}()) }); |
| 41 |
|
| 42 |
function togCtrl(label, key) { |
| 43 |
return el(ToggleControl, { |
| 44 |
__nextHasNoMarginBottom: true, |
| 45 |
label: label, |
| 46 |
checked: attributes[key], |
| 47 |
onChange: function (v) { var o = {}; o[key] = v; setAttributes(o); } |
| 48 |
}); |
| 49 |
} |
| 50 |
function rng(label, key, min, max, step) { |
| 51 |
return el(RangeControl, { |
| 52 |
__nextHasNoMarginBottom: true, |
| 53 |
label: label, value: attributes[key], min: min, max: max, step: step || 1, |
| 54 |
onChange: function (v) { var o = {}; o[key] = v; setAttributes(o); } |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
var colors = [ |
| 59 |
{ label: 'Accent', value: attributes.accentColor, onChange: function (v) { setAttributes({ accentColor: v }); } }, |
| 60 |
{ label: 'Mean color', value: attributes.meanColor, onChange: function (v) { setAttributes({ meanColor: v }); } }, |
| 61 |
{ label: 'Median color', value: attributes.medianColor, onChange: function (v) { setAttributes({ medianColor: v }); } }, |
| 62 |
{ label: 'Mode color', value: attributes.modeColor, onChange: function (v) { setAttributes({ modeColor: v }); } }, |
| 63 |
{ label: 'Std Dev color', value: attributes.stdDevColor, onChange: function (v) { setAttributes({ stdDevColor: v }); } }, |
| 64 |
{ label: 'Bar / histogram', value: attributes.barColor, onChange: function (v) { setAttributes({ barColor: v }); } }, |
| 65 |
{ label: 'Section BG', value: attributes.sectionBg, onChange: function (v) { setAttributes({ sectionBg: v }); } }, |
| 66 |
{ label: 'Card BG', value: attributes.cardBg, onChange: function (v) { setAttributes({ cardBg: v }); } }, |
| 67 |
{ label: 'Stat card BG', value: attributes.statCardBg, onChange: function (v) { setAttributes({ statCardBg: v }); } }, |
| 68 |
{ label: 'Title', value: attributes.titleColor, onChange: function (v) { setAttributes({ titleColor: v }); } }, |
| 69 |
{ label: 'Subtitle', value: attributes.subtitleColor, onChange: function (v) { setAttributes({ subtitleColor: v }); } }, |
| 70 |
{ label: 'Label', value: attributes.labelColor, onChange: function (v) { setAttributes({ labelColor: v }); } }, |
| 71 |
{ label: 'Input BG', value: attributes.inputBg, onChange: function (v) { setAttributes({ inputBg: v }); } }, |
| 72 |
{ label: 'Input border', value: attributes.inputBorder, onChange: function (v) { setAttributes({ inputBorder: v }); } } |
| 73 |
]; |
| 74 |
|
| 75 |
return el(Fragment, null, |
| 76 |
el(InspectorControls, null, |
| 77 |
el(PanelBody, { title: 'Content', initialOpen: true }, |
| 78 |
togCtrl('Show title', 'showTitle'), |
| 79 |
attributes.showTitle && el(TextControl, { |
| 80 |
__nextHasNoMarginBottom: true, label: 'Title', value: attributes.title, |
| 81 |
onChange: function (v) { setAttributes({ title: v }); } |
| 82 |
}), |
| 83 |
togCtrl('Show subtitle', 'showSubtitle'), |
| 84 |
attributes.showSubtitle && el(TextControl, { |
| 85 |
__nextHasNoMarginBottom: true, label: 'Subtitle', value: attributes.subtitle, |
| 86 |
onChange: function (v) { setAttributes({ subtitle: v }); } |
| 87 |
}), |
| 88 |
el(TextControl, { |
| 89 |
__nextHasNoMarginBottom: true, label: 'Default dataset (comma/space sep.)', |
| 90 |
value: attributes.defaultData, |
| 91 |
onChange: function (v) { setAttributes({ defaultData: v }); } |
| 92 |
}), |
| 93 |
rng('Decimal places', 'decimalPlaces', 0, 10) |
| 94 |
), |
| 95 |
el(PanelBody, { title: 'Display', initialOpen: false }, |
| 96 |
togCtrl('Show histogram', 'showHistogram'), |
| 97 |
togCtrl('Show box plot', 'showBoxPlot'), |
| 98 |
togCtrl('Show sorted list','showSortedList') |
| 99 |
), |
| 100 |
el(PanelBody, { title: 'Typography', initialOpen: false }, |
| 101 |
getTypoControl()({ label: __('Title', 'blockenberg'), value: attributes.titleTypo, onChange: function (v) { setAttributes({ titleTypo: v }); } }), |
| 102 |
getTypoControl()({ label: __('Subtitle', 'blockenberg'), value: attributes.subtitleTypo, onChange: function (v) { setAttributes({ subtitleTypo: v }); } }) |
| 103 |
), |
| 104 |
el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colors }), |
| 105 |
el(PanelBody, { title: 'Sizing', initialOpen: false }, |
| 106 |
rng('Max width (px)', 'maxWidth', 400, 1200, 10), |
| 107 |
rng('Card radius (px)', 'cardRadius', 0, 32), |
| 108 |
rng('Stat radius (px)', 'statRadius', 0, 24), |
| 109 |
rng('Input radius (px)', 'inputRadius', 0, 24), |
| 110 |
rng('Padding top (px)', 'paddingTop', 0, 160, 4), |
| 111 |
rng('Padding bottom (px)', 'paddingBottom', 0, 160, 4) |
| 112 |
) |
| 113 |
), |
| 114 |
el('div', blockProps, |
| 115 |
el('div', { |
| 116 |
style: { |
| 117 |
background: attributes.cardBg, borderRadius: attributes.cardRadius + 'px', |
| 118 |
maxWidth: attributes.maxWidth + 'px', margin: '0 auto', padding: '40px 36px', |
| 119 |
boxShadow: '0 4px 32px rgba(0,0,0,0.08)' |
| 120 |
} |
| 121 |
}, |
| 122 |
attributes.showTitle && el('div', { |
| 123 |
className: 'bkbg-stc-title', |
| 124 |
style: { color: attributes.titleColor, marginBottom: '6px' } |
| 125 |
}, attributes.title), |
| 126 |
attributes.showSubtitle && el('div', { |
| 127 |
className: 'bkbg-stc-subtitle', |
| 128 |
style: { color: attributes.subtitleColor, marginBottom: '24px' } |
| 129 |
}, attributes.subtitle), |
| 130 |
el('div', { style: { fontSize: '13px', color: attributes.labelColor, fontWeight: '600', marginBottom: '6px' } }, 'Dataset'), |
| 131 |
el('div', { |
| 132 |
style: { |
| 133 |
background: attributes.inputBg, border: '1.5px solid ' + attributes.inputBorder, |
| 134 |
borderRadius: attributes.inputRadius + 'px', padding: '12px 14px', |
| 135 |
fontFamily: 'monospace', fontSize: '14px', color: '#374151' |
| 136 |
} |
| 137 |
}, attributes.defaultData), |
| 138 |
el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: '12px', marginTop: '24px' } }, |
| 139 |
[ |
| 140 |
{ label: 'Mean', color: attributes.meanColor, val: '—' }, |
| 141 |
{ label: 'Median', color: attributes.medianColor, val: '—' }, |
| 142 |
{ label: 'Mode', color: attributes.modeColor, val: '—' }, |
| 143 |
{ label: 'Std Dev', color: attributes.stdDevColor, val: '—' }, |
| 144 |
{ label: 'Min', color: attributes.accentColor, val: '—' }, |
| 145 |
{ label: 'Max', color: attributes.accentColor, val: '—' } |
| 146 |
].map(function (s) { |
| 147 |
return el('div', { |
| 148 |
key: s.label, |
| 149 |
style: { |
| 150 |
background: attributes.statCardBg, borderRadius: attributes.statRadius + 'px', |
| 151 |
padding: '14px 12px', textAlign: 'center', |
| 152 |
borderTop: '3px solid ' + s.color |
| 153 |
} |
| 154 |
}, |
| 155 |
el('div', { style: { fontSize: '11px', fontWeight: '700', color: '#9ca3af', marginBottom: '4px', textTransform: 'uppercase', letterSpacing: '0.06em' } }, s.label), |
| 156 |
el('div', { style: { fontSize: '22px', fontWeight: '800', color: s.color } }, s.val) |
| 157 |
); |
| 158 |
}) |
| 159 |
) |
| 160 |
) |
| 161 |
) |
| 162 |
); |
| 163 |
}, |
| 164 |
|
| 165 |
save: function (props) { |
| 166 |
var a = props.attributes; |
| 167 |
return el('div', wp.blockEditor.useBlockProps.save(), |
| 168 |
el('div', { className: 'bkbg-stc-app', 'data-opts': JSON.stringify(a) }) |
| 169 |
); |
| 170 |
} |
| 171 |
}); |
| 172 |
}() ); |
| 173 |
|