BigW Consortium Gitlab

milestone_select.js.coffee 1.71 KB
Newer Older
Phil Hughes committed
1 2 3 4
class @MilestoneSelect
  constructor: ->
    $('.js-milestone-select').each (i, dropdown) ->
      projectId = $(dropdown).data('project-id')
5
      milestonesUrl = $(dropdown).data('milestones')
Phil Hughes committed
6
      selectedMilestone = $(dropdown).data('selected')
7 8
      showNo = $(dropdown).data('show-no')
      showAny = $(dropdown).data('show-any')
9
      useId = $(dropdown).data('use-id')
Phil Hughes committed
10 11

      $(dropdown).glDropdown(
12
        data: (term, callback) ->
13 14 15 16 17 18 19 20 21 22 23
          $.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()
              )
24 25 26

            if showNo
              data.unshift(
27
                id: "0"
Phil Hughes committed
28
                title: 'No Milestone'
29
              )
Phil Hughes committed
30

31 32
            if showAny
              data.unshift(
Phil Hughes committed
33
                title: 'Any Milestone'
34 35 36 37 38 39
              )

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

            callback(data)
Phil Hughes committed
40 41
        filterable: true
        search:
42
          fields: ['title']
Phil Hughes committed
43 44 45 46 47
        selectable: true
        fieldName: $(dropdown).data('field-name')
        text: (milestone) ->
          milestone.title
        id: (milestone) ->
48 49 50 51 52
          if !useId
            if milestone.title isnt "Any milestone"
              milestone.title
            else
              ""
53
          else
54
            milestone.id
Phil Hughes committed
55 56 57
        isSelected: (milestone) ->
          milestone.title is selectedMilestone
        clicked: ->
Phil Hughes committed
58 59
          if $(dropdown).hasClass "js-filter-submit"
            $(dropdown).parents('form').submit()
Phil Hughes committed
60
      )