Commit b4593a1b authored by Alfredo Sumaran's avatar Alfredo Sumaran

Fix multiple ajax calls and plugin instantiation

parent 9008457a
...@@ -33,6 +33,7 @@ class @SearchAutocomplete ...@@ -33,6 +33,7 @@ class @SearchAutocomplete
@saveOriginalState() @saveOriginalState()
@searchInput.addClass('disabled') @searchInput.addClass('disabled')
@autocomplete = false
@bindEvents() @bindEvents()
...@@ -61,8 +62,12 @@ class @SearchAutocomplete ...@@ -61,8 +62,12 @@ class @SearchAutocomplete
@searchInput.on 'blur', @onSearchInputBlur @searchInput.on 'blur', @onSearchInputBlur
enableAutocomplete: -> enableAutocomplete: ->
return if @autocomplete
dropdownMenu = @dropdown.find('.dropdown-menu') dropdownMenu = @dropdown.find('.dropdown-menu')
_this = @ _this = @
loading = false
@searchInput.glDropdown @searchInput.glDropdown
filterInputBlur: false filterInputBlur: false
filterable: true filterable: true
...@@ -72,7 +77,19 @@ class @SearchAutocomplete ...@@ -72,7 +77,19 @@ class @SearchAutocomplete
search: search:
fields: ['text'] fields: ['text']
data: (term, callback) -> data: (term, callback) ->
$.get(_this.autocompletePath, { # Ensure this is not called when autocomplete is disabled because
# this method still will be called because `GitLabDropdownFilter` is triggering this on keyup
return if _this.autocomplete is false
# Do not trigger request if input is empty
return if _this.searchInput.val() is ''
# Prevent multiple ajax calls
return if loading
loading = true
jqXHR = $.get(_this.autocompletePath, {
project_id: _this.projectId project_id: _this.projectId
project_ref: _this.projectRef project_ref: _this.projectRef
term: term term: term
...@@ -99,7 +116,8 @@ class @SearchAutocomplete ...@@ -99,7 +116,8 @@ class @SearchAutocomplete
text: item.label text: item.label
url: item.url url: item.url
callback(data) callback(data)
) ).always ->
loading = false
@dropdown.addClass('open') @dropdown.addClass('open')
@searchInput.removeClass('disabled') @searchInput.removeClass('disabled')
...@@ -109,23 +127,26 @@ class @SearchAutocomplete ...@@ -109,23 +127,26 @@ class @SearchAutocomplete
@dropdown.dropdown('toggle') @dropdown.dropdown('toggle')
onSearchInputKeyDown: (e) => onSearchInputKeyDown: (e) =>
# Remove tag when pressing backspace and input search is empty switch e.keyCode
if e.keyCode is KEYCODE.BACKSPACE and e.currentTarget.value is '' when KEYCODE.BACKSPACE
@removeLocationBadge() if e.currentTarget.value is ''
@searchInput.focus() @removeLocationBadge()
@searchInput.focus()
when KEYCODE.ESCAPE
if @badgePresent()
else
@restoreOriginalState()
else if e.keyCode is KEYCODE.ESCAPE # If after restoring there's a badge
@searchInput.val('') @disableAutocomplete() if @badgePresent()
@restoreOriginalState()
else
# Create new autocomplete if it hasn't been created yet and there's no badge
if @autocomplete is undefined
if !@badgePresent()
@enableAutocomplete()
else else
# There's a badge
if @badgePresent() if @badgePresent()
@disableAutocomplete() @disableAutocomplete()
else
@enableAutocomplete()
# Avoid falsy value to be returned
return
onSearchInputFocus: => onSearchInputFocus: =>
@wrap.addClass('search-active') @wrap.addClass('search-active')
...@@ -193,6 +214,6 @@ class @SearchAutocomplete ...@@ -193,6 +214,6 @@ class @SearchAutocomplete
@resetSearchState() @resetSearchState()
disableAutocomplete: -> disableAutocomplete: ->
if @autocomplete? if @autocomplete
@searchInput.addClass('disabled') @searchInput.addClass('disabled')
@autocomplete = null @autocomplete = false
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