Merge pull request #2225 from aignatov-bio/ai-sci-4089-add-arrow-control-and-tag-select-on-enter

Add arrows control for dropdown [SCI-4089]
This commit is contained in:
aignatov-bio 2019-11-25 13:54:45 +01:00 committed by GitHub
commit 30e78a0b3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 3 deletions

View file

@ -3,7 +3,6 @@ var ProtocolRepositoryHeader = (function() {
function initEditKeywords() { function initEditKeywords() {
dropdownSelector.init('#keyword-input-field', { dropdownSelector.init('#keyword-input-field', {
inputTagMode: true, inputTagMode: true,
selectKeys: [13],
onChange: function() { onChange: function() {
$.ajax({ $.ajax({
url: $('#keyword-input-field').data('update-url'), url: $('#keyword-input-field').data('update-url'),

View file

@ -35,7 +35,7 @@
onUnSelect: function(), // Run action after unselect onUnSelect: function(), // Run action after unselect
customDropdownIcon: function(), // Add custom dropdown icon customDropdownIcon: function(), // Add custom dropdown icon
inputTagMode: boolean, // Use as simple input tag field inputTagMode: boolean, // Use as simple input tag field
selectKeys: array, // array of keys id which use for fast select selectKeys: array, // array of keys id which use for fast select // default - [13]
noEmptyOption: boolean, // use defaut select (only for single option select). default 'false' noEmptyOption: boolean, // use defaut select (only for single option select). default 'false'
singleSelect: boolean, // disable multiple select. default 'false' singleSelect: boolean, // disable multiple select. default 'false'
selectAppearance: string, // 'tag' or 'simple'. Default 'tag' selectAppearance: string, // 'tag' or 'simple'. Default 'tag'
@ -209,6 +209,29 @@ var dropdownSelector = (function() {
updateTags(selector, container, { select: true }); updateTags(selector, container, { select: true });
} }
// intialization keyboard control
function initKeyboardControl(container) {
container.find('.search-field').keydown(function(e) {
var dropdownContainer = container.find('.dropdown-container');
var pressedKey = e.keyCode;
var selectedOption = dropdownContainer.find('.highlight');
if (selectedOption.length === 0 && (pressedKey === 38 || pressedKey === 40)) {
dropdownContainer.find('.dropdown-option').first().addClass('highlight');
}
if (pressedKey === 38) {
if (selectedOption.prev('.dropdown-option').length) {
selectedOption.removeClass('highlight').prev('.dropdown-option').addClass('highlight');
}
} else if (pressedKey === 40) {
if (selectedOption.next('.dropdown-option').length) {
selectedOption.removeClass('highlight').next('.dropdown-option').addClass('highlight');
}
}
});
}
// ////////////////////// // //////////////////////
// Private functions /// // Private functions ///
// ///////////////////// // /////////////////////
@ -265,13 +288,22 @@ var dropdownSelector = (function() {
// When we start typing it will dynamically update data // When we start typing it will dynamically update data
dropdownContainer.find('.search-field') dropdownContainer.find('.search-field')
.keyup((e) => { .keyup((e) => {
if (e.keyCode === 38
|| e.keyCode === 40
|| (config.selectKeys || []).includes(e.keyCode)) {
return;
}
e.stopPropagation(); e.stopPropagation();
loadData(selectElement, dropdownContainer); loadData(selectElement, dropdownContainer);
}) })
.keypress((e) => { .keypress((e) => {
if ((config.selectKeys || []).includes(e.keyCode)) { if ((config.selectKeys || [13]).includes(e.keyCode)) {
if (config.inputTagMode) { if (config.inputTagMode) {
addNewTag(selectElement, dropdownContainer); addNewTag(selectElement, dropdownContainer);
} else if (dropdownContainer.find('.highlight').length) {
dropdownContainer.find('.highlight').click();
} else {
dropdownContainer.find('.dropdown-option').first().click();
} }
} }
}).click((e) =>{ }).click((e) =>{
@ -332,6 +364,7 @@ var dropdownSelector = (function() {
// When user will click away, we must close dropdown // When user will click away, we must close dropdown
$(window).click(() => { $(window).click(() => {
if (dropdownContainer.hasClass('open') && config.onClose) { if (dropdownContainer.hasClass('open') && config.onClose) {
dropdownContainer.find('.search-field').val('');
config.onClose(); config.onClose();
} }
dropdownContainer.removeClass('open active'); dropdownContainer.removeClass('open active');
@ -362,6 +395,9 @@ var dropdownSelector = (function() {
dropdownContainer.addClass('simple-mode'); dropdownContainer.addClass('simple-mode');
} }
// initialization keyboard controll
initKeyboardControl(dropdownContainer);
// In some case dropdown position not correclty calculated // In some case dropdown position not correclty calculated
updateDropdownDirection(selectElement, dropdownContainer); updateDropdownDirection(selectElement, dropdownContainer);
} }

View file

@ -166,6 +166,7 @@
background: $color-concrete; background: $color-concrete;
} }
&.highlight,
&:hover { &:hover {
background: $brand-primary-light; background: $brand-primary-light;
} }