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

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

169 lines 7.5 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 document.querySelectorAll('.bkbg-qz-card').forEach(function (card) {
5 var oneAtATime = card.getAttribute('data-one-at-a-time') !== 'false';
6 var shouldRandom = card.getAttribute('data-randomize') === 'true';
7 var progressStyle = card.getAttribute('data-progress-style') || 'bar';
8 var showProgress = card.getAttribute('data-show-progress') !== 'false';
9
10 var questionsWrap = card.querySelector('.bkbg-qz-questions-wrap');
11 var resultsWrap = card.querySelector('.bkbg-qz-results-wrap');
12 var progressWrap = card.querySelector('.bkbg-qz-progress-wrap');
13
14 if (!questionsWrap || !resultsWrap) return;
15
16 var questionEls = Array.prototype.slice.call(questionsWrap.querySelectorAll('.bkbg-qz-question'));
17 var total = questionEls.length;
18 if (total === 0) return;
19
20 /* Randomise order if needed */
21 if (shouldRandom) {
22 questionEls.forEach(function (q) { q.parentNode.removeChild(q); });
23 questionEls.sort(function () { return Math.random() - 0.5; });
24 questionEls.forEach(function (q) { questionsWrap.appendChild(q); });
25 }
26
27 var answers = {}; /* { questionId: pointsValue } */
28 var currentIdx = 0;
29
30 /* ── Progress UI builder ─────────────────────────────────── */
31 function buildProgressUI() {
32 progressWrap.innerHTML = '';
33 if (!showProgress) return;
34
35 if (progressStyle === 'fraction') {
36 var frac = document.createElement('div');
37 frac.className = 'bkbg-qz-progress-fraction';
38 frac.textContent = (currentIdx + 1) + ' / ' + total;
39 progressWrap.appendChild(frac);
40
41 } else if (progressStyle === 'dots') {
42 var dotsRow = document.createElement('div');
43 dotsRow.className = 'bkbg-qz-progress-dots';
44 for (var d = 0; d < total; d++) {
45 var dot = document.createElement('div');
46 dot.className = 'bkbg-qz-progress-dot' + (d <= currentIdx ? ' is-done' : '');
47 dotsRow.appendChild(dot);
48 }
49 progressWrap.appendChild(dotsRow);
50
51 } else { /* bar */
52 var barRow = document.createElement('div');
53 barRow.className = 'bkbg-qz-progress-bar-row';
54 var leftLabel = document.createElement('span');
55 leftLabel.textContent = 'Question ' + (currentIdx + 1);
56 var rightLabel = document.createElement('span');
57 rightLabel.textContent = total + ' total';
58 barRow.appendChild(leftLabel);
59 barRow.appendChild(rightLabel);
60
61 var track = document.createElement('div');
62 track.className = 'bkbg-qz-progress-track';
63 var fill = document.createElement('div');
64 fill.className = 'bkbg-qz-progress-fill';
65 var pct = total > 1 ? (currentIdx / (total - 1)) * 100 : 0;
66 fill.style.width = pct + '%';
67 track.appendChild(fill);
68
69 progressWrap.appendChild(barRow);
70 progressWrap.appendChild(track);
71 }
72 }
73
74 /* ── Show a specific question ─────────────────────────────── */
75 function showQuestion(idx) {
76 currentIdx = idx;
77 questionEls.forEach(function (q, i) {
78 var show = !oneAtATime || i === idx;
79 q.style.display = show ? 'block' : 'none';
80 q.setAttribute('aria-hidden', show ? 'false' : 'true');
81 });
82 buildProgressUI();
83 }
84
85 /* ── Compute total score and show result ──────────────────── */
86 function submitQuiz() {
87 var score = 0;
88 Object.keys(answers).forEach(function (k) { score += (answers[k] || 0); });
89
90 var resultEls = resultsWrap.querySelectorAll('.bkbg-qz-result');
91 var matched = null;
92 resultEls.forEach(function (r) {
93 var min = parseInt(r.getAttribute('data-min'), 10);
94 var max = parseInt(r.getAttribute('data-max'), 10);
95 if (score >= min && score <= max) matched = r;
96 });
97 if (!matched && resultEls.length > 0) {
98 /* fall back to first result */
99 matched = resultEls[0];
100 }
101
102 /* Hide questions, show result */
103 questionsWrap.style.display = 'none';
104 progressWrap.innerHTML = '';
105 resultsWrap.classList.remove('is-hidden');
106 resultEls.forEach(function (r) {
107 r.style.display = r === matched ? 'block' : 'none';
108 });
109 }
110
111 /* ── Retake ───────────────────────────────────────────────── */
112 resultsWrap.querySelectorAll('.bkbg-qz-retake').forEach(function (btn) {
113 btn.addEventListener('click', function () {
114 answers = {};
115 currentIdx = 0;
116
117 /* Reset question states */
118 questionEls.forEach(function (q) {
119 q.querySelectorAll('.bkbg-qz-option').forEach(function (o) {
120 o.classList.remove('is-selected');
121 });
122 var nextBtn = q.querySelector('.bkbg-qz-btn-next');
123 if (nextBtn) nextBtn.disabled = true;
124 });
125
126 /* Hide results, show questions */
127 resultsWrap.classList.add('is-hidden');
128 resultsWrap.querySelectorAll('.bkbg-qz-result').forEach(function (r) { r.style.display = 'none'; });
129 questionsWrap.style.display = 'block';
130 showQuestion(0);
131 });
132 });
133
134 /* ── Wire up each question ────────────────────────────────── */
135 questionEls.forEach(function (questionEl, qi) {
136 var qId = questionEl.getAttribute('data-question-id') || String(qi);
137 var options = questionEl.querySelectorAll('.bkbg-qz-option');
138 var nextBtn = questionEl.querySelector('.bkbg-qz-btn-next');
139 var isLast = nextBtn && nextBtn.getAttribute('data-is-last') === 'true';
140
141 options.forEach(function (opt) {
142 opt.addEventListener('click', function () {
143 var pts = parseInt(opt.getAttribute('data-points'), 10) || 0;
144 answers[qId] = pts;
145
146 options.forEach(function (o) { o.classList.remove('is-selected'); });
147 opt.classList.add('is-selected');
148
149 if (nextBtn) nextBtn.disabled = false;
150 });
151 });
152
153 if (nextBtn) {
154 nextBtn.addEventListener('click', function () {
155 if (nextBtn.disabled) return;
156 if (isLast) {
157 submitQuiz();
158 } else {
159 showQuestion(qi + 1);
160 }
161 });
162 }
163 });
164
165 /* ── Initial state ────────────────────────────────────────── */
166 showQuestion(0);
167 });
168 })();
169