Compare commits

...

2 Commits

Author SHA1 Message Date
pvincent a6d3713912 remote ok 2 months ago
pvincent 95b2fb4c3a remote_dialog part1 2 months ago
  1. 2
      app/controllers/edge_controller.rb
  2. 5
      app/helpers/turbo_stream_actions_helper.rb
  3. 20
      app/javascript/initializers/forms.js
  4. 9
      app/javascript/initializers/stream_actions.js
  5. 8
      app/views/edge/_turbo_edit.html.erb
  6. 2
      app/views/edge/index.html.erb
  7. 2
      app/views/layouts/application.html.erb
  8. 7
      app/views/layouts/components/_dialog.html.erb
  9. 22
      app/views/layouts/components/_dialogs.html.erb
  10. 29
      app/views/layouts/components/_main_dialog.html.erb

2
app/controllers/edge_controller.rb

@ -2,6 +2,6 @@ class EdgeController < ApplicationController
def index; end def index; end
def turbo_edit def turbo_edit
render turbo_stream: turbo_stream.console_log(text: 'Hello world server-side')
render turbo_stream: turbo_stream.remote_dialog(title: 'my remote title', partial: 'turbo_edit')
end end
end end

5
app/helpers/turbo_stream_actions_helper.rb

@ -2,6 +2,11 @@ module TurboStreamActionsHelper
def console_log(text) def console_log(text)
turbo_stream_action_tag :console_log, text: text turbo_stream_action_tag :console_log, text: text
end end
def remote_dialog(content = nil, title:, id: 'remote-dialog', **rendering, &)
template = render_template(nil, content, **rendering, &)
turbo_stream_action_tag :remoteDialog, dialogId: id, dialogTitle: title, template:
end
end end
Turbo::Streams::TagBuilder.prepend(TurboStreamActionsHelper) Turbo::Streams::TagBuilder.prepend(TurboStreamActionsHelper)

20
app/javascript/initializers/forms.js

