2021-10-29 21:18:20 +02:00
|
|
|
const autocomplete_query_delay_ms = 60;
|
|
|
|
|
2021-11-08 00:56:33 +01:00
|
|
|
class AutocompleteCustomUi {
|
2021-11-11 20:51:02 +01:00
|
|
|
|
|
|
|
static create_media_div(img_src, text_nodes) {
|
|
|
|
var ui = document.createElement('div');
|
|
|
|
ui.setAttribute('class', 'd-flex align-items-center');
|
|
|
|
var img_div = document.createElement('div');
|
|
|
|
img_div.setAttribute('class', 'flex-shrink-0');
|
|
|
|
var img = document.createElement('img');
|
|
|
|
if (img_src != null && img_src != undefined && img_src != '') {
|
|
|
|
img.setAttribute('src', img_src);
|
|
|
|
img.setAttribute('class', 'component-img-small');
|
|
|
|
} else {
|
|
|
|
img.setAttribute('src', fallback_img_path);
|
|
|
|
img.setAttribute('class', 'component-img-def-small');
|
|
|
|
}
|
|
|
|
|
|
|
|
img_div.appendChild(img);
|
|
|
|
ui.appendChild(img_div);
|
|
|
|
var text_div = document.createElement('div');
|
2022-01-01 17:18:08 +01:00
|
|
|
text_div.setAttribute('class', 'ms-3');
|
2021-11-11 20:51:02 +01:00
|
|
|
|
|
|
|
if (text_nodes != undefined) {
|
|
|
|
for (var i = 0; i < text_nodes.length; i++) {
|
|
|
|
text_div.appendChild(text_nodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui.appendChild(text_div)
|
|
|
|
|
|
|
|
return ui
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:18:20 +02:00
|
|
|
constructor(text_id, dropdown_id, query_function)
|
|
|
|
{
|
|
|
|
this.text_id = text_id;
|
|
|
|
this.dropdown_id = dropdown_id;
|
|
|
|
this.query_callback = query_function.bind(this);
|
|
|
|
|
|
|
|
document.getElementById(text_id).addEventListener("keyup", this.ac_delay(function(event) {
|
2024-11-18 23:55:38 +01:00
|
|
|
if (event.key != 'Enter') {
|
|
|
|
this.query_callback(document.getElementById(this.text_id).value, this);
|
|
|
|
}
|
2021-10-29 21:18:20 +02:00
|
|
|
}, autocomplete_query_delay_ms).bind(this));
|
2021-11-08 00:56:33 +01:00
|
|
|
|
2021-11-08 23:11:05 +01:00
|
|
|
this.dropdown_data = {};
|
|
|
|
|
2021-11-08 00:56:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2021-11-11 20:51:02 +01:00
|
|
|
* @param {*} nodes_to_add A list of dictionaries containing the shown objects and the data called when clicked.
|
2021-11-08 00:56:33 +01:00
|
|
|
*
|
|
|
|
* The list: {{'ui': <nodes>, 'data': 'my_data'}, {}, ...}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
show_results(nodes_to_add, data_clicked_callback) {
|
|
|
|
var ul = document.getElementById(this.dropdown_id);
|
|
|
|
ul.innerHTML = '';
|
|
|
|
this.select_data = {};
|
2021-11-08 23:11:05 +01:00
|
|
|
this.dropdown_data = {};
|
2021-11-08 00:56:33 +01:00
|
|
|
|
|
|
|
for (var i = 0; i < nodes_to_add.length; i++) {
|
|
|
|
var ui = nodes_to_add[i]['ui'];
|
|
|
|
var data = nodes_to_add[i]['data']
|
|
|
|
var dropdown_node = document.createElement('li');
|
2021-11-08 23:11:05 +01:00
|
|
|
dropdown_node.dataset.ac_clicked = i;
|
|
|
|
this.dropdown_data[i] = data;
|
2021-11-08 00:56:33 +01:00
|
|
|
dropdown_node.addEventListener('click',
|
|
|
|
(e) => {
|
2021-11-08 23:11:05 +01:00
|
|
|
data_clicked_callback(this.dropdown_data[e.currentTarget.dataset.ac_clicked]);
|
2021-11-08 00:56:33 +01:00
|
|
|
var text_box = document.getElementById(this.text_id);
|
|
|
|
var dropdown = bootstrap.Dropdown.getOrCreateInstance(text_box);
|
|
|
|
dropdown.hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
dropdown_node.setAttribute('class', 'dropdown-item');
|
|
|
|
dropdown_node.appendChild(ui);
|
|
|
|
ul.appendChild(dropdown_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dropdown = bootstrap.Dropdown.getOrCreateInstance(document.getElementById(this.text_id));
|
|
|
|
dropdown.show();
|
2021-10-29 21:18:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:56:33 +01:00
|
|
|
ac_delay(callback, ms) {
|
|
|
|
var timer = 0;
|
|
|
|
return function() {
|
|
|
|
var context = this, args = arguments;
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(function() {
|
|
|
|
callback.apply(context, args);
|
|
|
|
}, ms || 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AutocompleteText extends AutocompleteCustomUi {
|
2021-10-29 21:18:20 +02:00
|
|
|
show_results(results) {
|
|
|
|
var ul = document.getElementById(this.dropdown_id);
|
|
|
|
ul.innerHTML = '';
|
|
|
|
for (var i = 0; i < results.length; i++) {
|
|
|
|
var node = document.createElement('li');
|
|
|
|
node.setAttribute('class', 'dropdown-item');
|
|
|
|
node.appendChild(document.createTextNode(results[i]));
|
|
|
|
ul.appendChild(node);
|
|
|
|
node.addEventListener('click',
|
|
|
|
(e) => {
|
|
|
|
var element = e.target;
|
|
|
|
var text_box = document.getElementById(this.text_id);
|
|
|
|
text_box.value = element.innerHTML;
|
|
|
|
var dropdown = bootstrap.Dropdown.getOrCreateInstance(text_box);
|
|
|
|
dropdown.hide();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (results.length == 0) {
|
|
|
|
var node = document.createElement('li');
|
|
|
|
node.setAttribute('class', 'dropdown-item text-danger');
|
|
|
|
node.appendChild(document.createTextNode('No results'));
|
|
|
|
ul.appendChild(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dropdown = bootstrap.Dropdown.getOrCreateInstance(document.getElementById(this.text_id));
|
|
|
|
dropdown.show();
|
|
|
|
}
|
|
|
|
}
|