11df9c3589
Three changes needed to support navigating back from a card to the activity page: - Add root_path to the prefer_referrer allowlist on the card show page - Switch event links from HTML target="_top" to data-turbo-frame="_top" so Turbo handles the navigation and turbo:before-visit fires to save the referrer - Normalize trailing slashes in the referrer path comparison so /account_id and /account_id/ both match ref: https://app.fizzy.do/5986089/cards/2390
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
|
|
export default class extends Controller {
|
|
static values = { label: String }
|
|
static targets = [ "referrerBackLink" ]
|
|
|
|
rememberLocation() {
|
|
sessionStorage.setItem("referrerUrl", window.location.href)
|
|
sessionStorage.setItem("referrerLabel", this.labelValue)
|
|
}
|
|
|
|
backIfSamePath(event) {
|
|
if (event.ctrlKey || event.metaKey || event.shiftKey) { return }
|
|
|
|
const link = event.target.closest("a")
|
|
const targetUrl = new URL(link.href)
|
|
|
|
if (this.#referrerPath && targetUrl.pathname === this.#referrerPath) {
|
|
event.preventDefault()
|
|
Turbo.visit(this.#referrerUrl)
|
|
}
|
|
}
|
|
|
|
referrerBackLinkTargetConnected(link) {
|
|
if (!this.#referrerUrl || !this.#referrerLabel) { return }
|
|
|
|
const stripTrailingSlash = path => path.replace(/\/$/, "")
|
|
const allowedPaths = (link.dataset.turboNavigationAllowedReferrerPaths || "").split(",").map(stripTrailingSlash)
|
|
const referrerPath = stripTrailingSlash(new URL(this.#referrerUrl).pathname)
|
|
if (!allowedPaths.includes(referrerPath)) { return }
|
|
|
|
link.href = this.#referrerUrl
|
|
const strong = link.querySelector("strong")
|
|
if (strong) { strong.textContent = `Back to ${this.#referrerLabel}` }
|
|
}
|
|
|
|
get #referrerPath() {
|
|
if (!this.#referrerUrl) return null
|
|
return new URL(this.#referrerUrl).pathname
|
|
}
|
|
|
|
get #referrerUrl() {
|
|
return sessionStorage.getItem("referrerUrl")
|
|
}
|
|
|
|
get #referrerLabel() {
|
|
return sessionStorage.getItem("referrerLabel")
|
|
}
|
|
}
|