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 / retirement-calculator / index.js

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

283 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 SelectControl = wp.components.SelectControl;
13 var TextControl = wp.components.TextControl;
14 var ToggleControl = wp.components.ToggleControl;
15 var Button = wp.components.Button;
16
17 var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
18 var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
19
20 function calcRetirement(curAge, retAge, curSavings, monthlyContrib, annualReturn, inflationRate) {
21 var years = Math.max(0, retAge - curAge);
22 var monthlyRate = annualReturn / 100 / 12;
23 var months = years * 12;
24 var futureExisting = curSavings * Math.pow(1 + monthlyRate, months);
25 var futureContribs = monthlyRate > 0
26 ? monthlyContrib * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate
27 : monthlyContrib * months;
28 var total = futureExisting + futureContribs;
29 var totalContribs = curSavings + monthlyContrib * months;
30 var growth = total - totalContribs;
31 var inflFactor = Math.pow(1 + inflationRate / 100, years);
32 var inflAdjusted = total / inflFactor;
33 return { years: years, total: total, totalContribs: totalContribs, growth: growth, inflAdjusted: inflAdjusted };
34 }
35
36 function fmtM(val, cur, pos) {
37 var n = parseFloat(val);
38 var s;
39 if (n >= 1e6) { s = (n / 1e6).toFixed(2) + 'M'; }
40 else if (n >= 1e3) { s = (n / 1e3).toFixed(1) + 'K'; }
41 else { s = n.toFixed(0); }
42 return pos === 'after' ? s + cur : cur + s;
43 }
44 function fmtFull(val, cur, pos) {
45 var s = Number(parseFloat(val).toFixed(0)).toLocaleString();
46 return pos === 'after' ? s + cur : cur + s;
47 }
48
49 function InputRow(props) {
50 var label = props.label;
51 var value = props.value;
52 var suffix = props.suffix;
53 var min = props.min || 0;
54 var max = props.max || 999999;
55 var step = props.step || 1;
56 var onChange = props.onChange;
57 var radius = props.radius;
58 var labelC = props.labelColor;
59 return el('div', { style: { marginBottom: '14px' } },
60 el('label', { style: { display: 'flex', justifyContent: 'space-between', fontSize: '13px', fontWeight: 600, color: labelC, marginBottom: '5px' } },
61 el('span', null, label),
62 el('span', { style: { fontWeight: 400, color: '#9ca3af' } }, suffix || '')
63 ),
64 el('input', {
65 type: 'number', value: value, min: min, max: max, step: step,
66 style: { width: '100%', padding: '10px 14px', borderRadius: radius, border: '1.5px solid #e5e7eb', fontSize: '16px', outline: 'none', boxSizing: 'border-box' },
67 onChange: function(e) { onChange(parseFloat(e.target.value) || 0); }
68 })
69 );
70 }
71
72 function RetirementPreview(props) {
73 var a = props.attributes;
74 var sa = props.setAttributes;
75
76 var _curAge = useState(a.currentAge); var curAge = _curAge[0]; var setCurAge = _curAge[1];
77 var _retAge = useState(a.retirementAge); var retAge = _retAge[0]; var setRetAge = _retAge[1];
78 var _curSav = useState(a.currentSavings);var curSav = _curSav[0]; var setCurSav = _curSav[1];
79 var _contrib = useState(a.monthlyContrib);var contrib = _contrib[0]; var setContrib = _contrib[1];
80 var _ret = useState(a.annualReturn); var ret = _ret[0]; var setRet = _ret[1];
81 var _infl = useState(a.inflationRate); var infl = _infl[0]; var setInfl = _infl[1];
82
83 var r = calcRetirement(curAge, retAge, curSav, contrib, ret, infl);
84
85 var contribFrac = r.total > 0 ? Math.min(1, r.totalContribs / r.total) : 0;
86 var growthFrac = r.total > 0 ? Math.min(1, r.growth / r.total) : 0;
87
88 var containerStyle = {
89 background: a.sectionBg || undefined,
90 paddingTop: a.paddingTop + 'px',
91 paddingBottom: a.paddingBottom + 'px',
92 fontFamily: 'inherit'
93 };
94 var cardStyle = {
95 background: a.cardBg,
96 borderRadius: a.cardRadius + 'px',
97 padding: '36px 32px',
98 maxWidth: a.maxWidth + 'px',
99 margin: '0 auto',
100 boxShadow: '0 4px 24px rgba(0,0,0,0.09)'
101 };
102 var inpRadius = a.inputRadius + 'px';
103
104 return el('div', { className: 'bkbg-ret-editor', style: containerStyle },
105 el('div', { style: cardStyle },
106
107 // Title / subtitle
108 (a.showTitle || a.showSubtitle) && el('div', { style: { marginBottom: '20px' } },
109 a.showTitle && el('div', {
110 className: 'bkbg-ret-title',
111 style: { color: a.titleColor, marginBottom: '6px', contentEditable: true, suppressContentEditableWarning: true, outline: 'none' },
112 onBlur: function(e) { sa({ title: e.target.innerText }); },
113 dangerouslySetInnerHTML: { __html: a.title }
114 }),
115 a.showSubtitle && el('div', {
116 className: 'bkbg-ret-subtitle',
117 style: { color: a.subtitleColor, contentEditable: true, suppressContentEditableWarning: true, outline: 'none' },
118 onBlur: function(e) { sa({ subtitle: e.target.innerText }); },
119 dangerouslySetInnerHTML: { __html: a.subtitle }
120 })
121 ),
122
123 // Input grid
124 el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '0 20px', marginBottom: '24px' } },
125 el(InputRow, { label: 'Current Age', value: curAge, min: 18, max: 80, step: 1, onChange: setCurAge, radius: inpRadius, labelColor: a.labelColor }),
126 el(InputRow, { label: 'Retirement Age', value: retAge, min: 40, max: 90, step: 1, onChange: setRetAge, radius: inpRadius, labelColor: a.labelColor }),
127 el(InputRow, { label: 'Current Savings', value: curSav, min: 0, max: 10000000, step: 1000, suffix: a.currency, onChange: setCurSav, radius: inpRadius, labelColor: a.labelColor }),
128 el(InputRow, { label: 'Monthly Contribution', value: contrib, min: 0, max: 100000, step: 50, suffix: a.currency + '/mo', onChange: setContrib, radius: inpRadius, labelColor: a.labelColor }),
129 el(InputRow, { label: 'Annual Return', value: ret, min: 0, max: 20, step: 0.1, suffix: '%', onChange: setRet, radius: inpRadius, labelColor: a.labelColor }),
130 a.showInflation && el(InputRow, { label: 'Inflation Rate', value: infl, min: 0, max: 15, step: 0.1, suffix: '%', onChange: setInfl, radius: inpRadius, labelColor: a.labelColor })
131 ),
132
133 // Result hero
134 el('div', {
135 style: {
136 background: a.resultBg,
137 border: '2px solid ' + a.resultBorder,
138 borderRadius: a.cardRadius + 'px',
139 padding: '28px 28px 22px',
140 marginBottom: '16px',
141 textAlign: 'center'
142 }
143 },
144 el('div', { style: { fontSize: '13px', fontWeight: 600, color: a.labelColor, marginBottom: '6px' } },
145 'Projected savings at age ' + retAge + ' (' + r.years + ' years)'
146 ),
147 el('div', { style: { fontSize: a.totalSize + 'px', fontWeight: 800, color: a.totalColor, lineHeight: 1.1, marginBottom: '6px' } },
148 fmtM(r.total, a.currency, a.currencyPos)
149 ),
150 r.total !== 0 && el('div', { style: { fontSize: '13px', color: a.labelColor } },
151 '(' + fmtFull(r.total, a.currency, a.currencyPos) + ' total)'
152 )
153 ),
154
155 // Inflation-adjusted & breakdown
156 el('div', { style: { display: 'grid', gridTemplateColumns: a.showInflation ? '1fr 1fr' : '1fr', gap: '12px', marginBottom: a.showBreakdown ? '16px' : '0' } },
157 a.showInflation && el('div', { style: { background: a.projBgB, borderRadius: a.cardRadius + 'px', padding: '16px 18px' } },
158 el('div', { style: { fontSize: '11px', fontWeight: 700, color: a.inflationColor, textTransform: 'uppercase', letterSpacing: '0.05em', marginBottom: '4px' } }, 'Inflation-Adjusted'),
159 el('div', { style: { fontSize: '24px', fontWeight: 800, color: a.inflationColor } }, fmtM(r.inflAdjusted, a.currency, a.currencyPos)),
160 el('div', { style: { fontSize: '11px', color: a.labelColor, marginTop: '2px' } }, 'In today\'s dollars')
161 ),
162 el('div', { style: { background: a.projBgA, borderRadius: a.cardRadius + 'px', padding: '16px 18px' } },
163 el('div', { style: { fontSize: '11px', fontWeight: 700, color: a.contribColor, textTransform: 'uppercase', letterSpacing: '0.05em', marginBottom: '4px' } }, 'Total Contributions'),
164 el('div', { style: { fontSize: '24px', fontWeight: 800, color: a.contribColor } }, fmtM(r.totalContribs, a.currency, a.currencyPos)),
165 el('div', { style: { fontSize: '11px', color: a.labelColor, marginTop: '2px' } }, 'What you put in')
166 )
167 ),
168
169 // Breakdown bar
170 a.showBreakdown && el('div', null,
171 el('div', { style: { display: 'flex', justifyContent: 'space-between', fontSize: '12px', color: a.labelColor, marginBottom: '6px' } },
172 el('span', null, 'Contributions'),
173 el('span', null, 'Investment Growth')
174 ),
175 el('div', { style: { height: '14px', borderRadius: '100px', background: a.barTrackBg, overflow: 'hidden', display: 'flex' } },
176 el('div', { style: { width: (contribFrac * 100).toFixed(1) + '%', background: a.contribColor, borderRadius: '100px 0 0 100px', transition: 'width .4s' } }),
177 el('div', { style: { width: (growthFrac * 100).toFixed(1) + '%', background: a.growthColor, borderRadius: '0 100px 100px 0', transition: 'width .4s' } })
178 ),
179 el('div', { style: { display: 'flex', gap: '16px', marginTop: '8px', fontSize: '12px' } },
180 el('span', null,
181 el('span', { style: { display: 'inline-block', width: '10px', height: '10px', borderRadius: '2px', background: a.contribColor, marginRight: '5px', verticalAlign: 'middle' } }),
182 'Contributions: ' + fmtM(r.totalContribs, a.currency, a.currencyPos)
183 ),
184 el('span', null,
185 el('span', { style: { display: 'inline-block', width: '10px', height: '10px', borderRadius: '2px', background: a.growthColor, marginRight: '5px', verticalAlign: 'middle' } }),
186 'Growth: ' + fmtM(r.growth, a.currency, a.currencyPos)
187 )
188 )
189 )
190 )
191 );
192 }
193
194 registerBlockType('blockenberg/retirement-calculator', {
195 edit: function(props) {
196 var a = props.attributes;
197 var set = props.setAttributes;
198 var TC = getTypoControl();
199 var blockProps = useBlockProps((function(){
200 var _tv = getTypoCssVars();
201 var s = {};
202 Object.assign(s, _tv(a.titleTypo, '--bkrc-tt-'));
203 Object.assign(s, _tv(a.bodyTypo, '--bkrc-bt-'));
204 return { style: s };
205 })());
206
207 var colorSettings = [
208 { value: a.accentColor, onChange: function(v) { set({ accentColor: v }); }, label: 'Accent Color' },
209 { value: a.cardBg, onChange: function(v) { set({ cardBg: v }); }, label: 'Card Background' },
210 { value: a.resultBg, onChange: function(v) { set({ resultBg: v }); }, label: 'Result Background' },
211 { value: a.resultBorder, onChange: function(v) { set({ resultBorder: v }); }, label: 'Result Border' },
212 { value: a.totalColor, onChange: function(v) { set({ totalColor: v }); }, label: 'Total Amount Color' },
213 { value: a.inflationColor, onChange: function(v) { set({ inflationColor: v }); }, label: 'Inflation-Adjusted Color' },
214 { value: a.contribColor, onChange: function(v) { set({ contribColor: v }); }, label: 'Contributions Color' },
215 { value: a.growthColor, onChange: function(v) { set({ growthColor: v }); }, label: 'Growth Color' },
216 { value: a.barTrackBg, onChange: function(v) { set({ barTrackBg: v }); }, label: 'Bar Track Background' },
217 { value: a.projBgA, onChange: function(v) { set({ projBgA: v }); }, label: 'Contributions Card BG' },
218 { value: a.projBgB, onChange: function(v) { set({ projBgB: v }); }, label: 'Inflation Card BG' },
219 { value: a.titleColor, onChange: function(v) { set({ titleColor: v }); }, label: 'Title Color' },
220 { value: a.subtitleColor, onChange: function(v) { set({ subtitleColor: v }); }, label: 'Subtitle Color' },
221 { value: a.labelColor, onChange: function(v) { set({ labelColor: v }); }, label: 'Label Color' },
222 { value: a.sectionBg, onChange: function(v) { set({ sectionBg: v }); }, label: 'Section Background' }
223 ];
224
225 return el(Fragment, null,
226 el(InspectorControls, null,
227
228 el(PanelBody, { title: 'Header', initialOpen: false },
229 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Title', checked: a.showTitle, onChange: function(v) { set({ showTitle: v }); } }),
230 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Subtitle', checked: a.showSubtitle, onChange: function(v) { set({ showSubtitle: v }); } }),
231 el(TextControl, { label: 'Title', value: a.title, onChange: function(v) { set({ title: v }); } }),
232 el(TextControl, { label: 'Subtitle', value: a.subtitle, onChange: function(v) { set({ subtitle: v }); } })
233 ),
234
235 el(PanelBody, { title: 'Calculator Defaults', initialOpen: true },
236 el(TextControl, { label: 'Currency Symbol', value: a.currency, onChange: function(v) { set({ currency: v }); } }),
237 el(SelectControl, {
238 label: 'Currency Position',
239 value: a.currencyPos,
240 options: [{ label: 'Before ($100)', value: 'before' }, { label: 'After (100$)', value: 'after' }],
241 onChange: function(v) { set({ currencyPos: v }); }
242 }),
243 el(RangeControl, { label: 'Default Current Age', value: a.currentAge, min: 18, max: 80, step: 1, onChange: function(v) { set({ currentAge: v }); } }),
244 el(RangeControl, { label: 'Default Retirement Age', value: a.retirementAge, min: 40, max: 90, step: 1, onChange: function(v) { set({ retirementAge: v }); } }),
245 el(TextControl, { label: 'Default Current Savings', type: 'number', value: String(a.currentSavings), onChange: function(v) { set({ currentSavings: parseFloat(v) || 0 }); } }),
246 el(TextControl, { label: 'Default Monthly Contribution', type: 'number', value: String(a.monthlyContrib), onChange: function(v) { set({ monthlyContrib: parseFloat(v) || 0 }); } }),
247 el(TextControl, { label: 'Default Annual Return (%)', type: 'number', value: String(a.annualReturn), onChange: function(v) { set({ annualReturn: parseFloat(v) || 0 }); } }),
248 el(TextControl, { label: 'Default Inflation Rate (%)', type: 'number', value: String(a.inflationRate), onChange: function(v) { set({ inflationRate: parseFloat(v) || 0 }); } }),
249 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Inflation-Adjusted Result', checked: a.showInflation, onChange: function(v) { set({ showInflation: v }); } }),
250 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Contributions vs Growth Bar', checked: a.showBreakdown, onChange: function(v) { set({ showBreakdown: v }); } })
251 ),
252
253
254 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
255 TC && el(TC, { label: 'Title', value: a.titleTypo, onChange: function(v){ set({titleTypo:v}); } }),
256 TC && el(TC, { label: 'Body / Labels', value: a.bodyTypo, onChange: function(v){ set({bodyTypo:v}); } })
257 ),
258 el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }),
259
260 el(PanelBody, { title: 'Sizing & Layout', initialOpen: false },
261 el(RangeControl, { label: 'Total Amount Size', value: a.totalSize, min: 28, max: 96, step: 2, onChange: function(v) { set({ totalSize: v }); } }),
262 el(RangeControl, { label: 'Card Border Radius', value: a.cardRadius, min: 0, max: 40, step: 1, onChange: function(v) { set({ cardRadius: v }); } }),
263 el(RangeControl, { label: 'Input Border Radius', value: a.inputRadius, min: 0, max: 24, step: 1, onChange: function(v) { set({ inputRadius: v }); } }),
264 el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 300, max: 1000, step: 10, onChange: function(v) { set({ maxWidth: v }); } }),
265 el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingTop: v }); } }),
266 el(RangeControl, { label: 'Padding Bottom (px)', value: a.paddingBottom, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingBottom: v }); } })
267 )
268 ),
269 el('div', blockProps,
270 el(RetirementPreview, { attributes: a, setAttributes: set })
271 )
272 );
273 },
274 save: function(props) {
275 var a = props.attributes;
276 var blockProps = wp.blockEditor.useBlockProps.save();
277 return el('div', blockProps,
278 el('div', { className: 'bkbg-ret-app', 'data-opts': JSON.stringify(a) })
279 );
280 }
281 });
282 }() );
283