Fixes empty menu when typing on search input for the very first time
This commit is contained in:
parent
4fcd7ba954
commit
44817726fe
|
@ -31,6 +31,8 @@ class @SearchAutocomplete
|
||||||
|
|
||||||
@saveOriginalState()
|
@saveOriginalState()
|
||||||
|
|
||||||
|
@createAutocomplete()
|
||||||
|
|
||||||
@searchInput.addClass('disabled')
|
@searchInput.addClass('disabled')
|
||||||
@autocomplete = false
|
@autocomplete = false
|
||||||
|
|
||||||
|
@ -43,32 +45,7 @@ class @SearchAutocomplete
|
||||||
saveOriginalState: ->
|
saveOriginalState: ->
|
||||||
@originalState = @serializeState()
|
@originalState = @serializeState()
|
||||||
|
|
||||||
serializeState: ->
|
createAutocomplete: ->
|
||||||
{
|
|
||||||
# Search Criteria
|
|
||||||
project_id: @projectInputEl.val()
|
|
||||||
group_id: @groupInputEl.val()
|
|
||||||
search_code: @searchCodeInputEl.val()
|
|
||||||
repository_ref: @repositoryInputEl.val()
|
|
||||||
scope: @scopeInputEl.val()
|
|
||||||
|
|
||||||
# Location badge
|
|
||||||
_location: $.trim(@locationText.text())
|
|
||||||
}
|
|
||||||
|
|
||||||
bindEvents: ->
|
|
||||||
@searchInput.on 'keydown', @onSearchInputKeyDown
|
|
||||||
@searchInput.on 'focus', @onSearchInputFocus
|
|
||||||
@searchInput.on 'blur', @onSearchInputBlur
|
|
||||||
@clearInput.on 'click', @onRemoveLocationClick
|
|
||||||
|
|
||||||
enableAutocomplete: ->
|
|
||||||
return if @autocomplete
|
|
||||||
|
|
||||||
dropdownMenu = @dropdown.find('.dropdown-menu')
|
|
||||||
_this = @
|
|
||||||
loading = false
|
|
||||||
|
|
||||||
@searchInput.glDropdown
|
@searchInput.glDropdown
|
||||||
filterInputBlur: false
|
filterInputBlur: false
|
||||||
filterable: true
|
filterable: true
|
||||||
|
@ -77,22 +54,26 @@ class @SearchAutocomplete
|
||||||
filterInput: 'input#search'
|
filterInput: 'input#search'
|
||||||
search:
|
search:
|
||||||
fields: ['text']
|
fields: ['text']
|
||||||
data: (term, callback) ->
|
data: @getData.bind(@)
|
||||||
|
|
||||||
|
getData: (term, callback) ->
|
||||||
|
_this = @
|
||||||
|
|
||||||
# Ensure this is not called when autocomplete is disabled because
|
# Ensure this is not called when autocomplete is disabled because
|
||||||
# this method still will be called because `GitLabDropdownFilter` is triggering this on keyup
|
# this method still will be called because `GitLabDropdownFilter` is triggering this on keyup
|
||||||
return if _this.autocomplete is false
|
return if @autocomplete is false
|
||||||
|
|
||||||
# Do not trigger request if input is empty
|
# Do not trigger request if input is empty
|
||||||
return if _this.searchInput.val() is ''
|
return if @searchInput.val() is ''
|
||||||
|
|
||||||
# Prevent multiple ajax calls
|
# Prevent multiple ajax calls
|
||||||
return if loading
|
return if @loadingSuggestions
|
||||||
|
|
||||||
loading = true
|
@loadingSuggestions = true
|
||||||
|
|
||||||
jqXHR = $.get(_this.autocompletePath, {
|
jqXHR = $.get(@autocompletePath, {
|
||||||
project_id: _this.projectId
|
project_id: @projectId
|
||||||
project_ref: _this.projectRef
|
project_ref: @projectRef
|
||||||
term: term
|
term: term
|
||||||
}, (response) ->
|
}, (response) ->
|
||||||
data = []
|
data = []
|
||||||
|
@ -113,7 +94,34 @@ class @SearchAutocomplete
|
||||||
|
|
||||||
callback(data)
|
callback(data)
|
||||||
).always ->
|
).always ->
|
||||||
loading = false
|
_this.loadingSuggestions = false
|
||||||
|
|
||||||
|
serializeState: ->
|
||||||
|
{
|
||||||
|
# Search Criteria
|
||||||
|
project_id: @projectInputEl.val()
|
||||||
|
group_id: @groupInputEl.val()
|
||||||
|
search_code: @searchCodeInputEl.val()
|
||||||
|
repository_ref: @repositoryInputEl.val()
|
||||||
|
scope: @scopeInputEl.val()
|
||||||
|
|
||||||
|
# Location badge
|
||||||
|
_location: $.trim(@locationText.text())
|
||||||
|
}
|
||||||
|
|
||||||
|
bindEvents: ->
|
||||||
|
@searchInput.on 'keyup', @onSearchInputKeyUp
|
||||||
|
@searchInput.on 'click', @onSearchInputClick
|
||||||
|
@searchInput.on 'focus', @onSearchInputFocus
|
||||||
|
@searchInput.on 'blur', @onSearchInputBlur
|
||||||
|
@clearInput.on 'click', @onRemoveLocationClick
|
||||||
|
|
||||||
|
enableAutocomplete: ->
|
||||||
|
return if @autocomplete
|
||||||
|
|
||||||
|
dropdownMenu = @dropdown.find('.dropdown-menu')
|
||||||
|
_this = @
|
||||||
|
@loadingSuggestions = false
|
||||||
|
|
||||||
@dropdown.addClass('open')
|
@dropdown.addClass('open')
|
||||||
@searchInput.removeClass('disabled')
|
@searchInput.removeClass('disabled')
|
||||||
|
@ -122,7 +130,7 @@ class @SearchAutocomplete
|
||||||
onDropdownOpen: (e) =>
|
onDropdownOpen: (e) =>
|
||||||
@dropdown.dropdown('toggle')
|
@dropdown.dropdown('toggle')
|
||||||
|
|
||||||
onSearchInputKeyDown: (e) =>
|
onSearchInputKeyUp: (e) =>
|
||||||
switch e.keyCode
|
switch e.keyCode
|
||||||
when KEYCODE.BACKSPACE
|
when KEYCODE.BACKSPACE
|
||||||
if e.currentTarget.value is ''
|
if e.currentTarget.value is ''
|
||||||
|
@ -139,11 +147,18 @@ class @SearchAutocomplete
|
||||||
if @badgePresent()
|
if @badgePresent()
|
||||||
@disableAutocomplete()
|
@disableAutocomplete()
|
||||||
else
|
else
|
||||||
|
|
||||||
|
# We should display the menu only when input is not empty
|
||||||
|
if @searchInput.val() isnt ''
|
||||||
@enableAutocomplete()
|
@enableAutocomplete()
|
||||||
|
|
||||||
# Avoid falsy value to be returned
|
# Avoid falsy value to be returned
|
||||||
return
|
return
|
||||||
|
|
||||||
|
onSearchInputClick: =>
|
||||||
|
if (@searchInput.val() is '')
|
||||||
|
@disableAutocomplete()
|
||||||
|
|
||||||
onSearchInputFocus: =>
|
onSearchInputFocus: =>
|
||||||
@wrap.addClass('search-active')
|
@wrap.addClass('search-active')
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,10 @@
|
||||||
.dropdown{ data: {url: search_autocomplete_path } }
|
.dropdown{ data: {url: search_autocomplete_path } }
|
||||||
= search_field_tag "search", nil, placeholder: 'Search', class: "search-input dropdown-menu-toggle", spellcheck: false, tabindex: "1", autocomplete: 'off', data: { toggle: 'dropdown' }
|
= search_field_tag "search", nil, placeholder: 'Search', class: "search-input dropdown-menu-toggle", spellcheck: false, tabindex: "1", autocomplete: 'off', data: { toggle: 'dropdown' }
|
||||||
.dropdown-menu.dropdown-select
|
.dropdown-menu.dropdown-select
|
||||||
= dropdown_content
|
= dropdown_content do
|
||||||
|
%li
|
||||||
|
%a.is-focused
|
||||||
|
Loading...
|
||||||
= dropdown_loading
|
= dropdown_loading
|
||||||
%i.search-icon
|
%i.search-icon
|
||||||
%i.clear-icon.js-clear-input
|
%i.clear-icon.js-clear-input
|
||||||
|
|
Loading…
Reference in New Issue