@ -1,6 +1,7 @@
/** the confirm action opens the 'front-dialog' by default, customize if needed! */ /** the confirm action opens the 'front-dialog' by default, customize if needed! */
Turbo.config.forms.confirm_dialog_id = 'front-dialog' Turbo.config.forms.confirm_dialog_id = 'front-dialog'
Turbo.config.forms.confirm_dialog_title_selector = '[data-dialog-title]'
Turbo.config.forms.confirm_dialog_content_selector = '[data-dialog-content]' Turbo.config.forms.confirm_dialog_content_selector = '[data-dialog-content]'
/** content might be either a String or a Boolean /** content might be either a String or a Boolean
@ -27,14 +28,29 @@ Turbo.config.forms.confirm = (content) => {
* *
* in case of True: default dialog gets selected, (ie `Turbo.config.forms.confirm_dialog_id`) * in case of True: default dialog gets selected, (ie `Turbo.config.forms.confirm_dialog_id`)
*/ */
Turbo.config.forms.showModal = (dialogId) => {
Turbo.config.forms.showModal = (dialogId, dialogTitle, dialogContent) => {
if (dialogId.toString() == 'false') return if (dialogId.toString() == 'false') return
if (dialogId.toString() == 'true') dialogId = Turbo.config.forms.confirm_dialog_id if (dialogId.toString() == 'true') dialogId = Turbo.config.forms.confirm_dialog_id
if (dialogTitle == null) dialogTitle = dialogId
const dialog = document.getElementById(dialogId) const dialog = document.getElementById(dialogId)
if (dialog) dialog.showModal()
if (dialog) {
const dialogTitleElement = dialog.querySelector(Turbo.config.forms.confirm_dialog_title_selector)
if (dialogTitleElement) dialogTitleElement.textContent = dialogTitle
else console.warn(`dialogTitle element not found! missing selector='${Turbo.config.forms.confirm_dialog_title_selector}'`)
if (dialogContent) {
const dialogContentElement = dialog.querySelector(Turbo.config.forms.confirm_dialog_content_selector)
if (dialogContentElement) dialogContentElement.replaceChildren(dialogContent)
else console.warn(`dialogContent element not found! missing selector='${Turbo.config.forms.confirm_dialog_content_selector}'`)
}
dialog.showModal()
}
else console.warn(`dialog id=<${dialogId}> not found!`) else console.warn(`dialog id=<${dialogId}> not found!`)
} }
document.addEventListener('turbo:submit-start', (e) => { document.addEventListener('turbo:submit-start', (e) => {
const dialogId = e.explicitOriginalTarget.dataset.turboShowModal const dialogId = e.explicitOriginalTarget.dataset.turboShowModal
if (dialogId) { if (dialogId) {

9
app/javascript/initializers/stream_actions.js

@ -1,4 +1,11 @@
Turbo.StreamActions.console_log = function () { Turbo.StreamActions.console_log = function () {
const text = this.getAttribute("text") const text = this.getAttribute("text")
console.log(text) console.log(text)
}
}
Turbo.StreamActions.remoteDialog = function () {
const dialogId = this.getAttribute("dialogId")
const dialogTitle = this.getAttribute("dialogTitle")
const dialogContent = this.firstChild.content
Turbo.config.forms.showModal(dialogId, dialogTitle, dialogContent)
}

8
app/views/edge/_turbo_edit.html.erb

@ -0,0 +1,8 @@
<p class="mb-6">
Here is a content + actions
</p>
<form method="dialog" class="modal-action items-end text-center">
<%= content_tag :button, 'OK2', value: :confirm, class: "btn btn-primary" %>
<%= content_tag :button, 'Cancel2', value: :cancel, class: "btn btn-ghost" %>
</form>

2
app/views/edge/index.html.erb

@ -8,7 +8,7 @@
<li> <li>
<form method="dialog" class="flex flex-col"> <form method="dialog" class="flex flex-col">
<%= content_tag :button, 'Show Modal Remote', class: "btn btn-primary", onclick: "Turbo.config.forms.showModal('remote-dialog')" %>
<%= content_tag :button, 'Show Modal Remote', class: "btn btn-primary", onclick: "Turbo.config.forms.showModal('remote-dialog',' My Remote')" %>
<%= content_tag :button, 'Show Modal Remote2', class: "btn btn-primary", onclick: "Turbo.config.forms.showModal(false)" %> <%= content_tag :button, 'Show Modal Remote2', class: "btn btn-primary", onclick: "Turbo.config.forms.showModal(false)" %>
</form> </form>
</li> </li>

2
app/views/layouts/application.html.erb

@ -26,9 +26,9 @@
</head> </head>
<body class="flex flex-col"> <body class="flex flex-col">
<%= render '/layouts/components/main_dialog' %>
<%= render '/layouts/components/navbar' %> <%= render '/layouts/components/navbar' %>
<main class="m-8 min-h-60"><%= yield %></main> <main class="m-8 min-h-60"><%= yield %></main>
<%= render '/layouts/components/footer' %> <%= render '/layouts/components/footer' %>
<%= render '/layouts/components/dialogs' %>
</body> </body>
</html> </html>

7
app/views/layouts/components/_dialog.html.erb

@ -0,0 +1,7 @@
<%# locals: (id:, title:) %>
<dialog id="<%= id %>" class="modal">
<div class="modal-box bg-base-200">
<h3 class="text-lg font-bold" data-dialog-title><%= title %></h3>
<div class="modal-content" data-dialog-content><%= yield %></div>
</div>
</dialog>

22
app/views/layouts/components/_dialogs.html.erb

@ -0,0 +1,22 @@
<%= render 'layouts/components/dialog', id: 'front-dialog', title:'Demande de confirmation' do %>
<p data-dialog-content class="mb-6">CONTENT</p>
<form method="dialog" class="modal-action">
<%= content_tag :button, 'OK', value: :confirm, class: "btn btn-primary" %>
<%= content_tag :button, 'Cancel', value: :cancel, class: "btn btn-ghost" %>
</form>
<% end %>
<%= render 'layouts/components/dialog', id: 'remote-dialog', title:'Demande de confirmation' do %>
<p class="mb-6">
This is a centered dialog using the HTML dialog element with Tailwind CSS.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Porro quisquam
enim blanditiis iusto neque accusantium minus, aspernatur ex eius at.
Aliquam nam ex sit vel nesciunt qui ducimus velit nulla.
</p>
<form method="dialog" class="modal-action items-end text-center">
<%= content_tag :button, 'OK', value: :confirm, class: "btn btn-primary" %>
<%= content_tag :button, 'Cancel', value: :cancel, class: "btn btn-ghost" %>
</form>
<% end %>

29
app/views/layouts/components/_main_dialog.html.erb

@ -1,29 +0,0 @@
<dialog id="front-dialog" class="modal">
<div class="modal-box bg-base-200">
<h3 class="text-lg font-bold">Demande de confirmation</h3>
<p data-dialog-content class="mb-6"></p>
<form method="dialog" class="modal-action">
<%= content_tag :button, 'OK', value: :confirm, class: "btn btn-primary" %>
<%= content_tag :button, 'Cancel', value: :cancel, class: "btn btn-ghost" %>
</form>
</div>
</dialog>
<dialog id="remote-dialog" class="modal">
<div class="modal-box bg-base-200">
<h3 data-modal="title" class="text-lg font-bold">Remote Dialog</h3>
<p class="mb-6">
This is a centered dialog using the HTML dialog element with Tailwind CSS.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Porro quisquam
enim blanditiis iusto neque accusantium minus, aspernatur ex eius at.
Aliquam nam ex sit vel nesciunt qui ducimus velit nulla.
</p>
<form method="dialog" class="modal-action items-end text-center">
<%= content_tag :button, 'OK', value: :confirm, class: "btn btn-primary" %>
<%= content_tag :button, 'Cancel', value: :cancel, class: "btn btn-ghost" %>
</form>
</div>
</dialog>
Loading…
Cancel
Save