Commit ee87d15c authored by Alfredo Sumaran's avatar Alfredo Sumaran

Change approach, do not use timeouts

parent 7baebd32
...@@ -137,13 +137,17 @@ class @SearchAutocomplete ...@@ -137,13 +137,17 @@ class @SearchAutocomplete
} }
bindEvents: -> bindEvents: ->
$(document).on 'click', @onDocumentClick
@searchInput.on 'keydown', @onSearchInputKeyDown @searchInput.on 'keydown', @onSearchInputKeyDown
@searchInput.on 'keyup', @onSearchInputKeyUp @searchInput.on 'keyup', @onSearchInputKeyUp
@searchInput.on 'click', @onSearchInputClick @searchInput.on 'click', @onSearchInputClick
@searchInput.on 'focus', @onSearchInputFocus @searchInput.on 'focus', @onSearchInputFocus
@searchInput.on 'blur', @onSearchInputBlur
@clearInput.on 'click', @onRemoveLocationClick @clearInput.on 'click', @onRemoveLocationClick
onDocumentClick: (e) =>
if not $.contains(@dropdown[0], e.target) and @isFocused
@onSearchInputBlur()
enableAutocomplete: -> enableAutocomplete: ->
# No need to enable anything if user is not logged in # No need to enable anything if user is not logged in
return if !gon.current_user_id return if !gon.current_user_id
...@@ -193,27 +197,21 @@ class @SearchAutocomplete ...@@ -193,27 +197,21 @@ class @SearchAutocomplete
e.stopImmediatePropagation() e.stopImmediatePropagation()
onSearchInputFocus: => onSearchInputFocus: =>
@isFocused = true
@wrap.addClass('search-active') @wrap.addClass('search-active')
onRemoveLocationClick: (e) => onRemoveLocationClick: (e) =>
e.preventDefault() e.preventDefault()
@removeLocationBadge() @removeLocationBadge()
@searchInput.val('').focus() @searchInput.val('').focus()
@skipBlurEvent = true
onSearchInputBlur: (e) => onSearchInputBlur: (e) =>
@skipBlurEvent = false @isFocused = false
# We should wait to make sure we are not clearing the input instead
setTimeout( =>
return if @skipBlurEvent
@wrap.removeClass('search-active') @wrap.removeClass('search-active')
# If input is blank then restore state # If input is blank then restore state
if @searchInput.val() is '' if @searchInput.val() is ''
@restoreOriginalState() @restoreOriginalState()
, 150)
addLocationBadge: (item) -> addLocationBadge: (item) ->
category = if item.category? then "#{item.category}: " else '' category = if item.category? then "#{item.category}: " else ''
...@@ -291,9 +289,5 @@ class @SearchAutocomplete ...@@ -291,9 +289,5 @@ class @SearchAutocomplete
$el.removeClass('is-active') $el.removeClass('is-active')
@disableAutocomplete() @disableAutocomplete()
@searchInput.val('').focus()
# We need to wait because of @skipBlurEvent
setTimeout( =>
@onSearchInputFocus() @onSearchInputFocus()
, 200) @searchInput.val('').focus()
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