diff options
author | carson <carson@leaddyno.com> | 2020-06-30 10:45:38 -0700 |
---|---|---|
committer | carson <carson@leaddyno.com> | 2020-06-30 10:45:38 -0700 |
commit | b22b1d104c2a7e7097923c3d7a66e1d19ff56c04 (patch) | |
tree | 61eb96fa822ac0893e1697aa6408613c4ee300af | |
parent | 4f3cf2d889ff549422ed2bb135fc5775f7e54dab (diff) | |
parent | 2fbe230853512b05f0ead8afaf63517280204b86 (diff) | |
download | htmx-b22b1d104c2a7e7097923c3d7a66e1d19ff56c04.tar.gz htmx-b22b1d104c2a7e7097923c3d7a66e1d19ff56c04.zip |
Merge remote-tracking branch 'origin/dev' into dev
-rw-r--r-- | src/htmx.js | 10 | ||||
-rw-r--r-- | test/attributes/hx-push-url.js | 15 | ||||
-rw-r--r-- | www/attributes/hx-push-url.md | 10 | ||||
-rw-r--r-- | www/attributes/hx-vars.md | 2 |
4 files changed, 32 insertions, 5 deletions
diff --git a/src/htmx.js b/src/htmx.js index 50cb172d..2c311130 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1123,10 +1123,16 @@ return (function () { } function shouldPush(elt) { - return getClosestAttributeValue(elt, "hx-push-url") === "true" || + var pushUrl = getClosestAttributeValue(elt, "hx-push-url"); + return (pushUrl && pushUrl !== "false") || (elt.tagName === "A" && getInternalData(elt).boosted); } + function getPushUrl(elt) { + var pushUrl = getClosestAttributeValue(elt, "hx-push-url"); + return (pushUrl === "true" || pushUrl === "false") ? null : pushUrl; + } + function addRequestIndicatorClasses(elt) { mutateRequestIndicatorClasses(elt, "add"); } @@ -1560,7 +1566,7 @@ return (function () { }); // push URL and save new page if (shouldSaveHistory) { - var pathToPush = pushedUrl || finalPathForGet || path; + var pathToPush = pushedUrl || getPushUrl(elt) || finalPathForGet || path; pushUrlIntoHistory(pathToPush); triggerEvent(getDocument().body, 'htmx:pushedIntoHistory', {path:pathToPush}); } diff --git a/test/attributes/hx-push-url.js b/test/attributes/hx-push-url.js index e137d4a0..a1bad296 100644 --- a/test/attributes/hx-push-url.js +++ b/test/attributes/hx-push-url.js @@ -13,7 +13,7 @@ describe("hx-push-url attribute", function() { localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); }); - it("navigation should push an element into the cache ", function () { + it("navigation should push an element into the cache when true", function () { this.server.respondWith("GET", "/test", "second"); getWorkArea().innerHTML.should.be.equal(""); var div = make('<div hx-push-url="true" hx-get="/test">first</div>'); @@ -22,6 +22,19 @@ describe("hx-push-url attribute", function() { getWorkArea().textContent.should.equal("second") var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(1); + cache[0].url.should.equal("/test"); + }); + + it("navigation should push an element into the cache when string", function () { + this.server.respondWith("GET", "/test", "second"); + getWorkArea().innerHTML.should.be.equal(""); + var div = make('<div hx-push-url="/abc123" hx-get="/test">first</div>'); + div.click(); + this.server.respond(); + getWorkArea().textContent.should.equal("second") + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); + cache.length.should.equal(1); + cache[0].url.should.equal("/abc123"); }); it("restore should return old value", function () { diff --git a/www/attributes/hx-push-url.md b/www/attributes/hx-push-url.md index 98b9bd3b..0cb95298 100644 --- a/www/attributes/hx-push-url.md +++ b/www/attributes/hx-push-url.md @@ -7,7 +7,7 @@ title: </> htmx - hx-push-url The `hx-push-url` attribute allows you to "push" a new entry into the browser location bar, which creates a new history entry, allowing back-button and general history navigation. The possible values of this -attribute are `true` and `false`. +attribute are `true`, `false` or a custom string. Here is an example: @@ -20,6 +20,14 @@ Here is an example: This will cause htmx to snapshot the current DOM to `localStorage` and push the URL `/account' into the browser location bar. +```html +<div hx-get="/account" hx-push-url="/account/home"> + Go to My Account +</div> +``` + +This will push the URL `/account/home' into the browser location bar. + ### Notes * `hx-push-url` is inherited and can be placed on a parent element diff --git a/www/attributes/hx-vars.md b/www/attributes/hx-vars.md index 0e98ef47..0022c12b 100644 --- a/www/attributes/hx-vars.md +++ b/www/attributes/hx-vars.md @@ -16,6 +16,6 @@ syntax of javascript [Object Literals](https://developer.mozilla.org/en-US/docs/ ### Notes -* `hx-params` is inherited and can be placed on a parent element. +* `hx-vars` is inherited and can be placed on a parent element. * A child declaration of a variable overrides a parent declaration. * Input values with the same name override variable declarations. |