debian-mirror-gitlab/app/assets/javascripts/compare_autocomplete.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
/* eslint-disable func-names */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as flash } from './flash';
2018-10-15 14:42:47 +05:30
import { capitalizeFirstCharacter } from './lib/utils/text_utility';
2021-01-29 00:20:46 +05:30
import { fixTitle } from '~/tooltips';
2020-11-24 15:15:51 +05:30
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
export default function initCompareAutocomplete(limitTo = null, clickHandler = () => {}) {
2018-03-17 18:26:18 +05:30
$('.js-compare-dropdown').each(function() {
2019-12-26 22:10:19 +05:30
const $dropdown = $(this);
const selected = $dropdown.data('selected');
2018-03-17 18:26:18 +05:30
const $dropdownContainer = $dropdown.closest('.dropdown');
2018-03-27 19:54:05 +05:30
const $fieldInput = $(`input[name="${$dropdown.data('fieldName')}"]`, $dropdownContainer);
2018-03-17 18:26:18 +05:30
const $filterInput = $('input[type="search"]', $dropdownContainer);
2020-11-24 15:15:51 +05:30
initDeprecatedJQueryDropdown($dropdown, {
2019-12-04 20:38:33 +05:30
data(term, callback) {
2018-10-15 14:42:47 +05:30
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')));
2018-03-17 18:26:18 +05:30
},
selectable: true,
filterable: true,
2019-09-04 21:01:54 +05:30
filterRemote: Boolean($dropdown.data('refsUrl')),
2018-03-27 19:54:05 +05:30
fieldName: $dropdown.data('fieldName'),
2018-03-17 18:26:18 +05:30
filterInput: 'input[type="search"]',
2019-12-04 20:38:33 +05:30
renderRow(ref) {
2019-12-26 22:10:19 +05:30
const link = $('<a />')
.attr('href', '#')
.addClass(ref === selected ? 'is-active' : '')
.text(ref)
.attr('data-ref', ref);
2018-03-17 18:26:18 +05:30
if (ref.header != null) {
2018-10-15 14:42:47 +05:30
return $('<li />')
.addClass('dropdown-header')
.text(ref.header);
2017-08-17 22:00:37 +05:30
}
2020-05-24 23:13:21 +05:30
return $('<li />').append(link);
2018-03-17 18:26:18 +05:30
},
2019-12-04 20:38:33 +05:30
id(obj, $el) {
2018-03-17 18:26:18 +05:30
return $el.attr('data-ref');
},
2019-12-04 20:38:33 +05:30
toggleLabel(obj, $el) {
2018-03-17 18:26:18 +05:30
return $el.text().trim();
2018-10-15 14:42:47 +05:30
},
clicked: () => clickHandler($dropdown),
2018-03-17 18:26:18 +05:30
});
2018-10-15 14:42:47 +05:30
$filterInput.on('keyup', e => {
2018-03-17 18:26:18 +05:30
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-08-17 22:00:37 +05:30
});
2018-10-15 14:42:47 +05:30
$dropdownContainer.on('click', '.dropdown-content a', e => {
2018-03-17 18:26:18 +05:30
$dropdown.prop('title', e.target.text.replace(/_+?/g, '-'));
if ($dropdown.hasClass('has-tooltip')) {
2021-01-29 00:20:46 +05:30
fixTitle($dropdown);
2018-03-17 18:26:18 +05:30
}
});
});
}