|
| 1 | +/* |
| 2 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 3 | + * |
| 4 | + * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +import {Controller} from "@hotwired/stimulus"; |
| 21 | + |
| 22 | +import "tom-select/dist/css/tom-select.bootstrap5.css"; |
| 23 | +import '../../css/components/tom-select_extensions.css'; |
| 24 | +import TomSelect from "tom-select"; |
| 25 | + |
| 26 | +import TomSelect_click_to_edit from '../../tomselect/click_to_edit/click_to_edit' |
| 27 | +import TomSelect_autoselect_typed from '../../tomselect/autoselect_typed/autoselect_typed' |
| 28 | + |
| 29 | +TomSelect.define('click_to_edit', TomSelect_click_to_edit) |
| 30 | +TomSelect.define('autoselect_typed', TomSelect_autoselect_typed) |
| 31 | + |
| 32 | +export default class extends Controller { |
| 33 | + _tomSelect; |
| 34 | + |
| 35 | + _platformSelector; |
| 36 | + |
| 37 | + connect() { |
| 38 | + |
| 39 | + let dropdownParent = "body"; |
| 40 | + if (this.element.closest('.modal')) { |
| 41 | + dropdownParent = null |
| 42 | + } |
| 43 | + |
| 44 | + //Try to find the platform selector |
| 45 | + const platformSelector = document.querySelector("select[data-platform-selector-label='" + this.element.dataset.platformSelector + "']"); |
| 46 | + //Clear tomselect options, if the platform selector changes |
| 47 | + if (platformSelector) { |
| 48 | + this.platformSelector = platformSelector; |
| 49 | + platformSelector.addEventListener('change', () => { |
| 50 | + //Force reload of options by clearing the cache and options of TomSelect and triggering a search with an empty string |
| 51 | + this._tomSelect.clearOptions(); |
| 52 | + this._tomSelect.clearCache(); |
| 53 | + this._tomSelect.load(''); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + let settings = { |
| 58 | + persistent: false, |
| 59 | + create: true, |
| 60 | + maxItems: 1, |
| 61 | + preload: 'focus', |
| 62 | + createOnBlur: true, |
| 63 | + selectOnTab: true, |
| 64 | + clearAfterSelect: true, |
| 65 | + shouldLoad: ((query) => true), |
| 66 | + maxOptions: null, |
| 67 | + //This a an ugly solution to disable the delimiter parsing of the TomSelect plugin |
| 68 | + delimiter: 'VERY_L0NG_D€LIMITER_WHICH_WILL_NEVER_BE_ENCOUNTERED_IN_A_STRING', |
| 69 | + dropdownParent: dropdownParent, |
| 70 | + render: { |
| 71 | + item: (data, escape) => { |
| 72 | + return '<span>' + escape(data.label) + '</span>'; |
| 73 | + }, |
| 74 | + option: (data, escape) => { |
| 75 | + if (data.image) { |
| 76 | + return "<div class='row m-0'><div class='col-2 pl-0 pr-1'><img class='typeahead-image' src='" + data.image + "'/></div><div class='col-10'>" + data.label + "</div></div>" |
| 77 | + } |
| 78 | + return '<div>' + escape(data.label) + '</div>'; |
| 79 | + } |
| 80 | + }, |
| 81 | + plugins: { |
| 82 | + 'autoselect_typed': {}, |
| 83 | + 'click_to_edit': {}, |
| 84 | + 'clear_button': {}, |
| 85 | + "restore_on_backspace": {} |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + if(this.element.dataset.urlTemplate) { |
| 90 | + const base_url = this.element.dataset.urlTemplate; |
| 91 | + settings.searchField = "label"; |
| 92 | + settings.sortField = "label"; |
| 93 | + settings.valueField = "label"; |
| 94 | + settings.load = (query, callback) => { |
| 95 | + |
| 96 | + |
| 97 | + if (!this.platformSelector) { |
| 98 | + console.error("Platform selector not found for AI model autocomplete"); |
| 99 | + callback(); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + //Platform is the selected option |
| 104 | + const platform = this.platformSelector.value; |
| 105 | + if (!platform) { |
| 106 | + callback(); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const self = this; |
| 111 | + |
| 112 | + //Only fetch each platform once |
| 113 | + if(self.platformLoaded === platform) { |
| 114 | + callback(); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + const url = base_url.replace('__PLATFORM__', encodeURIComponent(platform)); |
| 119 | + |
| 120 | + fetch(url) |
| 121 | + .then(response => response.json()) |
| 122 | + .then(json => { |
| 123 | + |
| 124 | + self.platformLoaded = platform; |
| 125 | + |
| 126 | + var data = []; |
| 127 | + |
| 128 | + for (const name in json) { |
| 129 | + data.push({ |
| 130 | + "label": name, |
| 131 | + "capabilities": json[name].capabilities, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + callback(data); |
| 136 | + }).catch(()=>{ |
| 137 | + callback(); |
| 138 | + }); |
| 139 | + }; |
| 140 | + } |
| 141 | + this._tomSelect = new TomSelect(this.element, settings); |
| 142 | + } |
| 143 | + |
| 144 | + disconnect() { |
| 145 | + super.disconnect(); |
| 146 | + //Destroy the TomSelect instance |
| 147 | + this._tomSelect.destroy(); |
| 148 | + } |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | + |
0 commit comments