labels_select.js.coffee 9.3 KB
Newer Older
1 2 3
class @LabelsSelect
  constructor: ->
    $('.js-label-select').each (i, dropdown) ->
P
Phil Hughes 已提交
4 5 6
      $dropdown = $(dropdown)
      projectId = $dropdown.data('project-id')
      labelUrl = $dropdown.data('labels')
7
      issueUpdateURL = $dropdown.data('issueUpdate')
P
Phil Hughes 已提交
8
      selectedLabel = $dropdown.data('selected')
J
Jacob Schatz 已提交
9
      if selectedLabel? and not $dropdown.hasClass 'js-multiselect'
10
        selectedLabel = selectedLabel.split(',')
P
Phil Hughes 已提交
11 12
      newLabelField = $('#new_label_name')
      newColorField = $('#new_label_color')
P
Phil Hughes 已提交
13 14
      showNo = $dropdown.data('show-no')
      showAny = $dropdown.data('show-any')
15
      defaultLabel = $dropdown.data('default-label')
16
      abilityName = $dropdown.data('ability-name')
17 18
      $selectbox = $dropdown.closest('.selectbox')
      $block = $selectbox.closest('.block')
19
      $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon span')
20 21
      $value = $block.find('.value')
      $loading = $block.find('.block-loading').fadeOut()
P
Phil Hughes 已提交
22 23

      if newLabelField.length
24 25
        $newLabelCreateButton = $('.js-new-label-btn')
        $colorPreview = $('.js-dropdown-label-color-preview')
26 27 28
        $newLabelError = $dropdown.parent().find('.js-label-error')
        $newLabelError.hide()

29
        # Suggested colors in the dropdown to chose from pre-chosen colors
P
Phil Hughes 已提交
30
        $('.suggest-colors-dropdown a').on 'click', (e) ->
31

32 33
      issueURLSplit = issueUpdateURL.split('/') if issueUpdateURL?
      if issueUpdateURL
34
        labelHTMLTemplate = _.template(
35
            '<% _.each(labels, function(label){ %>
J
Jacob Schatz 已提交
36
            <a href="<%= ["",issueURLSplit[1], issueURLSplit[2],""].join("/") %>issues?label_name=<%= label.title %>">
37
            <span class="label has-tooltip color-label" title="<%= label.description %>" style="background-color: <%= label.color %>;">
38 39 40 41 42
            <%= label.title %>
            </span>
            </a>
            <% }); %>'
        );
J
Jacob Schatz 已提交
43
        labelNoneHTMLTemplate = _.template('<div class="light">None</div>')
44 45 46

      if newLabelField.length and $dropdown.hasClass 'js-extra-options'
        $('.suggest-colors-dropdown a').on "click", (e) ->
P
Phil Hughes 已提交
47 48
          e.preventDefault()
          e.stopPropagation()
49 50 51 52
          newColorField
            .val($(this).data('color'))
            .trigger('change')
          $colorPreview
P
Phil Hughes 已提交
53
            .css 'background-color', $(this).data('color')
54
            .parent()
P
Phil Hughes 已提交
55 56
            .addClass 'is-active'

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        # Cancel button takes back to first page
        resetForm = ->
          newLabelField
            .val ''
            .trigger 'change'
          newColorField
            .val ''
            .trigger 'change'
          $colorPreview
            .css 'background-color', ''
            .parent()
            .removeClass 'is-active'

        $('.dropdown-menu-back').on 'click', ->
          resetForm()

73 74 75
        $('.js-cancel-label-btn').on 'click', (e) ->
          e.preventDefault()
          e.stopPropagation()
76
          resetForm()
77 78
          $('.dropdown-menu-back', $dropdown.parent()).trigger 'click'

79 80 81
        # Listen for change and keyup events on label and color field
        # This allows us to enable the button when ready
        enableLabelCreateButton = ->
P
Phil Hughes 已提交
82
          if newLabelField.val() isnt '' and newColorField.val() isnt ''
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            $newLabelError.hide()
            $('.js-new-label-btn').disable()

            # Create new label with API
            Api.newLabel projectId, {
              name: newLabelField.val()
              color: newColorField.val()
            }, (label) ->
              $('.js-new-label-btn').enable()

              if label.message?
                $newLabelError
                  .text label.message
                  .show()
              else
                $('.dropdown-menu-back', $dropdown.parent()).trigger 'click'

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
            $newLabelCreateButton.enable()
          else
            $newLabelCreateButton.disable()

        newLabelField.on 'keyup change', enableLabelCreateButton

        newColorField.on 'keyup change', enableLabelCreateButton

        # Send the API call to create the label
        $newLabelCreateButton
          .disable()
          .on 'click', (e) ->
            e.preventDefault()
            e.stopPropagation()

            if newLabelField.val() isnt '' and newColorField.val() isnt ''
              $newLabelError.hide()
              $('.js-new-label-btn').disable()

              # Create new label with API
              Api.newLabel projectId, {
                name: newLabelField.val()
                color: newColorField.val()
              }, (label) ->
                $('.js-new-label-btn').enable()

                if label.message?
                  $newLabelError
                    .text label.message
                    .show()
                else
                  $('.dropdown-menu-back', $dropdown.parent()).trigger 'click'
132

133 134 135 136 137 138 139 140 141 142 143 144 145
      saveLabelData = ->
        selected = $dropdown
          .closest('.selectbox')
          .find("input[name='#{$dropdown.data('field-name')}']")
          .map(->
            @value
          ).get()
        data = {}
        data[abilityName] = {}
        data[abilityName].label_ids = selected
        if not selected.length
          data[abilityName].label_ids = ['']
        $loading.fadeIn()
J
Jacob Schatz 已提交
146
        $dropdown.trigger('loading.gl.dropdown')
147 148
        $.ajax(
          type: 'PUT'
149
          url: issueUpdateURL
A
Alfredo Sumaran 已提交
150
          dataType: 'JSON'
151 152 153
          data: data
        ).done (data) ->
          $loading.fadeOut()
J
Jacob Schatz 已提交
154
          $dropdown.trigger('loaded.gl.dropdown')
155
          $selectbox.hide()
J
Jacob Schatz 已提交
156
          data.issueURLSplit = issueURLSplit
157 158
          labelCount = 0
          if data.labels.length
159
            template = labelHTMLTemplate(data)
160 161 162 163 164 165 166 167
            labelCount = data.labels.length
          else
            template = labelNoneHTMLTemplate()
          $value
            .removeAttr('style')
            .html(template)
          $sidebarCollapsedValue.text(labelCount)

168 169
          $('.has-tooltip', $value).tooltip(container: 'body')

170 171 172 173 174 175 176 177 178
          $value
            .find('a')
            .each((i) ->
              setTimeout(=>
                glAnimate($(@), 'pulse')
              ,200 * i
              )
            )

179

P
Phil Hughes 已提交
180
      $dropdown.glDropdown(
P
Phil Hughes 已提交
181
        data: (term, callback) ->
182 183 184
          $.ajax(
            url: labelUrl
          ).done (data) ->
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            if $dropdown.hasClass 'js-extra-options'
              if showNo
                data.unshift(
                  id: 0
                  title: 'No Label'
                )

              if showAny
                data.unshift(
                  isAny: true
                  title: 'Any Label'
                )

              if data.length > 2
                data.splice 2, 0, 'divider'
200
            callback data
201

202
        renderRow: (label) ->
203 204 205 206 207
          selectedClass = ''
          if $selectbox.find("input[type='hidden']\
            [name='#{$dropdown.data('field-name')}']\
            [value='#{label.id}']").length
            selectedClass = 'is-active'
P
Phil Hughes 已提交
208

P
Phil Hughes 已提交
209 210
          color = if label.color? then "<span class='dropdown-label-box' style='background-color: #{label.color}'></span>" else ""

211
          "<li>
212
            <a href='#' class='#{selectedClass}'>
P
Phil Hughes 已提交
213
              #{color}
214
              #{label.title}
215 216 217 218
            </a>
          </li>"
        filterable: true
        search:
219
          fields: ['title']
220
        selectable: true
221

222
        toggleLabel: (selected) ->
223
          if selected and selected.title?
224 225 226
            selected.title
          else
            defaultLabel
P
Phil Hughes 已提交
227
        fieldName: $dropdown.data('field-name')
228
        id: (label) ->
P
Phil Hughes 已提交
229 230
          if label.isAny?
            ''
231
          else if $dropdown.hasClass "js-filter-submit"
232
            label.title
233 234 235 236
          else
            label.id

        hidden: ->
J
Jacob Schatz 已提交
237 238 239 240
          page = $('body').data 'page'
          isIssueIndex = page is 'projects:issues:index'
          isMRIndex = page is page is 'projects:merge_requests:index'
          
241
          $selectbox.hide()
242 243
          # display:block overrides the hide-collapse rule
          $value.removeAttr('style')
244
          if $dropdown.hasClass 'js-multiselect'
J
Jacob Schatz 已提交
245
            if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
246 247 248
              selectedLabels = $dropdown
                .closest('form')
                .find("input[type='hidden'][name='#{$dropdown.data('field-name')}']")
J
Jacob Schatz 已提交
249 250 251 252 253 254
              Issues.filterResults(
                $dropdown.closest('form'),
                selectedLabels,
                $dropdown.data('singularFieldName'),
                $dropdown.data('fieldName')
              )
J
Jacob Schatz 已提交
255 256 257 258
            else if $dropdown.hasClass('js-filter-submit')
              $dropdown.closest('form').submit()
            else
              saveLabelData()
259

J
Jacob Schatz 已提交
260
        multiSelect: $dropdown.hasClass 'js-multiselect'
P
Phil Hughes 已提交
261
        clicked: (label) ->
P
Phil Hughes 已提交
262 263 264 265
          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)
266 267 268
            if not $dropdown.hasClass 'js-multiselect'
              selectedLabel = label.title
              Issues.filterResults $dropdown.closest('form')
P
Phil Hughes 已提交
269 270
          else if $dropdown.hasClass 'js-filter-submit'
            $dropdown.closest('form').submit()
271
          else
272 273 274 275
            if $dropdown.hasClass 'js-multiselect'
              return
            else
              saveLabelData()
276
      )