summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/milestone_select.js.coffee
blob: 5e884454a6585e9963263dcda575f83ef7fccc34 (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
class @MilestoneSelect
  constructor: ->
    $('.js-milestone-select').each (i, dropdown) ->
      projectId = $(dropdown).data('project-id')
      milestonesUrl = $(dropdown).data('milestones')
      selectedMilestone = $(dropdown).data('selected')
      showNo = $(dropdown).data('show-no')
      showAny = $(dropdown).data('show-any')
      useId = $(dropdown).data('use-id')

      $(dropdown).glDropdown(
        data: (term, callback) ->
          $.ajax(
            url: milestonesUrl
          ).done (data) ->
            html = $(data)
            data = []
            html.find('.milestone strong a').each ->
              link = $(@).attr("href").split("/")
              data.push(
                id: link[link.length - 1]
                title: $(@).text().trim()
              )

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

            if showAny
              data.unshift(
                title: 'Any Milestone'
              )

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

            callback(data)
        filterable: true
        search:
          fields: ['title']
        selectable: true
        fieldName: $(dropdown).data('field-name')
        text: (milestone) ->
          milestone.title
        id: (milestone) ->
          if !useId
            if milestone.title isnt "Any milestone"
              milestone.title
            else
              ""
          else
            milestone.id
        isSelected: (milestone) ->
          milestone.title is selectedMilestone
        clicked: ->
          if $(dropdown).hasClass "js-filter-submit"
            $(dropdown).parents('form').submit()
      )