| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 6 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 7 |
var PanelBody = wp.components.PanelBody; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var ToggleControl = wp.components.ToggleControl; |
| 10 |
var RangeControl = wp.components.RangeControl; |
| 11 |
var SelectControl = wp.components.SelectControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
var TextareaControl = wp.components.TextareaControl; |
| 14 |
var Button = wp.components.Button; |
| 15 |
|
| 16 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 17 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 18 |
|
| 19 |
function upd(arr, idx, field, val) { |
| 20 |
return arr.map(function (e, i) { |
| 21 |
if (i !== idx) return e; |
| 22 |
var u = {}; u[field] = val; |
| 23 |
return Object.assign({}, e, u); |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
function updOpt(questions, qi, oi, field, val) { |
| 28 |
return questions.map(function (q, i) { |
| 29 |
if (i !== qi) return q; |
| 30 |
var newOpts = q.options.map(function (o, j) { |
| 31 |
if (j !== oi) return o; |
| 32 |
var u = {}; u[field] = val; |
| 33 |
return Object.assign({}, o, u); |
| 34 |
}); |
| 35 |
return Object.assign({}, q, { options: newOpts }); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
function fmtN(n) { |
| 40 |
return n >= 1000 ? (n / 1000).toFixed(0) + 'k' : String(n); |
| 41 |
} |
| 42 |
|
| 43 |
registerBlockType('blockenberg/survey-results', { |
| 44 |
edit: function (props) { |
| 45 |
var a = props.attributes; |
| 46 |
var set = props.setAttributes; |
| 47 |
var blockProps = useBlockProps((function () { |
| 48 |
var _tvf = getTypoCssVars(); |
| 49 |
var s = {}; |
| 50 |
if (_tvf) { |
| 51 |
Object.assign(s, _tvf(a.titleTypo, '--bksr-tt-')); |
| 52 |
Object.assign(s, _tvf(a.textTypo, '--bksr-tx-')); |
| 53 |
} |
| 54 |
return { className: 'bkbg-sr-editor-wrap', style: s }; |
| 55 |
})()); |
| 56 |
|
| 57 |
function renderQuestion(q, qi) { |
| 58 |
var maxPct = Math.max.apply(null, q.options.map(function (o) { return o.percent; })); |
| 59 |
return el('div', { key: qi, style: { padding: '18px 22px', borderBottom: '1px solid ' + a.borderColor } }, |
| 60 |
el('p', { className: 'bkbg-sr-q-text', style: { margin: '0 0 14px', color: a.questionColor } }, (qi + 1) + '. ' + q.question), |
| 61 |
q.options.map(function (opt, oi) { |
| 62 |
var isWinner = a.highlightWinner && opt.percent === maxPct; |
| 63 |
return el('div', { key: oi, style: { marginBottom: 10, padding: isWinner ? '8px 10px' : '0', background: isWinner ? a.winnerBg : 'transparent', borderRadius: 6 } }, |
| 64 |
// Label row |
| 65 |
el('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 5 } }, |
| 66 |
el('span', { style: { color: a.labelColor, fontWeight: isWinner ? 700 : 400 } }, opt.label), |
| 67 |
el('span', { style: { display: 'flex', gap: 6 } }, |
| 68 |
a.showPercentages && el('strong', { style: { color: a.percentColor, fontVariantNumeric: 'tabular-nums' } }, opt.percent + '%'), |
| 69 |
a.showCounts && el('span', { style: { color: a.countColor } }, '(' + fmtN(opt.count) + ')') |
| 70 |
) |
| 71 |
), |
| 72 |
// Bar |
| 73 |
a.showBars && el('div', { style: { height: a.barHeight + 'px', background: a.barBg, borderRadius: 100 } }, |
| 74 |
el('div', { style: { width: opt.percent + '%', height: '100%', background: isWinner ? a.barFillWinner : a.barFill, borderRadius: 100, transition: 'width .3s' } }) |
| 75 |
) |
| 76 |
); |
| 77 |
}) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
var preview = el('div', { className: 'bkbg-sr-wrap', style: { border: '1px solid ' + a.borderColor, borderRadius: a.borderRadius + 'px', overflow: 'hidden', background: a.bgColor } }, |
| 82 |
// Header |
| 83 |
el('div', { className: 'bkbg-sr-header', style: { background: a.headerBg, padding: '18px 22px' } }, |
| 84 |
el('h2', { className: 'bkbg-sr-title', style: { margin: '0 0 6px', color: a.headerColor } }, a.surveyTitle), |
| 85 |
a.showMeta && el('div', { className: 'bkbg-sr-meta', style: { color: a.metaColor } }, |
| 86 |
el('span', null, '🏢 ' + a.organization), |
| 87 |
el('span', null, '👥 ' + a.respondents.toLocaleString() + ' ' + a.respondentsLabel), |
| 88 |
el('span', null, '� |
| 89 |
' + a.surveyDate) |
| 90 |
) |
| 91 |
), |
| 92 |
// Questions |
| 93 |
el('div', null, a.questions.map(renderQuestion)), |
| 94 |
// Methodology |
| 95 |
a.showMethodology && a.methodology && el('div', { className: 'bkbg-sr-methodology', style: { background: a.methodologyBg, padding: '12px 22px', borderTop: '1px solid ' + a.borderColor } }, |
| 96 |
el('p', { className: 'bkbg-sr-method-text', style: { margin: 0, color: a.methodologyColor, fontStyle: 'italic' } }, |
| 97 |
el('strong', null, 'Methodology: '), |
| 98 |
a.methodology |
| 99 |
) |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
return el(Fragment, null, |
| 104 |
el('div', blockProps, preview), |
| 105 |
el(InspectorControls, null, |
| 106 |
// Survey Info |
| 107 |
el(PanelBody, { title: 'Survey Info', initialOpen: true }, |
| 108 |
el(TextControl, { label: 'Survey Title', value: a.surveyTitle, __nextHasNoMarginBottom: true, onChange: function (v) { set({ surveyTitle: v }); } }), |
| 109 |
el('div', { style: { marginTop: 8 } }, |
| 110 |
el(TextControl, { label: 'Organization', value: a.organization, __nextHasNoMarginBottom: true, onChange: function (v) { set({ organization: v }); } }) |
| 111 |
), |
| 112 |
el('div', { style: { marginTop: 8, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 } }, |
| 113 |
el(TextControl, { label: 'Date', value: a.surveyDate, __nextHasNoMarginBottom: true, onChange: function (v) { set({ surveyDate: v }); } }), |
| 114 |
el(TextControl, { label: 'Respondents Label', value: a.respondentsLabel, __nextHasNoMarginBottom: true, onChange: function (v) { set({ respondentsLabel: v }); } }) |
| 115 |
), |
| 116 |
el('div', { style: { marginTop: 8 } }, |
| 117 |
el(RangeControl, { label: 'Number of Respondents', value: a.respondents, min: 100, max: 1000000, step: 100, __nextHasNoMarginBottom: true, onChange: function (v) { set({ respondents: v }); } }) |
| 118 |
), |
| 119 |
el('div', { style: { marginTop: 8 } }, |
| 120 |
el(ToggleControl, { label: 'Show Meta (org, respondents, date)', checked: a.showMeta, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showMeta: v }); } }) |
| 121 |
) |
| 122 |
), |
| 123 |
// Display Options |
| 124 |
el(PanelBody, { title: 'Display Options', initialOpen: false }, |
| 125 |
el(ToggleControl, { label: 'Show Bars', checked: a.showBars, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showBars: v }); } }), |
| 126 |
el('div', { style: { marginTop: 8 } }, |
| 127 |
el(ToggleControl, { label: 'Show Percentages', checked: a.showPercentages, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showPercentages: v }); } }) |
| 128 |
), |
| 129 |
el('div', { style: { marginTop: 8 } }, |
| 130 |
el(ToggleControl, { label: 'Show Counts', checked: a.showCounts, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showCounts: v }); } }) |
| 131 |
), |
| 132 |
el('div', { style: { marginTop: 8 } }, |
| 133 |
el(ToggleControl, { label: 'Highlight Winner Option', checked: a.highlightWinner, __nextHasNoMarginBottom: true, onChange: function (v) { set({ highlightWinner: v }); } }) |
| 134 |
), |
| 135 |
a.showBars && el('div', { style: { marginTop: 8 } }, |
| 136 |
el(RangeControl, { label: 'Bar Height (px)', value: a.barHeight, min: 4, max: 24, __nextHasNoMarginBottom: true, onChange: function (v) { set({ barHeight: v }); } }) |
| 137 |
) |
| 138 |
), |
| 139 |
// Questions |
| 140 |
el(PanelBody, { title: 'Questions & Data', initialOpen: false }, |
| 141 |
a.questions.map(function (q, qi) { |
| 142 |
return el('div', { key: qi, style: { marginBottom: 16, padding: '10px 12px', border: '1px solid #e5e7eb', borderRadius: 8, background: '#f8fafc' } }, |
| 143 |
el('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 } }, |
| 144 |
el('strong', { style: { fontSize: 11, color: '#374151' } }, 'Question ' + (qi + 1)), |
| 145 |
el(Button, { isDestructive: true, variant: 'link', style: { fontSize: 11 }, onClick: function () { set({ questions: a.questions.filter(function (_, j) { return j !== qi; }) }); } }, '✕') |
| 146 |
), |
| 147 |
el(TextareaControl, { label: 'Question Text', value: q.question, rows: 2, __nextHasNoMarginBottom: true, onChange: function (v) { set({ questions: upd(a.questions, qi, 'question', v) }); } }), |
| 148 |
el('div', { style: { marginTop: 8 } }, |
| 149 |
el('label', { style: { display: 'block', fontSize: 11, fontWeight: 600, color: '#374151', marginBottom: 4 } }, 'Options'), |
| 150 |
q.options.map(function (opt, oi) { |
| 151 |
return el('div', { key: oi, style: { display: 'grid', gridTemplateColumns: '2fr 60px 80px auto', gap: 4, alignItems: 'center', marginBottom: 4 } }, |
| 152 |
el(TextControl, { label: '', value: opt.label, placeholder: 'Option label', __nextHasNoMarginBottom: true, onChange: function (v) { set({ questions: updOpt(a.questions, qi, oi, 'label', v) }); } }), |
| 153 |
el(TextControl, { label: '', value: String(opt.percent), placeholder: '%', __nextHasNoMarginBottom: true, type: 'number', onChange: function (v) { set({ questions: updOpt(a.questions, qi, oi, 'percent', parseInt(v) || 0) }); } }), |
| 154 |
el(TextControl, { label: '', value: String(opt.count), placeholder: 'Count', __nextHasNoMarginBottom: true, type: 'number', onChange: function (v) { set({ questions: updOpt(a.questions, qi, oi, 'count', parseInt(v) || 0) }); } }), |
| 155 |
el(Button, { isDestructive: true, variant: 'link', style: { fontSize: 11 }, onClick: function () { |
| 156 |
var newOpts = q.options.filter(function (_, j) { return j !== oi; }); |
| 157 |
set({ questions: upd(a.questions, qi, 'options', newOpts) }); |
| 158 |
} }, '✕') |
| 159 |
); |
| 160 |
}), |
| 161 |
el(Button, { variant: 'secondary', style: { width: '100%', justifyContent: 'center', marginTop: 4 }, onClick: function () { |
| 162 |
var newOpts = q.options.concat([{ label: 'Option', percent: 20, count: 0 }]); |
| 163 |
set({ questions: upd(a.questions, qi, 'options', newOpts) }); |
| 164 |
} }, '+ Add Option') |
| 165 |
) |
| 166 |
); |
| 167 |
}), |
| 168 |
el(Button, { variant: 'primary', style: { width: '100%', justifyContent: 'center', marginTop: 4 }, onClick: function () { |
| 169 |
set({ questions: a.questions.concat([{ question: 'New Question?', options: [{ label: 'Option A', percent: 60, count: 0 }, { label: 'Option B', percent: 40, count: 0 }] }]) }); |
| 170 |
} }, '+ Add Question') |
| 171 |
), |
| 172 |
// Methodology |
| 173 |
el(PanelBody, { title: 'Methodology', initialOpen: false }, |
| 174 |
el(ToggleControl, { label: 'Show Methodology', checked: a.showMethodology, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showMethodology: v }); } }), |
| 175 |
el('div', { style: { marginTop: 8 } }, |
| 176 |
el(TextareaControl, { label: 'Methodology Text', value: a.methodology, rows: 4, __nextHasNoMarginBottom: true, onChange: function (v) { set({ methodology: v }); } }) |
| 177 |
) |
| 178 |
), |
| 179 |
// Typography |
| 180 |
el(PanelBody, { title: 'Typography', initialOpen: false }, |
| 181 |
getTypoControl()({ label: 'Title', value: a.titleTypo, onChange: function (v) { set({ titleTypo: v }); } }), |
| 182 |
getTypoControl()({ label: 'Text', value: a.textTypo, onChange: function (v) { set({ textTypo: v }); } }) |
| 183 |
), |
| 184 |
el(PanelBody, { title: 'Spacing', initialOpen: false }, |
| 185 |
el(RangeControl, { label: 'Border Radius (px)', value: a.borderRadius, min: 0, max: 24, __nextHasNoMarginBottom: true, onChange: function (v) { set({ borderRadius: v }); } }) |
| 186 |
), |
| 187 |
// Colors |
| 188 |
el(PanelColorSettings, { |
| 189 |
title: 'Header & Layout Colors', |
| 190 |
initialOpen: false, |
| 191 |
colorSettings: [ |
| 192 |
{ label: 'Block Background', value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#ffffff' }); } }, |
| 193 |
{ label: 'Border', value: a.borderColor, onChange: function (v) { set({ borderColor: v || '#e5e7eb' }); } }, |
| 194 |
{ label: 'Header BG', value: a.headerBg, onChange: function (v) { set({ headerBg: v || '#0f172a' }); } }, |
| 195 |
{ label: 'Header Text', value: a.headerColor, onChange: function (v) { set({ headerColor: v || '#ffffff' }); } }, |
| 196 |
{ label: 'Meta Text', value: a.metaColor, onChange: function (v) { set({ metaColor: v || '#94a3b8' }); } }, |
| 197 |
{ label: 'Methodology BG', value: a.methodologyBg,onChange: function (v) { set({ methodologyBg: v || '#f8fafc' }); } }, |
| 198 |
{ label: 'Methodology Text', value: a.methodologyColor, onChange: function (v) { set({ methodologyColor: v || '#6b7280' }); } } |
| 199 |
] |
| 200 |
}), |
| 201 |
el(PanelColorSettings, { |
| 202 |
title: 'Chart & Option Colors', |
| 203 |
initialOpen: false, |
| 204 |
colorSettings: [ |
| 205 |
{ label: 'Question Text', value: a.questionColor, onChange: function (v) { set({ questionColor: v || '#111827' }); } }, |
| 206 |
{ label: 'Label Text', value: a.labelColor, onChange: function (v) { set({ labelColor: v || '#374151' }); } }, |
| 207 |
{ label: 'Percent Text', value: a.percentColor, onChange: function (v) { set({ percentColor: v || '#111827' }); } }, |
| 208 |
{ label: 'Count Text', value: a.countColor, onChange: function (v) { set({ countColor: v || '#6b7280' }); } }, |
| 209 |
{ label: 'Bar BG', value: a.barBg, onChange: function (v) { set({ barBg: v || '#f1f5f9' }); } }, |
| 210 |
{ label: 'Bar Fill', value: a.barFill, onChange: function (v) { set({ barFill: v || '#3b82f6' }); } }, |
| 211 |
{ label: 'Bar Fill (Winner)',value: a.barFillWinner, onChange: function (v) { set({ barFillWinner: v || '#1d4ed8' }); } }, |
| 212 |
{ label: 'Winner Row BG', value: a.winnerBg, onChange: function (v) { set({ winnerBg: v || '#eff6ff' }); } } |
| 213 |
] |
| 214 |
}) |
| 215 |
) |
| 216 |
); |
| 217 |
}, |
| 218 |
|
| 219 |
save: function (props) { |
| 220 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 221 |
return wp.element.createElement('div', useBlockProps.save(), |
| 222 |
wp.element.createElement('div', { |
| 223 |
className: 'bkbg-survey-results-app', |
| 224 |
'data-opts': JSON.stringify(props.attributes) |
| 225 |
}) |
| 226 |
); |
| 227 |
} |
| 228 |
}); |
| 229 |
}() ); |
| 230 |
|