2014-10-20 16:48:07 -04:00
|
|
|
|
class @SearchAutocomplete
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
|
|
|
|
KEYCODE =
|
|
|
|
|
ESCAPE: 27
|
|
|
|
|
BACKSPACE: 8
|
|
|
|
|
ENTER: 13
|
|
|
|
|
|
2016-03-08 02:56:43 -05:00
|
|
|
|
constructor: (opts = {}) ->
|
|
|
|
|
{
|
|
|
|
|
@wrap = $('.search')
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
2016-03-08 02:56:43 -05:00
|
|
|
|
@optsEl = @wrap.find('.search-autocomplete-opts')
|
|
|
|
|
@autocompletePath = @optsEl.data('autocomplete-path')
|
|
|
|
|
@projectId = @optsEl.data('autocomplete-project-id') || ''
|
|
|
|
|
@projectRef = @optsEl.data('autocomplete-project-ref') || ''
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
2016-03-08 02:56:43 -05:00
|
|
|
|
} = opts
|
2014-01-18 07:16:46 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
# Dropdown Element
|
|
|
|
|
@dropdown = @wrap.find('.dropdown')
|
2016-03-23 15:24:46 -04:00
|
|
|
|
@dropdownContent = @dropdown.find('.dropdown-content')
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-05-24 11:57:26 -04:00
|
|
|
|
@locationBadgeEl = @getElement('.location-badge')
|
2016-03-18 18:35:26 -04:00
|
|
|
|
@scopeInputEl = @getElement('#scope')
|
|
|
|
|
@searchInput = @getElement('.search-input')
|
|
|
|
|
@projectInputEl = @getElement('#search_project_id')
|
|
|
|
|
@groupInputEl = @getElement('#group_id')
|
|
|
|
|
@searchCodeInputEl = @getElement('#search_code')
|
|
|
|
|
@repositoryInputEl = @getElement('#repository_ref')
|
2016-03-21 17:00:53 -04:00
|
|
|
|
@clearInput = @getElement('.js-clear-input')
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
|
|
|
|
@saveOriginalState()
|
2016-03-08 19:39:14 -05:00
|
|
|
|
|
2016-03-24 14:31:19 -04:00
|
|
|
|
# Only when user is logged in
|
|
|
|
|
@createAutocomplete() if gon.current_user_id
|
2016-03-23 15:12:33 -04:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
@searchInput.addClass('disabled')
|
2016-03-08 19:39:14 -05:00
|
|
|
|
|
2016-03-23 18:30:11 -04:00
|
|
|
|
@saveTextLength()
|
|
|
|
|
|
2016-03-08 02:56:43 -05:00
|
|
|
|
@bindEvents()
|
|
|
|
|
|
2016-03-18 18:35:26 -04:00
|
|
|
|
# Finds an element inside wrapper element
|
|
|
|
|
getElement: (selector) ->
|
2016-03-08 02:56:43 -05:00
|
|
|
|
@wrap.find(selector)
|
|
|
|
|
|
|
|
|
|
saveOriginalState: ->
|
|
|
|
|
@originalState = @serializeState()
|
|
|
|
|
|
2016-03-23 18:30:11 -04:00
|
|
|
|
saveTextLength: ->
|
|
|
|
|
@lastTextLength = @searchInput.val().length
|
|
|
|
|
|
2016-03-23 15:12:33 -04:00
|
|
|
|
createAutocomplete: ->
|
|
|
|
|
@searchInput.glDropdown
|
|
|
|
|
filterInputBlur: false
|
|
|
|
|
filterable: true
|
|
|
|
|
filterRemote: true
|
|
|
|
|
highlight: true
|
2016-03-23 23:07:07 -04:00
|
|
|
|
enterCallback: false
|
2016-03-23 15:12:33 -04:00
|
|
|
|
filterInput: 'input#search'
|
|
|
|
|
search:
|
|
|
|
|
fields: ['text']
|
|
|
|
|
data: @getData.bind(@)
|
2016-03-31 16:28:45 -04:00
|
|
|
|
selectable: true
|
|
|
|
|
clicked: @onClick.bind(@)
|
2016-03-23 15:12:33 -04:00
|
|
|
|
|
|
|
|
|
getData: (term, callback) ->
|
|
|
|
|
_this = @
|
|
|
|
|
|
|
|
|
|
# Do not trigger request if input is empty
|
|
|
|
|
return if @searchInput.val() is ''
|
|
|
|
|
|
|
|
|
|
# Prevent multiple ajax calls
|
|
|
|
|
return if @loadingSuggestions
|
|
|
|
|
|
|
|
|
|
@loadingSuggestions = true
|
|
|
|
|
|
|
|
|
|
jqXHR = $.get(@autocompletePath, {
|
|
|
|
|
project_id: @projectId
|
|
|
|
|
project_ref: @projectRef
|
|
|
|
|
term: term
|
|
|
|
|
}, (response) ->
|
2016-03-27 14:28:45 -04:00
|
|
|
|
# Hide dropdown menu if no suggestions returns
|
|
|
|
|
if !response.length
|
|
|
|
|
_this.disableAutocomplete()
|
|
|
|
|
return
|
|
|
|
|
|
2016-03-23 15:12:33 -04:00
|
|
|
|
data = []
|
|
|
|
|
|
|
|
|
|
# List results
|
2016-03-24 14:32:05 -04:00
|
|
|
|
firstCategory = true
|
2016-03-23 15:12:33 -04:00
|
|
|
|
for suggestion in response
|
|
|
|
|
|
|
|
|
|
# Add group header before list each group
|
|
|
|
|
if lastCategory isnt suggestion.category
|
2016-03-24 18:13:37 -04:00
|
|
|
|
data.push 'separator' if !firstCategory
|
2016-03-24 14:32:05 -04:00
|
|
|
|
|
|
|
|
|
firstCategory = false if firstCategory
|
|
|
|
|
|
2016-03-23 15:12:33 -04:00
|
|
|
|
data.push
|
|
|
|
|
header: suggestion.category
|
|
|
|
|
|
|
|
|
|
lastCategory = suggestion.category
|
|
|
|
|
|
|
|
|
|
data.push
|
2016-03-31 20:31:01 -04:00
|
|
|
|
id: "#{suggestion.category.toLowerCase()}-#{suggestion.id}"
|
|
|
|
|
category: suggestion.category
|
2016-03-23 15:12:33 -04:00
|
|
|
|
text: suggestion.label
|
|
|
|
|
url: suggestion.url
|
|
|
|
|
|
2016-03-27 14:27:49 -04:00
|
|
|
|
# Add option to proceed with the search
|
|
|
|
|
if data.length
|
|
|
|
|
data.push('separator')
|
|
|
|
|
data.push
|
|
|
|
|
text: "Result name contains \"#{term}\""
|
|
|
|
|
url: "/search?\
|
|
|
|
|
search=#{term}\
|
|
|
|
|
&project_id=#{_this.projectInputEl.val()}\
|
|
|
|
|
&group_id=#{_this.groupInputEl.val()}"
|
|
|
|
|
|
2016-03-23 15:12:33 -04:00
|
|
|
|
callback(data)
|
|
|
|
|
).always ->
|
|
|
|
|
_this.loadingSuggestions = false
|
|
|
|
|
|
2016-03-08 02:56:43 -05:00
|
|
|
|
serializeState: ->
|
|
|
|
|
{
|
|
|
|
|
# Search Criteria
|
2016-03-23 18:58:18 -04:00
|
|
|
|
search_project_id: @projectInputEl.val()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
group_id: @groupInputEl.val()
|
|
|
|
|
search_code: @searchCodeInputEl.val()
|
|
|
|
|
repository_ref: @repositoryInputEl.val()
|
2016-03-21 15:01:20 -04:00
|
|
|
|
scope: @scopeInputEl.val()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
|
|
|
|
# Location badge
|
2016-05-24 11:57:26 -04:00
|
|
|
|
_location: @locationBadgeEl.text()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bindEvents: ->
|
2016-04-05 20:19:00 -04:00
|
|
|
|
$(document).on 'click', @onDocumentClick
|
2016-03-23 18:30:11 -04:00
|
|
|
|
@searchInput.on 'keydown', @onSearchInputKeyDown
|
2016-03-23 15:12:33 -04:00
|
|
|
|
@searchInput.on 'keyup', @onSearchInputKeyUp
|
|
|
|
|
@searchInput.on 'click', @onSearchInputClick
|
2016-03-08 19:39:14 -05:00
|
|
|
|
@searchInput.on 'focus', @onSearchInputFocus
|
2016-04-06 14:55:49 -04:00
|
|
|
|
@clearInput.on 'click', @onClearInputClick
|
2016-05-24 11:57:26 -04:00
|
|
|
|
@locationBadgeEl.on 'click', =>
|
|
|
|
|
@searchInput.focus()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-04-05 20:19:00 -04:00
|
|
|
|
onDocumentClick: (e) =>
|
2016-04-06 15:28:15 -04:00
|
|
|
|
# If clicking outside the search box
|
|
|
|
|
# And search input is not focused
|
|
|
|
|
# And we are not clicking inside a suggestion
|
2016-05-24 11:57:26 -04:00
|
|
|
|
if not $.contains(@dropdown[0], e.target) and @isFocused and not $(e.target).closest('.search-form').length
|
2016-04-05 20:19:00 -04:00
|
|
|
|
@onSearchInputBlur()
|
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
enableAutocomplete: ->
|
2016-03-24 14:31:19 -04:00
|
|
|
|
# No need to enable anything if user is not logged in
|
|
|
|
|
return if !gon.current_user_id
|
|
|
|
|
|
2016-05-27 10:05:46 -04:00
|
|
|
|
unless @dropdown.hasClass('open')
|
|
|
|
|
_this = @
|
|
|
|
|
@loadingSuggestions = false
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
2016-05-27 10:05:46 -04:00
|
|
|
|
@dropdown
|
|
|
|
|
.addClass('open')
|
|
|
|
|
.trigger('shown.bs.dropdown')
|
|
|
|
|
@searchInput.removeClass('disabled')
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
2016-03-23 18:30:11 -04:00
|
|
|
|
onSearchInputKeyDown: =>
|
|
|
|
|
# Saves last length of the entered text
|
|
|
|
|
@saveTextLength()
|
|
|
|
|
|
2016-03-23 15:12:33 -04:00
|
|
|
|
onSearchInputKeyUp: (e) =>
|
2016-03-18 22:50:49 -04:00
|
|
|
|
switch e.keyCode
|
|
|
|
|
when KEYCODE.BACKSPACE
|
2016-03-23 18:30:11 -04:00
|
|
|
|
# when trying to remove the location badge
|
|
|
|
|
if @lastTextLength is 0 and @badgePresent()
|
|
|
|
|
@removeLocationBadge()
|
|
|
|
|
|
|
|
|
|
# When removing the last character and no badge is present
|
2016-03-26 17:52:16 -04:00
|
|
|
|
if @lastTextLength is 1
|
2016-03-23 15:24:46 -04:00
|
|
|
|
@disableAutocomplete()
|
2016-03-26 17:52:16 -04:00
|
|
|
|
|
|
|
|
|
# When removing any character from existin value
|
|
|
|
|
if @lastTextLength > 1
|
|
|
|
|
@enableAutocomplete()
|
|
|
|
|
|
2016-03-18 22:50:49 -04:00
|
|
|
|
when KEYCODE.ESCAPE
|
2016-03-26 17:52:16 -04:00
|
|
|
|
@restoreOriginalState()
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
2016-03-08 21:26:24 -05:00
|
|
|
|
else
|
2016-03-27 14:52:42 -04:00
|
|
|
|
# Handle the case when deleting the input value other than backspace
|
|
|
|
|
# e.g. Pressing ctrl + backspace or ctrl + x
|
|
|
|
|
if @searchInput.val() is ''
|
|
|
|
|
@disableAutocomplete()
|
|
|
|
|
else
|
|
|
|
|
# We should display the menu only when input is not empty
|
2016-05-27 10:05:46 -04:00
|
|
|
|
@enableAutocomplete() if e.keyCode isnt KEYCODE.ENTER
|
2016-03-18 22:50:49 -04:00
|
|
|
|
|
2016-04-06 14:55:49 -04:00
|
|
|
|
@wrap.toggleClass 'has-value', !!e.target.value
|
|
|
|
|
|
2016-03-18 22:50:49 -04:00
|
|
|
|
# Avoid falsy value to be returned
|
|
|
|
|
return
|
2016-03-08 19:39:14 -05:00
|
|
|
|
|
2016-03-23 17:36:44 -04:00
|
|
|
|
onSearchInputClick: (e) =>
|
|
|
|
|
# Prevents closing the dropdown menu
|
|
|
|
|
e.stopImmediatePropagation()
|
2016-03-23 15:12:33 -04:00
|
|
|
|
|
2016-03-08 19:39:14 -05:00
|
|
|
|
onSearchInputFocus: =>
|
2016-04-05 20:19:00 -04:00
|
|
|
|
@isFocused = true
|
2016-03-08 19:39:14 -05:00
|
|
|
|
@wrap.addClass('search-active')
|
|
|
|
|
|
2016-04-06 14:55:49 -04:00
|
|
|
|
onClearInputClick: (e) =>
|
2016-03-21 17:00:53 -04:00
|
|
|
|
e.preventDefault()
|
|
|
|
|
@searchInput.val('').focus()
|
2016-03-08 19:39:14 -05:00
|
|
|
|
|
2016-03-21 17:00:53 -04:00
|
|
|
|
onSearchInputBlur: (e) =>
|
2016-04-05 20:19:00 -04:00
|
|
|
|
@isFocused = false
|
|
|
|
|
@wrap.removeClass('search-active')
|
2016-03-21 17:00:53 -04:00
|
|
|
|
|
2016-04-05 20:19:00 -04:00
|
|
|
|
# If input is blank then restore state
|
|
|
|
|
if @searchInput.val() is ''
|
|
|
|
|
@restoreOriginalState()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
|
|
|
|
addLocationBadge: (item) ->
|
|
|
|
|
category = if item.category? then "#{item.category}: " else ''
|
|
|
|
|
value = if item.value? then item.value else ''
|
|
|
|
|
|
2016-05-24 11:57:26 -04:00
|
|
|
|
badgeText = "#{category}#{value}"
|
|
|
|
|
@locationBadgeEl.text(badgeText).show()
|
2016-03-21 17:00:53 -04:00
|
|
|
|
@wrap.addClass('has-location-badge')
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
restoreOriginalState: ->
|
|
|
|
|
inputs = Object.keys @originalState
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
for input in inputs
|
2016-03-18 18:35:26 -04:00
|
|
|
|
@getElement("##{input}").val(@originalState[input])
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
if @originalState._location is ''
|
2016-05-24 11:57:26 -04:00
|
|
|
|
@locationBadgeEl.hide()
|
2016-03-14 17:14:29 -04:00
|
|
|
|
else
|
|
|
|
|
@addLocationBadge(
|
|
|
|
|
value: @originalState._location
|
|
|
|
|
)
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
@dropdown.removeClass 'open'
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
badgePresent: ->
|
2016-05-24 11:57:26 -04:00
|
|
|
|
@locationBadgeEl.length
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
|
|
|
|
resetSearchState: ->
|
2016-03-21 15:01:20 -04:00
|
|
|
|
inputs = Object.keys @originalState
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-21 15:01:20 -04:00
|
|
|
|
for input in inputs
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-21 15:01:20 -04:00
|
|
|
|
# _location isnt a input
|
|
|
|
|
break if input is '_location'
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-21 15:01:20 -04:00
|
|
|
|
@getElement("##{input}").val('')
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
removeLocationBadge: ->
|
2016-05-24 11:57:26 -04:00
|
|
|
|
@locationBadgeEl.hide()
|
2016-03-14 17:14:29 -04:00
|
|
|
|
|
|
|
|
|
# Reset state
|
|
|
|
|
@resetSearchState()
|
2016-03-08 02:56:43 -05:00
|
|
|
|
|
2016-03-21 17:00:53 -04:00
|
|
|
|
@wrap.removeClass('has-location-badge')
|
|
|
|
|
|
2016-03-14 17:14:29 -04:00
|
|
|
|
disableAutocomplete: ->
|
2016-03-23 17:13:46 -04:00
|
|
|
|
@searchInput.addClass('disabled')
|
|
|
|
|
@dropdown.removeClass('open')
|
|
|
|
|
@restoreMenu()
|
2016-03-23 15:24:46 -04:00
|
|
|
|
|
|
|
|
|
restoreMenu: ->
|
|
|
|
|
html = "<ul>
|
2016-03-25 02:02:22 -04:00
|
|
|
|
<li><a class='dropdown-menu-empty-link is-focused'>Loading...</a></li>
|
2016-03-23 15:24:46 -04:00
|
|
|
|
</ul>"
|
|
|
|
|
@dropdownContent.html(html)
|
2016-03-31 16:28:45 -04:00
|
|
|
|
|
2016-03-31 20:31:01 -04:00
|
|
|
|
onClick: (item, $el, e) ->
|
2016-03-31 16:28:45 -04:00
|
|
|
|
if location.pathname.indexOf(item.url) isnt -1
|
|
|
|
|
e.preventDefault()
|
2016-03-31 20:31:01 -04:00
|
|
|
|
if not @badgePresent
|
|
|
|
|
if item.category is 'Projects'
|
|
|
|
|
@projectInputEl.val(item.id)
|
|
|
|
|
@addLocationBadge(
|
|
|
|
|
value: 'This project'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if item.category is 'Groups'
|
|
|
|
|
@groupInputEl.val(item.id)
|
|
|
|
|
@addLocationBadge(
|
|
|
|
|
value: 'This group'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$el.removeClass('is-active')
|
2016-03-31 16:28:45 -04:00
|
|
|
|
@disableAutocomplete()
|
2016-03-31 20:31:01 -04:00
|
|
|
|
@searchInput.val('').focus()
|