|
386 | 386 | </div> |
387 | 387 | </div> |
388 | 388 |
|
389 | | - <script> |
390 | | - document.addEventListener('htmx:load', function() { |
391 | | - var shouldPromptForTestimonial = <cfoutput>#session.keyExists("promptForTestimonial") ? 'true' : 'false'#</cfoutput>; |
392 | | - var clearFlagUrl = "<cfoutput>#session.delete('promptForTestimonial')#</cfoutput>"; |
| 389 | + <cfoutput> |
| 390 | + <cfif session.keyExists("promptForTestimonial")> |
| 391 | + <!--- This div triggers the clearing of the session flag ---> |
| 392 | + <div id="clear-prompt-trigger" |
| 393 | + hx-post="#urlFor(route='clear_testimonial_prompt')#" |
| 394 | + hx-trigger="load delay:50ms" <!--- Trigger on load ---> |
| 395 | + hx-swap="none" <!--- No HTML swap needed ---> |
| 396 | + style="display: none;"> |
| 397 | + </div> |
| 398 | + |
| 399 | + <script> |
| 400 | + // This script block only renders if the session flag exists. |
393 | 401 |
|
394 | | - if (shouldPromptForTestimonial) { |
| 402 | + // Get modal element immediately |
395 | 403 | var testimonialModalElement = document.getElementById('testimonialPromptModal'); |
| 404 | + var testimonialModalInstance = null; |
| 405 | + // Flag to ensure the 'shown.bs.modal' listener is attached only once |
| 406 | + let formLoadListenerAttached = false; |
396 | 407 |
|
397 | 408 | if (testimonialModalElement) { |
398 | | - // Get the Bootstrap Modal instance |
399 | | - var testimonialModal = bootstrap.Modal.getOrCreateInstance(testimonialModalElement); |
| 409 | + // Get or create the Bootstrap modal instance right away |
| 410 | + testimonialModalInstance = bootstrap.Modal.getOrCreateInstance(testimonialModalElement); |
400 | 411 |
|
401 | | - testimonialModalElement.addEventListener('shown.bs.modal', function () { |
402 | | -
|
403 | | - var formContainer = testimonialModalElement.querySelector('#testimonial-form-container'); |
404 | | - if (formContainer) { |
405 | | - // Check if content is already loaded (e.g., if modal was opened, closed, reopened quickly) |
406 | | - // Simple check: see if it still contains the spinner/loading text |
407 | | - var isLoadingIndicatorPresent = formContainer.querySelector('.spinner-border'); |
408 | | -
|
409 | | - if(isLoadingIndicatorPresent) { |
410 | | - console.log('Form container found, processing HTMX.'); // Debug log |
411 | | - // Manually process the container with HTMX to trigger the hx-get |
412 | | - htmx.process(formContainer); |
| 412 | + document.body.addEventListener('showTestimonialModal', function handleShowTrigger() { |
| 413 | + console.log('Received showTestimonialModal trigger from backend.'); |
| 414 | + if (testimonialModalInstance) { |
| 415 | + testimonialModalInstance.show(); |
| 416 | + } else { |
| 417 | + var currentModalInstance = bootstrap.Modal.getInstance(testimonialModalElement); |
| 418 | + if (currentModalInstance) { |
| 419 | + currentModalInstance.show(); |
413 | 420 | } else { |
414 | | - console.log('Form container already has content, skipping HTMX process.'); // Debug log |
| 421 | + console.error("Modal instance not found when trying to show via HX-Trigger."); |
415 | 422 | } |
416 | | - } else { |
417 | | - console.error('Form container #testimonial-form-container not found inside modal.'); |
418 | 423 | } |
| 424 | + }, { once: true }); |
419 | 425 |
|
420 | | - }, { once: true }); // Use { once: true } so this listener only fires ONCE per modal instance show |
421 | | -
|
422 | | - //Show the modal |
423 | | - testimonialModal.show(); |
424 | | -
|
| 426 | + if (!formLoadListenerAttached && testimonialModalInstance) { |
| 427 | + testimonialModalElement.addEventListener('shown.bs.modal', function handleModalShown() { |
| 428 | + var formContainer = testimonialModalElement.querySelector('##testimonial-form-container'); |
| 429 | + if (formContainer) { |
| 430 | + var isLoadingIndicatorPresent = formContainer.querySelector('.spinner-border'); |
| 431 | + if (isLoadingIndicatorPresent || formContainer.innerHTML.trim() === '') { |
| 432 | + console.log('Modal shown, processing HTMX for form container.'); |
| 433 | + htmx.process(formContainer); // Trigger the hx-get on the container |
| 434 | + } else { |
| 435 | + console.log('Modal shown, form container already has content.'); |
| 436 | + } |
| 437 | + } else { |
| 438 | + console.error('Form container ##testimonial-form-container not found inside modal.'); |
| 439 | + } |
| 440 | + }); |
| 441 | + formLoadListenerAttached = true; // Mark that this listener has been attached. |
| 442 | + console.log('Attached shown.bs.modal listener.'); |
| 443 | + } |
425 | 444 | } else { |
426 | | - console.error("Testimonial prompt modal element not found."); |
| 445 | + console.error("Testimonial prompt modal element not found when initializing script."); |
427 | 446 | } |
428 | | - } |
429 | | - }); |
430 | | - </script> |
431 | | - |
| 447 | + </script> |
| 448 | + |
| 449 | + </cfif> |
| 450 | + </cfoutput> |
| 451 | + |
432 | 452 | <script src="/javascripts/swiper.js"></script> |
433 | 453 | <script src="/javascripts/custom.js"></script> |
434 | 454 | <script src="/javascripts/infinite-scroll.pkgd.min.js"></script> |
|
0 commit comments