|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | +import TomSelect from 'tom-select'; |
| 3 | + |
| 4 | +/*! ***************************************************************************** |
| 5 | +Copyright (c) Microsoft Corporation. |
| 6 | +
|
| 7 | +Permission to use, copy, modify, and/or distribute this software for any |
| 8 | +purpose with or without fee is hereby granted. |
| 9 | +
|
| 10 | +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 11 | +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 12 | +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 13 | +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 14 | +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 15 | +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 16 | +PERFORMANCE OF THIS SOFTWARE. |
| 17 | +***************************************************************************** */ |
| 18 | + |
| 19 | +function __classPrivateFieldGet(receiver, state, kind, f) { |
| 20 | + if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); |
| 21 | + if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); |
| 22 | + return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); |
| 23 | +} |
| 24 | + |
| 25 | +var _instances, _getCommonConfig, _createAutocomplete, _createAutocompleteWithHtmlContents, _createAutocompleteWithRemoteData, _stripTags, _mergeObjects, _createTomSelect, _dispatchEvent; |
| 26 | +class default_1 extends Controller { |
| 27 | + constructor() { |
| 28 | + super(...arguments); |
| 29 | + _instances.add(this); |
| 30 | + } |
| 31 | + connect() { |
| 32 | + if (this.tomSelect) { |
| 33 | + return; |
| 34 | + } |
| 35 | + if (this.urlValue) { |
| 36 | + this.tomSelect = __classPrivateFieldGet(this, _instances, "m", _createAutocompleteWithRemoteData).call(this, this.urlValue); |
| 37 | + return; |
| 38 | + } |
| 39 | + if (this.optionsAsHtmlValue) { |
| 40 | + this.tomSelect = __classPrivateFieldGet(this, _instances, "m", _createAutocompleteWithHtmlContents).call(this); |
| 41 | + return; |
| 42 | + } |
| 43 | + this.tomSelect = __classPrivateFieldGet(this, _instances, "m", _createAutocomplete).call(this); |
| 44 | + } |
| 45 | + get selectElement() { |
| 46 | + if (!(this.element instanceof HTMLSelectElement)) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + return this.element; |
| 50 | + } |
| 51 | + get formElement() { |
| 52 | + if (!(this.element instanceof HTMLInputElement) && !(this.element instanceof HTMLSelectElement)) { |
| 53 | + throw new Error('Autocomplete Stimulus controller can only be used no an <input> or <select>.'); |
| 54 | + } |
| 55 | + return this.element; |
| 56 | + } |
| 57 | +} |
| 58 | +_instances = new WeakSet(), _getCommonConfig = function _getCommonConfig() { |
| 59 | + const plugins = {}; |
| 60 | + const isMultiple = !this.selectElement || this.selectElement.multiple; |
| 61 | + if (!this.formElement.disabled && !isMultiple) { |
| 62 | + plugins.clear_button = { title: '' }; |
| 63 | + } |
| 64 | + if (isMultiple) { |
| 65 | + plugins.remove_button = { title: '' }; |
| 66 | + } |
| 67 | + if (this.urlValue) { |
| 68 | + plugins.virtual_scroll = {}; |
| 69 | + } |
| 70 | + const config = { |
| 71 | + render: { |
| 72 | + no_results: () => { |
| 73 | + return `<div class="no-results">${this.noResultsFoundTextValue}</div>`; |
| 74 | + }, |
| 75 | + }, |
| 76 | + plugins: plugins, |
| 77 | + onItemAdd: () => { |
| 78 | + this.tomSelect.setTextboxValue(''); |
| 79 | + }, |
| 80 | + closeAfterSelect: true, |
| 81 | + }; |
| 82 | + if (!this.selectElement && !this.urlValue) { |
| 83 | + config.shouldLoad = () => false; |
| 84 | + } |
| 85 | + return __classPrivateFieldGet(this, _instances, "m", _mergeObjects).call(this, config, this.tomSelectOptionsValue); |
| 86 | +}, _createAutocomplete = function _createAutocomplete() { |
| 87 | + const config = __classPrivateFieldGet(this, _instances, "m", _mergeObjects).call(this, __classPrivateFieldGet(this, _instances, "m", _getCommonConfig).call(this), { |
| 88 | + maxOptions: this.selectElement ? this.selectElement.options.length : 50, |
| 89 | + }); |
| 90 | + return __classPrivateFieldGet(this, _instances, "m", _createTomSelect).call(this, config); |
| 91 | +}, _createAutocompleteWithHtmlContents = function _createAutocompleteWithHtmlContents() { |
| 92 | + const config = __classPrivateFieldGet(this, _instances, "m", _mergeObjects).call(this, __classPrivateFieldGet(this, _instances, "m", _getCommonConfig).call(this), { |
| 93 | + maxOptions: this.selectElement ? this.selectElement.options.length : 50, |
| 94 | + score: (search) => { |
| 95 | + const scoringFunction = this.tomSelect.getScoreFunction(search); |
| 96 | + return (item) => { |
| 97 | + return scoringFunction(Object.assign(Object.assign({}, item), { text: __classPrivateFieldGet(this, _instances, "m", _stripTags).call(this, item.text) })); |
| 98 | + }; |
| 99 | + }, |
| 100 | + render: { |
| 101 | + item: function (item) { |
| 102 | + return `<div>${item.text}</div>`; |
| 103 | + }, |
| 104 | + option: function (item) { |
| 105 | + return `<div>${item.text}</div>`; |
| 106 | + } |
| 107 | + }, |
| 108 | + }); |
| 109 | + return __classPrivateFieldGet(this, _instances, "m", _createTomSelect).call(this, config); |
| 110 | +}, _createAutocompleteWithRemoteData = function _createAutocompleteWithRemoteData(autocompleteEndpointUrl) { |
| 111 | + const config = __classPrivateFieldGet(this, _instances, "m", _mergeObjects).call(this, __classPrivateFieldGet(this, _instances, "m", _getCommonConfig).call(this), { |
| 112 | + firstUrl: (query) => { |
| 113 | + const separator = autocompleteEndpointUrl.includes('?') ? '&' : '?'; |
| 114 | + return `${autocompleteEndpointUrl}${separator}query=${encodeURIComponent(query)}`; |
| 115 | + }, |
| 116 | + load: function (query, callback) { |
| 117 | + const url = this.getUrl(query); |
| 118 | + fetch(url) |
| 119 | + .then(response => response.json()) |
| 120 | + .then(json => { this.setNextUrl(query, json.next_page); callback(json.results); }) |
| 121 | + .catch(() => callback()); |
| 122 | + }, |
| 123 | + score: function (search) { |
| 124 | + return function (item) { |
| 125 | + return 1; |
| 126 | + }; |
| 127 | + }, |
| 128 | + render: { |
| 129 | + option: function (item) { |
| 130 | + return `<div>${item.text}</div>`; |
| 131 | + }, |
| 132 | + item: function (item) { |
| 133 | + return `<div>${item.text}</div>`; |
| 134 | + }, |
| 135 | + no_more_results: () => { |
| 136 | + return `<div class="no-more-results">${this.noMoreResultsTextValue}</div>`; |
| 137 | + }, |
| 138 | + no_results: () => { |
| 139 | + return `<div class="no-results">${this.noResultsFoundTextValue}</div>`; |
| 140 | + }, |
| 141 | + }, |
| 142 | + preload: 'focus', |
| 143 | + }); |
| 144 | + return __classPrivateFieldGet(this, _instances, "m", _createTomSelect).call(this, config); |
| 145 | +}, _stripTags = function _stripTags(string) { |
| 146 | + return string.replace(/(<([^>]+)>)/gi, ''); |
| 147 | +}, _mergeObjects = function _mergeObjects(object1, object2) { |
| 148 | + return Object.assign(Object.assign({}, object1), object2); |
| 149 | +}, _createTomSelect = function _createTomSelect(options) { |
| 150 | + __classPrivateFieldGet(this, _instances, "m", _dispatchEvent).call(this, 'autocomplete:pre-connect', { options }); |
| 151 | + const tomSelect = new TomSelect(this.formElement, options); |
| 152 | + __classPrivateFieldGet(this, _instances, "m", _dispatchEvent).call(this, 'autocomplete:connect', { tomSelect, options }); |
| 153 | + return tomSelect; |
| 154 | +}, _dispatchEvent = function _dispatchEvent(name, payload) { |
| 155 | + this.element.dispatchEvent(new CustomEvent(name, { detail: payload, bubbles: true })); |
| 156 | +}; |
| 157 | +default_1.values = { |
| 158 | + url: String, |
| 159 | + optionsAsHtml: Boolean, |
| 160 | + noResultsFoundText: String, |
| 161 | + noMoreResultsText: String, |
| 162 | + tomSelectOptions: Object, |
| 163 | +}; |
| 164 | + |
| 165 | +export { default_1 as default }; |
0 commit comments