Skip to content

Commit

Permalink
Package Turbo with hotwired/turbo#1250
Browse files Browse the repository at this point in the history
  • Loading branch information
klevo committed Apr 21, 2024
1 parent 102a491 commit 730f426
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
72 changes: 69 additions & 3 deletions app/assets/javascripts/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4141,10 +4141,10 @@ class Session {
this.navigator.proposeVisit(expandURL(location), options);
}
}
refresh(url, requestId) {
refresh(requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId);
if (!isRecentRequest) {
this.visit(url, {
this.visit(window.location.href, {
action: "replace",
shouldCacheSnapshot: false
});
Expand Down Expand Up @@ -4950,6 +4950,69 @@ function activateElement(element, currentURL) {
}
}

function morph(streamElement) {
const morphStyle = streamElement.hasAttribute("children-only") ? "innerHTML" : "outerHTML";
streamElement.targetElements.forEach((element => {
Idiomorph.morph(element, streamElement.templateContent, {
morphStyle: morphStyle,
callbacks: {
beforeNodeAdded: beforeNodeAdded,
beforeNodeMorphed: beforeNodeMorphed,
beforeAttributeUpdated: beforeAttributeUpdated,
beforeNodeRemoved: beforeNodeRemoved,
afterNodeMorphed: afterNodeMorphed
}
});
}));
}

function beforeNodeAdded(node) {
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id));
}

function beforeNodeRemoved(node) {
return beforeNodeAdded(node);
}

function beforeNodeMorphed(target, newElement) {
if (target instanceof HTMLElement) {
if (!target.hasAttribute("data-turbo-permanent")) {
const event = dispatch("turbo:before-morph-element", {
cancelable: true,
target: target,
detail: {
newElement: newElement
}
});
return !event.defaultPrevented;
}
return false;
}
}

function beforeAttributeUpdated(attributeName, target, mutationType) {
const event = dispatch("turbo:before-morph-attribute", {
cancelable: true,
target: target,
detail: {
attributeName: attributeName,
mutationType: mutationType
}
});
return !event.defaultPrevented;
}

function afterNodeMorphed(target, newElement) {
if (newElement instanceof HTMLElement) {
dispatch("turbo:morph-element", {
target: target,
detail: {
newElement: newElement
}
});
}
}

const StreamActions = {
after() {
this.targetElements.forEach((e => e.parentElement?.insertBefore(this.templateContent, e.nextSibling)));
Expand Down Expand Up @@ -4978,7 +5041,10 @@ const StreamActions = {
}));
},
refresh() {
session.refresh(this.baseURI, this.requestId);
session.refresh(this.requestId);
},
morph() {
morph(this);
}
};

Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/turbo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release": "npm publish && git commit -am \"$npm_package_name v$npm_package_version\" && git push"
},
"dependencies": {
"@hotwired/turbo": "^8.0.4",
"@hotwired/turbo": "../turbo",
"@rails/actioncable": "^7.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
chalk "^2.4.2"
js-tokens "^4.0.0"

"@hotwired/turbo@^8.0.4":
"@hotwired/turbo@../turbo":
version "8.0.4"
resolved "https://registry.yarnpkg.com/@hotwired/turbo/-/turbo-8.0.4.tgz#5c5361c06a37cdf10dcba4223f1afd0ca1c75091"
integrity sha512-mlZEFUZrJnpfj+g/XeCWWuokvQyN68WvM78JM+0jfSFc98wegm259vCbC1zSllcspRwbgXK31ibehCy5PA78/Q==

"@jridgewell/gen-mapping@^0.3.0":
version "0.3.3"
Expand Down

0 comments on commit 730f426

Please sign in to comment.