Commit 3954b783 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Refactor UsersSelect to use internal gitlab autocomplete controller

parent 1fc8158f0f96
class @UsersSelect
constructor: ->
@usersPath = "/autocomplete/users.json"
@userPath = "/autocomplete/users/:id.json"
$('.ajax-users-select').each (i, select) =>
......@@ -3,5 +6,10 @@
$('.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
......@@ -4,7 +12,7 @@
$(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 }
......@@ -10,3 +18,24 @@
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)
......@@ -11,4 +40,4 @@
query.callback(data)
initSelection: (element, callback) ->
initSelection: (element, callback) =>
id = $(element).val()
......@@ -14,7 +43,6 @@
id = $(element).val()
if id isnt ""
Api.user(id, callback)
if id != "" && id != "0"
@user(id, callback)
formatResult: (args...) =>
@formatResult(args...)
......@@ -38,3 +66,34 @@
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
Supports Markdown
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