forked from fixin.me/fixin.me
Allow stream action to expand into multiple actions
e.g. by including 'onclick()' event handler
This commit is contained in:
parent
e95448ce9f
commit
76aae56ed5
@ -102,8 +102,10 @@ module ApplicationHelper
|
|||||||
tag.tr tag.td t('.no_items'), colspan: 10, class: 'hint'
|
tag.tr tag.td t('.no_items'), colspan: 10, class: 'hint'
|
||||||
end
|
end
|
||||||
|
|
||||||
def turbo_stream_handler(partial)
|
def render_turbo_stream(partial, locals)
|
||||||
"Turbo.renderStreamMessage('#{j(render partial: partial)}'); return false;"
|
# TODO: extend with smth like "if outside of rendering, render; otherwise
|
||||||
|
# appendChild() template within current render"
|
||||||
|
"Turbo.renderStreamMessage('#{j(render partial: partial, locals: locals)}'); return false;"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
// Configure your import map in config/importmap.rb. Read more:
|
||||||
|
// https://github.com/rails/importmap-rails
|
||||||
import "@hotwired/turbo-rails"
|
import "@hotwired/turbo-rails"
|
||||||
|
|
||||||
function showPage(event) {
|
function showPage(event) {
|
||||||
@ -6,6 +7,31 @@ function showPage(event) {
|
|||||||
}
|
}
|
||||||
document.addEventListener('turbo:load', showPage)
|
document.addEventListener('turbo:load', showPage)
|
||||||
|
|
||||||
|
/*
|
||||||
|
function beforeStreamRender(event) {
|
||||||
|
console.log(event.target)
|
||||||
|
}
|
||||||
|
document.addEventListener('turbo:before-stream-render', beforeStreamRender)
|
||||||
|
*/
|
||||||
|
|
||||||
|
Turbo.session.streamMessageRenderer.appendFragment = async function (fragment) {
|
||||||
|
for (let child of [...fragment.children]) {
|
||||||
|
document.documentElement.appendChild(child)
|
||||||
|
if (child.action == "click") {
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
new MutationObserver((mutations, observer) => {
|
||||||
|
mutations.forEach((m) => {
|
||||||
|
if ([...m.removedNodes].includes(child)) {
|
||||||
|
resolve(child)
|
||||||
|
observer.disconnect()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}).observe(document.documentElement, {childList: true})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Turbo.StreamActions.disable = function() {
|
Turbo.StreamActions.disable = function() {
|
||||||
this.targetElements.forEach((e) => {
|
this.targetElements.forEach((e) => {
|
||||||
@ -24,6 +50,15 @@ Turbo.StreamActions.enable = function() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Turbo.StreamActions.blur = function() {
|
||||||
|
blur()
|
||||||
|
}
|
||||||
|
|
||||||
Turbo.StreamActions.focus = function() {
|
Turbo.StreamActions.focus = function() {
|
||||||
|
// NOTE: call blur() before setting focus?
|
||||||
this.targetElements[0].focus({focusVisible: true})
|
this.targetElements[0].focus({focusVisible: true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Turbo.StreamActions.click = function() {
|
||||||
|
this.targetElements.forEach((e) => { e.click() })
|
||||||
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
<td class="actions">
|
<td class="actions">
|
||||||
<%= form.submit form: :unit_form %>
|
<%= form.submit form: :unit_form %>
|
||||||
<%= image_link_to t(:cancel), "close-circle-outline", units_path, class: 'dangerous',
|
<%= image_link_to t(:cancel), "close-circle-outline", units_path, class: 'dangerous',
|
||||||
onclick: turbo_stream_handler('form_close'), name: :cancel %>
|
name: :cancel, onclick: render_turbo_stream('form_close', {id: id}) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<!-- TODO: display error_messages_for unit -->
|
<!-- TODO: display error_messages_for unit -->
|
||||||
|
@ -15,7 +15,19 @@ ActiveSupport.on_load :turbo_streams_tag_builder do
|
|||||||
action_all :enable, targets, allow_inferred_rendering: false
|
action_all :enable, targets, allow_inferred_rendering: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def blur_all
|
||||||
|
action :blur, nil, allow_inferred_rendering: false
|
||||||
|
end
|
||||||
|
|
||||||
def focus(target)
|
def focus(target)
|
||||||
action :focus, target, allow_inferred_rendering: false
|
action :focus, target, allow_inferred_rendering: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def click(target)
|
||||||
|
action :click, target, allow_inferred_rendering: false
|
||||||
|
end
|
||||||
|
|
||||||
|
def click_all(targets)
|
||||||
|
action_all :click, targets, allow_inferred_rendering: false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user