hx-tomselect 1.0.12 → 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)}`);
@@ -219,6 +222,21 @@
219
222
  }
220
223
  })
221
224
  },
225
+ {
226
+ key: 'ts-on-change-navigate',
227
+ configChange: (elm, config) => ({
228
+ multiple: false,
229
+ onChange: function(value) {
230
+ const urlTemplate = elm.getAttribute('ts-on-change-navigate')
231
+ if(urlTemplate && value){
232
+ const url = urlTemplate.replace('{value}', value)
233
+ window.location.href = url
234
+ }else{
235
+ console.warn('tomSelect - ts-on-change-navigate - no url template found')
236
+ }
237
+ }
238
+ })
239
+ },
222
240
  {
223
241
  key: 'ts-raw-config',
224
242
  configChange: (elm, config) => elm.getAttribute('ts-raw-config')
@@ -295,7 +313,10 @@
295
313
  }
296
314
  }
297
315
 
298
- htmx.defineExtension('tomselect', {
316
+ function register() {
317
+ var h = getHtmx();
318
+ if (!h || !h.defineExtension) return false;
319
+ h.defineExtension('tomselect', {
299
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):
300
321
  onEvent: function (name, evt) {
301
322
  if (name === "htmx:afterProcessNode") {
@@ -329,5 +350,22 @@
329
350
  .forEach(elt => elt.tomselect.destroy())*/
330
351
  }
331
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
+ }
332
370
 
333
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;
@@ -262,6 +262,17 @@
262
262
  </code></pre>
263
263
  </section>
264
264
  </div>
265
+
266
+
267
+ <section class="mb-20">
268
+ <h2 class="text-xl font-semibold mb-3">Navigate on change</h2>
269
+ <p>navigate to a new page when the value changes</p>
270
+ <select hx-ext="tomselect" ts-on-change-navigate="{value}">
271
+ <option value="">Select</option>
272
+ <option value="https://www.google.com">Google </option>
273
+ <option value="https://www.bing.com">Bing</option>
274
+ </select>
275
+ </section>
265
276
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.0/prism.min.js"></script>
266
277
  </body>
267
278
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hx-tomselect",
3
- "version": "1.0.12",
4
- "description": "Provides a hx-ext=\"tomselect\" htmx extention tag",
3
+ "version": "1.0.15",
4
+ "description": "Htmx extension that allows rendering tom-select via plain html elements",
5
5
  "main": "hx-tomselect.js",
6
6
  "repository": {
7
7
  "type": "git",