users_select.js.coffee 1.3 KB
Newer Older
1 2
$ ->
  userFormatResult = (user) ->
3
    if user.avatar_url
4
      avatar = gon.relative_url_root + user.avatar_url
5
    else if gon.gravatar_enabled
6 7 8
      avatar = gon.gravatar_url
      avatar = avatar.replace('%{hash}', md5(user.email))
      avatar = avatar.replace('%{size}', '24')
9
    else
Marvin Frick's avatar
Marvin Frick committed
10
      avatar = gon.relative_url_root + "#{image_path('no_avatar.png')}"
11 12 13 14 15 16

    "<div class='user-result'>
       <div class='user-image'><img class='avatar s24' src='#{avatar}'></div>
       <div class='user-name'>#{user.name}</div>
       <div class='user-username'>#{user.username}</div>
     </div>"
17

18 19
  userFormatSelection = (user) ->
    user.name
20

21 22 23 24 25 26 27 28 29
  $('.ajax-users-select').each (i, select) ->
    $(select).select2
      placeholder: "Search for a user"
      multiple: $(select).hasClass('multiselect')
      minimumInputLength: 0
      query: (query) ->
        Api.users query.term, (users) ->
          data = { results: users }
          query.callback(data)
30

31 32 33 34
      initSelection: (element, callback) ->
        id = $(element).val()
        if id isnt ""
          Api.user(id, callback)
35 36


37 38 39 40 41
      formatResult: userFormatResult
      formatSelection: userFormatSelection
      dropdownCssClass: "ajax-users-dropdown"
      escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
        m