|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +// Connects to data-controller="better_together--event-hover-card" |
| 4 | +export default class extends Controller { |
| 5 | + static values = { |
| 6 | + eventId: String, |
| 7 | + eventUrl: String |
| 8 | + } |
| 9 | + |
| 10 | + connect() { |
| 11 | + this.popover = null |
| 12 | + this.isPopoverVisible = false |
| 13 | + this.isNavigating = false |
| 14 | + this.showTimeout = null |
| 15 | + this.hideTimeout = null |
| 16 | + this.eventCardContent = null |
| 17 | + this.contentLoaded = false |
| 18 | + |
| 19 | + // Pre-fetch the event card content immediately |
| 20 | + this.prefetchEventCard() |
| 21 | + this.setupPopover() |
| 22 | + } |
| 23 | + |
| 24 | + disconnect() { |
| 25 | + this.cleanupPopover() |
| 26 | + } |
| 27 | + |
| 28 | + async setupPopover() { |
| 29 | + // Setup popover with initial loading content |
| 30 | + this.popover = new bootstrap.Popover(this.element, { |
| 31 | + content: this.getPopoverContent(), |
| 32 | + html: true, |
| 33 | + placement: 'auto', |
| 34 | + fallbackPlacements: ['top', 'bottom', 'right', 'left'], |
| 35 | + trigger: 'manual', // Use manual trigger for better control |
| 36 | + delay: { show: 0, hide: 100 }, // No delay for show since content is pre-fetched |
| 37 | + customClass: 'event-hover-card-popover', |
| 38 | + sanitize: false, |
| 39 | + container: 'body', // Render in body to avoid positioning issues |
| 40 | + boundary: 'viewport', |
| 41 | + offset: [0, 8] // Add some offset from the trigger element |
| 42 | + }) |
| 43 | + |
| 44 | + // Setup manual hover events |
| 45 | + this.setupHoverEvents() |
| 46 | + } |
| 47 | + |
| 48 | + getPopoverContent() { |
| 49 | + if (this.contentLoaded && this.eventCardContent) { |
| 50 | + return this.eventCardContent |
| 51 | + } else { |
| 52 | + return '<div class="text-center py-3"><div class="spinner-border spinner-border-sm" role="status"><span class="visually-hidden">Loading...</span></div><div class="small text-muted mt-2">Loading event details...</div></div>' |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + async prefetchEventCard() { |
| 57 | + const requestUrl = `${this.eventUrlValue}?format=card` |
| 58 | + |
| 59 | + try { |
| 60 | + const response = await fetch(`${this.eventUrlValue}?format=card`, { |
| 61 | + headers: { |
| 62 | + 'Accept': 'text/html', |
| 63 | + 'X-Requested-With': 'XMLHttpRequest', |
| 64 | + 'X-Card-Request': 'true' |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + if (response.ok) { |
| 69 | + const cardHtml = await response.text() |
| 70 | + this.eventCardContent = cardHtml |
| 71 | + this.contentLoaded = true |
| 72 | + |
| 73 | + // Update popover content if it exists and is already shown |
| 74 | + if (this.popover && this.isPopoverVisible) { |
| 75 | + this.updatePopoverContent(cardHtml) |
| 76 | + } |
| 77 | + } else { |
| 78 | + this.eventCardContent = this.generateFallbackContent() |
| 79 | + this.contentLoaded = true |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + console.error('Error pre-fetching event card:', error) |
| 83 | + this.eventCardContent = this.generateFallbackContent() |
| 84 | + this.contentLoaded = true |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + setupHoverEvents() { |
| 89 | + const showPopover = () => { |
| 90 | + // Don't show popover if we're navigating |
| 91 | + if (this.isNavigating) return |
| 92 | + |
| 93 | + clearTimeout(this.hideTimeout) |
| 94 | + this.showTimeout = setTimeout(() => { |
| 95 | + if (!this.isNavigating) { |
| 96 | + this.isPopoverVisible = true |
| 97 | + |
| 98 | + // Update content if it's been loaded since popover creation |
| 99 | + if (this.contentLoaded) { |
| 100 | + this.popover.setContent({ |
| 101 | + '.popover-body': this.eventCardContent |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + this.popover.show() |
| 106 | + } |
| 107 | + }, 300) // Reduced delay since content is pre-fetched |
| 108 | + } |
| 109 | + |
| 110 | + const hidePopover = () => { |
| 111 | + clearTimeout(this.showTimeout) |
| 112 | + |
| 113 | + // Don't hide if we're navigating (cleanup will handle it) |
| 114 | + if (this.isNavigating) return |
| 115 | + |
| 116 | + this.hideTimeout = setTimeout(() => { |
| 117 | + if (!this.isNavigating) { |
| 118 | + this.isPopoverVisible = false |
| 119 | + this.popover.hide() |
| 120 | + } |
| 121 | + }, 100) |
| 122 | + } |
| 123 | + |
| 124 | + // Show on hover |
| 125 | + this.element.addEventListener('mouseenter', showPopover) |
| 126 | + this.element.addEventListener('focus', showPopover) |
| 127 | + |
| 128 | + // Hide when leaving the trigger element |
| 129 | + this.element.addEventListener('mouseleave', hidePopover) |
| 130 | + this.element.addEventListener('blur', hidePopover) |
| 131 | + |
| 132 | + // Keep popover open when hovering over it |
| 133 | + this.element.addEventListener('shown.bs.popover', () => { |
| 134 | + const popoverElement = document.querySelector('.event-hover-card-popover') |
| 135 | + if (popoverElement) { |
| 136 | + popoverElement.addEventListener('mouseenter', () => { |
| 137 | + clearTimeout(this.hideTimeout) |
| 138 | + }) |
| 139 | + popoverElement.addEventListener('mouseleave', hidePopover) |
| 140 | + |
| 141 | + // Intercept link clicks within the popover |
| 142 | + this.setupPopoverLinkInterception(popoverElement) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + // Handle popover hiding |
| 147 | + this.element.addEventListener('hidden.bs.popover', () => { |
| 148 | + this.isPopoverVisible = false |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + setupPopoverLinkInterception(popoverElement) { |
| 153 | + // Find all links within the popover |
| 154 | + const links = popoverElement.querySelectorAll('a[href]') |
| 155 | + |
| 156 | + links.forEach(link => { |
| 157 | + link.addEventListener('click', (event) => { |
| 158 | + // Set a flag to prevent any hover events from interfering |
| 159 | + this.isNavigating = true |
| 160 | + |
| 161 | + // Hide the popover gracefully without disposing immediately |
| 162 | + if (this.popover) { |
| 163 | + this.popover.hide() |
| 164 | + } |
| 165 | + |
| 166 | + // Schedule cleanup after Bootstrap's hide animation completes |
| 167 | + setTimeout(() => { |
| 168 | + this.safeCleanup() |
| 169 | + }, 200) |
| 170 | + |
| 171 | + // Note: We don't preventDefault() here to allow normal navigation |
| 172 | + }) |
| 173 | + }) |
| 174 | + } |
| 175 | + |
| 176 | + safeCleanup() { |
| 177 | + // Clear all timeouts |
| 178 | + if (this.showTimeout) clearTimeout(this.showTimeout) |
| 179 | + if (this.hideTimeout) clearTimeout(this.hideTimeout) |
| 180 | + |
| 181 | + // Reset state |
| 182 | + this.isPopoverVisible = false |
| 183 | + this.isNavigating = false |
| 184 | + |
| 185 | + // Only dispose if popover still exists and is not in transition |
| 186 | + if (this.popover) { |
| 187 | + try { |
| 188 | + this.popover.dispose() |
| 189 | + this.popover = null |
| 190 | + } catch (error) { |
| 191 | + console.warn('Error disposing popover:', error) |
| 192 | + this.popover = null |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Clean up any remaining popover DOM elements |
| 197 | + const remainingPopovers = document.querySelectorAll('.event-hover-card-popover') |
| 198 | + remainingPopovers.forEach(popover => { |
| 199 | + try { |
| 200 | + popover.remove() |
| 201 | + } catch (error) { |
| 202 | + console.warn('Error removing popover element:', error) |
| 203 | + } |
| 204 | + }) |
| 205 | + } |
| 206 | + |
| 207 | + cleanupPopover() { |
| 208 | + // Clear all timeouts |
| 209 | + if (this.showTimeout) clearTimeout(this.showTimeout) |
| 210 | + if (this.hideTimeout) clearTimeout(this.hideTimeout) |
| 211 | + |
| 212 | + // Reset state |
| 213 | + this.isPopoverVisible = false |
| 214 | + |
| 215 | + // Hide and dispose popover safely |
| 216 | + if (this.popover) { |
| 217 | + try { |
| 218 | + this.popover.hide() |
| 219 | + // Delay disposal to allow hide animation to complete |
| 220 | + setTimeout(() => { |
| 221 | + if (this.popover) { |
| 222 | + try { |
| 223 | + this.popover.dispose() |
| 224 | + this.popover = null |
| 225 | + } catch (error) { |
| 226 | + console.warn('Error disposing popover:', error) |
| 227 | + this.popover = null |
| 228 | + } |
| 229 | + } |
| 230 | + }, 150) |
| 231 | + } catch (error) { |
| 232 | + console.warn('Error hiding popover:', error) |
| 233 | + this.popover = null |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + // Clean up any remaining popover DOM elements |
| 238 | + setTimeout(() => { |
| 239 | + const remainingPopovers = document.querySelectorAll('.event-hover-card-popover') |
| 240 | + remainingPopovers.forEach(popover => { |
| 241 | + try { |
| 242 | + popover.remove() |
| 243 | + } catch (error) { |
| 244 | + console.warn('Error removing popover element:', error) |
| 245 | + } |
| 246 | + }) |
| 247 | + }, 200) |
| 248 | + } |
| 249 | + |
| 250 | + updatePopoverContent(content) { |
| 251 | + if (this.popover) { |
| 252 | + this.popover.setContent({ |
| 253 | + '.popover-body': content |
| 254 | + }) |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + generateFallbackContent() { |
| 259 | + return ` |
| 260 | + <div class="event-hover-card"> |
| 261 | + <div class="text-center"> |
| 262 | + <i class="fas fa-exclamation-triangle text-warning"></i> |
| 263 | + <p class="mb-0 small">Unable to load event details</p> |
| 264 | + <a href="${this.eventUrlValue}" class="btn btn-sm btn-outline-primary mt-2"> |
| 265 | + <i class="fas fa-eye me-1"></i> View Event |
| 266 | + </a> |
| 267 | + </div> |
| 268 | + </div> |
| 269 | + ` |
| 270 | + } |
| 271 | +} |
0 commit comments