summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/labels_select.js.coffee
blob: 269f2008e2dcab5db4e9e901f4c6716c2fb26ac0 (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
class @LabelsSelect
  constructor: ->
    $('.js-label-select').each (i, dropdown) ->
      projectId = $(dropdown).data('project-id')
      labelUrl = $(dropdown).data("labels")
      selectedLabel = $(dropdown).data('selected')
      if selectedLabel
        selectedLabel = selectedLabel.split(",")
      newLabelField = $('#new_label_name')
      newColorField = $('#new_label_color')
      showNo = $(dropdown).data('show-no')
      showAny = $(dropdown).data('show-any')
      defaultLabel = $(dropdown).text().trim()

      if newLabelField.length
        $('.suggest-colors-dropdown a').on "click", (e) ->
          e.preventDefault()
          e.stopPropagation()
          newColorField.val $(this).data("color")
          $('.js-dropdown-label-color-preview')
            .css 'background-color', $(this).data("color")
            .addClass 'is-active'

        $('.js-new-label-btn').on "click", (e) ->
          e.preventDefault()
          e.stopPropagation()

          if newLabelField.val() isnt "" && newColorField.val() isnt ""
            $('.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()
              $('.dropdown-menu-back', $(dropdown).parent()).trigger "click"

      $(dropdown).glDropdown(
        data: (term, callback) ->
          # We have to fetch the JS version of the labels list because there is no
          # public facing JSON url for labels
          $.ajax(
            url: labelUrl
          ).done (data) ->
            html = $(data)
            data = []
            html.find('.label-row a').each ->
              data.push(
                title: $(@).text().trim()
              )

            if showNo
              data.unshift(
                id: 0
                title: 'No Label'
              )

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

            if data.length > 2
              data.splice 2, 0, "divider"

            callback data
        renderRow: (label) ->
          if $.isArray(selectedLabel)
            selected = ""
            $.each selectedLabel, (i, selectedLbl) ->
              selectedLbl = selectedLbl.trim()
              if selected is "" && label.title is selectedLbl
                selected = "is-active"
          else
            selected = if label.title is selectedLabel then "is-active" else ""

          "<li>
            <a href='#' class='#{selected}'>
              #{label.title}
            </a>
          </li>"
        filterable: true
        search:
          fields: ['title']
        selectable: true
        toggleLabel: (selected) ->
          if selected && selected.title isnt "Any Label"
            selected.title
          else
            defaultLabel
        fieldName: $(dropdown).data('field-name')
        id: (label) ->
          if label.any?
            ""
          else
            label.title
        clicked: ->
          page = $("body").data "page"
          
          if $(dropdown).hasClass("js-filter-submit") && page is "projects:issues:index"
            Issues.filterResults $(dropdown).parents("form")
          else if $(dropdown).hasClass "js-filter-submit"
            $(dropdown).parents("form").submit()
      )