gitlab-org--gitlab-foss/app/assets/javascripts/ref_select_dropdown.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
class RefSelectDropdown {
constructor($dropdownButton, availableRefs) {
const availableRefsValue =
availableRefs || JSON.parse(document.getElementById('availableRefs').innerHTML);
initDeprecatedJQueryDropdown($dropdownButton, {
data: availableRefsValue,
filterable: true,
filterByText: true,
remote: false,
2018-02-20 17:20:48 -05:00
fieldName: $dropdownButton.data('fieldName'),
filterInput: 'input[type="search"]',
selectable: true,
isSelectable(branch, $el) {
return !$el.hasClass('is-active');
},
text(branch) {
return branch;
},
id(branch) {
return branch;
},
toggleLabel(branch) {
return branch;
},
});
const $dropdownContainer = $dropdownButton.closest('.dropdown');
2018-02-20 17:20:48 -05:00
const $fieldInput = $(`input[name="${$dropdownButton.data('fieldName')}"]`, $dropdownContainer);
const $filterInput = $('input[type="search"]', $dropdownContainer);
$filterInput.on('keyup', (e) => {
const keyCode = e.keyCode || e.which;
if (keyCode !== 13) return;
const ref = $filterInput.val().trim();
if (ref === '') {
return;
}
$fieldInput.val(ref);
$('.dropdown-toggle-text', $dropdownButton).text(ref);
$dropdownContainer.removeClass('open');
});
}
}
export default RefSelectDropdown;