|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +export default class extends Controller { |
| 4 | + static targets = ["input", "dropdown", "container"] |
| 5 | + static values = { |
| 6 | + searchUrl: String, |
| 7 | + createUrl: String, |
| 8 | + agendaItemId: Number |
| 9 | + } |
| 10 | + |
| 11 | + connect() { |
| 12 | + this.selectedIndex = -1 |
| 13 | + this.debounceTimer = null |
| 14 | + this.handleClickOutside = this.handleClickOutside.bind(this) |
| 15 | + document.addEventListener("click", this.handleClickOutside) |
| 16 | + } |
| 17 | + |
| 18 | + disconnect() { |
| 19 | + document.removeEventListener("click", this.handleClickOutside) |
| 20 | + if (this.debounceTimer) clearTimeout(this.debounceTimer) |
| 21 | + } |
| 22 | + |
| 23 | + onInput() { |
| 24 | + if (this.debounceTimer) clearTimeout(this.debounceTimer) |
| 25 | + this.debounceTimer = setTimeout(() => this.search(), 200) |
| 26 | + } |
| 27 | + |
| 28 | + onKeydown(event) { |
| 29 | + const items = this.dropdownTarget.querySelectorAll("li") |
| 30 | + |
| 31 | + switch (event.key) { |
| 32 | + case "ArrowDown": |
| 33 | + event.preventDefault() |
| 34 | + this.selectedIndex = Math.min(this.selectedIndex + 1, items.length - 1) |
| 35 | + this.highlightItem(items) |
| 36 | + break |
| 37 | + case "ArrowUp": |
| 38 | + event.preventDefault() |
| 39 | + this.selectedIndex = Math.max(this.selectedIndex - 1, 0) |
| 40 | + this.highlightItem(items) |
| 41 | + break |
| 42 | + case "Enter": |
| 43 | + event.preventDefault() |
| 44 | + if (this.selectedIndex >= 0 && items[this.selectedIndex]) { |
| 45 | + items[this.selectedIndex].click() |
| 46 | + } |
| 47 | + break |
| 48 | + case "Escape": |
| 49 | + this.hideDropdown() |
| 50 | + break |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + async search() { |
| 55 | + const query = this.inputTarget.value.trim() |
| 56 | + if (query.length === 0) { |
| 57 | + this.hideDropdown() |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + const response = await fetch(`${this.searchUrlValue}?q=${encodeURIComponent(query)}`) |
| 62 | + const tags = await response.json() |
| 63 | + |
| 64 | + this.renderDropdown(tags, query) |
| 65 | + } |
| 66 | + |
| 67 | + renderDropdown(tags, query) { |
| 68 | + this.dropdownTarget.innerHTML = "" |
| 69 | + this.selectedIndex = -1 |
| 70 | + |
| 71 | + const exactMatch = tags.some(t => t.name.toLowerCase() === query.toLowerCase()) |
| 72 | + |
| 73 | + tags.forEach(tag => { |
| 74 | + const li = document.createElement("li") |
| 75 | + li.textContent = tag.name |
| 76 | + li.className = "px-3 py-1.5 cursor-pointer hover:bg-indigo-50" |
| 77 | + li.addEventListener("click", () => this.selectTag(tag.name)) |
| 78 | + this.dropdownTarget.appendChild(li) |
| 79 | + }) |
| 80 | + |
| 81 | + if (!exactMatch && query.length > 0) { |
| 82 | + const li = document.createElement("li") |
| 83 | + li.innerHTML = `Create <em>${this.escapeHtml(query)}</em>` |
| 84 | + li.className = "px-3 py-1.5 cursor-pointer hover:bg-indigo-50 text-indigo-600 border-t border-gray-100" |
| 85 | + li.addEventListener("click", () => this.selectTag(query)) |
| 86 | + this.dropdownTarget.appendChild(li) |
| 87 | + } |
| 88 | + |
| 89 | + if (this.dropdownTarget.children.length > 0) { |
| 90 | + this.showDropdown() |
| 91 | + } else { |
| 92 | + this.hideDropdown() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + highlightItem(items) { |
| 97 | + items.forEach((item, index) => { |
| 98 | + item.classList.toggle("bg-indigo-50", index === this.selectedIndex) |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + async selectTag(tagName) { |
| 103 | + this.hideDropdown() |
| 104 | + this.inputTarget.value = "" |
| 105 | + |
| 106 | + const formData = new FormData() |
| 107 | + formData.append("tag_name", tagName) |
| 108 | + formData.append("agenda_item_id", this.agendaItemIdValue) |
| 109 | + |
| 110 | + const response = await fetch(this.createUrlValue, { |
| 111 | + method: "POST", |
| 112 | + headers: { |
| 113 | + "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content, |
| 114 | + "Accept": "text/vnd.turbo-stream.html" |
| 115 | + }, |
| 116 | + body: formData |
| 117 | + }) |
| 118 | + |
| 119 | + if (response.ok) { |
| 120 | + const html = await response.text() |
| 121 | + window.Turbo.renderStreamMessage(html) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + showDropdown() { |
| 126 | + this.dropdownTarget.classList.remove("hidden") |
| 127 | + } |
| 128 | + |
| 129 | + hideDropdown() { |
| 130 | + this.dropdownTarget.classList.add("hidden") |
| 131 | + this.selectedIndex = -1 |
| 132 | + } |
| 133 | + |
| 134 | + handleClickOutside(event) { |
| 135 | + if (this.hasContainerTarget && !this.containerTarget.contains(event.target)) { |
| 136 | + this.hideDropdown() |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + escapeHtml(text) { |
| 141 | + const div = document.createElement("div") |
| 142 | + div.textContent = text |
| 143 | + return div.innerHTML |
| 144 | + } |
| 145 | +} |
0 commit comments