diff --git a/app/javascript/kpop/application.js b/app/javascript/kpop/application.js index 991d5bc..bd2967f 100644 --- a/app/javascript/kpop/application.js +++ b/app/javascript/kpop/application.js @@ -1,14 +1,12 @@ -import CloseController from "../kpop/controllers/close_controller"; import FrameController from "../kpop/controllers/frame_controller"; import ModalController from "../kpop/controllers/modal_controller"; -import RedirectController from "../kpop/controllers/redirect_controller"; import ScrimController from "../kpop/controllers/scrim_controller"; +import "./turbo_actions"; + const Definitions = [ - { identifier: "kpop--close", controllerConstructor: CloseController }, { identifier: "kpop--frame", controllerConstructor: FrameController }, { identifier: "kpop--modal", controllerConstructor: ModalController }, - { identifier: "kpop--redirect", controllerConstructor: RedirectController }, { identifier: "scrim", controllerConstructor: ScrimController }, ]; diff --git a/app/javascript/kpop/controllers/close_controller.js b/app/javascript/kpop/controllers/close_controller.js deleted file mode 100644 index 93093ac..0000000 --- a/app/javascript/kpop/controllers/close_controller.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Controller } from "@hotwired/stimulus"; - -export default class Kpop__CloseController extends Controller { - static outlets = ["kpop--frame"]; - - kpopFrameOutletConnected(frame) { - frame.dismiss(); - } -} diff --git a/app/javascript/kpop/controllers/frame_controller.js b/app/javascript/kpop/controllers/frame_controller.js index c3622c9..92482b4 100644 --- a/app/javascript/kpop/controllers/frame_controller.js +++ b/app/javascript/kpop/controllers/frame_controller.js @@ -1,25 +1,8 @@ import { Controller } from "@hotwired/stimulus"; -import { Turbo } from "@hotwired/turbo-rails"; import DEBUG from "../debug"; import { ContentModal } from "../modals/content_modal"; import { FrameModal } from "../modals/frame_modal"; -import { StreamModal } from "../modals/stream_modal"; -import { StreamRenderer } from "../modals/stream_renderer"; - -Turbo.StreamActions.kpop_open = function () { - const frame = () => { - return this.targetElements[0]; - }; - const animate = !frame?.kpop?.openValue; - - frame() - .kpop.dismiss({ animate, reason: "before-turbo-stream" }) - .then(() => { - new StreamRenderer(frame(), this).render(); - frame().kpop.open(new StreamModal(this.target, this), { animate }); - }); -}; export default class Kpop__FrameController extends Controller { static outlets = ["scrim"]; diff --git a/app/javascript/kpop/controllers/redirect_controller.js b/app/javascript/kpop/controllers/redirect_controller.js deleted file mode 100644 index 48bb317..0000000 --- a/app/javascript/kpop/controllers/redirect_controller.js +++ /dev/null @@ -1,22 +0,0 @@ -import { Controller } from "@hotwired/stimulus"; -import { Turbo } from "@hotwired/turbo-rails"; - -export default class Kpop__RedirectController extends Controller { - static outlets = ["kpop--frame"]; - static values = { - path: String, - target: String, - }; - - kpopFrameOutletConnected(frame) { - if (this.targetValue === frame.element.id) { - frame.dismiss().then(() => { - document.getElementById(this.targetValue).src = this.pathValue; - }); - } else { - Turbo.visit(this.pathValue, { action: "replace" }); - } - - this.element.remove(); - } -} diff --git a/app/javascript/kpop/turbo_actions.js b/app/javascript/kpop/turbo_actions.js new file mode 100644 index 0000000..71ddb8f --- /dev/null +++ b/app/javascript/kpop/turbo_actions.js @@ -0,0 +1,33 @@ +import { Turbo } from "@hotwired/turbo-rails"; + +import { StreamModal } from "./modals/stream_modal"; +import { StreamRenderer } from "./utils/stream_renderer"; + +function kpop(action) { + return action.targetElements[0]?.kpop; +} + +Turbo.StreamActions.kpop_open = function () { + const animate = !kpop(this).openValue; + + kpop(this) + ?.dismiss({ animate, reason: "before-turbo-stream" }) + .then(() => { + new StreamRenderer(this.targetElements[0], this).render(); + kpop(this)?.open(new StreamModal(this.target, this), { animate }); + }); +}; + +Turbo.StreamActions.kpop_dismiss = function () { + kpop(this)?.dismiss({ reason: "turbo_stream.kpop.dismiss" }); +}; + +Turbo.StreamActions.kpop_redirect_to = function () { + if (this.dataset.turboFrame === this.target) { + this.targetElements[0].src = this.getAttribute("href"); + } else { + Turbo.visit(this.getAttribute("href"), { + action: this.dataset.turboAction, + }); + } +}; diff --git a/app/javascript/kpop/modals/stream_renderer.js b/app/javascript/kpop/utils/stream_renderer.js similarity index 100% rename from app/javascript/kpop/modals/stream_renderer.js rename to app/javascript/kpop/utils/stream_renderer.js diff --git a/lib/katalyst/kpop/matchers.rb b/lib/katalyst/kpop/matchers.rb index 9adf684..4cbc8cb 100644 --- a/lib/katalyst/kpop/matchers.rb +++ b/lib/katalyst/kpop/matchers.rb @@ -6,7 +6,6 @@ require "katalyst/kpop/matchers/chained_matcher" require "katalyst/kpop/matchers/frame_matcher" require "katalyst/kpop/matchers/modal_matcher" -require "katalyst/kpop/matchers/redirect_finder" require "katalyst/kpop/matchers/redirect_matcher" require "katalyst/kpop/matchers/response_matcher" require "katalyst/kpop/matchers/stream_matcher" @@ -22,7 +21,9 @@ module Matchers # @example # expect(response).to kpop_dismiss def kpop_dismiss(id: "kpop") - ChainedMatcher.new(ResponseMatcher, CapybaraParser, StreamMatcher.new(id:, action: "append")) + ChainedMatcher.new(ResponseMatcher, + CapybaraParser, + StreamMatcher.new(id:, action: "kpop_dismiss")) end # @api public @@ -36,8 +37,7 @@ def kpop_redirect_to(target, id: "kpop") ChainedMatcher.new(ResponseMatcher, CapybaraParser, - StreamMatcher.new(id:, action: "append"), - RedirectFinder, + StreamMatcher.new(id:, action: "kpop_redirect_to"), RedirectMatcher.new(target)) end diff --git a/lib/katalyst/kpop/matchers/redirect_finder.rb b/lib/katalyst/kpop/matchers/redirect_finder.rb deleted file mode 100644 index cf4ec64..0000000 --- a/lib/katalyst/kpop/matchers/redirect_finder.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require "katalyst/kpop/matchers/capybara_matcher" - -module Katalyst - module Kpop - module Matchers - # @api private - class RedirectFinder < CapybaraMatcher - def initialize - super("[data-controller='kpop--redirect']") - end - end - end - end -end diff --git a/lib/katalyst/kpop/matchers/redirect_matcher.rb b/lib/katalyst/kpop/matchers/redirect_matcher.rb index 503272e..b780f78 100644 --- a/lib/katalyst/kpop/matchers/redirect_matcher.rb +++ b/lib/katalyst/kpop/matchers/redirect_matcher.rb @@ -8,7 +8,7 @@ module Matchers # @api private class RedirectMatcher < Base def match(expected, actual) - actual["data-kpop--redirect-path-value"].to_s.match?(expected) + actual["href"].to_s.match?(expected) end def description diff --git a/lib/katalyst/kpop/turbo.rb b/lib/katalyst/kpop/turbo.rb index 1cf1235..91cbcc1 100644 --- a/lib/katalyst/kpop/turbo.rb +++ b/lib/katalyst/kpop/turbo.rb @@ -6,7 +6,7 @@ module Katalyst module Kpop module Turbo class TagBuilder - delegate :action, :append, :tag, to: :@builder + delegate :action, :turbo_stream_action_tag, to: :@builder def initialize(builder) @builder = builder @@ -24,31 +24,25 @@ def initialize(builder) # <% end %> # <% end %> def open(content = nil, id: "kpop", **, &) - @builder.action(:kpop_open, id, content, **, &) + action(:kpop_open, id, content, **, &) end # Render a turbo stream action that will dismiss any open kpop modal. def dismiss(id: "kpop") - append(id) do - tag.div("", data: { - controller: "kpop--close", - kpop__close_kpop__frame_outlet: "##{id}", - turbo_temporary: "", - }) - end + turbo_stream_action_tag(:kpop_dismiss, target: id) end # Renders a kpop redirect controller response that will escape the frame and navigate to the given URL. - def redirect_to(url, id: "kpop", target: nil) - append(id) do - tag.div("", data: { - controller: "kpop--redirect", - kpop__redirect_kpop__frame_outlet: "##{id}", - kpop__redirect_path_value: url, - kpop__redirect_target_value: target, - turbo_temporary: "", - }) - end + def redirect_to(href, id: "kpop", action: "replace", target: nil) + turbo_stream_action_tag( + :kpop_redirect_to, + target: id, + href:, + data: { + turbo_action: action, + turbo_frame: target, + }, + ) end end end