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 / loan-affordability / index.js

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

260 lines 16.5 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, _tv;
18 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
19 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
20
21 /* ── Affordability math ─────────────────────────── */
22 function calcAffordability(income, monthlyDebts, rate, years, downPct, feRatio, beRatio, incomeShare, paymentMode) {
23 var monthlyIncome = income / 12;
24 var maxFront = monthlyIncome * (feRatio / 100);
25 var maxBack = monthlyIncome * (beRatio / 100) - monthlyDebts;
26 var maxDti = Math.max(0, Math.min(maxFront, maxBack));
27 var maxShare = Math.max(0, monthlyIncome * ((incomeShare || 30) / 100));
28 var mode = paymentMode || 'min';
29 var maxPmt;
30
31 if (mode === 'incomeShare') {
32 maxPmt = maxShare;
33 } else if (mode === 'dti') {
34 maxPmt = maxDti;
35 } else {
36 maxPmt = Math.min(maxDti, maxShare);
37 }
38
39 /* Reverse mortgage: P = pmt × ((1-(1+r)^-n)/r) */
40 var r = rate / 100 / 12;
41 var n = years * 12;
42 var maxLoan;
43 if (r === 0) {
44 maxLoan = maxPmt * n;
45 } else {
46 maxLoan = maxPmt * ((1 - Math.pow(1 + r, -n)) / r);
47 }
48 var downAmount = maxLoan > 0 ? maxLoan / (1 - downPct / 100) * (downPct / 100) : 0;
49 var maxHome = maxLoan + downAmount;
50 var dti = monthlyIncome > 0 ? ((maxPmt + monthlyDebts) / monthlyIncome) * 100 : 0;
51
52 return {
53 maxHome: Math.max(0, maxHome),
54 maxLoan: Math.max(0, maxLoan),
55 maxMonthly: maxPmt,
56 downAmount: Math.max(0, downAmount),
57 dti: dti
58 };
59 }
60
61 function fmtMoney(val, cur, pos) {
62 var s = Math.round(val).toLocaleString('en-US');
63 return pos === 'after' ? s + cur : cur + s;
64 }
65
66 /* ── Editor Preview ──────────────────────────────── */
67 function AffordPreview(props) {
68 var a = props.attrs;
69 var res = calcAffordability(
70 a.defaultIncome,
71 a.defaultDebts,
72 a.defaultRate,
73 a.defaultTerm,
74 a.defaultDown,
75 a.frontEndRatio,
76 a.backEndRatio,
77 a.incomeShareLimit,
78 a.monthlyPaymentMode
79 );
80
81 var wrapStyle = { paddingTop: a.paddingTop + 'px', paddingBottom: a.paddingBottom + 'px', background: a.sectionBg || undefined };
82 var cardStyle = { background: a.cardBg, borderRadius: a.cardRadius + 'px', padding: '32px', maxWidth: a.maxWidth + 'px', margin: '0 auto', boxShadow: '0 4px 24px rgba(0,0,0,0.06)' };
83
84 function inputRow(label, val, pfx, sfx) {
85 return el('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '10px 0', borderBottom: '1px solid #f3f4f6' } },
86 el('label', { style: { color: a.labelColor, fontSize: 14 } }, label),
87 el('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
88 pfx ? el('span', { style: { color: a.subtitleColor, fontSize: 13 } }, pfx) : null,
89 el('input', { readOnly: true, value: val, type: 'number', style: { width: 100, textAlign: 'right', border: '1px solid #d1d5db', borderRadius: a.inputRadius + 'px', padding: '4px 8px', fontSize: 14 } }),
90 sfx ? el('span', { style: { color: a.subtitleColor, fontSize: 13 } }, sfx) : null
91 )
92 );
93 }
94
95 function statCard(label, val, color) {
96 return el('div', { style: { background: a.statCardBg, borderRadius: 12, padding: '20px 16px', textAlign: 'center', flex: 1, minWidth: 120 } },
97 el('div', { style: { fontWeight: 700, color: color || a.labelColor, lineHeight: 1.2 } }, val),
98 el('div', { style: { fontSize: 12, color: a.subtitleColor, marginTop: 6 } }, label)
99 );
100 }
101
102 return el('div', { style: wrapStyle },
103 (a.showTitle || a.showSubtitle) ? el('div', { style: { textAlign: 'center', maxWidth: a.maxWidth + 'px', margin: '0 auto 32px' } },
104 a.showTitle ? el('h3', { className: 'bkbg-la-title', style: { color: a.titleColor } }, a.title) : null,
105 a.showSubtitle ? el('p', { className: 'bkbg-la-subtitle', style: { color: a.subtitleColor } }, a.subtitle) : null
106 ) : null,
107 el('div', { style: cardStyle },
108
109 /* Max home hero */
110 el('div', { style: { background: a.resultBg, border: '2px solid ' + a.resultBorder, borderRadius: a.cardRadius + 'px', padding: '28px 32px', textAlign: 'center', marginBottom: 24 } },
111 el('div', { style: { fontSize: 12, color: a.subtitleColor, fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.06em' } }, 'Maximum Home Price'),
112 el('div', { className: 'bkbg-la-result-hero', style: { color: a.maxHomeColor } }, fmtMoney(res.maxHome, a.currency, a.currencyPos)),
113 el('div', { style: { fontSize: 13, color: a.subtitleColor } }, 'with ' + a.defaultDown + '% down · ' + a.defaultRate + '% rate · ' + a.defaultTerm + ' yr term')
114 ),
115
116 /* Inputs */
117 inputRow('Annual Gross Income', a.defaultIncome, a.currency),
118 inputRow('Monthly Existing Debts', a.defaultDebts, a.currency),
119 inputRow('Interest Rate (%)', a.defaultRate, null, '%'),
120 inputRow('Loan Term', a.defaultTerm, null, 'years'),
121 inputRow('Down Payment (%)', a.defaultDown, null, '%'),
122
123 /* Stat cards */
124 el('div', { style: { display: 'flex', gap: 12, marginTop: 24, flexWrap: 'wrap' } },
125 a.showMaxLoan ? statCard('Max Loan', fmtMoney(res.maxLoan, a.currency, a.currencyPos), a.maxLoanColor) : null,
126 a.showMonthly ? statCard('Max Monthly', fmtMoney(res.maxMonthly, a.currency, a.currencyPos), a.monthlyColor) : null,
127 a.showDTI ? statCard('DTI Ratio', res.dti.toFixed(1) + '%', a.dtiColor) : null,
128 a.showDownPayment ? statCard('Down Payment', fmtMoney(res.downAmount, a.currency, a.currencyPos), a.labelColor) : null
129 )
130 )
131 );
132 }
133
134 /* ── Edit ────────────────────────────────────────── */
135 function AffordEdit(props) {
136 var a = props.attributes;
137 var set = props.setAttributes;
138 var blockProps = useBlockProps((function () {
139 var _tvFn = getTypoCssVars();
140 var s = {};
141 if (_tvFn) {
142 Object.assign(s, _tvFn(a.titleTypo, '--bkbg-la-tt-'));
143 Object.assign(s, _tvFn(a.resultTypo, '--bkbg-la-rt-'));
144 }
145 return { className: 'bkbg-la-editor', style: s };
146 })());
147
148 function s(key) { return function (v) { var o = {}; o[key] = v; set(o); }; }
149 function n(key) { return function (v) { var o = {}; o[key] = Number(v) || 0; set(o); }; }
150 function nf(key){ return function (v) { var o = {}; o[key] = parseFloat(v) || 0; set(o); }; }
151 function t(key) { return function (v) { var o = {}; o[key] = v; set(o); }; }
152
153 return el(Fragment, null,
154 el(InspectorControls, null,
155
156 /* Header */
157 el(PanelBody, { title: __('Header', 'blockenberg'), initialOpen: true },
158 el(ToggleControl, { label: __('Show Title', 'blockenberg'), checked: a.showTitle, onChange: t('showTitle'), __nextHasNoMarginBottom: true }),
159 a.showTitle ? el(TextControl, { label: __('Title', 'blockenberg'), value: a.title, onChange: s('title') }) : null,
160 el(ToggleControl, { label: __('Show Subtitle', 'blockenberg'), checked: a.showSubtitle, onChange: t('showSubtitle'), __nextHasNoMarginBottom: true }),
161 a.showSubtitle ? el(TextControl, { label: __('Subtitle', 'blockenberg'), value: a.subtitle, onChange: s('subtitle') }) : null
162 ),
163
164 /* Defaults */
165 el(PanelBody, { title: __('Calculator Defaults', 'blockenberg'), initialOpen: true },
166 el(TextControl, { label: __('Currency Symbol', 'blockenberg'), value: a.currency, onChange: s('currency') }),
167 el(SelectControl, { label: __('Currency Position', 'blockenberg'), value: a.currencyPos, options: [{ label: 'Before ($100)', value: 'before' }, { label: 'After (100$)', value: 'after' }], onChange: s('currencyPos') }),
168 el(TextControl, { label: __('Default Annual Income', 'blockenberg'), value: String(a.defaultIncome), onChange: n('defaultIncome'), type: 'number' }),
169 el(TextControl, { label: __('Default Monthly Debts', 'blockenberg'), value: String(a.defaultDebts), onChange: n('defaultDebts'), type: 'number', help: __('Existing monthly debt payments (car, student loan, etc.)', 'blockenberg') }),
170 el(TextControl, { label: __('Default Interest Rate (%)', 'blockenberg'), value: String(a.defaultRate), onChange: nf('defaultRate'), type: 'number', step: '0.1' }),
171 el(TextControl, { label: __('Default Loan Term (years)', 'blockenberg'), value: String(a.defaultTerm), onChange: n('defaultTerm'), type: 'number' }),
172 el(TextControl, { label: __('Default Down Payment (%)', 'blockenberg'), value: String(a.defaultDown), onChange: n('defaultDown'), type: 'number' })
173 ),
174
175 /* DTI Ratios */
176 el(PanelBody, { title: __('DTI Ratio Limits', 'blockenberg'), initialOpen: false },
177 el(RangeControl, { label: __('Front-End Ratio (%)', 'blockenberg'), value: a.frontEndRatio, onChange: n('frontEndRatio'), min: 20, max: 45, help: __('Max % of income for housing costs (default 28%)', 'blockenberg') }),
178 el(RangeControl, { label: __('Back-End Ratio (%)', 'blockenberg'), value: a.backEndRatio, onChange: n('backEndRatio'), min: 30, max: 55, help: __('Max % of income for all debts (default 43%)', 'blockenberg') }),
179 el(SelectControl, {
180 label: __('Max Monthly Payment Rule', 'blockenberg'),
181 value: a.monthlyPaymentMode,
182 options: [
183 { label: __('Conservative: min(DTI limit, income share)', 'blockenberg'), value: 'min' },
184 { label: __('DTI ratios only', 'blockenberg'), value: 'dti' },
185 { label: __('Income share only', 'blockenberg'), value: 'incomeShare' }
186 ],
187 onChange: s('monthlyPaymentMode')
188 }),
189 el(RangeControl, {
190 label: __('Income Share Limit (%)', 'blockenberg'),
191 value: a.incomeShareLimit,
192 onChange: n('incomeShareLimit'),
193 min: 10,
194 max: 60,
195 help: __('Cap max monthly payment to this % of gross monthly income.', 'blockenberg')
196 })
197 ),
198
199 /* Display */
200 el(PanelBody, { title: __('Display Options', 'blockenberg'), initialOpen: false },
201 el(ToggleControl, { label: __('Show Max Loan Amount', 'blockenberg'), checked: a.showMaxLoan, onChange: t('showMaxLoan'), __nextHasNoMarginBottom: true }),
202 el(ToggleControl, { label: __('Show Max Monthly Payment', 'blockenberg'), checked: a.showMonthly, onChange: t('showMonthly'), __nextHasNoMarginBottom: true }),
203 el(ToggleControl, { label: __('Show DTI Ratio', 'blockenberg'), checked: a.showDTI, onChange: t('showDTI'), __nextHasNoMarginBottom: true }),
204 el(ToggleControl, { label: __('Show Down Payment Amount', 'blockenberg'), checked: a.showDownPayment, onChange: t('showDownPayment'), __nextHasNoMarginBottom: true })
205 ),
206
207 /* Colors */
208
209 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
210 getTypoControl() && el(getTypoControl(), { label: __('Title', 'blockenberg'), value: a.titleTypo || {}, onChange: function (v) { set({ titleTypo: v }); } }),
211 getTypoControl() && el(getTypoControl(), { label: __('Result', 'blockenberg'), value: a.resultTypo || {}, onChange: function (v) { set({ resultTypo: v }); } })
212 ),
213 el(PanelColorSettings, {
214 title: __('Colors', 'blockenberg'),
215 initialOpen: false,
216 colorSettings: [
217 { value: a.accentColor, onChange: s('accentColor'), label: __('Accent', 'blockenberg') },
218 { value: a.cardBg, onChange: s('cardBg'), label: __('Card Background', 'blockenberg') },
219 { value: a.resultBg, onChange: s('resultBg'), label: __('Result Box BG', 'blockenberg') },
220 { value: a.resultBorder, onChange: s('resultBorder'), label: __('Result Box Border', 'blockenberg') },
221 { value: a.maxHomeColor, onChange: s('maxHomeColor'), label: __('Max Home Price', 'blockenberg') },
222 { value: a.maxLoanColor, onChange: s('maxLoanColor'), label: __('Max Loan', 'blockenberg') },
223 { value: a.monthlyColor, onChange: s('monthlyColor'), label: __('Monthly Payment', 'blockenberg') },
224 { value: a.dtiColor, onChange: s('dtiColor'), label: __('DTI Ratio', 'blockenberg') },
225 { value: a.statCardBg, onChange: s('statCardBg'), label: __('Stat Card BG', 'blockenberg') },
226 { value: a.titleColor, onChange: s('titleColor'), label: __('Title Color', 'blockenberg') },
227 { value: a.subtitleColor, onChange: s('subtitleColor'), label: __('Subtitle Color', 'blockenberg') },
228 { value: a.labelColor, onChange: s('labelColor'), label: __('Label Color', 'blockenberg') },
229 { value: a.sectionBg, onChange: s('sectionBg'), label: __('Section Background', 'blockenberg') }
230 ]
231 }),
232
233 /* Sizing */
234 el(PanelBody, { title: __('Sizing', 'blockenberg'), initialOpen: false },
235 el(RangeControl, { label: __('Card Radius (px)', 'blockenberg'), value: a.cardRadius, onChange: n('cardRadius'), min: 0, max: 32 }),
236 el(RangeControl, { label: __('Input Radius (px)', 'blockenberg'), value: a.inputRadius, onChange: n('inputRadius'), min: 0, max: 20 }),
237 el(RangeControl, { label: __('Max Width (px)', 'blockenberg'), value: a.maxWidth, onChange: n('maxWidth'), min: 360, max: 1000, step: 20 }),
238 el(RangeControl, { label: __('Padding Top (px)', 'blockenberg'), value: a.paddingTop, onChange: n('paddingTop'), min: 0, max: 160 }),
239 el(RangeControl, { label: __('Padding Bottom (px)', 'blockenberg'), value: a.paddingBottom, onChange: n('paddingBottom'), min: 0, max: 160 })
240 )
241 ),
242
243 el('div', blockProps,
244 el(AffordPreview, { attrs: a })
245 )
246 );
247 }
248
249 registerBlockType('blockenberg/loan-affordability', {
250 edit: AffordEdit,
251 save: function (props) {
252 var a = props.attributes;
253 return el('div', wp.blockEditor.useBlockProps.save({
254 className: 'bkbg-la-app',
255 'data-opts': JSON.stringify(a)
256 }));
257 }
258 });
259 }() );
260