Skip to content
Snippets Groups Projects
Commit 1c2711f7 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Refactor UsersSelect to use internal gitlab autocomplete controller

parent 26053c87
No related branches found
No related tags found
No related merge requests found
class @UsersSelect
constructor: ->
@usersPath = "/autocomplete/users.json"
@userPath = "/autocomplete/users/:id.json"
$('.ajax-users-select').each (i, select) =>
@projectId = $(select).data('project-id')
@groupId = $(select).data('group-id')
showNullUser = $(select).data('null-user')
showAnyUser = $(select).data('any-user')
$(select).select2
placeholder: "Search for a user"
multiple: $(select).hasClass('multiselect')
minimumInputLength: 0
query: (query) ->
Api.users query.term, (users) ->
query: (query) =>
@users query.term, (users) =>
data = { results: users }
if query.term.length == 0
anyUser = {
name: 'Any',
avatar: null,
username: 'none',
id: null
}
nullUser = {
name: 'Unassigned',
avatar: null,
username: 'none',
id: 0
}
if showNullUser
data.results.unshift(nullUser)
if showAnyUser
data.results.unshift(anyUser)
query.callback(data)
 
initSelection: (element, callback) ->
initSelection: (element, callback) =>
id = $(element).val()
if id isnt ""
Api.user(id, callback)
if id != "" && id != "0"
@user(id, callback)
 
formatResult: (args...) =>
@formatResult(args...)
Loading
Loading
@@ -38,3 +66,34 @@ class @UsersSelect
 
formatSelection: (user) ->
user.name
user: (user_id, callback) =>
url = @buildUrl(@userPath)
url = url.replace(':id', user_id)
$.ajax(
url: url
dataType: "json"
).done (user) ->
callback(user)
# Return users list. Filtered by query
# Only active users retrieved
users: (query, callback) =>
url = @buildUrl(@usersPath)
$.ajax(
url: url
data:
search: query
per_page: 20
active: true
project_id: @projectId
group_id: @groupId
dataType: "json"
).done (users) ->
callback(users)
buildUrl: (url) ->
url = gon.relative_url_root + url if gon.relative_url_root?
return url
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment