From 34582b839cd376f134bd9c6b6d9e6d3059dd1d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Rege=C5=A1?= Date: Sun, 23 Oct 2022 16:59:03 +0200 Subject: [PATCH] extensions: added new extension 'multi-swap' with mocha tests and docs (#1096) * extensions: added new extension 'multi-swap' with mocha tests and docs * multi-swap extension: removed forgotten debug console.log() * multi-swap extension: removed forgotten debug comment from mocha test --- src/ext/multi-swap.js | 45 +++++++++++++++++++++++ test/ext/multi-swap.js | 52 ++++++++++++++++++++++++++ test/index.html | 3 ++ www/docs.md | 1 + www/extensions.md | 1 + www/extensions/multi-swap.md | 71 ++++++++++++++++++++++++++++++++++++ 6 files changed, 173 insertions(+) create mode 100644 src/ext/multi-swap.js create mode 100644 test/ext/multi-swap.js create mode 100644 www/extensions/multi-swap.md diff --git a/src/ext/multi-swap.js b/src/ext/multi-swap.js new file mode 100644 index 00000000..f38f5b06 --- /dev/null +++ b/src/ext/multi-swap.js @@ -0,0 +1,45 @@ +(function () { + + /** @type {import("../htmx").HtmxInternalApi} */ + var api; + + htmx.defineExtension('multi-swap', { + init: function (apiRef) { + api = apiRef; + }, + isInlineSwap: function (swapStyle) { + return swapStyle.indexOf('multi:') === 0; + }, + handleSwap: function (swapStyle, target, fragment, settleInfo) { + if (swapStyle.indexOf('multi:') === 0) { + var selectorToSwapStyle = {}; + var elements = swapStyle.replace(/^multi\s*:\s*/, '').split(/\s*,\s*/); + + elements.map(function (element) { + var split = element.split(/\s*:\s*/); + var elementSelector = split[0]; + var elementSwapStyle = typeof (split[1]) !== "undefined" ? split[1] : "innerHTML"; + + if (elementSelector.charAt(0) !== '#') { + console.error("HTMX multi-swap: unsupported selector '" + elementSelector + "'. Only ID selectors starting with '#' are supported."); + return; + } + + selectorToSwapStyle[elementSelector] = elementSwapStyle; + }); + + for (var selector in selectorToSwapStyle) { + var swapStyle = selectorToSwapStyle[selector]; + var elementToSwap = fragment.querySelector(selector); + if (elementToSwap) { + api.oobSwap(swapStyle, elementToSwap, settleInfo); + } else { + console.warn("HTMX multi-swap: selector '" + selector + "' not found in source content."); + } + } + + return true; + } + } + }); +})(); \ No newline at end of file diff --git a/test/ext/multi-swap.js b/test/ext/multi-swap.js new file mode 100644 index 00000000..70b09821 --- /dev/null +++ b/test/ext/multi-swap.js @@ -0,0 +1,52 @@ +describe("multi-swap extension", function() { + beforeEach(function () { + this.server = makeServer(); + clearWorkArea(); + }); + afterEach(function () { + this.server.restore(); + clearWorkArea(); + }); + + it('swap only one element with default innerHTML', function () { + this.server.respondWith("GET", "/test", '
New A
'); + var content = make('
Foo
Old A
'); + var btn = make(''); + btn.click(); + this.server.respond(); + should.equal(content.innerHTML, 'Foo
New A
'); + }); + + it('swap multiple elements with outerHTML, beforeend, afterend, beforebegin and delete methods', function () { + this.server.respondWith("GET", "/test", + '
' + + '
New A
foo ' + + '
New B
bar ' + + '
New C
dummy ' + + '
New D
lorem ' + + '
TO DELETE
' + + '
' + ); + var content = make( + '
Foo ' + + '
Old A
A ' + + '
Old B
B ' + + '
Old C
C ' + + '
Old D
D ' + + '
Old E
E ' + + '
' + ); + var btn = make(''); + btn.click(); + this.server.respond(); + should.equal(content.outerHTML, + '
Foo ' + + '
New A
A ' + + '
Old BNew B
B ' + + '
Old C
New C C ' + + ' New D
Old D
D ' + + ' E ' + + '
' + ); + }); +}); diff --git a/test/index.html b/test/index.html index 86113143..15aa3ef4 100644 --- a/test/index.html +++ b/test/index.html @@ -134,6 +134,9 @@ + + + diff --git a/www/docs.md b/www/docs.md index 6da2fdc2..b3bc0cc4 100644 --- a/www/docs.md +++ b/www/docs.md @@ -952,6 +952,7 @@ Htmx includes some extensions that are tested against the htmx code base. Here | [`client-side-templates`](/extensions/client-side-templates) | support for client side template processing of JSON responses | [`path-deps`](/extensions/path-deps) | an extension for expressing path-based dependencies [similar to intercoolerjs](http://intercoolerjs.org/docs.html#dependencies) | [`class-tools`](/extensions/class-tools) | an extension for manipulating timed addition and removal of classes on HTML elements +| [`multi-swap`](/extensions/multi-swap) | allows to swap multiple elements with different swap methods See the [extensions page](/extensions#included) for a complete list. diff --git a/www/extensions.md b/www/extensions.md index 043ba2e4..2ecfdb8c 100644 --- a/www/extensions.md +++ b/www/extensions.md @@ -75,6 +75,7 @@ See the individual extension documentation for more details. | [`loading-states`](/extensions/loading-states) | allows you to disable inputs, add and remove CSS classes to any element while a request is in-flight. | [`method-override`](/extensions/method-override) | use the `X-HTTP-Method-Override` header for non-`GET` and `POST` requests | [`morphdom-swap`](/extensions/morphdom-swap) | an extension for using the [morphdom](https://github.com/patrick-steele-idem/morphdom) library as the swapping mechanism in htmx. +| [`multi-swap`](/extensions/multi-swap) | allows to swap multiple elements with different swap methods | [`path-deps`](/extensions/path-deps) | an extension for expressing path-based dependencies [similar to intercoolerjs](http://intercoolerjs.org/docs.html#dependencies) | [`preload`](/extensions/preload) | preloads selected `href` and `hx-get` targets based on rules you control. | [`remove-me`](/extensions/remove-me) | allows you to remove an element after a given amount of time diff --git a/www/extensions/multi-swap.md b/www/extensions/multi-swap.md new file mode 100644 index 00000000..b12eff1a --- /dev/null +++ b/www/extensions/multi-swap.md @@ -0,0 +1,71 @@ +--- +layout: layout.njk +title: htmx - multi-swap extension +--- + +## The `multi-swap` extension + +This extension allows you to swap multiple elements marked with the `id` attribute from the HTML response. You can also choose for each element which [swap method](/docs/#swapping) should be used. + +Multi-swap can help in cases where OOB ([Out of Band Swaps](/docs/#oob_swaps)) is not enough for you. OOB requires HTML tags marked with `hx-swap-oob` attributes to be at the TOP level of HTML, which significantly limited its use. With OOB is not possible to swap multiple elements arbitrarily placed and nested in the DOM tree. + +It is a very powerful tool in conjunction with `hx-boost` and `preload` extension. + +#### Usage + +1. Set `hx-ext="multi-swap"` attribute on ``, on some parent element, or on each action element that should trigger an action (typically anchors or buttons). +2. On your action elements set `hx-swap="multi:ID-SELECTORS"`, e.g. `hx-swap="multi:#id1,#id2:outerHTML,#id3:afterend"`. +3. If you're not using e.g. `hx-get` to enable HTMX behavior, set `hx-boost="true"` on your action elements, or on some parent element, so that all elements inherit the hx-boost setting. + +Selectors must be separated by a comma (without surrounding spaces) and a colon with the desired swap method can optionally be placed after the selector. Default swap method is `innerHTML`. + +```html + + <-- simple example how to swap #id1 and #id2 from /example by innerHTML (default swap method) --> + + + <-- advanced example how to swap multiple elements from /example by different swap methods --> + Click to swap #id1 and #id2, extend #id3 content and delete #id4 element + +
Old 1 content
+
Old 2 content
+
Old 3 content
+
Old 4 content
+ +``` + +**Real world example with preloading** + +The use case below shows how to ensure that only the `#submenu` and `#content` elements are redrawn when the main menu items are clicked. Thanks to the combination with the preload extension, the page, including its images, is preloaded on `mouseover` event. + +```html + + + + + + +
...
+ + + + +
... +
...
+ +``` + + +#### Notes and limitations + +* Attribute `hx-swap` value **must not contain spaces**, otherwise only the part of the value up to the first space will be accepted. +* If the `delete` swap method is used, the HTML response must also contain deleted element (it can be empty div with `id` attribute). +* Only elements with an `id` selector are supported, as the function internally uses OOB internal method. So it is not possible to use `class` or any other selectors. + +#### Source + + +