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

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

219 lines 14.4 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 _fuelTC, _fuelTV;
18 function _tc() { return _fuelTC || (_fuelTC = window.bkbgTypographyControl); }
19 function _tv() { return _fuelTV || (_fuelTV = window.bkbgTypoCssVars); }
20
21 /* ── Unit definitions ──────────────────────────── */
22 var UNITS = {
23 mpg: { distLabel: 'Distance (miles)', effLabel: 'Fuel Economy (MPG)', priceLabel: 'Fuel Price (per gallon)', volUnit: 'gal', distUnit: 'mi' },
24 lper100:{ distLabel: 'Distance (km)', effLabel: 'Fuel Economy (L/100km)', priceLabel: 'Fuel Price (per litre)', volUnit: 'L', distUnit: 'km' },
25 kmpl: { distLabel: 'Distance (km)', effLabel: 'Fuel Economy (km/L)', priceLabel: 'Fuel Price (per litre)', volUnit: 'L', distUnit: 'km' }
26 };
27
28 function calcFuel(dist, eff, price, unit) {
29 var litres = 0;
30 if (unit === 'mpg') {
31 /* 1 US gallon = 3.78541 L, 1 mile = 1.60934 km */
32 var gallons = eff > 0 ? dist / eff : 0;
33 litres = gallons * 3.78541;
34 return { cost: gallons * price, litres: litres, gallons: gallons };
35 } else if (unit === 'lper100') {
36 litres = eff > 0 ? (dist / 100) * eff : 0;
37 return { cost: litres * price, litres: litres, gallons: litres / 3.78541 };
38 } else { /* kmpl */
39 litres = eff > 0 ? dist / eff : 0;
40 return { cost: litres * price, litres: litres, gallons: litres / 3.78541 };
41 }
42 }
43
44 /* CO2: gasoline ≈ 2.31 kg/L, diesel ≈ 2.68 kg/L — use gasoline */
45 function co2Kg(litres) { return litres * 2.31; }
46
47 /* ── Preview ────────────────────────────────────── */
48 function FuelPreview(props) {
49 var a = props.attrs;
50 var u = UNITS[a.unit] || UNITS.mpg;
51 var res = calcFuel(a.defaultDist, a.defaultEff, a.defaultPrice, a.unit);
52 var totalCost = res.cost;
53 var perPerson = a.defaultPassengers > 0 ? totalCost / a.defaultPassengers : totalCost;
54 var co2 = co2Kg(res.litres);
55
56 var wrapStyle = {
57 paddingTop: a.paddingTop + 'px', paddingBottom: a.paddingBottom + 'px',
58 background: a.sectionBg || undefined
59 };
60 var cardStyle = {
61 background: a.cardBg, borderRadius: a.cardRadius + 'px',
62 padding: '32px', maxWidth: a.maxWidth + 'px', margin: '0 auto',
63 boxShadow: '0 4px 24px rgba(0,0,0,0.06)'
64 };
65
66 function inputRow(label, val, suffix) {
67 return el('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '10px 0', borderBottom: '1px solid #f3f4f6' } },
68 el('label', { style: { color: a.labelColor, fontSize: 14 } }, label),
69 el('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
70 el('input', { type: 'number', value: val, readOnly: true, style: { width: 80, textAlign: 'right', border: '1px solid #d1d5db', borderRadius: a.inputRadius + 'px', padding: '4px 8px', fontSize: 14 } }),
71 el('span', { style: { color: a.subtitleColor, fontSize: 13 } }, suffix)
72 )
73 );
74 }
75
76 function statCard(label, val, color) {
77 return el('div', { style: { background: a.statCardBg, borderRadius: 12, padding: '16px', textAlign: 'center', flex: 1 } },
78 el('div', { style: { fontSize: 20, fontWeight: 700, color: color || a.labelColor } }, val),
79 el('div', { style: { fontSize: 12, color: a.subtitleColor, marginTop: 4 } }, label)
80 );
81 }
82
83 return el('div', { style: wrapStyle },
84 (a.showTitle || a.showSubtitle) ? el('div', { style: { textAlign: 'center', marginBottom: 32, maxWidth: a.maxWidth + 'px', margin: '0 auto 32px' } },
85 a.showTitle ? el('h3', { className: 'bkbg-fuel-title', style: { color: a.titleColor, margin: '0 0 8px' } }, a.title) : null,
86 a.showSubtitle ? el('p', { className: 'bkbg-fuel-subtitle', style: { color: a.subtitleColor, margin: 0 } }, a.subtitle) : null
87 ) : null,
88 el('div', { style: cardStyle },
89
90 /* Unit toggle */
91 el('div', { style: { display: 'flex', gap: 8, marginBottom: 24 } },
92 ['mpg', 'lper100', 'kmpl'].map(function (k) {
93 return el('button', {
94 key: k,
95 style: { flex: 1, padding: '8px', border: 'none', borderRadius: a.inputRadius + 'px', fontWeight: 600, fontSize: 13, cursor: 'pointer', background: a.unit === k ? a.accentColor : a.statCardBg, color: a.unit === k ? '#fff' : a.labelColor }
96 }, k === 'lper100' ? 'L/100km' : k.toUpperCase());
97 })
98 ),
99
100 /* Inputs */
101 inputRow(u.distLabel, a.defaultDist, u.distUnit),
102 inputRow(u.effLabel, a.defaultEff, u.unit === 'mpg' ? 'mpg' : u.unit === 'lper100' ? 'L/100km' : 'km/L'),
103 inputRow(u.priceLabel, a.defaultPrice.toFixed(2), a.currency + '/' + u.volUnit),
104 a.showPerPerson ? inputRow('Passengers', a.defaultPassengers, 'people') : null,
105
106 /* Result */
107 el('div', { style: { background: a.resultBg, border: '2px solid ' + a.resultBorder, borderRadius: a.cardRadius + 'px', padding: '24px', textAlign: 'center', margin: '24px 0' } },
108 el('div', { style: { fontSize: 13, color: a.subtitleColor, marginBottom: 4 } }, 'Total Fuel Cost'),
109 el('div', { className: 'bkbg-fuel-result-value', style: { color: a.totalColor } }, a.currency + totalCost.toFixed(2)),
110 a.showRoundTrip ? el('div', { style: { fontSize: 12, color: a.subtitleColor, marginTop: 4 } }, 'one way · ' + a.currency + (totalCost * 2).toFixed(2) + ' round trip') : null
111 ),
112
113 /* Stat cards */
114 el('div', { style: { display: 'flex', gap: 12, flexWrap: 'wrap' } },
115 a.showLitres ? statCard(res.litres > 0 ? (res.litres.toFixed(1) + ' L / ' + res.gallons.toFixed(1) + ' gal') : '', 'Fuel Used', a.labelColor) : null,
116 a.showPerPerson ? statCard('Per Person', a.currency + perPerson.toFixed(2), a.accentColor) : null,
117 a.showCO2 ? statCard('CO₂ Emitted', co2.toFixed(1) + ' kg', a.co2Color) : null
118 )
119 )
120 );
121 }
122
123 /* ── Edit ────────────────────────────────────────── */
124 function FuelEdit(props) {
125 var a = props.attributes;
126 var set = props.setAttributes;
127 var blockProps = useBlockProps({ className: 'bkbg-fuel-editor', style: Object.assign({}, _tv()(a.typoTitle, '--bkbg-fuel-tt-'), _tv()(a.typoSubtitle, '--bkbg-fuel-st-'), _tv()(a.typoResult, '--bkbg-fuel-rs-')) });
128
129 function s(key) { return function (v) { var o = {}; o[key] = v; set(o); }; }
130 function n(key) { return function (v) { var o = {}; o[key] = Number(v) || 0; set(o); }; }
131 function nf(key) { return function (v) { var o = {}; o[key] = parseFloat(v) || 0; set(o); }; }
132 function t(key) { return function (v) { var o = {}; o[key] = v; set(o); }; }
133
134 return el(Fragment, null,
135 el(InspectorControls, null,
136
137 /* Header */
138 el(PanelBody, { title: __('Header', 'blockenberg'), initialOpen: true },
139 el(ToggleControl, { label: __('Show Title', 'blockenberg'), checked: a.showTitle, onChange: t('showTitle'), __nextHasNoMarginBottom: true }),
140 a.showTitle ? el(TextControl, { label: __('Title', 'blockenberg'), value: a.title, onChange: s('title') }) : null,
141 el(ToggleControl, { label: __('Show Subtitle', 'blockenberg'), checked: a.showSubtitle, onChange: t('showSubtitle'), __nextHasNoMarginBottom: true }),
142 a.showSubtitle ? el(TextControl, { label: __('Subtitle', 'blockenberg'), value: a.subtitle, onChange: s('subtitle') }) : null
143 ),
144
145 /* Calculator Defaults */
146 el(PanelBody, { title: __('Calculator Defaults', 'blockenberg'), initialOpen: true },
147 el(SelectControl, { label: __('Fuel Unit System', 'blockenberg'), value: a.unit, options: [
148 { label: 'MPG (US)', value: 'mpg' },
149 { label: 'L/100km', value: 'lper100' },
150 { label: 'km/L', value: 'kmpl' }
151 ], onChange: s('unit') }),
152 el(TextControl, { label: __('Currency Symbol', 'blockenberg'), value: a.currency, onChange: s('currency') }),
153 el(TextControl, { label: __('Default Distance', 'blockenberg'), value: String(a.defaultDist), onChange: n('defaultDist'), type: 'number' }),
154 el(TextControl, { label: __('Default Fuel Efficiency', 'blockenberg'), value: String(a.defaultEff), onChange: n('defaultEff'), type: 'number' }),
155 el(TextControl, { label: __('Default Fuel Price', 'blockenberg'), value: String(a.defaultPrice), onChange: nf('defaultPrice'), type: 'number', step: '0.01' }),
156 el(TextControl, { label: __('Default Passengers', 'blockenberg'), value: String(a.defaultPassengers), onChange: n('defaultPassengers'), type: 'number' })
157 ),
158
159 /* Display */
160 el(PanelBody, { title: __('Display Options', 'blockenberg'), initialOpen: false },
161 el(ToggleControl, { label: __('Show Round-Trip Cost', 'blockenberg'), checked: a.showRoundTrip, onChange: t('showRoundTrip'), __nextHasNoMarginBottom: true }),
162 el(ToggleControl, { label: __('Show Cost Per Person', 'blockenberg'), checked: a.showPerPerson, onChange: t('showPerPerson'), __nextHasNoMarginBottom: true }),
163 el(ToggleControl, { label: __('Show Fuel Used (L / gal)', 'blockenberg'), checked: a.showLitres, onChange: t('showLitres'), __nextHasNoMarginBottom: true }),
164 el(ToggleControl, { label: __('Show CO₂ Emissions', 'blockenberg'), checked: a.showCO2, onChange: t('showCO2'), __nextHasNoMarginBottom: true })
165 ),
166
167 /* Colors */
168
169 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
170 _tc()({ label: __('Title', 'blockenberg'), value: a.typoTitle, onChange: function (v) { set({ typoTitle: v }); } }),
171 _tc()({ label: __('Subtitle', 'blockenberg'), value: a.typoSubtitle, onChange: function (v) { set({ typoSubtitle: v }); } }),
172 _tc()({ label: __('Result Value', 'blockenberg'), value: a.typoResult, onChange: function (v) { set({ typoResult: v }); } })
173 ),
174 el(PanelColorSettings, {
175 title: __('Colors', 'blockenberg'),
176 initialOpen: false,
177 colorSettings: [
178 { value: a.accentColor, onChange: s('accentColor'), label: __('Accent', 'blockenberg') },
179 { value: a.cardBg, onChange: s('cardBg'), label: __('Card Background', 'blockenberg') },
180 { value: a.resultBg, onChange: s('resultBg'), label: __('Result Box BG', 'blockenberg') },
181 { value: a.resultBorder, onChange: s('resultBorder'), label: __('Result Box Border', 'blockenberg') },
182 { value: a.totalColor, onChange: s('totalColor'), label: __('Total Cost Color', 'blockenberg') },
183 { value: a.statCardBg, onChange: s('statCardBg'), label: __('Stat Card BG', 'blockenberg') },
184 { value: a.co2Color, onChange: s('co2Color'), label: __('CO₂ Color', 'blockenberg') },
185 { value: a.titleColor, onChange: s('titleColor'), label: __('Title Color', 'blockenberg') },
186 { value: a.subtitleColor, onChange: s('subtitleColor'), label: __('Subtitle Color', 'blockenberg') },
187 { value: a.labelColor, onChange: s('labelColor'), label: __('Label Color', 'blockenberg') },
188 { value: a.sectionBg, onChange: s('sectionBg'), label: __('Section Background', 'blockenberg') }
189 ]
190 }),
191
192 /* Sizing */
193 el(PanelBody, { title: __('Sizing', 'blockenberg'), initialOpen: false },
194 el(RangeControl, { label: __('Card Radius (px)', 'blockenberg'), value: a.cardRadius, onChange: n('cardRadius'), min: 0, max: 32 }),
195 el(RangeControl, { label: __('Input Radius (px)', 'blockenberg'), value: a.inputRadius, onChange: n('inputRadius'), min: 0, max: 20 }),
196 el(RangeControl, { label: __('Max Width (px)', 'blockenberg'), value: a.maxWidth, onChange: n('maxWidth'), min: 320, max: 960, step: 20 }),
197 el(RangeControl, { label: __('Padding Top (px)', 'blockenberg'), value: a.paddingTop, onChange: n('paddingTop'), min: 0, max: 160 }),
198 el(RangeControl, { label: __('Padding Bottom (px)', 'blockenberg'), value: a.paddingBottom, onChange: n('paddingBottom'), min: 0, max: 160 })
199 )
200 ),
201
202 el('div', blockProps,
203 el(FuelPreview, { attrs: a })
204 )
205 );
206 }
207
208 registerBlockType('blockenberg/fuel-calculator', {
209 edit: FuelEdit,
210 save: function (props) {
211 var a = props.attributes;
212 return el('div', wp.blockEditor.useBlockProps.save({
213 className: 'bkbg-fuel-app',
214 'data-opts': JSON.stringify(a)
215 }));
216 }
217 });
218 }() );
219