2023-01-29 18:52:24 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import "tom-select/dist/css/tom-select.bootstrap5.css";
|
|
|
|
import '../../css/components/tom-select_extensions.css';
|
|
|
|
import TomSelect from "tom-select";
|
|
|
|
import {Controller} from "@hotwired/stimulus";
|
|
|
|
|
2023-07-12 23:58:40 +02:00
|
|
|
import {trans, ENTITY_SELECT_GROUP_NEW_NOT_ADDED_TO_DB} from '../../translator.js'
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
_tomSelect;
|
|
|
|
|
2023-01-29 19:27:51 +01:00
|
|
|
_emptyMessage;
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
connect() {
|
|
|
|
|
2023-01-29 19:27:51 +01:00
|
|
|
//Extract empty message from data attribute
|
|
|
|
this._emptyMessage = this.element.getAttribute("data-empty-message") ?? "";
|
|
|
|
|
2023-01-29 20:42:18 +01:00
|
|
|
const allowAdd = this.element.getAttribute("data-allow-add") === "true";
|
2023-01-30 23:08:22 +01:00
|
|
|
const addHint = this.element.getAttribute("data-add-hint") ?? "";
|
2023-01-29 20:42:18 +01:00
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
let settings = {
|
|
|
|
allowEmptyOption: true,
|
|
|
|
selectOnTab: true,
|
2023-01-29 19:27:51 +01:00
|
|
|
maxOptions: null,
|
2023-07-12 23:58:40 +02:00
|
|
|
create: allowAdd ? this.createItem.bind(this) : false,
|
2023-01-29 18:52:24 +01:00
|
|
|
|
2024-03-04 21:51:26 +01:00
|
|
|
// This three options allow us to paste element names with commas: (see issue #538)
|
|
|
|
maxItems: 1,
|
|
|
|
delimiter: "$$VERY_LONG_DELIMITER_THAT_SHOULD_NEVER_APPEAR$$",
|
|
|
|
splitOn: null,
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
searchField: [
|
|
|
|
{field: "text", weight : 2},
|
|
|
|
{field: "parent", weight : 0.5},
|
2023-01-30 23:08:22 +01:00
|
|
|
{field: "path", weight : 1.0},
|
2023-01-29 18:52:24 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
render: {
|
2023-01-29 19:27:51 +01:00
|
|
|
item: this.renderItem.bind(this),
|
|
|
|
option: this.renderOption.bind(this),
|
2023-01-30 23:08:22 +01:00
|
|
|
option_create: function(data, escape) {
|
2023-01-30 23:58:53 +01:00
|
|
|
return '<div class="create"><i class="fa-solid fa-plus fa-fw"></i> <strong>' + escape(data.input) + '</strong>… ' +
|
2023-01-30 23:08:22 +01:00
|
|
|
'<small class="text-muted float-end">(' + addHint +')</small>' +
|
|
|
|
'</div>';
|
|
|
|
},
|
2023-06-06 23:05:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
//Add callbacks to update validity
|
|
|
|
onInitialize: this.updateValidity.bind(this),
|
|
|
|
onChange: this.updateValidity.bind(this),
|
2023-01-29 18:52:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this._tomSelect = new TomSelect(this.element, settings);
|
2023-10-08 01:04:04 +02:00
|
|
|
//Do not do a sync here as this breaks the initial rendering of the empty option
|
|
|
|
//this._tomSelect.sync();
|
2023-06-06 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
2023-07-12 23:58:40 +02:00
|
|
|
createItem(input, callback) {
|
|
|
|
callback({
|
2023-09-24 15:28:35 +02:00
|
|
|
//$%$ is a special value prefix, that is used to identify items, that are not yet in the DB
|
|
|
|
value: '$%$' + input,
|
2023-07-12 23:58:40 +02:00
|
|
|
text: input,
|
|
|
|
not_in_db_yet: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-06 23:05:44 +02:00
|
|
|
|
|
|
|
updateValidity() {
|
|
|
|
//Mark this input as invalid, if the selected option is disabled
|
|
|
|
|
|
|
|
const input = this.element;
|
|
|
|
const selectedOption = input.options[input.selectedIndex];
|
|
|
|
|
|
|
|
if (selectedOption && selectedOption.disabled) {
|
|
|
|
input.setCustomValidity("This option was disabled. Please select another option.");
|
|
|
|
} else {
|
|
|
|
input.setCustomValidity("");
|
|
|
|
}
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 00:08:32 +01:00
|
|
|
getTomSelect() {
|
|
|
|
return this._tomSelect;
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
renderItem(data, escape) {
|
2023-01-29 19:27:51 +01:00
|
|
|
//Render empty option as full row
|
|
|
|
if (data.value === "") {
|
|
|
|
if (this._emptyMessage) {
|
|
|
|
return '<div class="tom-select-empty-option"><span class="text-muted"><b>' + escape(this._emptyMessage) + '</b></span></div>';
|
|
|
|
} else {
|
|
|
|
return '<div> </div>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.short) {
|
2023-07-14 00:09:22 +02:00
|
|
|
let short = escape(data.short)
|
|
|
|
|
|
|
|
//Make text italic, if the item is not yet in the DB
|
|
|
|
if (data.not_in_db_yet) {
|
|
|
|
short = '<i>' + short + '</i>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<div><b>' + short + '</b></div>';
|
2023-01-29 19:27:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
let name = "";
|
|
|
|
if (data.parent) {
|
|
|
|
name += escape(data.parent) + " → ";
|
|
|
|
}
|
2023-07-12 23:58:40 +02:00
|
|
|
|
|
|
|
if (data.not_in_db_yet) {
|
|
|
|
//Not yet added items are shown italic and with a badge
|
|
|
|
name += "<i><b>" + escape(data.text) + "</b></i>" + "<span class='ms-3 badge bg-info badge-info'>" + trans(ENTITY_SELECT_GROUP_NEW_NOT_ADDED_TO_DB) + "</span>";
|
|
|
|
} else {
|
|
|
|
name += "<b>" + escape(data.text) + "</b>";
|
|
|
|
}
|
2023-01-29 18:52:24 +01:00
|
|
|
|
2023-02-05 20:23:52 +01:00
|
|
|
return '<div>' + (data.image ? "<img class='structural-entity-select-image' style='margin-right: 5px;' ' src='" + data.image + "'/>" : "") + name + '</div>';
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
renderOption(data, escape) {
|
|
|
|
//Render empty option as full row
|
|
|
|
if (data.value === "") {
|
2023-01-29 19:27:51 +01:00
|
|
|
if (this._emptyMessage) {
|
|
|
|
return '<div class="tom-select-empty-option"><span class="text-muted">' + escape(this._emptyMessage) + '</span></div>';
|
|
|
|
} else {
|
|
|
|
return '<div> </div>';
|
|
|
|
}
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Indent the option according to the level
|
2023-01-29 19:27:51 +01:00
|
|
|
let level_html = ' '.repeat(data.level);
|
2023-01-29 18:52:24 +01:00
|
|
|
|
|
|
|
let filter_badge = "";
|
|
|
|
if (data.filetype_filter) {
|
|
|
|
filter_badge = '<span class="badge bg-warning float-end"><i class="fa-solid fa-file-circle-exclamation"></i> ' + escape(data.filetype_filter) + '</span>';
|
|
|
|
}
|
|
|
|
|
2023-01-29 19:27:51 +01:00
|
|
|
let symbol_badge = "";
|
|
|
|
if (data.symbol) {
|
|
|
|
symbol_badge = '<span class="badge bg-primary ms-2">' + escape(data.symbol) + '</span>';
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
let parent_badge = "";
|
|
|
|
if (data.parent) {
|
|
|
|
parent_badge = '<span class="ms-3 badge rounded-pill bg-secondary float-end picker-us"><i class="fa-solid fa-folder-tree"></i> ' + escape(data.parent) + '</span>';
|
|
|
|
}
|
|
|
|
|
|
|
|
let image = "";
|
|
|
|
if (data.image) {
|
2023-02-05 20:23:52 +01:00
|
|
|
image = '<img class="structural-entity-select-image" style="margin-left: 5px;" src="' + data.image + '"/>';
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 19:27:51 +01:00
|
|
|
return '<div>' + level_html + escape(data.text) + image + symbol_badge + parent_badge + filter_badge + '</div>';
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 17:53:10 +01:00
|
|
|
disconnect() {
|
|
|
|
super.disconnect();
|
|
|
|
//Destroy the TomSelect instance
|
|
|
|
this._tomSelect.destroy();
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:52:24 +01:00
|
|
|
}
|