2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable func-names */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2018-01-26 10:04:44 -05:00
|
|
|
import { __ } from './locale';
|
2018-01-26 05:14:44 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as flash } from './flash';
|
2018-05-02 18:23:48 -04:00
|
|
|
import { capitalizeFirstCharacter } from './lib/utils/text_utility';
|
2020-10-20 14:08:54 -04:00
|
|
|
import { fixTitle } from '~/tooltips';
|
2020-08-24 14:10:19 -04:00
|
|
|
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2018-05-02 18:23:48 -04:00
|
|
|
export default function initCompareAutocomplete(limitTo = null, clickHandler = () => {}) {
|
2020-12-23 07:10:26 -05:00
|
|
|
$('.js-compare-dropdown').each(function () {
|
2019-11-14 10:06:08 -05:00
|
|
|
const $dropdown = $(this);
|
|
|
|
const selected = $dropdown.data('selected');
|
2017-10-12 16:57:29 -04:00
|
|
|
const $dropdownContainer = $dropdown.closest('.dropdown');
|
2018-02-20 17:20:48 -05:00
|
|
|
const $fieldInput = $(`input[name="${$dropdown.data('fieldName')}"]`, $dropdownContainer);
|
2017-10-12 16:57:29 -04:00
|
|
|
const $filterInput = $('input[type="search"]', $dropdownContainer);
|
2020-08-24 14:10:19 -04:00
|
|
|
initDeprecatedJQueryDropdown($dropdown, {
|
2019-09-18 10:02:45 -04:00
|
|
|
data(term, callback) {
|
2018-05-02 18:23:48 -04:00
|
|
|
const params = {
|
|
|
|
ref: $dropdown.data('ref'),
|
|
|
|
search: term,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (limitTo) {
|
|
|
|
params.find = limitTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios
|
|
|
|
.get($dropdown.data('refsUrl'), {
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
if (limitTo) {
|
|
|
|
callback(data[capitalizeFirstCharacter(limitTo)] || []);
|
|
|
|
} else {
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => flash(__('Error fetching refs')));
|
2017-10-12 16:57:29 -04:00
|
|
|
},
|
|
|
|
selectable: true,
|
|
|
|
filterable: true,
|
2019-06-03 18:51:02 -04:00
|
|
|
filterRemote: Boolean($dropdown.data('refsUrl')),
|
2018-02-20 17:20:48 -05:00
|
|
|
fieldName: $dropdown.data('fieldName'),
|
2017-10-12 16:57:29 -04:00
|
|
|
filterInput: 'input[type="search"]',
|
2019-09-18 10:02:45 -04:00
|
|
|
renderRow(ref) {
|
2019-11-14 10:06:08 -05:00
|
|
|
const link = $('<a />')
|
|
|
|
.attr('href', '#')
|
|
|
|
.addClass(ref === selected ? 'is-active' : '')
|
|
|
|
.text(ref)
|
|
|
|
.attr('data-ref', ref);
|
2017-10-12 16:57:29 -04:00
|
|
|
if (ref.header != null) {
|
2020-12-23 07:10:26 -05:00
|
|
|
return $('<li />').addClass('dropdown-header').text(ref.header);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return $('<li />').append(link);
|
2017-10-12 16:57:29 -04:00
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
id(obj, $el) {
|
2017-10-12 16:57:29 -04:00
|
|
|
return $el.attr('data-ref');
|
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
toggleLabel(obj, $el) {
|
2017-10-12 16:57:29 -04:00
|
|
|
return $el.text().trim();
|
2018-05-02 18:23:48 -04:00
|
|
|
},
|
|
|
|
clicked: () => clickHandler($dropdown),
|
2017-10-12 16:57:29 -04:00
|
|
|
});
|
2020-12-23 16:10:24 -05:00
|
|
|
$filterInput.on('keyup', (e) => {
|
2017-10-12 16:57:29 -04:00
|
|
|
const keyCode = e.keyCode || e.which;
|
|
|
|
if (keyCode !== 13) return;
|
|
|
|
const text = $filterInput.val();
|
|
|
|
$fieldInput.val(text);
|
|
|
|
$('.dropdown-toggle-text', $dropdown).text(text);
|
|
|
|
$dropdownContainer.removeClass('open');
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
$dropdownContainer.on('click', '.dropdown-content a', (e) => {
|
2017-10-12 16:57:29 -04:00
|
|
|
$dropdown.prop('title', e.target.text.replace(/_+?/g, '-'));
|
|
|
|
if ($dropdown.hasClass('has-tooltip')) {
|
2020-10-20 14:08:54 -04:00
|
|
|
fixTitle($dropdown);
|
2017-10-12 16:57:29 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|