Skip to content

Commit

Permalink
add moveBefore support
Browse files Browse the repository at this point in the history
  • Loading branch information
1cg committed Aug 23, 2024
1 parent 27fc37c commit b4048eb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,15 +1460,39 @@ var htmx = (function() {
return oobValue
}

function restorePreservedElements(){

Check failure on line 1463 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Missing space before opening brace
let pantry = find("#--htmx-preserve-pantry--");

Check failure on line 1464 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

'pantry' is never reassigned. Use 'const' instead

Check failure on line 1464 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Strings must use singlequote

Check failure on line 1464 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
if (pantry) {
for (const preservedElt of pantry.children) {
let existingElement = find("#" + preservedElt.id);

Check failure on line 1467 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

'existingElement' is never reassigned. Use 'const' instead

Check failure on line 1467 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Strings must use singlequote

Check failure on line 1467 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
// @ts-ignore - use proposed moveBefore feature
existingElement.parentNode.moveBefore(preservedElt, existingElement);

Check failure on line 1469 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
existingElement.remove();

Check failure on line 1470 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
}
pantry.remove();

Check failure on line 1472 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
}
}

/**
* @param {DocumentFragment} fragment
*/
function handlePreservedElements(fragment) {
forEach(findAll(fragment, '[hx-preserve], [data-hx-preserve]'), function(preservedElt) {
const id = getAttributeValue(preservedElt, 'id')
const oldElt = getDocument().getElementById(id)
if (oldElt != null) {
preservedElt.parentNode.replaceChild(oldElt, preservedElt)
const existingElement = getDocument().getElementById(id)
if (existingElement != null) {
if (preservedElt.moveBefore) { // if the moveBefore API exists, use it
// get or create a storage spot for stuff
let pantry = find("#--htmx-preserve-pantry--");
if (pantry == null) {
getDocument().body.insertAdjacentHTML("afterend", "<div id='--htmx-preserve-pantry--'></div>")
pantry = find("#--htmx-preserve-pantry--");
}
// @ts-ignore - use proposed moveBefore feature
pantry.moveBefore(existingElement, null);
} else {
preservedElt.parentNode.replaceChild(existingElement, preservedElt)
}
}
})
}
Expand Down Expand Up @@ -1871,6 +1895,7 @@ var htmx = (function() {
}
handlePreservedElements(fragment)
swapWithStyle(swapSpec.swapStyle, swapOptions.contextElement, target, fragment, settleInfo)
restorePreservedElements();
}

// apply saved focus and selection information to swapped content
Expand Down Expand Up @@ -3153,7 +3178,9 @@ var htmx = (function() {
const settleInfo = makeSettleInfo(historyElement)
handleTitle(fragment.title)

handlePreservedElements(fragment);
swapInnerHTML(historyElement, content, settleInfo)
restorePreservedElements();
settleImmediately(settleInfo.tasks)
currentPathForHistory = path
triggerEvent(getDocument().body, 'htmx:historyRestore', { path, cacheMiss: true, serverResponse: this.response })
Expand All @@ -3176,7 +3203,9 @@ var htmx = (function() {
const historyElement = getHistoryElement()
const settleInfo = makeSettleInfo(historyElement)
handleTitle(fragment.title)
handlePreservedElements(fragment);
swapInnerHTML(historyElement, fragment, settleInfo)
restorePreservedElements();
settleImmediately(settleInfo.tasks)
getWindow().setTimeout(function() {
window.scrollTo(0, cached.scroll)
Expand Down
2 changes: 2 additions & 0 deletions test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ <h2>Functionality</h2>
<li><a href="poll-clears-on-reinit-test.html">Polling Cancels On Reprocessing</a></li>
<li><a href="cache-buster">Cache Busting Test</a></li>
<li><a href="body-swap">Body Swap Test</a></li>
<li><a href="keep-indicators">Keep Indicators Test</a></li>
<li><a href="move-before">Move Before Test</a></li>
<li>Scroll Tests
<ul>
<li><a href="scroll-test-eventHandler.html">Scroll Event Handler</a></li>
Expand Down
23 changes: 23 additions & 0 deletions test/manual/move-before/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<script type="application/javascript" src="../../../src/htmx.js"></script>
</head>
<body>
<main>
<button hx-get="video1.html"
hx-target="#video-container">
Load Video Layout 1
</button>
<button hx-get="video2.html"
hx-push-url="true"
hx-target="#video-container">
Load Video Layout 2
</button>
&dot;&dot;
<hr/>
<div id="video-container">
---
</div>
</main>
</body>
</html>
8 changes: 8 additions & 0 deletions test/manual/move-before/video1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>The Best Song On The Internet</h1>

<iframe id="rick-roll" width="600" height="300" hx-preserve="true"
src="https://www.youtube.com/embed/eBGIQ7ZuuiU" title="Rick Roll" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin" allowfullscreen>

</iframe>
38 changes: 38 additions & 0 deletions test/manual/move-before/video2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<h1>The Best Song On The Internet</h1>

<section id="summary">
<h2>The Song</h2>
<p>
"Never Gonna Give You Up" is a song by English singer Rick Astley, released on 27 July 1987. Written and
produced by Stock Aitken Waterman, it was released by RCA Records as the first single from Astley's debut studio
album, Whenever You Need Somebody (1987). The song became a worldwide hit, initially in the United Kingdom in
1987, where it stayed at the top of the chart for five weeks and was the best-selling single of that year. It
eventually topped charts in 25 different countries, including the United States and West Germany,[7] and winning
Best British Single at the 1988 Brit Awards. The song is widely regarded as Astley's most popular, as well as
his signature song, and it is often played at the end of his live concerts.
</p>
</section>

<section id="the-video">
<h2>The Video</h2>
<div style="margin: auto;text-align: center">
<iframe id="rick-roll" width="600" height="300" hx-preserve="true"
src="https://www.youtube.com/embed/eBGIQ7ZuuiU" title="Rick Roll" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin" allowfullscreen>

</iframe>
</div>
</section>

<section id="artist">
<h2>The Artist</h2>
<p>
Richard Paul Astley (born 6 February 1966) is an English singer. He gained fame through his association with the
production trio Stock Aitken Waterman, releasing the 1987 album Whenever You Need Somebody, which sold 15
million copies worldwide. His debut single "Never Gonna Give You Up" was a No. 1 hit in 25 countries, winning
the 1988 Brit Award for Best British Single.[1][2] His 1988 single "Together Forever" became his second song to
top the US Billboard Hot 100 and was one of his eight songs to reach the Top 10 on the UK Singles Chart. The
title track, "Whenever You Need Somebody", was also a No. 1 hit.
</p>
</section>

0 comments on commit b4048eb

Please sign in to comment.