-
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
21 changed files
with
486 additions
and
88 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
65 changes: 64 additions & 1 deletion
65
app/assets/javascripts/controllers/kpop/modal_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 |
---|---|---|
@@ -1,12 +1,75 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
const DEBUG = false; | ||
|
||
export default class Kpop__ModalController extends Controller { | ||
static values = { | ||
temporary: { type: Boolean, default: true }, | ||
}; | ||
|
||
get scrimConfig() { | ||
return { temporary: this.temporaryValue }; | ||
} | ||
|
||
connect() { | ||
// Set the modal content to temporary to ensure its omitted when caching the page | ||
if (DEBUG) console.debug("modal 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. | ||
// Persistent modals push a history entry via Turbo. | ||
if (this.temporaryValue) { | ||
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() { | ||
if (DEBUG) console.debug("modal disconnect"); | ||
|
||
window.removeEventListener("popstate", this.popstate); | ||
window.removeEventListener("turbo:before-visit", this.beforeVisit); | ||
|
||
if (!this.popped) { | ||
this.dismiss(); | ||
} | ||
} | ||
|
||
dismiss() { | ||
if (DEBUG) console.debug("modal dismiss"); | ||
|
||
if (!this.popped) { | ||
this.popped = true; | ||
window.history.back(); | ||
} | ||
} | ||
|
||
popstate = () => { | ||
this.popped = true; | ||
|
||
if (DEBUG) console.debug("modal popped"); | ||
|
||
// Remove the modal from the DOM if it's a temporary modal. | ||
if (this.temporaryValue) { | ||
this.element.remove(); | ||
} | ||
}; | ||
|
||
beforeVisit = (e) => { | ||
if (this.popped) return; | ||
|
||
// visit fires when persistent modals load, so we need to ignore those. | ||
if (e.detail.url === this.element.closest("turbo-frame").src) return; | ||
|
||
if (DEBUG) console.debug("visiting", e.detail.url); | ||
|
||
e.preventDefault(); | ||
this.popped = true; | ||
Turbo.visit(e.detail.url, { action: "replace" }); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
class HomeController < ApplicationController | ||
def index; end | ||
|
||
def show; end | ||
def test; end | ||
end |
Oops, something went wrong.