|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +export default class extends Controller { |
| 4 | + static targets = ["search", "perPage"] |
| 5 | + static values = { |
| 6 | + src: String, |
| 7 | + sortColumn: String, |
| 8 | + sortDirection: String, |
| 9 | + page: { type: Number, default: 1 }, |
| 10 | + perPage: { type: Number, default: 10 }, |
| 11 | + searchQuery: String, |
| 12 | + debounceMs: { type: Number, default: 300 } |
| 13 | + } |
| 14 | + |
| 15 | + connect() { |
| 16 | + this.searchTimeout = null |
| 17 | + } |
| 18 | + |
| 19 | + disconnect() { |
| 20 | + if (this.searchTimeout) clearTimeout(this.searchTimeout) |
| 21 | + } |
| 22 | + |
| 23 | + sort(event) { |
| 24 | + const { column, direction } = event.params |
| 25 | + this.sortColumnValue = column |
| 26 | + this.sortDirectionValue = direction || "" |
| 27 | + this.pageValue = 1 |
| 28 | + this._reload() |
| 29 | + } |
| 30 | + |
| 31 | + search() { |
| 32 | + if (this.searchTimeout) clearTimeout(this.searchTimeout) |
| 33 | + this.searchTimeout = setTimeout(() => { |
| 34 | + this.searchQueryValue = this.searchTarget.value |
| 35 | + this.pageValue = 1 |
| 36 | + this._reload() |
| 37 | + }, this.debounceMsValue) |
| 38 | + } |
| 39 | + |
| 40 | + nextPage() { |
| 41 | + this.pageValue += 1 |
| 42 | + this._reload() |
| 43 | + } |
| 44 | + |
| 45 | + previousPage() { |
| 46 | + if (this.pageValue > 1) { |
| 47 | + this.pageValue -= 1 |
| 48 | + this._reload() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + changePerPage() { |
| 53 | + this.perPageValue = parseInt(this.perPageTarget.value) |
| 54 | + this.pageValue = 1 |
| 55 | + this._reload() |
| 56 | + } |
| 57 | + |
| 58 | + _reload() { |
| 59 | + if (!this.hasSrcValue || !this.srcValue) return |
| 60 | + |
| 61 | + const url = new URL(this.srcValue, window.location.origin) |
| 62 | + if (this.sortColumnValue) url.searchParams.set("sort", this.sortColumnValue) |
| 63 | + if (this.sortDirectionValue) url.searchParams.set("direction", this.sortDirectionValue) |
| 64 | + if (this.searchQueryValue) url.searchParams.set("search", this.searchQueryValue) |
| 65 | + url.searchParams.set("page", this.pageValue) |
| 66 | + url.searchParams.set("per_page", this.perPageValue) |
| 67 | + |
| 68 | + // Use Turbo to fetch and replace the content frame |
| 69 | + const frame = this.element.querySelector("turbo-frame") |
| 70 | + if (frame) { |
| 71 | + frame.src = url.toString() |
| 72 | + } else { |
| 73 | + // Fallback: dispatch custom event for consumer to handle |
| 74 | + this.dispatch("navigate", { detail: { url: url.toString() } }) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments