| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var __ = wp.i18n.__; |
| 5 |
var useState = wp.element.useState; |
| 6 |
var registerBlockType = wp.blocks.registerBlockType; |
| 7 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 8 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 9 |
var RichText = wp.blockEditor.RichText; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 12 |
var ToggleControl = wp.components.ToggleControl; |
| 13 |
var RangeControl = wp.components.RangeControl; |
| 14 |
var SelectControl = wp.components.SelectControl; |
| 15 |
var TextControl = wp.components.TextControl; |
| 16 |
|
| 17 |
function getTypographyControl() { return window.bkbgTypographyControl; } |
| 18 |
function getTypoCssVars() { return window.bkbgTypoCssVars; } |
| 19 |
|
| 20 |
var styleOptions = [ |
| 21 |
{ label: __('Centered', 'blockenberg'), value: 'centered' }, |
| 22 |
{ label: __('Left Aligned', 'blockenberg'), value: 'left' }, |
| 23 |
{ label: __('Card', 'blockenberg'), value: 'card' }, |
| 24 |
{ label: __('Gradient Card', 'blockenberg'), value: 'gradient' }, |
| 25 |
{ label: __('Border Left', 'blockenberg'), value: 'border-left' }, |
| 26 |
{ label: __('Pill Badge', 'blockenberg'), value: 'pill' } |
| 27 |
]; |
| 28 |
|
| 29 |
var iconOptions = [ |
| 30 |
'star-filled', 'chart-line', 'chart-bar', 'heart', 'thumbs-up', 'smiley', |
| 31 |
'performance', 'award', 'trophy', 'shield', 'checkmark', 'yes', 'yes-alt', |
| 32 |
'groups', 'networking', 'cloud', 'update', 'info', 'warning', 'flag' |
| 33 |
]; |
| 34 |
|
| 35 |
registerBlockType('blockenberg/big-stat', { |
| 36 |
title: __('Big Stat', 'blockenberg'), |
| 37 |
icon: 'chart-bar', |
| 38 |
category: 'bkbg-content', |
| 39 |
description: __('Display a single large impactful statistic.', 'blockenberg'), |
| 40 |
|
| 41 |
edit: function (props) { |
| 42 |
var a = props.attributes; |
| 43 |
var setAttributes = props.setAttributes; |
| 44 |
|
| 45 |
function wrapStyle(a) { |
| 46 |
var _tv = getTypoCssVars(); |
| 47 |
var s = { |
| 48 |
'--bkbg-bs-accent': a.accentColor, |
| 49 |
'--bkbg-bs-value-c': a.valueColor, |
| 50 |
'--bkbg-bs-label-c': a.labelColor, |
| 51 |
'--bkbg-bs-sub-c': a.sublabelColor, |
| 52 |
'--bkbg-bs-bg': a.bgColor, |
| 53 |
'--bkbg-bs-icon-c': a.iconColor, |
| 54 |
'--bkbg-bs-icon-sz': a.iconSize + 'px', |
| 55 |
'--bkbg-bs-r': a.borderRadius + 'px', |
| 56 |
'--bkbg-bs-p': a.padding + 'px', |
| 57 |
'--bkbg-bs-div-w': a.dividerWidth + 'px', |
| 58 |
'--bkbg-bs-ps-sz': a.prefixSuffixSize + 'px' |
| 59 |
}; |
| 60 |
if (_tv) { |
| 61 |
Object.assign(s, _tv(a.valueTypo || {}, '--bkbg-bs-value-')); |
| 62 |
Object.assign(s, _tv(a.labelTypo || {}, '--bkbg-bs-label-')); |
| 63 |
Object.assign(s, _tv(a.sublabelTypo || {}, '--bkbg-bs-sub-')); |
| 64 |
} |
| 65 |
return s; |
| 66 |
} |
| 67 |
|
| 68 |
var inspector = el(InspectorControls, {}, |
| 69 |
el(PanelBody, { title: __('Value & Labels', 'blockenberg'), initialOpen: true }, |
| 70 |
el(TextControl, { |
| 71 |
label: __('Prefix (e.g. $, +)', 'blockenberg'), |
| 72 |
value: a.prefix, |
| 73 |
onChange: function (v) { setAttributes({ prefix: v }); } |
| 74 |
}), |
| 75 |
el(TextControl, { |
| 76 |
label: __('Value', 'blockenberg'), |
| 77 |
value: a.value, |
| 78 |
onChange: function (v) { setAttributes({ value: v }); } |
| 79 |
}), |
| 80 |
el(TextControl, { |
| 81 |
label: __('Suffix (e.g. %, x, k)', 'blockenberg'), |
| 82 |
value: a.suffix, |
| 83 |
onChange: function (v) { setAttributes({ suffix: v }); } |
| 84 |
}), |
| 85 |
el(TextControl, { |
| 86 |
label: __('Label', 'blockenberg'), |
| 87 |
value: a.label, |
| 88 |
onChange: function (v) { setAttributes({ label: v }); } |
| 89 |
}), |
| 90 |
el(ToggleControl, { |
| 91 |
label: __('Show Sub-label / Source', 'blockenberg'), |
| 92 |
checked: a.showSublabel, |
| 93 |
__nextHasNoMarginBottom: true, |
| 94 |
onChange: function (v) { setAttributes({ showSublabel: v }); } |
| 95 |
}), |
| 96 |
a.showSublabel && el(TextControl, { |
| 97 |
label: __('Sub-label', 'blockenberg'), |
| 98 |
value: a.sublabel, |
| 99 |
onChange: function (v) { setAttributes({ sublabel: v }); } |
| 100 |
}) |
| 101 |
), |
| 102 |
|
| 103 |
el(PanelBody, { title: __('Style & Layout', 'blockenberg'), initialOpen: false }, |
| 104 |
el(SelectControl, { |
| 105 |
label: __('Visual Style', 'blockenberg'), |
| 106 |
value: a.style, |
| 107 |
options: styleOptions, |
| 108 |
onChange: function (v) { setAttributes({ style: v }); } |
| 109 |
}), |
| 110 |
el(ToggleControl, { |
| 111 |
label: __('Show Divider', 'blockenberg'), |
| 112 |
checked: a.showDivider, |
| 113 |
__nextHasNoMarginBottom: true, |
| 114 |
onChange: function (v) { setAttributes({ showDivider: v }); } |
| 115 |
}), |
| 116 |
a.showDivider && el(RangeControl, { |
| 117 |
label: __('Divider Width (px)', 'blockenberg'), |
| 118 |
value: a.dividerWidth, |
| 119 |
onChange: function (v) { setAttributes({ dividerWidth: v }); }, |
| 120 |
min: 16, max: 200 |
| 121 |
}), |
| 122 |
el(RangeControl, { |
| 123 |
label: __('Padding (px)', 'blockenberg'), |
| 124 |
value: a.padding, |
| 125 |
onChange: function (v) { setAttributes({ padding: v }); }, |
| 126 |
min: 0, max: 80 |
| 127 |
}), |
| 128 |
el(RangeControl, { |
| 129 |
label: __('Border Radius (px)', 'blockenberg'), |
| 130 |
value: a.borderRadius, |
| 131 |
onChange: function (v) { setAttributes({ borderRadius: v }); }, |
| 132 |
min: 0, max: 40 |
| 133 |
}) |
| 134 |
), |
| 135 |
|
| 136 |
el(PanelBody, { title: __('Icon', 'blockenberg'), initialOpen: false }, |
| 137 |
el(ToggleControl, { |
| 138 |
label: __('Show Icon', 'blockenberg'), |
| 139 |
checked: a.showIcon, |
| 140 |
__nextHasNoMarginBottom: true, |
| 141 |
onChange: function (v) { setAttributes({ showIcon: v }); } |
| 142 |
}), |
| 143 |
a.showIcon && el(SelectControl, { |
| 144 |
label: __('Icon', 'blockenberg'), |
| 145 |
value: a.icon, |
| 146 |
options: iconOptions.map(function (ic) { return { label: ic, value: ic }; }), |
| 147 |
onChange: function (v) { setAttributes({ icon: v }); } |
| 148 |
}), |
| 149 |
a.showIcon && el(RangeControl, { |
| 150 |
label: __('Icon Size (px)', 'blockenberg'), |
| 151 |
value: a.iconSize, |
| 152 |
onChange: function (v) { setAttributes({ iconSize: v }); }, |
| 153 |
min: 20, max: 80 |
| 154 |
}) |
| 155 |
), |
| 156 |
|
| 157 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 158 |
(function () { var TC = getTypographyControl(); return TC ? el(TC, { label: __('Value', 'blockenberg'), value: a.valueTypo || {}, onChange: function (v) { setAttributes({ valueTypo: v }); } }) : null; })(), |
| 159 |
(function () { var TC = getTypographyControl(); return TC ? el(TC, { label: __('Label', 'blockenberg'), value: a.labelTypo || {}, onChange: function (v) { setAttributes({ labelTypo: v }); } }) : null; })(), |
| 160 |
(function () { var TC = getTypographyControl(); return TC ? el(TC, { label: __('Sub-label', 'blockenberg'), value: a.sublabelTypo || {}, onChange: function (v) { setAttributes({ sublabelTypo: v }); } }) : null; })(), |
| 161 |
el(RangeControl, { |
| 162 |
label: __('Prefix/Suffix Size (px)', 'blockenberg'), |
| 163 |
value: a.prefixSuffixSize, |
| 164 |
onChange: function (v) { setAttributes({ prefixSuffixSize: v }); }, |
| 165 |
min: 16, max: 100 |
| 166 |
}) |
| 167 |
), |
| 168 |
|
| 169 |
el(PanelColorSettings, { |
| 170 |
title: __('Colors', 'blockenberg'), |
| 171 |
initialOpen: false, |
| 172 |
colorSettings: [ |
| 173 |
{ value: a.valueColor, onChange: function (v) { setAttributes({ valueColor: v || '#0f172a' }); }, label: __('Value Color', 'blockenberg') }, |
| 174 |
{ value: a.labelColor, onChange: function (v) { setAttributes({ labelColor: v || '#1e293b' }); }, label: __('Label Color', 'blockenberg') }, |
| 175 |
{ value: a.sublabelColor, onChange: function (v) { setAttributes({ sublabelColor: v || '#94a3b8' }); }, label: __('Sub-label Color', 'blockenberg') }, |
| 176 |
{ value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#6c3fb5' }); }, label: __('Accent Color', 'blockenberg') }, |
| 177 |
{ value: a.bgColor, onChange: function (v) { setAttributes({ bgColor: v || '#f8fafc' }); }, label: __('Background Color', 'blockenberg') }, |
| 178 |
{ value: a.iconColor, onChange: function (v) { setAttributes({ iconColor: v || '#6c3fb5' }); }, label: __('Icon Color', 'blockenberg') } |
| 179 |
] |
| 180 |
}) |
| 181 |
); |
| 182 |
|
| 183 |
var wrapClass = 'bkbg-bs-wrap bkbg-bs-style--' + a.style; |
| 184 |
var blockProps = useBlockProps({ className: wrapClass, style: wrapStyle(a) }); |
| 185 |
|
| 186 |
var iconEl = a.showIcon |
| 187 |
? el('span', { className: 'bkbg-bs-icon dashicons dashicons-' + a.icon }) |
| 188 |
: null; |
| 189 |
|
| 190 |
var valueEl = el('div', { className: 'bkbg-bs-value-row' }, |
| 191 |
a.prefix && el('span', { className: 'bkbg-bs-prefix' }, a.prefix), |
| 192 |
el('span', { className: 'bkbg-bs-value' }, a.value || '0'), |
| 193 |
a.suffix && el('span', { className: 'bkbg-bs-suffix' }, a.suffix) |
| 194 |
); |
| 195 |
|
| 196 |
var dividerEl = a.showDivider |
| 197 |
? el('div', { className: 'bkbg-bs-divider' }) |
| 198 |
: null; |
| 199 |
|
| 200 |
var labelEl = el('div', { className: 'bkbg-bs-label' }, a.label); |
| 201 |
var sublabelEl = a.showSublabel |
| 202 |
? el('div', { className: 'bkbg-bs-sublabel' }, a.sublabel) |
| 203 |
: null; |
| 204 |
|
| 205 |
return el(Fragment, {}, |
| 206 |
inspector, |
| 207 |
el('div', blockProps, |
| 208 |
el('div', { className: 'bkbg-bs-inner' }, |
| 209 |
iconEl, |
| 210 |
valueEl, |
| 211 |
dividerEl, |
| 212 |
labelEl, |
| 213 |
sublabelEl |
| 214 |
) |
| 215 |
) |
| 216 |
); |
| 217 |
}, |
| 218 |
|
| 219 |
save: function (props) { |
| 220 |
var a = props.attributes; |
| 221 |
|
| 222 |
function wrapStyle(a) { |
| 223 |
var _tv = getTypoCssVars(); |
| 224 |
var s = { |
| 225 |
'--bkbg-bs-accent': a.accentColor, |
| 226 |
'--bkbg-bs-value-c': a.valueColor, |
| 227 |
'--bkbg-bs-label-c': a.labelColor, |
| 228 |
'--bkbg-bs-sub-c': a.sublabelColor, |
| 229 |
'--bkbg-bs-bg': a.bgColor, |
| 230 |
'--bkbg-bs-icon-c': a.iconColor, |
| 231 |
'--bkbg-bs-icon-sz': a.iconSize + 'px', |
| 232 |
'--bkbg-bs-r': a.borderRadius + 'px', |
| 233 |
'--bkbg-bs-p': a.padding + 'px', |
| 234 |
'--bkbg-bs-div-w': a.dividerWidth + 'px', |
| 235 |
'--bkbg-bs-ps-sz': a.prefixSuffixSize + 'px' |
| 236 |
}; |
| 237 |
if (_tv) { |
| 238 |
Object.assign(s, _tv(a.valueTypo || {}, '--bkbg-bs-value-')); |
| 239 |
Object.assign(s, _tv(a.labelTypo || {}, '--bkbg-bs-label-')); |
| 240 |
Object.assign(s, _tv(a.sublabelTypo || {}, '--bkbg-bs-sub-')); |
| 241 |
} |
| 242 |
return s; |
| 243 |
} |
| 244 |
|
| 245 |
var wrapClass = 'bkbg-bs-wrap bkbg-bs-style--' + a.style; |
| 246 |
var blockProps = wp.blockEditor.useBlockProps.save({ className: wrapClass, style: wrapStyle(a) }); |
| 247 |
|
| 248 |
return el('div', blockProps, |
| 249 |
el('div', { className: 'bkbg-bs-inner' }, |
| 250 |
a.showIcon && el('span', { className: 'bkbg-bs-icon dashicons dashicons-' + a.icon }), |
| 251 |
el('div', { className: 'bkbg-bs-value-row' }, |
| 252 |
a.prefix && el('span', { className: 'bkbg-bs-prefix' }, a.prefix), |
| 253 |
el('span', { |
| 254 |
className: 'bkbg-bs-value', |
| 255 |
'data-target': a.value, |
| 256 |
'data-animate': a.animate ? 'true' : 'false', |
| 257 |
'data-duration': a.animateDuration |
| 258 |
}, a.value || '0'), |
| 259 |
a.suffix && el('span', { className: 'bkbg-bs-suffix' }, a.suffix) |
| 260 |
), |
| 261 |
a.showDivider && el('div', { className: 'bkbg-bs-divider' }), |
| 262 |
el('div', { className: 'bkbg-bs-label' }, a.label), |
| 263 |
a.showSublabel && el('div', { className: 'bkbg-bs-sublabel' }, a.sublabel) |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
}); |
| 268 |
}() ); |
| 269 |
|