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 / salary-guide / index.js

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

204 lines 15.0 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 Fragment = wp.element.Fragment;
4 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
5 var useBlockProps = wp.blockEditor.useBlockProps;
6 var InspectorControls = wp.blockEditor.InspectorControls;
7 var TextControl = wp.components.TextControl;
8 var TextareaControl = wp.components.TextareaControl;
9 var ToggleControl = wp.components.ToggleControl;
10 var RangeControl = wp.components.RangeControl;
11 var SelectControl = wp.components.SelectControl;
12 var PanelBody = wp.components.PanelBody;
13 var Button = wp.components.Button;
14 var __ = wp.i18n.__;
15
16 var _tc, _tv;
17 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
18 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
19
20 var levelOptions = [
21 { label: 'Junior', value: 'Junior' },
22 { label: 'Mid', value: 'Mid' },
23 { label: 'Senior', value: 'Senior' },
24 { label: 'Lead', value: 'Lead' },
25 { label: 'Principal', value: 'Principal' }
26 ];
27 var periodOptions = [
28 { label: 'per year', value: 'year' },
29 { label: 'per month', value: 'month' },
30 { label: 'per hour', value: 'hour' }
31 ];
32
33 function upd(arr, idx, field, val) {
34 return arr.map(function (e, i) {
35 if (i !== idx) return e;
36 var u = {}; u[field] = val;
37 return Object.assign({}, e, u);
38 });
39 }
40
41 function formatSalary(n, currency) {
42 if (n >= 1000) return currency + Math.round(n / 1000) + 'k';
43 return currency + n;
44 }
45
46 function getGlobalMinMax(roles) {
47 var allVals = [];
48 roles.forEach(function (r) { allVals.push(r.min, r.max); });
49 return { gMin: Math.min.apply(null, allVals), gMax: Math.max.apply(null, allVals) };
50 }
51
52 function renderBar(role, a, gMin, gMax) {
53 var range = gMax - gMin || 1;
54 var leftPct = ((role.min - gMin) / range) * 100;
55 var widthPct = ((role.max - role.min) / range) * 100;
56 var medPct = ((role.median - gMin) / range) * 100;
57
58 return el('div', { className: 'bkbg-sg-bar-wrap' },
59 el('div', { className: 'bkbg-sg-bar-track', style: { height: a.barHeight + 'px', background: a.barTrackColor, borderRadius: a.barHeight + 'px', position: 'relative', overflow: 'visible' } },
60 el('div', { className: 'bkbg-sg-bar-fill', style: {
61 position: 'absolute', left: leftPct.toFixed(2) + '%', width: widthPct.toFixed(2) + '%',
62 height: '100%', background: a.barFillColor, borderRadius: a.barHeight + 'px'
63 } }),
64 a.showMedian && el('div', { className: 'bkbg-sg-bar-median', style: {
65 position: 'absolute', left: medPct.toFixed(2) + '%',
66 top: '-3px', width: '3px', height: (a.barHeight + 6) + 'px',
67 background: a.medianColor, borderRadius: 2, transform: 'translateX(-50%)'
68 } })
69 )
70 );
71 }
72
73 function renderRow(role, a, gMin, gMax) {
74 return el('div', { className: 'bkbg-sg-row', key: role.title + role.level, style: { borderTopColor: a.borderColor } },
75 el('div', { className: 'bkbg-sg-row-header' },
76 el('div', { className: 'bkbg-sg-role-info' },
77 el('span', { className: 'bkbg-sg-role-title', style: { color: a.roleTitleColor } }, role.title),
78 a.showLevel && el('span', { className: 'bkbg-sg-level-badge', style: { background: a.levelBg, color: a.levelColor } }, role.level)
79 ),
80 el('div', { className: 'bkbg-sg-range-labels', style: { color: a.rangeLabelColor } },
81 el('span', null, formatSalary(role.min, a.currency)),
82 a.showMedian && el('span', { className: 'bkbg-sg-median-label', style: { color: a.medianColor, fontWeight: 600 } }, formatSalary(role.median, a.currency) + ' median'),
83 el('span', null, formatSalary(role.max, a.currency))
84 )
85 ),
86 a.showBars && renderBar(role, a, gMin, gMax)
87 );
88 }
89
90 function getCategories(roles) {
91 var cats = [], seen = {};
92 roles.forEach(function (r) { if (!seen[r.category]) { seen[r.category] = 1; cats.push(r.category); } });
93 return cats;
94 }
95
96 wp.blocks.registerBlockType('blockenberg/salary-guide', {
97 edit: function (props) {
98 var a = props.attributes;
99 var setAttr = props.setAttributes;
100 var mm = getGlobalMinMax(a.roles.length ? a.roles : [{ min: 0, max: 100, median: 50 }]);
101
102 var rows;
103 if (a.groupByCategory) {
104 var cats = getCategories(a.roles);
105 rows = cats.map(function (cat) {
106 var catRoles = a.roles.filter(function (r) { return r.category === cat; });
107 return el('div', { key: cat, className: 'bkbg-sg-category-group' },
108 el('div', { className: 'bkbg-sg-category-head', style: { color: a.categoryHeadColor, background: a.categoryHeadBg, borderColor: a.borderColor } }, cat),
109 catRoles.map(function (role) { return renderRow(role, a, mm.gMin, mm.gMax); })
110 );
111 });
112 } else {
113 rows = a.roles.map(function (role) { return renderRow(role, a, mm.gMin, mm.gMax); });
114 }
115
116 return el(Fragment, null,
117 el(InspectorControls, null,
118 el(PanelBody, { title: __('Guide Settings'), initialOpen: true },
119 el(TextControl, { label: __('Title'), value: a.guideTitle, onChange: function (v) { setAttr({ guideTitle: v }); }, __nextHasNoMarginBottom: true }),
120 el(TextareaControl, { label: __('Description'), value: a.description, onChange: function (v) { setAttr({ description: v }); }, __nextHasNoMarginBottom: true }),
121 el(ToggleControl, { label: __('Show Description'), checked: a.showDescription, onChange: function (v) { setAttr({ showDescription: v }); }, __nextHasNoMarginBottom: true }),
122 el(TextControl, { label: __('Currency Symbol'), value: a.currency, onChange: function (v) { setAttr({ currency: v }); }, __nextHasNoMarginBottom: true }),
123 el(SelectControl, { label: __('Period'), value: a.period, options: periodOptions, onChange: function (v) { setAttr({ period: v }); }, __nextHasNoMarginBottom: true }),
124 el(TextControl, { label: __('Location'), value: a.location, onChange: function (v) { setAttr({ location: v }); }, __nextHasNoMarginBottom: true }),
125 el(ToggleControl, { label: __('Show Location'), checked: a.showLocation, onChange: function (v) { setAttr({ showLocation: v }); }, __nextHasNoMarginBottom: true }),
126 el(ToggleControl, { label: __('Show Bars'), checked: a.showBars, onChange: function (v) { setAttr({ showBars: v }); }, __nextHasNoMarginBottom: true }),
127 el(ToggleControl, { label: __('Show Median Marker'), checked: a.showMedian, onChange: function (v) { setAttr({ showMedian: v }); }, __nextHasNoMarginBottom: true }),
128 el(ToggleControl, { label: __('Show Level Badge'), checked: a.showLevel, onChange: function (v) { setAttr({ showLevel: v }); }, __nextHasNoMarginBottom: true }),
129 el(ToggleControl, { label: __('Group by Category'), checked: a.groupByCategory, onChange: function (v) { setAttr({ groupByCategory: v }); }, __nextHasNoMarginBottom: true })
130 ),
131 el(PanelBody, { title: __('Roles'), initialOpen: false },
132 a.roles.map(function (role, i) {
133 return el('div', { key: i, style: { border: '1px solid #e2e8f0', borderRadius: 8, padding: 12, marginBottom: 12, background: '#fafafa' } },
134 el('strong', { style: { display: 'block', marginBottom: 6 } }, role.title + '' + role.level),
135 el(TextControl, { label: __('Role Title'), value: role.title, onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'title', v) }); }, __nextHasNoMarginBottom: true }),
136 el(SelectControl, { label: __('Level'), value: role.level, options: levelOptions, onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'level', v) }); }, __nextHasNoMarginBottom: true }),
137 el(TextControl, { label: __('Category'), value: role.category, onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'category', v) }); }, __nextHasNoMarginBottom: true }),
138 el(TextControl, { label: __('Min'), type: 'number', value: String(role.min), onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'min', parseInt(v, 10) || 0) }); }, __nextHasNoMarginBottom: true }),
139 el(TextControl, { label: __('Median'), type: 'number', value: String(role.median), onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'median', parseInt(v, 10) || 0) }); }, __nextHasNoMarginBottom: true }),
140 el(TextControl, { label: __('Max'), type: 'number', value: String(role.max), onChange: function (v) { setAttr({ roles: upd(a.roles, i, 'max', parseInt(v, 10) || 0) }); }, __nextHasNoMarginBottom: true }),
141 el(Button, { isDestructive: true, variant: 'secondary', onClick: function () { setAttr({ roles: a.roles.filter(function (_, j) { return j !== i; }) }); } }, __('Remove'))
142 );
143 }),
144 el(Button, { variant: 'primary', onClick: function () { setAttr({ roles: a.roles.concat([{ title: 'New Role', level: 'Mid', min: 80000, median: 100000, max: 130000, category: 'Engineering' }]) }); } }, __('+ Add Role'))
145 ),
146 el(PanelBody, { title: __('Typography'), initialOpen: false },
147 el(RangeControl, { label: __('Base Font Size'), value: a.fontSize, min: 10, max: 22, onChange: function (v) { setAttr({ fontSize: v }); }, __nextHasNoMarginBottom: true }),
148 el(RangeControl, { label: __('Line Height (%)'), value: a.lineHeight, min: 120, max: 220, onChange: function (v) { setAttr({ lineHeight: v }); }, __nextHasNoMarginBottom: true }),
149 getTypoControl() && el( getTypoControl(), { label: __('Title Typography'), value: a.titleTypo || {}, onChange: function(v){ setAttr({ titleTypo: v }); } }),
150 getTypoControl() && el( getTypoControl(), { label: __('Description Typography'), value: a.descTypo || {}, onChange: function(v){ setAttr({ descTypo: v }); } })
151 ),
152 el(PanelBody, { title: __('Appearance'), initialOpen: false },
153 el(RangeControl, { label: __('Border Radius'), value: a.borderRadius, min: 0, max: 32, onChange: function (v) { setAttr({ borderRadius: v }); }, __nextHasNoMarginBottom: true }),
154 el(RangeControl, { label: __('Bar Height (px)'), value: a.barHeight, min: 4, max: 24, onChange: function (v) { setAttr({ barHeight: v }); }, __nextHasNoMarginBottom: true })
155 ),
156 el(PanelColorSettings, {
157 title: __('Colors'),
158 initialOpen: false,
159 colorSettings: [
160 { label: __('Background'), value: a.bgColor, onChange: function (v) { setAttr({ bgColor: v || '#ffffff' }); } },
161 { label: __('Border'), value: a.borderColor, onChange: function (v) { setAttr({ borderColor: v || '#e5e7eb' }); } },
162 { label: __('Title'), value: a.titleColor, onChange: function (v) { setAttr({ titleColor: v || '#111827' }); } },
163 { label: __('Description'), value: a.descColor, onChange: function (v) { setAttr({ descColor: v || '#6b7280' }); } },
164 { label: __('Role Title'), value: a.roleTitleColor, onChange: function (v) { setAttr({ roleTitleColor: v || '#1f2937' }); } },
165 { label: __('Level Badge BG'), value: a.levelBg, onChange: function (v) { setAttr({ levelBg: v || '#f3f4f6' }); } },
166 { label: __('Level Badge Text'), value: a.levelColor, onChange: function (v) { setAttr({ levelColor: v || '#374151' }); } },
167 { label: __('Range Labels'), value: a.rangeLabelColor, onChange: function (v) { setAttr({ rangeLabelColor: v || '#6b7280' }); } },
168 { label: __('Bar Track'), value: a.barTrackColor, onChange: function (v) { setAttr({ barTrackColor: v || '#e5e7eb' }); } },
169 { label: __('Bar Fill'), value: a.barFillColor, onChange: function (v) { setAttr({ barFillColor: v || '#3b82f6' }); } },
170 { label: __('Median Marker'), value: a.medianColor, onChange: function (v) { setAttr({ medianColor: v || '#0f172a' }); } },
171 { label: __('Category Heading'), value: a.categoryHeadColor, onChange: function (v) { setAttr({ categoryHeadColor: v || '#374151' }); } },
172 { label: __('Category Heading BG'), value: a.categoryHeadBg, onChange: function (v) { setAttr({ categoryHeadBg: v || '#f8fafc' }); } }
173 ]
174 })
175 ),
176 el('div', useBlockProps((function () {
177 var _tv = getTypoCssVars();
178 var s = { background: a.bgColor, borderRadius: a.borderRadius + 'px', border: '1px solid ' + a.borderColor, overflow: 'hidden', fontSize: a.fontSize + 'px', lineHeight: (a.lineHeight / 100).toFixed(2) };
179 if (_tv) {
180 Object.assign(s, _tv(a.titleTypo, '--bksg-tt-'));
181 Object.assign(s, _tv(a.descTypo, '--bksg-dt-'));
182 }
183 return { className: 'bkbg-sg-wrap', style: s };
184 })()),
185 el('div', { className: 'bkbg-sg-header', style: { padding: '24px 28px', borderBottom: '1px solid ' + a.borderColor } },
186 el('h2', { className: 'bkbg-sg-title', style: { color: a.titleColor } }, a.guideTitle),
187 el('div', { className: 'bkbg-sg-meta', style: { color: a.descColor } },
188 a.showDescription && a.description && el('p', { style: { margin: 0 } }, a.description),
189 a.showLocation && el('span', { className: 'bkbg-sg-location' }, '📍 ' + a.location + ' · ' + a.currency + ' / ' + a.period)
190 )
191 ),
192 el('div', { className: 'bkbg-sg-body' }, rows)
193 )
194 );
195 },
196 save: function (props) {
197 var useBlockProps = wp.blockEditor.useBlockProps;
198 return el('div', useBlockProps.save(),
199 el('div', { className: 'bkbg-salary-guide-app', 'data-opts': JSON.stringify(props.attributes) })
200 );
201 }
202 });
203 }() );
204