labels_select.js.coffee 3.63 KB
Newer Older
1 2 3
class @LabelsSelect
  constructor: ->
    $('.js-label-select').each (i, dropdown) ->
Phil Hughes's avatar
Phil Hughes committed
4 5 6 7
      $dropdown = $(dropdown)
      projectId = $dropdown.data('project-id')
      labelUrl = $dropdown.data('labels')
      selectedLabel = $dropdown.data('selected')
8
      if selectedLabel
Phil Hughes's avatar
Phil Hughes committed
9
        selectedLabel = selectedLabel.split(',')
10 11
      newLabelField = $('#new_label_name')
      newColorField = $('#new_label_color')
Phil Hughes's avatar
Phil Hughes committed
12 13
      showNo = $dropdown.data('show-no')
      showAny = $dropdown.data('show-any')
14
      defaultLabel = $dropdown.data('default-label')
15 16

      if newLabelField.length
17 18 19
        $newLabelError = $dropdown.parent().find('.js-label-error')
        $newLabelError.hide()

Phil Hughes's avatar
Phil Hughes committed
20
        $('.suggest-colors-dropdown a').on 'click', (e) ->
21 22
          e.preventDefault()
          e.stopPropagation()
Phil Hughes's avatar
Phil Hughes committed
23
          newColorField.val $(this).data('color')
24
          $('.js-dropdown-label-color-preview')
Phil Hughes's avatar
Phil Hughes committed
25
            .css 'background-color', $(this).data('color')
26 27
            .addClass 'is-active'

Phil Hughes's avatar
Phil Hughes committed
28
        $('.js-new-label-btn').on 'click', (e) ->
29 30 31
          e.preventDefault()
          e.stopPropagation()

Phil Hughes's avatar
Phil Hughes committed
32
          if newLabelField.val() isnt '' and newColorField.val() isnt ''
33
            $newLabelError.hide()
34 35 36 37 38 39 40 41
            $('.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()
42 43 44 45 46 47 48

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

Phil Hughes's avatar
Phil Hughes committed
50
      $dropdown.glDropdown(
Phil Hughes's avatar
Phil Hughes committed
51
        data: (term, callback) ->
52 53 54
          $.ajax(
            url: labelUrl
          ).done (data) ->
55 56
            if showNo
              data.unshift(
57 58
                id: 0
                title: 'No Label'
59 60 61 62
              )

            if showAny
              data.unshift(
Phil Hughes's avatar
Phil Hughes committed
63
                isAny: true
64
                title: 'Any Label'
65 66 67
              )

            if data.length > 2
Phil Hughes's avatar
Phil Hughes committed
68
              data.splice 2, 0, 'divider'
69

70
            callback data
71
        renderRow: (label) ->
72
          if $.isArray(selectedLabel)
Phil Hughes's avatar
Phil Hughes committed
73
            selected = ''
74 75
            $.each selectedLabel, (i, selectedLbl) ->
              selectedLbl = selectedLbl.trim()
Phil Hughes's avatar
Phil Hughes committed
76 77
              if selected is '' and label.title is selectedLbl
                selected = 'is-active'
78
          else
Phil Hughes's avatar
Phil Hughes committed
79
            selected = if label.title is selectedLabel then 'is-active' else ''
Phil Hughes's avatar
Phil Hughes committed
80

81 82
          color = if label.color? then "<span class='dropdown-label-box' style='background-color: #{label.color}'></span>" else ""

83
          "<li>
Phil Hughes's avatar
Phil Hughes committed
84
            <a href='#' class='#{selected}'>
85
              #{color}
86
              #{label.title}
87 88 89 90
            </a>
          </li>"
        filterable: true
        search:
91
          fields: ['title']
92
        selectable: true
93
        toggleLabel: (selected) ->
Phil Hughes's avatar
Phil Hughes committed
94
          if selected and selected.title isnt 'Any Label'
95 96 97
            selected.title
          else
            defaultLabel
Phil Hughes's avatar
Phil Hughes committed
98
        fieldName: $dropdown.data('field-name')
99
        id: (label) ->
Phil Hughes's avatar
Phil Hughes committed
100 101
          if label.isAny?
            ''
102 103
          else
            label.title
104
        clicked: ->
Phil Hughes's avatar
Phil Hughes committed
105 106 107 108 109 110 111 112
          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()
113
      )