|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | +import { initFlowbite, initCarousels, initDropdowns } from "flowbite" |
| 3 | + |
| 4 | +/** |
| 5 | + * Flowbite Controller |
| 6 | + * |
| 7 | + * Initializes Flowbite UI components and handles re-initialization |
| 8 | + * after Turbo navigation. Only Carousel and Dropdown components |
| 9 | + * are actively used in this application. |
| 10 | + * |
| 11 | + * Usage: Add data-controller="flowbite" to the body or a parent element |
| 12 | + * that contains Flowbite components. |
| 13 | + */ |
| 14 | +export default class extends Controller { |
| 15 | + connect() { |
| 16 | + this.initializeFlowbite() |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Initialize Flowbite components. |
| 21 | + * Called on initial page load and after Turbo navigation. |
| 22 | + */ |
| 23 | + initializeFlowbite() { |
| 24 | + // Initialize all Flowbite components |
| 25 | + // This handles data-carousel-*, data-dropdown-*, etc. |
| 26 | + if (typeof initFlowbite === 'function') { |
| 27 | + initFlowbite() |
| 28 | + } else { |
| 29 | + // Fallback: initialize specific components we use |
| 30 | + this.initializeCarousels() |
| 31 | + this.initializeDropdowns() |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Initialize carousel components specifically. |
| 37 | + * Used for property image galleries. |
| 38 | + */ |
| 39 | + initializeCarousels() { |
| 40 | + if (typeof initCarousels === 'function') { |
| 41 | + initCarousels() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Initialize dropdown components specifically. |
| 47 | + * Used for search form select menus. |
| 48 | + */ |
| 49 | + initializeDropdowns() { |
| 50 | + if (typeof initDropdowns === 'function') { |
| 51 | + initDropdowns() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Re-initialize after Turbo renders new content. |
| 57 | + * This handles both full page navigations and partial updates. |
| 58 | + */ |
| 59 | + turboLoad() { |
| 60 | + this.initializeFlowbite() |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Re-initialize after Turbo Stream updates. |
| 65 | + */ |
| 66 | + turboFrameLoad() { |
| 67 | + this.initializeFlowbite() |
| 68 | + } |
| 69 | +} |
0 commit comments