/*jslint esversion: 6*/
import { $gettext } from './gettext.js';
/* ------------------------------------------------------------------------
* QuickSearch inputs
* ------------------------------------------------------------------------ */
const QuickSearch = {
/**
* the function to be called from the QuickSearch class template
* @param name string: ID of input
* @param url string: URL of AJAX-response
* @param func string: name of a possible function executed
* when user has selected something
* @return: void
*/
autocomplete: function(name, url, func, disabled) {
if (disabled === undefined || disabled !== true) {
var appendTo = 'body';
if (jQuery(`#${name}_frame`).length > 0) {
appendTo = `#${name}_frame`;
} else if ($(`#${name}`).closest('.ui-dialog').length > 0) {
appendTo = $(`#${name}`).closest('.ui-dialog');
}
jQuery('#' + name).quicksearch({
delay: 500,
minLength: 3,
appendTo: appendTo,
create: function() {
if ($(this).is('[autofocus]')) {
$(this).focus();
}
},
position: $('#' + name).is('.expand-to-left')
? {
my: 'right top',
at: 'right bottom',
collision: 'none'
}
: {
my: 'left top',
at: 'left bottom',
collision: 'none'
},
source: function(input, add) {
//get the variables that should be sent:
var send_vars = jQuery('#' + name)
.closest('form')
.serializeArray();
send_vars.push({
name: 'request',
value: input.term
});
jQuery
.ajax({
url: url,
type: 'post',
data: send_vars
})
.done(function(data) {
var stripTags = /<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi;
//an array of possible selections
if (!data.length) {
add([{
value: '',
label: $gettext('Kein Ergebnis gefunden.'),
disabled: true
}]);
return;
}
var suggestions = _.map(data, function(val) {
//adding a label and a hidden item_id - don't use "value":
var label_text = val.item_name;
if (val.item_description !== undefined) {
label_text += '
' + val.item_description;
}
return {
//what is displayed in the drop down box
label: label_text,
//the hidden ID of the item
item_id: val.item_id,
//what is inserted in the visible input box
value:
val.item_search_name !== null
? val.item_search_name
: jQuery('