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 / phone-button / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/phone-button/frontend.js

175 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
4 var _typoKeys = {
5 family:'font-family', weight:'font-weight', style:'font-style',
6 decoration:'text-decoration', transform:'text-transform',
7 sizeDesktop:'font-size-d', sizeTablet:'font-size-t', sizeMobile:'font-size-m',
8 lineHeightDesktop:'line-height-d', lineHeightTablet:'line-height-t', lineHeightMobile:'line-height-m',
9 letterSpacingDesktop:'letter-spacing-d', letterSpacingTablet:'letter-spacing-t', letterSpacingMobile:'letter-spacing-m',
10 wordSpacingDesktop:'word-spacing-d', wordSpacingTablet:'word-spacing-t', wordSpacingMobile:'word-spacing-m'
11 };
12 function typoCssVarsForEl(el, obj, prefix) {
13 if (!obj || typeof obj !== 'object') return;
14 Object.keys(_typoKeys).forEach(function (k) {
15 var v = obj[k];
16 if (v === undefined || v === '' || v === null) return;
17 if (k === 'sizeDesktop' || k === 'sizeTablet' || k === 'sizeMobile') v = v + (obj.sizeUnit || 'px');
18 else if (k === 'lineHeightDesktop' || k === 'lineHeightTablet' || k === 'lineHeightMobile') v = v + (obj.lineHeightUnit || '');
19 else if (k === 'letterSpacingDesktop' || k === 'letterSpacingTablet' || k === 'letterSpacingMobile') v = v + (obj.letterSpacingUnit || 'px');
20 else if (k === 'wordSpacingDesktop' || k === 'wordSpacingTablet' || k === 'wordSpacingMobile') v = v + (obj.wordSpacingUnit || 'px');
21 el.style.setProperty(prefix + _typoKeys[k], String(v));
22 });
23 }
24
25 function buildWhatsAppUrl(number, message) {
26 var num = (number || '').replace(/[^0-9]/g, '');
27 var msg = encodeURIComponent(message || '');
28 return 'https://wa.me/' + num + (msg ? '?text=' + msg : '');
29 }
30
31 function initBlock(root) {
32 var app = root.querySelector('.bkbg-phb-app');
33 if (!app) return;
34
35 var raw = app.getAttribute('data-opts') || '{}';
36 var o;
37 try { o = JSON.parse(raw); } catch (e) { o = {}; }
38
39 var mode = o.mode || 'both';
40 var layout = o.layout || 'floating';
41 var position = o.position || 'bottom-right';
42 var showPulse = o.showPulse !== false;
43 var showText = !!o.showText;
44 var buttonText = o.buttonText || 'Call us';
45 var buttonSize = Number(o.buttonSize) || 56;
46 var iconSize = Number(o.iconSize) || 26;
47 var borderRadius = Number(o.borderRadius) || 999;
48 var gap = Number(o.gap) || 14;
49 var shadowStrength = Number(o.shadowStrength) || 30;
50 var scrollOffset = Number(o.scrollOffset) || 0;
51 var phoneBg = o.phoneBg || '#6366f1';
52 var phoneColor = o.phoneColor || '#ffffff';
53 var phonePulse = o.phonePulseBg || '#6366f1';
54 var whatsappBg = o.whatsappBg || '#25d366';
55 var whatsappColor = o.whatsappColor || '#ffffff';
56 var whatsappPulse = o.whatsappPulseBg || '#25d366';
57 var labelBg = o.labelBg || '#1e1b4b';
58 var labelColor = o.labelColor || '#ffffff';
59 var labelTypo = o.labelTypo;
60
61 var shadowAlpha = (shadowStrength / 100).toFixed(2);
62 var shadowStyle = '0 4px 20px rgba(0,0,0,' + shadowAlpha + ')';
63
64 var showPhone = mode !== 'whatsapp';
65 var showWhatsapp = mode !== 'phone';
66
67 /* ── Build container ──────────────────────────────────────────── */
68 var isFloating = layout === 'floating';
69 var container = document.createElement('div');
70
71 if (isFloating) {
72 container.className = 'bkbg-phb-float bkbg-phb-' + position +
73 (scrollOffset > 0 ? '' : ' bkbg-phb-visible');
74 container.style.setProperty('--phb-gap', gap + 'px');
75 document.body.appendChild(container);
76 } else {
77 container.className = 'bkbg-phb-inline';
78 container.style.setProperty('--phb-gap', gap + 'px');
79 app.appendChild(container);
80 }
81
82 /* ── Helper: build one button item ────────────────────────────── */
83 function makeItem(type, icon, bg, color, pulse, href, labelText) {
84 var item = document.createElement('div');
85 item.className = 'bkbg-phb-item bkbg-phb-' + type;
86
87 // Pulse ring
88 if (showPulse) {
89 var ring = document.createElement('div');
90 ring.className = 'bkbg-phb-pulse';
91 ring.style.borderRadius = borderRadius + 'px';
92 ring.style.setProperty('--phb-' + (type === 'phone' ? 'phone' : 'wa') + '-pulse', pulse);
93 ring.style.background = pulse;
94 item.appendChild(ring);
95 }
96
97 // Button
98 var btn = document.createElement('a');
99 btn.className = 'bkbg-phb-btn';
100 btn.href = href;
101 btn.target = '_blank';
102 btn.rel = 'noopener noreferrer';
103 btn.setAttribute('aria-label', labelText || type);
104 btn.style.cssText = [
105 'width:' + buttonSize + 'px',
106 'height:' + buttonSize + 'px',
107 'border-radius:' + borderRadius + 'px',
108 'background:' + bg,
109 'color:' + color,
110 'font-size:' + iconSize + 'px',
111 'box-shadow:' + shadowStyle
112 ].join(';');
113 btn.textContent = icon;
114 item.appendChild(btn);
115
116 // Label
117 if (showText && labelText) {
118 var lbl = document.createElement('span');
119 lbl.className = 'bkbg-phb-label';
120 lbl.textContent = labelText;
121 lbl.style.background = labelBg;
122 lbl.style.color = labelColor;
123 typoCssVarsForEl(lbl, labelTypo, '--bkbg-phb-lb-');
124 item.appendChild(lbl);
125 }
126
127 return item;
128 }
129
130 /* ── Add phone button ─────────────────────────────────────────── */
131 if (showPhone && o.phoneNumber) {
132 var phoneHref = 'tel:' + o.phoneNumber.replace(/\s/g, '');
133 container.appendChild(
134 makeItem('phone', '📞', phoneBg, phoneColor, phonePulse, phoneHref, buttonText)
135 );
136 }
137
138 /* ── Add WhatsApp button ──────────────────────────────────────── */
139 if (showWhatsapp && o.whatsappNumber) {
140 var waHref = buildWhatsAppUrl(o.whatsappNumber, o.whatsappMessage);
141 container.appendChild(
142 makeItem('whatsapp', '💬', whatsappBg, whatsappColor, whatsappPulse, waHref, 'WhatsApp')
143 );
144 }
145
146 /* ── Scroll offset (floating only) ────────────────────────────── */
147 if (isFloating && scrollOffset > 0) {
148 var ticking = false;
149 window.addEventListener('scroll', function () {
150 if (!ticking) {
151 requestAnimationFrame(function () {
152 if (window.scrollY >= scrollOffset) {
153 container.classList.add('bkbg-phb-visible');
154 } else {
155 container.classList.remove('bkbg-phb-visible');
156 }
157 ticking = false;
158 });
159 ticking = true;
160 }
161 }, { passive: true });
162 }
163 }
164
165 function init() {
166 document.querySelectorAll('.wp-block-blockenberg-phone-button').forEach(initBlock);
167 }
168
169 if (document.readyState === 'loading') {
170 document.addEventListener('DOMContentLoaded', init);
171 } else {
172 init();
173 }
174 })();
175