hx-tomselect 1.0.14 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hx-tomselect.js CHANGED
@@ -2,6 +2,9 @@
2
2
  /** stable build*/
3
3
  const version = '11'
4
4
 
5
+ /** Resolve htmx from global (script tag) so we work with htmx 4 and correct load order. */
6
+ function getHtmx() { return typeof window !== 'undefined' && window.htmx; }
7
+
5
8
  /**
6
9
  * @typedef {Object} SupportedAttribute
7
10
  * Defines an attribute supported by a configuration modification system.
@@ -166,7 +169,7 @@
166
169
  }
167
170
  return res.text();
168
171
  })
169
- .then((responseHtml) => htmx.process(elm, responseHtml))
172
+ .then((responseHtml) => getHtmx().process(elm, responseHtml))
170
173
  .catch(error => {
171
174
  console.error('Error adding item', error)
172
175
  elm.setAttribute('tom-select-warning', `ts-add-post-url - Error processing item: ${JSON.stringify(error)}`);
@@ -225,7 +228,7 @@
225
228
  multiple: false,
226
229
  onChange: function(value) {
227
230
  const urlTemplate = elm.getAttribute('ts-on-change-navigate')
228
- if(urlTemplate){
231
+ if(urlTemplate && value){
229
232
  const url = urlTemplate.replace('{value}', value)
230
233
  window.location.href = url
231
234
  }else{
@@ -310,7 +313,10 @@
310
313
  }
311
314
  }
312
315
 
313
- htmx.defineExtension('tomselect', {
316
+ function register() {
317
+ var h = getHtmx();
318
+ if (!h || !h.defineExtension) return false;
319
+ h.defineExtension('tomselect', {
314
320
  // This is doing all the tom-select attachment at this stage, but relies on this full document scan (would prefer onLoad of speicfic content):
315
321
  onEvent: function (name, evt) {
316
322
  if (name === "htmx:afterProcessNode") {
@@ -344,5 +350,22 @@
344
350
  .forEach(elt => elt.tomselect.destroy())*/
345
351
  }
346
352
  });
353
+ return true;
354
+ }
355
+ if (!register()) {
356
+ function tryRegister() {
357
+ if (register()) return;
358
+ if (document.readyState === 'loading') {
359
+ document.addEventListener('DOMContentLoaded', tryRegister);
360
+ } else {
361
+ setTimeout(tryRegister, 50);
362
+ }
363
+ }
364
+ if (document.readyState === 'loading') {
365
+ document.addEventListener('DOMContentLoaded', tryRegister);
366
+ } else {
367
+ setTimeout(tryRegister, 50);
368
+ }
369
+ }
347
370
 
348
371
  })();
package/index.html CHANGED
@@ -5,8 +5,8 @@
5
5
  <meta charset="UTF-8">
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
7
  <title>hx-tomselect</title>
8
- <script defer src="./hx-tomselect.js"></script>
9
- <script src="https://unpkg.com/htmx.org@latest"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/htmx.org@4.0.0-alpha7/dist/htmx.min.js"></script>
9
+ <script src="./hx-tomselect.js"></script>
10
10
  <link href="https://cdn.jsdelivr.net/npm/tom-select@latest/dist/css/tom-select.css" rel="stylesheet"/>
11
11
  <script src="https://cdn.jsdelivr.net/npm/tom-select@latest/dist/js/tom-select.complete.min.js"></script>
12
12
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@latest/dist/tailwind.min.css" rel="stylesheet">
@@ -60,15 +60,15 @@
60
60
  <pre
61
61
  class="mb-2 code-container"><code class="language-markup wrap">&lt;script src="https://unpkg.com/hx-tomselect/hx-tomselect.js"&gt;&lt;/script&gt;</code></pre>
62
62
 
63
- this extention relies on <a class="underline" href="https://v1.htmx.org">htmx</a> and <a class="underline"
64
- href="https://tom-select.js.org/">tom-select.js</a> being loaded before it is included:
63
+ this extention relies on <a class="underline" href="https://htmx.org">htmx</a> and <a class="underline"
64
+ href="https://tom-select.js.org/">tom-select.js</a> being loaded before it is included (htmx first, then this extension):
65
65
  <pre class="mb-2 code-container"><code class="language-markup">&lt;!DOCTYPE html&gt;
66
66
  &lt;html&gt;
67
67
  &lt;head&gt;
68
- &lt;script src="https://unpkg.com/htmx.org@latest"&gt;
68
+ &lt;script src="https://cdn.jsdelivr.net/npm/htmx.org@4.0.0-alpha7/dist/htmx.min.js"&gt;&lt;/script&gt;
69
69
  &lt;link href="https://unpkg.com/tom-select@latest/dist/css/tom-select.css" rel="stylesheet" /&gt;
70
- &lt;script src="https://unpkg.com/tom-select@latest/dist/js/tom-select.complete.min.js"&gt;
71
- &lt;script src="https://unpkg.com/hx-tomselect/hx-tomselect.js"/&gt;
70
+ &lt;script src="https://unpkg.com/tom-select@latest/dist/js/tom-select.complete.min.js"&gt;&lt;/script&gt;
71
+ &lt;script src="https://unpkg.com/hx-tomselect/hx-tomselect.js"&gt;&lt;/script&gt;
72
72
  &lt;/head&gt;
73
73
  &lt;body&gt;
74
74
  &lt;select hx-ext="tomselect"&gt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hx-tomselect",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Htmx extension that allows rendering tom-select via plain html elements",
5
5
  "main": "hx-tomselect.js",
6
6
  "repository": {