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:
@@ -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"
|
||||
|
||||
function showPage(event) {
|
||||
@@ -6,6 +7,31 @@ function showPage(event) {
|
||||
}
|
||||
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() {
|
||||
this.targetElements.forEach((e) => {
|
||||
@@ -24,6 +50,15 @@ Turbo.StreamActions.enable = function() {
|
||||
})
|
||||
}
|
||||
|
||||
Turbo.StreamActions.blur = function() {
|
||||
blur()
|
||||
}
|
||||
|
||||
Turbo.StreamActions.focus = function() {
|
||||
// NOTE: call blur() before setting focus?
|
||||
this.targetElements[0].focus({focusVisible: true})
|
||||
}
|
||||
|
||||
Turbo.StreamActions.click = function() {
|
||||
this.targetElements.forEach((e) => { e.click() })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user