Commit 1c2711f7 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor UsersSelect to use internal gitlab autocomplete controller

parent 26053c87
class @UsersSelect class @UsersSelect
constructor: -> constructor: ->
@usersPath = "/autocomplete/users.json"
@userPath = "/autocomplete/users/:id.json"
$('.ajax-users-select').each (i, select) => $('.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 $(select).select2
placeholder: "Search for a user" placeholder: "Search for a user"
multiple: $(select).hasClass('multiselect') multiple: $(select).hasClass('multiselect')
minimumInputLength: 0 minimumInputLength: 0
query: (query) -> query: (query) =>
Api.users query.term, (users) -> @users query.term, (users) =>
data = { results: 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) query.callback(data)
initSelection: (element, callback) -> initSelection: (element, callback) =>
id = $(element).val() id = $(element).val()
if id isnt "" if id != "" && id != "0"
Api.user(id, callback) @user(id, callback)
formatResult: (args...) => formatResult: (args...) =>
@formatResult(args...) @formatResult(args...)
...@@ -38,3 +66,34 @@ class @UsersSelect ...@@ -38,3 +66,34 @@ class @UsersSelect
formatSelection: (user) -> formatSelection: (user) ->
user.name 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
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