demarches-normaliennes/app/helpers/turbo_stream_helper.rb

55 lines
1.4 KiB
Ruby
Raw Normal View History

2022-04-14 20:46:28 +02:00
module TurboStreamHelper
def turbo_stream
TagBuilder.new(self)
end
class TagBuilder < Turbo::Streams::TagBuilder
include ActionView::Helpers::TagHelper
2022-04-14 20:46:28 +02:00
def show(target, delay: nil)
2023-01-16 21:31:07 +01:00
turbo_stream_action_tag :show, target:, delay:
2022-04-14 20:46:28 +02:00
end
def show_all(targets, delay: nil)
2023-01-16 21:31:07 +01:00
turbo_stream_action_tag :show, targets:, delay:
2022-04-14 20:46:28 +02:00
end
def hide(target, delay: nil)
2023-01-16 21:31:07 +01:00
turbo_stream_action_tag :hide, target:, delay:
2022-04-14 20:46:28 +02:00
end
def hide_all(targets, delay: nil)
2023-01-16 21:31:07 +01:00
turbo_stream_action_tag :hide, targets:, delay:
2022-04-14 20:46:28 +02:00
end
2023-01-16 21:31:07 +01:00
def focus(target, delay: nil)
turbo_stream_action_tag :focus, target:, delay:
2022-04-14 20:46:28 +02:00
end
2023-01-16 21:31:07 +01:00
def focus_all(targets, delay: nil)
turbo_stream_action_tag :focus, targets:, delay:
end
2023-01-16 21:31:07 +01:00
def enable(target, delay: nil)
turbo_stream_action_tag :enable, target:, delay:
end
2023-01-16 21:31:07 +01:00
def enable_all(targets, delay: nil)
turbo_stream_action_tag :enable, targets:, delay:
2022-04-14 20:46:28 +02:00
end
2023-01-16 21:31:07 +01:00
def disable(target, delay: nil)
turbo_stream_action_tag :disable, target:, delay:
end
2023-01-16 21:31:07 +01:00
def disable_all(targets, delay: nil)
turbo_stream_action_tag :disable, targets:, delay:
end
def dispatch(type, detail = nil)
content = detail.present? ? tag.script(cdata_section(detail.to_json), type: 'application/json') : nil
action_all :append, 'head', tag.dispatch_event(content, type:)
end
2022-04-14 20:46:28 +02:00
end
end