2018-10-09 11:25:53 -04:00
|
|
|
/* eslint-disable func-names, one-var, no-var, object-shorthand, no-else-return */
|
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';
|
|
|
|
import flash from './flash';
|
2018-05-02 18:23:48 -04:00
|
|
|
import { capitalizeFirstCharacter } from './lib/utils/text_utility';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2018-05-02 18:23:48 -04:00
|
|
|
export default function initCompareAutocomplete(limitTo = null, clickHandler = () => {}) {
|
2017-10-12 23:51:09 -04:00
|
|
|
$('.js-compare-dropdown').each(function() {
|
2017-10-12 16:57:29 -04:00
|
|
|
var $dropdown, selected;
|
|
|
|
$dropdown = $(this);
|
|
|
|
selected = $dropdown.data('selected');
|
|
|
|
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);
|
|
|
|
$dropdown.glDropdown({
|
|
|
|
data: function(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"]',
|
|
|
|
renderRow: function(ref) {
|
|
|
|
var link;
|
|
|
|
if (ref.header != null) {
|
2018-05-02 18:23:48 -04:00
|
|
|
return $('<li />')
|
|
|
|
.addClass('dropdown-header')
|
|
|
|
.text(ref.header);
|
2017-10-12 16:57:29 -04:00
|
|
|
} else {
|
2018-05-02 18:23:48 -04:00
|
|
|
link = $('<a />')
|
|
|
|
.attr('href', '#')
|
|
|
|
.addClass(ref === selected ? 'is-active' : '')
|
|
|
|
.text(ref)
|
2018-05-25 10:22:17 -04:00
|
|
|
.attr('data-ref', ref);
|
2017-10-12 16:57:29 -04:00
|
|
|
return $('<li />').append(link);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-10-12 16:57:29 -04:00
|
|
|
},
|
|
|
|
id: function(obj, $el) {
|
|
|
|
return $el.attr('data-ref');
|
|
|
|
},
|
|
|
|
toggleLabel: function(obj, $el) {
|
|
|
|
return $el.text().trim();
|
2018-05-02 18:23:48 -04:00
|
|
|
},
|
|
|
|
clicked: () => clickHandler($dropdown),
|
2017-10-12 16:57:29 -04:00
|
|
|
});
|
2018-05-02 18:23:48 -04: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
|
|
|
|
2018-05-02 18:23:48 -04: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')) {
|
2018-04-19 11:40:23 -04:00
|
|
|
$dropdown.tooltip('_fixTitle');
|
2017-10-12 16:57:29 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|