|
216 | 216 | } |
217 | 217 | }); |
218 | 218 | } |
| 219 | + |
| 220 | + const settingStoreID = `WW.${document.getElementsByName('courseID')[0]?.value ?? 'unknownCourse'}.${ |
| 221 | + document.getElementsByName('user')[0]?.value ?? 'unknownUser' |
| 222 | + }.problem_grader`; |
| 223 | + let gradersOpen = localStorage.getItem(`${settingStoreID}.open`) === 'true'; |
| 224 | + |
| 225 | + const graderCollapses = []; |
| 226 | + |
| 227 | + for (const grader of document.querySelectorAll('.problem-grader')) { |
| 228 | + const problemId = grader.id.replace('problem-grader-'); |
| 229 | + |
| 230 | + grader.classList.add('accordion'); |
| 231 | + |
| 232 | + const accordionItem = document.createElement('div'); |
| 233 | + accordionItem.classList.add('accordion-item'); |
| 234 | + |
| 235 | + const accordionHeader = document.createElement('h2'); |
| 236 | + accordionHeader.classList.add('accordion-header'); |
| 237 | + |
| 238 | + const accordionButton = document.createElement('button'); |
| 239 | + accordionButton.classList.add('accordion-button'); |
| 240 | + accordionButton.type = 'button'; |
| 241 | + accordionButton.textContent = grader.dataset.graderTitle ?? 'Problem Grader'; |
| 242 | + accordionButton.dataset.bsToggle = 'collapse'; |
| 243 | + accordionButton.dataset.bsTarget = `#problem-grader-collapse-${problemId}`; |
| 244 | + accordionButton.setAttribute('aria-controls', `#problem-grader-collapse-${problemId}`); |
| 245 | + accordionButton.setAttribute('aria-expanded', gradersOpen); |
| 246 | + if (!gradersOpen) accordionButton.classList.add('collapsed'); |
| 247 | + |
| 248 | + accordionHeader.append(accordionButton); |
| 249 | + |
| 250 | + const accordionCollapse = document.createElement('div'); |
| 251 | + accordionCollapse.classList.add('accordion-collapse', 'collapse'); |
| 252 | + accordionCollapse.id = `problem-grader-collapse-${problemId}`; |
| 253 | + accordionCollapse.dataset.bsParent = `problem-grader-${problemId}`; |
| 254 | + if (gradersOpen) accordionCollapse.classList.add('show'); |
| 255 | + |
| 256 | + const accordionBody = grader.querySelector('.problem-grader-table'); |
| 257 | + accordionBody.classList.add('accordion-body'); |
| 258 | + accordionCollapse.append(accordionBody); |
| 259 | + |
| 260 | + accordionItem.append(accordionHeader, accordionCollapse); |
| 261 | + grader.append(accordionItem); |
| 262 | + |
| 263 | + const graderCollapse = new bootstrap.Collapse(accordionCollapse, { toggle: false }); |
| 264 | + graderCollapses.push(graderCollapse); |
| 265 | + |
| 266 | + // Expand or collapse all problem graders on the page when any one of them is expanded or collapsed. |
| 267 | + let transitioning = false; |
| 268 | + accordionCollapse.addEventListener('show.bs.collapse', () => { |
| 269 | + if (transitioning) return; |
| 270 | + transitioning = true; |
| 271 | + for (const grader of graderCollapses) { |
| 272 | + if (grader !== graderCollapse) grader.show(); |
| 273 | + } |
| 274 | + transitioning = false; |
| 275 | + }); |
| 276 | + accordionCollapse.addEventListener('hide.bs.collapse', () => { |
| 277 | + if (transitioning) return; |
| 278 | + transitioning = true; |
| 279 | + for (const grader of graderCollapses) { |
| 280 | + if (grader !== graderCollapse) grader.hide(); |
| 281 | + } |
| 282 | + transitioning = false; |
| 283 | + }); |
| 284 | + |
| 285 | + // Make sure that the "Reveal" button in feedback is not shown if a feedback button is used while the problem |
| 286 | + // grader is open. However, also make sure that the "Reveal" button is shown for any feedback button that is |
| 287 | + // not used while the problem grader is open. |
| 288 | + |
| 289 | + const unrevealedFeedbackBtns = []; |
| 290 | + |
| 291 | + for (const feedbackBtn of document.querySelectorAll('.ww-feedback-btn')) { |
| 292 | + const container = document.createElement('div'); |
| 293 | + container.innerHTML = feedbackBtn.dataset.bsContent; |
| 294 | + const button = container.querySelector('.reveal-correct-btn'); |
| 295 | + if (!button) continue; |
| 296 | + |
| 297 | + button.nextElementSibling?.classList.remove('d-none'); |
| 298 | + button.remove(); |
| 299 | + |
| 300 | + const fragment = new DocumentFragment(); |
| 301 | + fragment.append(container); |
| 302 | + |
| 303 | + unrevealedFeedbackBtns.push([feedbackBtn, fragment.firstElementChild.innerHTML]); |
| 304 | + |
| 305 | + const handler = () => { |
| 306 | + const index = unrevealedFeedbackBtns.findIndex((data) => data[0] === feedbackBtn); |
| 307 | + if (index !== -1) { |
| 308 | + if (gradersOpen) { |
| 309 | + unrevealedFeedbackBtns.splice(index, 1); |
| 310 | + feedbackBtn.removeEventListener('shown.bs.popover', handler); |
| 311 | + } else { |
| 312 | + bootstrap.Popover.getInstance(feedbackBtn) |
| 313 | + ?.tip?.querySelector('.reveal-correct-btn') |
| 314 | + ?.addEventListener( |
| 315 | + 'click', |
| 316 | + () => { |
| 317 | + unrevealedFeedbackBtns.splice(index, 1); |
| 318 | + feedbackBtn.removeEventListener('shown.bs.popover', handler); |
| 319 | + }, |
| 320 | + { once: true } |
| 321 | + ); |
| 322 | + } |
| 323 | + } |
| 324 | + }; |
| 325 | + |
| 326 | + feedbackBtn.addEventListener('shown.bs.popover', handler); |
| 327 | + } |
| 328 | + |
| 329 | + const removeRevealButtons = () => { |
| 330 | + for (const data of unrevealedFeedbackBtns) { |
| 331 | + const feedbackPopover = bootstrap.Popover.getInstance(data[0]); |
| 332 | + feedbackPopover?.setContent({ '.popover-body': data[1] }); |
| 333 | + } |
| 334 | + }; |
| 335 | + |
| 336 | + if (gradersOpen) removeRevealButtons(); |
| 337 | + |
| 338 | + // In addition to removing and putting back the feedback "Reveal" buttons as needed, |
| 339 | + // preserve the collapsed/expanded status of the problem graders in local storage. |
| 340 | + accordionCollapse.addEventListener('shown.bs.collapse', () => { |
| 341 | + localStorage.setItem(`${settingStoreID}.open`, 'true'); |
| 342 | + gradersOpen = true; |
| 343 | + removeRevealButtons(); |
| 344 | + }); |
| 345 | + accordionCollapse.addEventListener('hidden.bs.collapse', () => { |
| 346 | + gradersOpen = false; |
| 347 | + localStorage.setItem(`${settingStoreID}.open`, 'false'); |
| 348 | + for (const data of unrevealedFeedbackBtns) { |
| 349 | + const feedbackPopover = bootstrap.Popover.getInstance(data[0]); |
| 350 | + feedbackPopover?.setContent({ '.popover-body': data[0].dataset.bsContent }); |
| 351 | + } |
| 352 | + }); |
| 353 | + } |
219 | 354 | })(); |
0 commit comments