2014-10-21 18:19:16 -04:00
|
|
|
class @UsersSelect
|
2016-03-19 20:49:36 -04:00
|
|
|
constructor: (currentUser) ->
|
2015-03-26 22:13:38 -04:00
|
|
|
@usersPath = "/autocomplete/users.json"
|
|
|
|
@userPath = "/autocomplete/users/:id.json"
|
2016-03-21 07:20:21 -04:00
|
|
|
if currentUser?
|
|
|
|
@currentUser = JSON.parse(currentUser)
|
2015-03-26 22:13:38 -04:00
|
|
|
|
2016-03-07 12:17:11 -05:00
|
|
|
$('.js-user-search').each (i, dropdown) =>
|
2016-03-18 06:12:52 -04:00
|
|
|
$dropdown = $(dropdown)
|
|
|
|
@projectId = $dropdown.data('project-id')
|
|
|
|
@showCurrentUser = $dropdown.data('current-user')
|
|
|
|
showNullUser = $dropdown.data('null-user')
|
|
|
|
showAnyUser = $dropdown.data('any-user')
|
|
|
|
firstUser = $dropdown.data('first-user')
|
2016-04-05 11:08:45 -04:00
|
|
|
@authorId = $dropdown.data('author-id')
|
2016-03-18 06:12:52 -04:00
|
|
|
selectedId = $dropdown.data('selected')
|
2016-03-18 08:21:07 -04:00
|
|
|
defaultLabel = $dropdown.data('default-label')
|
2016-03-16 16:52:07 -04:00
|
|
|
issueURL = $dropdown.data('issueUpdate')
|
|
|
|
$selectbox = $dropdown.closest('.selectbox')
|
|
|
|
$block = $selectbox.closest('.block')
|
2016-03-17 09:14:46 -04:00
|
|
|
abilityName = $dropdown.data('ability-name')
|
2016-03-16 16:52:07 -04:00
|
|
|
$value = $block.find('.value')
|
2016-03-26 11:43:42 -04:00
|
|
|
$collapsedSidebar = $block.find('.sidebar-collapsed-user')
|
2016-03-16 16:52:07 -04:00
|
|
|
$loading = $block.find('.block-loading').fadeOut()
|
2016-03-07 12:17:11 -05:00
|
|
|
|
2016-03-21 07:20:21 -04:00
|
|
|
$block.on('click', '.js-assign-yourself', (e) =>
|
2016-03-19 20:49:36 -04:00
|
|
|
e.preventDefault()
|
|
|
|
assignTo(@currentUser.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
assignTo = (selected) ->
|
|
|
|
data = {}
|
|
|
|
data[abilityName] = {}
|
|
|
|
data[abilityName].assignee_id = selected
|
|
|
|
$loading
|
|
|
|
.fadeIn()
|
2016-03-27 11:04:51 -04:00
|
|
|
$dropdown.trigger('loading.gl.dropdown')
|
2016-03-19 20:49:36 -04:00
|
|
|
$.ajax(
|
|
|
|
type: 'PUT'
|
|
|
|
dataType: 'json'
|
|
|
|
url: issueURL
|
|
|
|
data: data
|
|
|
|
).done (data) ->
|
2016-03-27 11:04:51 -04:00
|
|
|
$dropdown.trigger('loaded.gl.dropdown')
|
2016-03-19 20:49:36 -04:00
|
|
|
$loading.fadeOut()
|
|
|
|
$selectbox.hide()
|
2016-03-21 07:20:21 -04:00
|
|
|
|
2016-03-19 20:49:36 -04:00
|
|
|
if data.assignee
|
|
|
|
user =
|
|
|
|
name: data.assignee.name
|
|
|
|
username: data.assignee.username
|
2016-03-24 18:29:35 -04:00
|
|
|
avatar: data.assignee.avatar_url
|
2016-03-19 20:49:36 -04:00
|
|
|
else
|
|
|
|
user =
|
|
|
|
name: 'Unassigned'
|
|
|
|
username: ''
|
|
|
|
avatar: ''
|
2016-03-26 11:43:42 -04:00
|
|
|
$value.html(assigneeTemplate(user))
|
|
|
|
$collapsedSidebar.html(collapsedAssigneeTemplate(user))
|
2016-03-19 20:49:36 -04:00
|
|
|
|
|
|
|
|
2016-03-26 11:43:42 -04:00
|
|
|
collapsedAssigneeTemplate = _.template(
|
|
|
|
'<% if( avatar ) { %>
|
|
|
|
<a class="author_link" href="/u/<%= username %>">
|
|
|
|
<img width="24" class="avatar avatar-inline s24" alt="" src="<%= avatar %>">
|
|
|
|
<span class="author">Toni Boehm</span>
|
|
|
|
</a>
|
|
|
|
<% } else { %>
|
|
|
|
<i class="fa fa-user"></i>
|
|
|
|
<% } %>'
|
|
|
|
)
|
2016-03-19 20:49:36 -04:00
|
|
|
|
2016-03-26 11:43:42 -04:00
|
|
|
assigneeTemplate = _.template(
|
2016-03-19 20:19:51 -04:00
|
|
|
'<% if (username) { %>
|
|
|
|
<a class="author_link " href="/u/<%= username %>">
|
|
|
|
<% if( avatar ) { %>
|
|
|
|
<img width="32" class="avatar avatar-inline s32" alt="" src="<%= avatar %>">
|
|
|
|
<% } %>
|
|
|
|
<span class="author"><%= name %></span>
|
|
|
|
<span class="username">
|
|
|
|
@<%= username %>
|
|
|
|
</span>
|
|
|
|
</a>
|
|
|
|
<% } else { %>
|
|
|
|
<span class="assign-yourself">
|
2016-03-21 07:20:21 -04:00
|
|
|
No assignee -
|
2016-03-19 20:19:51 -04:00
|
|
|
<a href="#" class="js-assign-yourself">
|
|
|
|
assign yourself
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
<% } %>'
|
|
|
|
)
|
|
|
|
|
2016-03-18 06:12:52 -04:00
|
|
|
$dropdown.glDropdown(
|
2016-03-08 04:09:39 -05:00
|
|
|
data: (term, callback) =>
|
2016-04-19 10:58:20 -04:00
|
|
|
isAuthorFilter = $('.js-author-search')
|
|
|
|
|
|
|
|
@users term, term is '' and isAuthorFilter, (users) =>
|
2016-03-08 04:09:39 -05:00
|
|
|
if term.length is 0
|
2016-03-08 05:24:03 -05:00
|
|
|
showDivider = 0
|
|
|
|
|
2016-03-08 04:09:39 -05:00
|
|
|
if firstUser
|
|
|
|
# Move current user to the front of the list
|
|
|
|
for obj, index in users
|
|
|
|
if obj.username == firstUser
|
|
|
|
users.splice(index, 1)
|
|
|
|
users.unshift(obj)
|
|
|
|
break
|
|
|
|
|
|
|
|
if showNullUser
|
2016-03-08 05:24:03 -05:00
|
|
|
showDivider += 1
|
2016-03-08 04:09:39 -05:00
|
|
|
users.unshift(
|
2016-03-22 05:49:14 -04:00
|
|
|
beforeDivider: true
|
2016-03-08 04:09:39 -05:00
|
|
|
name: 'Unassigned',
|
|
|
|
id: 0
|
|
|
|
)
|
|
|
|
|
|
|
|
if showAnyUser
|
2016-03-08 05:24:03 -05:00
|
|
|
showDivider += 1
|
2016-03-08 04:09:39 -05:00
|
|
|
name = showAnyUser
|
|
|
|
name = 'Any User' if name == true
|
|
|
|
anyUser = {
|
2016-03-22 05:49:14 -04:00
|
|
|
beforeDivider: true
|
2016-03-08 04:09:39 -05:00
|
|
|
name: name,
|
|
|
|
id: null
|
|
|
|
}
|
|
|
|
users.unshift(anyUser)
|
2016-03-07 12:17:11 -05:00
|
|
|
|
2016-03-08 05:24:03 -05:00
|
|
|
if showDivider
|
|
|
|
users.splice(showDivider, 0, "divider")
|
|
|
|
|
2016-03-07 12:17:11 -05:00
|
|
|
# Send the data back
|
|
|
|
callback users
|
|
|
|
filterable: true
|
2016-03-08 04:09:39 -05:00
|
|
|
filterRemote: true
|
2016-03-07 12:17:11 -05:00
|
|
|
search:
|
|
|
|
fields: ['name', 'username']
|
|
|
|
selectable: true
|
2016-03-18 06:12:52 -04:00
|
|
|
fieldName: $dropdown.data('field-name')
|
2016-03-16 16:52:07 -04:00
|
|
|
|
2016-03-15 14:20:22 -04:00
|
|
|
toggleLabel: (selected) ->
|
2016-03-18 06:14:39 -04:00
|
|
|
if selected && 'id' of selected
|
2016-04-19 10:58:20 -04:00
|
|
|
if selected.text then selected.text else selected.name
|
2016-03-15 14:20:22 -04:00
|
|
|
else
|
|
|
|
defaultLabel
|
2016-03-16 16:52:07 -04:00
|
|
|
|
2016-03-22 15:18:29 -04:00
|
|
|
inputId: 'issue_assignee_id'
|
|
|
|
|
2016-03-24 18:29:35 -04:00
|
|
|
hidden: (e) ->
|
2016-03-16 16:52:07 -04:00
|
|
|
$selectbox.hide()
|
2016-03-26 11:43:42 -04:00
|
|
|
# display:block overrides the hide-collapse rule
|
2016-06-01 05:11:30 -04:00
|
|
|
$value.css('display', '')
|
2016-03-16 16:52:07 -04:00
|
|
|
|
2016-03-23 12:44:09 -04:00
|
|
|
clicked: (user) ->
|
2016-03-18 06:12:52 -04:00
|
|
|
page = $('body').data 'page'
|
|
|
|
isIssueIndex = page is 'projects:issues:index'
|
|
|
|
isMRIndex = page is page is 'projects:merge_requests:index'
|
2016-03-26 09:30:37 -04:00
|
|
|
if $dropdown.hasClass('js-filter-bulk-update')
|
|
|
|
return
|
2016-03-16 05:04:15 -04:00
|
|
|
|
2016-03-18 06:12:52 -04:00
|
|
|
if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
|
2016-03-23 12:44:09 -04:00
|
|
|
selectedId = user.id
|
2016-04-20 12:00:12 -04:00
|
|
|
Issuable.filterResults $dropdown.closest('form')
|
2016-03-18 06:12:52 -04:00
|
|
|
else if $dropdown.hasClass 'js-filter-submit'
|
|
|
|
$dropdown.closest('form').submit()
|
2016-03-15 18:26:50 -04:00
|
|
|
else
|
2016-03-16 16:52:07 -04:00
|
|
|
selected = $dropdown
|
|
|
|
.closest('.selectbox')
|
|
|
|
.find("input[name='#{$dropdown.data('field-name')}']").val()
|
2016-03-19 20:49:36 -04:00
|
|
|
assignTo(selected)
|
2016-03-19 20:19:51 -04:00
|
|
|
|
2016-03-07 12:17:11 -05:00
|
|
|
renderRow: (user) ->
|
|
|
|
username = if user.username then "@#{user.username}" else ""
|
2016-03-08 05:24:03 -05:00
|
|
|
avatar = if user.avatar_url then user.avatar_url else false
|
2016-03-07 12:17:11 -05:00
|
|
|
selected = if user.id is selectedId then "is-active" else ""
|
2016-03-08 05:24:03 -05:00
|
|
|
img = ""
|
|
|
|
|
2016-03-22 05:49:14 -04:00
|
|
|
if user.beforeDivider?
|
|
|
|
"<li>
|
|
|
|
<a href='#' class='#{selected}'>
|
2016-03-07 12:17:11 -05:00
|
|
|
#{user.name}
|
2016-03-22 05:49:14 -04:00
|
|
|
</a>
|
|
|
|
</li>"
|
|
|
|
else
|
|
|
|
if avatar
|
|
|
|
img = "<img src='#{avatar}' class='avatar avatar-inline' width='30' />"
|
|
|
|
|
2016-03-19 20:19:51 -04:00
|
|
|
# split into three parts so we can remove the username section if nessesary
|
|
|
|
listWithName = "<li>
|
|
|
|
<a href='#' class='dropdown-menu-user-link #{selected}'>
|
|
|
|
#{img}
|
|
|
|
<strong class='dropdown-menu-user-full-name'>
|
|
|
|
#{user.name}
|
|
|
|
</strong>"
|
|
|
|
|
|
|
|
listWithUserName = "<span class='dropdown-menu-user-username'>
|
|
|
|
#{username}
|
|
|
|
</span>"
|
|
|
|
listClosingTags = "</a>
|
|
|
|
</li>"
|
|
|
|
|
2016-03-21 07:20:21 -04:00
|
|
|
|
2016-03-19 20:19:51 -04:00
|
|
|
if username is ''
|
|
|
|
listWithUserName = ''
|
|
|
|
|
|
|
|
listWithName + listWithUserName + listClosingTags
|
2016-03-07 12:17:11 -05:00
|
|
|
)
|
2015-03-26 22:13:38 -04:00
|
|
|
|
2014-10-21 18:19:16 -04:00
|
|
|
$('.ajax-users-select').each (i, select) =>
|
2015-03-26 22:13:38 -04:00
|
|
|
@projectId = $(select).data('project-id')
|
|
|
|
@groupId = $(select).data('group-id')
|
2015-08-21 00:33:00 -04:00
|
|
|
@showCurrentUser = $(select).data('current-user')
|
2016-02-09 06:02:52 -05:00
|
|
|
@authorId = $(select).data('author-id')
|
2015-03-26 22:13:38 -04:00
|
|
|
showNullUser = $(select).data('null-user')
|
|
|
|
showAnyUser = $(select).data('any-user')
|
2015-04-10 09:25:32 -04:00
|
|
|
showEmailUser = $(select).data('email-user')
|
2015-03-31 02:53:42 -04:00
|
|
|
firstUser = $(select).data('first-user')
|
2016-04-05 11:08:45 -04:00
|
|
|
|
2014-10-21 18:19:16 -04:00
|
|
|
$(select).select2
|
|
|
|
placeholder: "Search for a user"
|
|
|
|
multiple: $(select).hasClass('multiselect')
|
|
|
|
minimumInputLength: 0
|
2015-03-26 22:13:38 -04:00
|
|
|
query: (query) =>
|
2016-05-26 05:38:53 -04:00
|
|
|
@users query.term, @projectId?, (users) =>
|
2014-10-21 18:19:16 -04:00
|
|
|
data = { results: users }
|
2015-03-26 22:13:38 -04:00
|
|
|
|
|
|
|
if query.term.length == 0
|
2015-03-31 02:53:42 -04:00
|
|
|
if firstUser
|
|
|
|
# Move current user to the front of the list
|
|
|
|
for obj, index in data.results
|
|
|
|
if obj.username == firstUser
|
|
|
|
data.results.splice(index, 1)
|
|
|
|
data.results.unshift(obj)
|
|
|
|
break
|
2015-04-10 09:25:32 -04:00
|
|
|
|
2015-03-26 22:13:38 -04:00
|
|
|
if showNullUser
|
2015-04-10 09:25:32 -04:00
|
|
|
nullUser = {
|
|
|
|
name: 'Unassigned',
|
|
|
|
id: 0
|
|
|
|
}
|
2015-03-26 22:13:38 -04:00
|
|
|
data.results.unshift(nullUser)
|
2015-04-10 09:25:32 -04:00
|
|
|
|
2015-03-26 22:13:38 -04:00
|
|
|
if showAnyUser
|
2015-12-02 08:11:32 -05:00
|
|
|
name = showAnyUser
|
|
|
|
name = 'Any User' if name == true
|
2015-04-10 09:25:32 -04:00
|
|
|
anyUser = {
|
2015-12-02 08:11:32 -05:00
|
|
|
name: name,
|
2015-04-10 09:25:32 -04:00
|
|
|
id: null
|
|
|
|
}
|
2015-03-26 22:13:38 -04:00
|
|
|
data.results.unshift(anyUser)
|
|
|
|
|
2015-04-10 09:25:32 -04:00
|
|
|
if showEmailUser && data.results.length == 0 && query.term.match(/^[^@]+@[^@]+$/)
|
|
|
|
emailUser = {
|
|
|
|
name: "Invite \"#{query.term}\"",
|
|
|
|
username: query.term,
|
|
|
|
id: query.term
|
|
|
|
}
|
|
|
|
data.results.unshift(emailUser)
|
|
|
|
|
2014-10-21 18:19:16 -04:00
|
|
|
query.callback(data)
|
|
|
|
|
2015-11-20 23:55:17 -05:00
|
|
|
initSelection: (args...) =>
|
|
|
|
@initSelection(args...)
|
2014-10-21 18:19:16 -04:00
|
|
|
formatResult: (args...) =>
|
|
|
|
@formatResult(args...)
|
|
|
|
formatSelection: (args...) =>
|
|
|
|
@formatSelection(args...)
|
|
|
|
dropdownCssClass: "ajax-users-dropdown"
|
|
|
|
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
|
|
|
|
m
|
|
|
|
|
2015-11-20 23:55:17 -05:00
|
|
|
initSelection: (element, callback) ->
|
|
|
|
id = $(element).val()
|
|
|
|
if id == "0"
|
|
|
|
nullUser = { name: 'Unassigned' }
|
|
|
|
callback(nullUser)
|
|
|
|
else if id != ""
|
|
|
|
@user(id, callback)
|
|
|
|
|
2014-10-21 18:19:16 -04:00
|
|
|
formatResult: (user) ->
|
2014-02-13 08:45:24 -05:00
|
|
|
if user.avatar_url
|
2014-06-13 10:46:48 -04:00
|
|
|
avatar = user.avatar_url
|
2014-02-11 08:49:39 -05:00
|
|
|
else
|
2014-06-13 10:46:48 -04:00
|
|
|
avatar = gon.default_avatar_url
|
2013-12-20 12:31:45 -05:00
|
|
|
|
2015-12-02 08:11:32 -05:00
|
|
|
"<div class='user-result #{'no-username' unless user.username}'>
|
2013-12-20 12:31:45 -05:00
|
|
|
<div class='user-image'><img class='avatar s24' src='#{avatar}'></div>
|
|
|
|
<div class='user-name'>#{user.name}</div>
|
2015-12-02 08:11:32 -05:00
|
|
|
<div class='user-username'>#{user.username || ""}</div>
|
2013-12-20 12:31:45 -05:00
|
|
|
</div>"
|
2013-03-13 15:37:13 -04:00
|
|
|
|
2014-10-21 18:19:16 -04:00
|
|
|
formatSelection: (user) ->
|
2013-03-13 15:42:14 -04:00
|
|
|
user.name
|
2015-03-26 22:13:38 -04:00
|
|
|
|
|
|
|
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
|
2016-04-19 10:58:20 -04:00
|
|
|
users: (query, fromProject, callback) =>
|
2015-03-26 22:13:38 -04:00
|
|
|
url = @buildUrl(@usersPath)
|
|
|
|
|
|
|
|
$.ajax(
|
|
|
|
url: url
|
|
|
|
data:
|
|
|
|
search: query
|
|
|
|
per_page: 20
|
|
|
|
active: true
|
2016-04-19 10:58:20 -04:00
|
|
|
project_id: @projectId if fromProject
|
2015-03-26 22:13:38 -04:00
|
|
|
group_id: @groupId
|
2015-08-21 00:33:00 -04:00
|
|
|
current_user: @showCurrentUser
|
2016-02-09 06:02:52 -05:00
|
|
|
author_id: @authorId
|
2015-03-26 22:13:38 -04:00
|
|
|
dataType: "json"
|
|
|
|
).done (users) ->
|
|
|
|
callback(users)
|
|
|
|
|
|
|
|
buildUrl: (url) ->
|
2015-12-17 07:15:29 -05:00
|
|
|
url = gon.relative_url_root.replace(/\/$/, '') + url if gon.relative_url_root?
|
2015-03-26 22:13:38 -04:00
|
|
|
return url
|