summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/users_select.js.coffee
blob: 84193400890b4f8a19f6fe67146c2101bf68b08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class @UsersSelect
  constructor: ->
    @usersPath = "/autocomplete/users.json"
    @userPath = "/autocomplete/users/:id.json"

    $('.js-user-search').each (i, dropdown) =>
      $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')
      selectedId = $dropdown.data('selected')
      defaultLabel = $dropdown.data('default-label')

      $dropdown.glDropdown(
        data: (term, callback) =>
          @users term, (users) =>
            if term.length is 0
              showDivider = 0

              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
                showDivider += 1
                users.unshift(
                  beforeDivider: true
                  name: 'Unassigned',
                  id: 0
                )

              if showAnyUser
                showDivider += 1
                name = showAnyUser
                name = 'Any User' if name == true
                anyUser = {
                  beforeDivider: true
                  name: name,
                  id: null
                }
                users.unshift(anyUser)

            if showDivider
              users.splice(showDivider, 0, "divider")

            # Send the data back
            callback users
        filterable: true
        filterRemote: true
        search:
          fields: ['name', 'username']
        selectable: true
        fieldName: $dropdown.data('field-name')
        toggleLabel: (selected) ->
          if selected && 'id' of selected
            selected.name
          else
            defaultLabel
        clicked: ->
          page = $('body').data 'page'
          isIssueIndex = page is 'projects:issues:index'
          isMRIndex = page is page is 'projects:merge_requests:index'

          if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
            Issues.filterResults $dropdown.closest('form')
          else if $dropdown.hasClass 'js-filter-submit'
            $dropdown.closest('form').submit()
        renderRow: (user) ->
          username = if user.username then "@#{user.username}" else ""
          avatar = if user.avatar_url then user.avatar_url else false
          selected = if user.id is selectedId then "is-active" else ""
          img = ""

          if user.beforeDivider?
            "<li>
              <a href='#' class='#{selected}'>
                #{user.name}
              </a>
            </li>"
          else
            if avatar
              img = "<img src='#{avatar}' class='avatar avatar-inline' width='30' />"

            "<li>
              <a href='#' class='dropdown-menu-user-link #{selected}'>
                #{img}
                <strong class='dropdown-menu-user-full-name'>
                  #{user.name}
                </strong>
                <span class='dropdown-menu-user-username'>
                  #{username}
                </span>
              </a>
            </li>"
      )

    $('.ajax-users-select').each (i, select) =>
      @projectId = $(select).data('project-id')
      @groupId = $(select).data('group-id')
      @showCurrentUser = $(select).data('current-user')
      showNullUser = $(select).data('null-user')
      showAnyUser = $(select).data('any-user')
      showEmailUser = $(select).data('email-user')
      firstUser = $(select).data('first-user')

      $(select).select2
        placeholder: "Search for a user"
        multiple: $(select).hasClass('multiselect')
        minimumInputLength: 0
        query: (query) =>
          @users query.term, (users) =>
            data = { results: users }

            if query.term.length == 0
              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

              if showNullUser
                nullUser = {
                  name: 'Unassigned',
                  id: 0
                }
                data.results.unshift(nullUser)

              if showAnyUser
                name = showAnyUser
                name = 'Any User' if name == true
                anyUser = {
                  name: name,
                  id: null
                }
                data.results.unshift(anyUser)

            if showEmailUser && data.results.length == 0 && query.term.match(/^[^@]+@[^@]+$/)
              emailUser = {
                name: "Invite \"#{query.term}\"",
                username: query.term,
                id: query.term
              }
              data.results.unshift(emailUser)

            query.callback(data)

        initSelection: (args...) =>
          @initSelection(args...)
        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

  initSelection: (element, callback) ->
    id = $(element).val()
    if id == "0"
      nullUser = { name: 'Unassigned' }
      callback(nullUser)
    else if id != ""
      @user(id, callback)

  formatResult: (user) ->
    if user.avatar_url
      avatar = user.avatar_url
    else
      avatar = gon.default_avatar_url

    "<div class='user-result #{'no-username' unless user.username}'>
       <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>"

  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
        current_user: @showCurrentUser
      dataType: "json"
    ).done (users) ->
      callback(users)

  buildUrl: (url) ->
    url = gon.relative_url_root.replace(/\/$/, '') + url if gon.relative_url_root?
    return url