Commit 0e290a84 authored by Phil Hughes's avatar Phil Hughes

Started arrow key movement on dropdowns

parent c3875b5f
...@@ -96,6 +96,7 @@ class GitLabDropdown ...@@ -96,6 +96,7 @@ class GitLabDropdown
LOADING_CLASS = "is-loading" LOADING_CLASS = "is-loading"
PAGE_TWO_CLASS = "is-page-two" PAGE_TWO_CLASS = "is-page-two"
ACTIVE_CLASS = "is-active" ACTIVE_CLASS = "is-active"
CURRENT_INDEX = 0
FILTER_INPUT = '.dropdown-input .dropdown-input-field' FILTER_INPUT = '.dropdown-input .dropdown-input-field'
...@@ -218,6 +219,8 @@ class GitLabDropdown ...@@ -218,6 +219,8 @@ class GitLabDropdown
return true return true
opened: => opened: =>
@addArrowKeyEvent()
contentHtml = $('.dropdown-content', @dropdown).html() contentHtml = $('.dropdown-content', @dropdown).html()
if @remote && contentHtml is "" if @remote && contentHtml is ""
@remote.execute() @remote.execute()
...@@ -228,6 +231,7 @@ class GitLabDropdown ...@@ -228,6 +231,7 @@ class GitLabDropdown
@dropdown.trigger('shown.gl.dropdown') @dropdown.trigger('shown.gl.dropdown')
hidden: (e) => hidden: (e) =>
@removeArrayKeyEvent()
if @options.filterable if @options.filterable
@dropdown @dropdown
.find(".dropdown-input-field") .find(".dropdown-input-field")
...@@ -322,8 +326,8 @@ class GitLabDropdown ...@@ -322,8 +326,8 @@ class GitLabDropdown
).join('') ).join('')
noResults: -> noResults: ->
html = "<li>" html = "<li class='dropdown-menu-empty-link is-focused'>"
html += "<a class='dropdown-menu-empty-link is-focused'>" html += "<a href='#'>"
html += "No matching results." html += "No matching results."
html += "</a>" html += "</a>"
html += "</li>" html += "</li>"
...@@ -343,7 +347,7 @@ class GitLabDropdown ...@@ -343,7 +347,7 @@ class GitLabDropdown
selectedObject = @renderedData[selectedIndex] selectedObject = @renderedData[selectedIndex]
value = if @options.id then @options.id(selectedObject, el) else selectedObject.id value = if @options.id then @options.id(selectedObject, el) else selectedObject.id
field = @dropdown.parent().find("input[name='#{fieldName}'][value='#{value}']") field = @dropdown.parent().find("input[name='#{fieldName}'][value='#{value}']")
if el.hasClass(ACTIVE_CLASS) if el.hasClass(ACTIVE_CLASS)
el.removeClass(ACTIVE_CLASS) el.removeClass(ACTIVE_CLASS)
field.remove() field.remove()
...@@ -384,8 +388,53 @@ class GitLabDropdown ...@@ -384,8 +388,53 @@ class GitLabDropdown
# simulate a click on the first link # simulate a click on the first link
$(selector).trigger "click" $(selector).trigger "click"
addArrowKeyEvent: ->
ARROW_KEY_CODES = [38, 40]
$input = @dropdown.find(".dropdown-input-field")
selector = '.dropdown-content li:not(.divider)'
if @dropdown.find(".dropdown-toggle-page").length
selector = ".dropdown-page-one #{selector}"
$('body').on 'keydown', (e) =>
currentKeyCode = e.keyCode
if ARROW_KEY_CODES.indexOf(currentKeyCode) >= 0
e.preventDefault()
e.stopPropagation()
$listItems = $(selector, @dropdown)
if @options.filterable
$input.blur()
if currentKeyCode is 40
# Move down
CURRENT_INDEX += 1 if CURRENT_INDEX < $listItems.length
else if currentKeyCode is 38
# Move up
CURRENT_INDEX -= 1 if CURRENT_INDEX > 0
@highlightRowAtIndex(CURRENT_INDEX)
return false
removeArrayKeyEvent: ->
$('body').off 'keydown'
highlightRowAtIndex: (index, prevIndex) ->
# Remove the class for the previously focused row
$('.is-focused', @dropdown).removeClass 'is-focused'
# Update the class for the row at the specific index
selector = ".dropdown-content li:not(.divider):eq(#{index})"
if @dropdown.find(".dropdown-toggle-page").length
selector = ".dropdown-page-one #{selector}"
$listItem = $(selector, @dropdown)
$listItem.addClass "is-focused"
$.fn.glDropdown = (opts) -> $.fn.glDropdown = (opts) ->
return @.each -> return @.each ->
if (!$.data @, 'glDropdown') if (!$.data @, 'glDropdown')
$.data(@, 'glDropdown', new GitLabDropdown @, opts) $.data(@, 'glDropdown', new GitLabDropdown @, opts)
...@@ -104,6 +104,14 @@ ...@@ -104,6 +104,14 @@
padding: 0 10px; padding: 0 10px;
} }
.is-focused {
a {
background-color: $dropdown-link-hover-bg;
text-decoration: none;
outline: 0;
}
}
.divider { .divider {
height: 1px; height: 1px;
margin: 8px 10px; margin: 8px 10px;
...@@ -132,8 +140,7 @@ ...@@ -132,8 +140,7 @@
overflow: hidden; overflow: hidden;
&:hover, &:hover,
&:focus, &:focus {
&.is-focused {
background-color: $dropdown-link-hover-bg; background-color: $dropdown-link-hover-bg;
text-decoration: none; text-decoration: none;
outline: 0; outline: 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment