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 / business-hours / frontend.js

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

94 lines 3.7 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 function initBusinessHours(wrap) {
5 var highlightToday = wrap.getAttribute('data-highlight-today') === '1';
6 var showStatus = wrap.getAttribute('data-show-status') === '1';
7 var openText = wrap.getAttribute('data-open-text') || 'Open Now';
8 var closedText = wrap.getAttribute('data-closed-text') || 'Closed Now';
9
10 var rows = wrap.querySelectorAll('.bkbg-bh-row');
11 var statusEl = wrap.querySelector('.bkbg-bh-status');
12
13 // Get current day index (0 = Monday, 6 = Sunday)
14 var now = new Date();
15 var jsDay = now.getDay(); // 0 = Sunday
16 var todayIndex = jsDay === 0 ? 6 : jsDay - 1;
17
18 // Highlight today
19 if (highlightToday) {
20 rows.forEach(function (row, index) {
21 if (index === todayIndex) {
22 row.classList.add('is-today');
23 // Add today badge if not exists
24 var dayEl = row.querySelector('.bkbg-bh-day');
25 if (dayEl && !dayEl.querySelector('.bkbg-bh-today-badge')) {
26 var badge = document.createElement('span');
27 badge.className = 'bkbg-bh-today-badge';
28 badge.textContent = 'Today';
29 dayEl.appendChild(badge);
30 }
31 }
32 });
33 }
34
35 // Check if currently open
36 if (showStatus && statusEl) {
37 var todayRow = rows[todayIndex];
38 if (todayRow) {
39 var isClosed = todayRow.getAttribute('data-closed') === '1';
40 var openTime = todayRow.getAttribute('data-open');
41 var closeTime = todayRow.getAttribute('data-close');
42
43 var isOpen = false;
44
45 if (!isClosed && openTime && closeTime) {
46 var currentMinutes = now.getHours() * 60 + now.getMinutes();
47
48 var openParts = openTime.split(':');
49 var openMinutes = parseInt(openParts[0], 10) * 60 + parseInt(openParts[1], 10);
50
51 var closeParts = closeTime.split(':');
52 var closeMinutes = parseInt(closeParts[0], 10) * 60 + parseInt(closeParts[1], 10);
53
54 // Handle overnight hours (close time < open time)
55 if (closeMinutes < openMinutes) {
56 isOpen = currentMinutes >= openMinutes || currentMinutes < closeMinutes;
57 } else {
58 isOpen = currentMinutes >= openMinutes && currentMinutes < closeMinutes;
59 }
60 }
61
62 var statusTextEl = statusEl.querySelector('.bkbg-bh-status-text');
63 if (statusTextEl) {
64 if (isOpen) {
65 statusEl.classList.remove('is-closed');
66 statusEl.classList.add('is-open');
67 statusTextEl.textContent = openText;
68 } else {
69 statusEl.classList.remove('is-open');
70 statusEl.classList.add('is-closed');
71 statusTextEl.textContent = closedText;
72 }
73 }
74 }
75 }
76 }
77
78 function init() {
79 var blocks = document.querySelectorAll('.bkbg-bh-wrap:not([data-initialized])');
80 blocks.forEach(function (block) {
81 block.setAttribute('data-initialized', '1');
82 initBusinessHours(block);
83 });
84 }
85
86 if (document.readyState === 'loading') {
87 document.addEventListener('DOMContentLoaded', init);
88 } else {
89 init();
90 }
91
92 window.addEventListener('load', init);
93 })();
94