-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
516 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ end | |
group :test do | ||
gem "capybara" | ||
gem "cuprite", github: "rubycdp/cuprite" | ||
gem "rails-controller-testing" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/assets/javascripts/controllers/kpop/private_url_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Visit strategies | ||
// 1. User closes the modal => pop history | ||
// 2. User clicks a turbo link => replace history | ||
// 3. User clicks a external link => ignore history | ||
// 4. User clicks a back button => close modal but don't pop history | ||
// 5. User refreshes the page => pop history??? maybe? | ||
export default class Kpop__PrivateUrlController extends Controller { | ||
connect() { | ||
this.element.toggleAttribute("data-turbo-temporary", this.temporaryValue); | ||
|
||
// Push a new history entry. This will be popped or replaced when the modal is dismissed. | ||
window.history.pushState({}, "", window.location); | ||
|
||
// Capture pops so we can dismiss the modal. | ||
window.addEventListener("popstate", this.popstate); | ||
|
||
// Capture visits so we can replace history. | ||
window.addEventListener("turbo:before-visit", this.beforeVisit); | ||
} | ||
|
||
disconnect() { | ||
window.removeEventListener("popstate", this.popstate); | ||
window.removeEventListener("turbo:before-visit", this.beforeVisit); | ||
|
||
if (!this.element.dataset.popped) { | ||
window.history.back(); | ||
} | ||
} | ||
|
||
popstate = () => { | ||
this.element.dataset.popped = "true"; | ||
this.element.remove(); | ||
}; | ||
|
||
beforeVisit = (e) => { | ||
if (this.element.dataset.popped) return; | ||
|
||
e.preventDefault(); | ||
this.element.dataset.popped = "true"; | ||
Turbo.visit(e.detail.url, { action: "replace" }); | ||
}; | ||
} |
43 changes: 43 additions & 0 deletions
43
app/assets/javascripts/controllers/kpop/public_url_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Visit strategies | ||
// 1. User closes the modal => pop history | ||
// 2. User clicks a turbo link => replace history | ||
// 3. User clicks a external link => ignore history | ||
// 4. User clicks a back button => close modal but don't pop history | ||
// 5. User refreshes the page => pop history??? maybe? | ||
export default class Kpop__PublicUrlController extends Controller { | ||
static values = { | ||
popped: Boolean, | ||
}; | ||
|
||
connect() { | ||
// Capture visits so we can replace history. | ||
window.addEventListener("turbo:before-visit", this.beforeVisit); | ||
|
||
// Capture pops so we can avoid duplicate pops. | ||
window.addEventListener("popstate", this.popstate, { once: true }); | ||
} | ||
|
||
disconnect() { | ||
window.removeEventListener("turbo:before-visit", this.beforeVisit); | ||
|
||
if (!this.element.dataset.popped) { | ||
this.element.dataset.popped = "true"; | ||
window.history.back(); | ||
} | ||
} | ||
|
||
beforeVisit = (e) => { | ||
if (this.element.dataset.popped) return; | ||
if (e.detail.url === this.element.closest("turbo-frame").src) return; | ||
|
||
e.preventDefault(); | ||
this.element.dataset.popped = "true"; | ||
Turbo.visit(e.detail.url, { action: "replace" }); | ||
}; | ||
|
||
popstate = () => { | ||
this.element.dataset.popped = "true"; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "kpop/matchers" | ||
|
||
module Kpop | ||
module Matchers | ||
class RedirectFinder < CapybaraMatcher | ||
def initialize | ||
super("[data-controller='kpop--redirect']") | ||
end | ||
end | ||
|
||
# @api private | ||
class RedirectMatcher < BaseMatcher | ||
def match(expected, actual) | ||
actual["data-kpop--redirect-path-value"].to_s.match?(expected) | ||
end | ||
|
||
def description | ||
"kpop redirect to #{expected.inspect}" | ||
end | ||
|
||
def failure_message | ||
"expected a kpop redirect to #{expected.inspect} but received #{actual.native.to_html.inspect} instead" | ||
end | ||
|
||
def failure_message_when_negated | ||
"expected not to find a kpop redirect to #{expected.inspect}" | ||
end | ||
end | ||
|
||
# @api public | ||
# Passes if `response` contains a turbo response with a kpop dismiss action. | ||
# | ||
# @example | ||
# expect(response).to kpop_dismiss | ||
def kpop_dismiss(id: "kpop") | ||
ChainedMatcher.new(ResponseMatcher.new, | ||
CapybaraParser, | ||
StreamMatcher.new(id:, action: "update")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.