70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { post } from "@rails/request.js"
|
|
|
|
export default class extends Controller {
|
|
static targets = [ "turboFrame", "searchInput", "askInput", "buttonsContainer" ]
|
|
static outlets = [ "dialog" ]
|
|
static values = {
|
|
searchUrl: String,
|
|
askUrl: String,
|
|
}
|
|
|
|
dialogOutletConnected(outlet, element) {
|
|
outlet.close()
|
|
this.#clearTurboFrame()
|
|
}
|
|
|
|
reset() {
|
|
this.dialogOutlet.close()
|
|
this.#clearTurboFrame()
|
|
|
|
this.#showItem(this.buttonsContainerTarget)
|
|
this.#hideItem(this.askInputTarget)
|
|
this.#hideItem(this.searchInputTarget)
|
|
}
|
|
|
|
showModal() {
|
|
this.dialogOutlet.open()
|
|
}
|
|
|
|
search(event) {
|
|
this.#openInTurboFrame("search", this.searchUrlValue)
|
|
|
|
this.#showItem(this.searchInputTarget)
|
|
this.#hideItem(this.askInputTarget)
|
|
this.#hideItem(this.buttonsContainerTarget)
|
|
}
|
|
|
|
ask(event) {
|
|
this.#initializeConversation()
|
|
this.#openInTurboFrame("conversation", this.askUrlValue)
|
|
|
|
this.#showItem(this.askInputTarget)
|
|
this.#hideItem(this.searchInputTarget)
|
|
this.#hideItem(this.buttonsContainerTarget)
|
|
}
|
|
|
|
#clearTurboFrame() {
|
|
this.turboFrameTarget.removeAttribute("id")
|
|
this.turboFrameTarget.removeAttribute("src")
|
|
this.turboFrameTarget.innerHtml = ""
|
|
}
|
|
|
|
#openInTurboFrame(name, url) {
|
|
this.turboFrameTarget.id = name
|
|
this.turboFrameTarget.src = url
|
|
}
|
|
|
|
#initializeConversation() {
|
|
post(this.askUrlValue)
|
|
}
|
|
|
|
#showItem(element) {
|
|
element.removeAttribute("hidden")
|
|
}
|
|
|
|
#hideItem(element) {
|
|
element.setAttribute("hidden", "hidden")
|
|
}
|
|
}
|