-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrogram.js
More file actions
113 lines (97 loc) · 3.69 KB
/
brogram.js
File metadata and controls
113 lines (97 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Brogram exercise modal
* Opens a modal with exercise description when help button is clicked.
*
* @package Liftz
*/
(function () {
'use strict';
const modalId = 'brogram-exercise-modal';
function createModal() {
const modal = document.createElement('div');
modal.id = modalId;
modal.className = 'brogram-modal';
modal.setAttribute('role', 'dialog');
modal.setAttribute('aria-modal', 'true');
modal.setAttribute('aria-labelledby', 'brogram-modal-title');
modal.innerHTML = `
<div class="brogram-modal__dialog" role="document">
<div class="brogram-modal__header">
<h3 id="brogram-modal-title" class="brogram-modal__title"></h3>
<button type="button" class="brogram-modal__close" aria-label="Close modal">×</button>
</div>
<div class="brogram-modal__body">
<p></p>
</div>
</div>
`;
document.body.appendChild(modal);
return modal;
}
function getModal() {
return document.getElementById(modalId) || createModal();
}
function openModal(name, description) {
const modal = getModal();
const titleEl = modal.querySelector('.brogram-modal__title');
const bodyEl = modal.querySelector('.brogram-modal__body p');
const closeBtn = modal.querySelector('.brogram-modal__close');
titleEl.textContent = name;
bodyEl.textContent = description || 'No description available.';
modal.classList.add('is-open');
document.body.style.overflow = 'hidden';
closeBtn.focus();
const focusable = modal.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
const firstFocusable = focusable[0];
const lastFocusable = focusable[focusable.length - 1];
function handleKeydown(e) {
if (e.key === 'Escape') {
closeModal();
return;
}
if (e.key === 'Tab') {
if (e.shiftKey && document.activeElement === firstFocusable) {
e.preventDefault();
lastFocusable.focus();
} else if (!e.shiftKey && document.activeElement === lastFocusable) {
e.preventDefault();
firstFocusable.focus();
}
}
}
modal._handleKeydown = handleKeydown;
document.addEventListener('keydown', handleKeydown);
}
function closeModal() {
const modal = getModal();
modal.classList.remove('is-open');
document.body.style.overflow = '';
document.removeEventListener('keydown', modal._handleKeydown);
}
function init() {
document.addEventListener('click', function (e) {
const btn = e.target.closest('.brogram-workout__help');
if (!btn) return;
e.preventDefault();
const name = btn.getAttribute('data-exercise-name');
const desc = btn.getAttribute('data-exercise-desc');
if (name && desc) {
openModal(name, desc);
}
});
document.addEventListener('click', function (e) {
const modal = getModal();
const closeBtn = e.target.closest('.brogram-modal__close');
const backdrop = e.target === modal;
if (closeBtn || backdrop) {
e.preventDefault();
closeModal();
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();