milestone_select.js.coffee 4.3 KB
Newer Older
P
Phil Hughes 已提交
1
class @MilestoneSelect
J
Jacob Schatz 已提交
2 3 4 5
  constructor: (currentProject) ->
    if currentProject?
      _this = @
      @currentProject = JSON.parse(currentProject)
P
Phil Hughes 已提交
6
    $('.js-milestone-select').each (i, dropdown) ->
P
Phil Hughes 已提交
7 8 9
      $dropdown = $(dropdown)
      projectId = $dropdown.data('project-id')
      milestonesUrl = $dropdown.data('milestones')
10
      issueUpdateURL = $dropdown.data('issueUpdate')
P
Phil Hughes 已提交
11 12 13
      selectedMilestone = $dropdown.data('selected')
      showNo = $dropdown.data('show-no')
      showAny = $dropdown.data('show-any')
14
      showUpcoming = $dropdown.data('show-upcoming')
P
Phil Hughes 已提交
15
      useId = $dropdown.data('use-id')
16
      defaultLabel = $dropdown.data('default-label')
17
      issuableId = $dropdown.data('issuable-id')
18
      abilityName = $dropdown.data('ability-name')
19 20 21
      $selectbox = $dropdown.closest('.selectbox')
      $block = $selectbox.closest('.block')
      $value = $block.find('.value')
22
      $loading = $block.find('.block-loading').fadeOut()
P
Phil Hughes 已提交
23

J
Jacob Schatz 已提交
24 25 26 27 28 29 30
      if issueUpdateURL
        milestoneLinkTemplate = _.template(
          '<a href="/<%= namespace %>/<%= path %>/milestones/<%= iid %>"><%= title %></a>'
        )

        milestoneLinkNoneTemplate = '<div class="light">None</div>'

P
Phil Hughes 已提交
31
      $dropdown.glDropdown(
P
Phil Hughes 已提交
32
        data: (term, callback) ->
33 34 35
          $.ajax(
            url: milestonesUrl
          ).done (data) ->
36
<<<<<<< c9dea7761dc69ff38d101d06e0e636e1f3b2a0c4
37 38 39 40 41 42
            if $dropdown.hasClass "js-extra-options"
              if showNo
                data.unshift(
                  id: '0'
                  title: 'No Milestone'
                )
P
Phil Hughes 已提交
43

44 45 46 47 48
              if showAny
                data.unshift(
                  isAny: true
                  title: 'Any Milestone'
                )
49 50 51 52
=======
            extraOptions = []
            if showAny
              extraOptions.push(
53 54
                id: 0
                name: ''
55 56 57 58 59
                title: 'Any Milestone'
              )

            if showNo
              extraOptions.push(
60 61
                id: -1
                name: 'No Milestone'
62 63 64 65 66
                title: 'No Milestone'
              )

            if showUpcoming
              extraOptions.push(
67 68
                id: -2
                name: '#upcoming'
69 70 71
                title: 'Upcoming'
              )
>>>>>>> Updated to only include upcoming on filters
72

73 74
              if data.length > 2
                data.splice 2, 0, 'divider'
75
            callback(data)
P
Phil Hughes 已提交
76 77
        filterable: true
        search:
P
Phil Hughes 已提交
78
          fields: ['title']
P
Phil Hughes 已提交
79
        selectable: true
80
        toggleLabel: (selected) ->
P
Phil Hughes 已提交
81
          if selected && 'id' of selected
82 83 84
            selected.title
          else
            defaultLabel
P
Phil Hughes 已提交
85
        fieldName: $dropdown.data('field-name')
P
Phil Hughes 已提交
86 87 88
        text: (milestone) ->
          milestone.title
        id: (milestone) ->
89
          if !useId
90
            milestone.name
91
          else
92
            milestone.id
P
Phil Hughes 已提交
93
        isSelected: (milestone) ->
94
          milestone.name is selectedMilestone
95 96 97
        hidden: ->
          $selectbox.hide()
          $value.show()
P
Phil Hughes 已提交
98
        clicked: (selected) ->
99 100
          if $dropdown.hasClass 'js-filter-bulk-update'
            return
P
Phil Hughes 已提交
101

102 103 104 105
          if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
            if selected.name?
              selectedMilestone = selected.name
            else if selected.title?
P
Phil Hughes 已提交
106
              selectedMilestone = selected.title
107 108
            else
              selectedMilestone = ''
109
            $dropdown.parents('form').submit()
110
          else
111
            selected = $selectbox
112 113
              .find('input[type="hidden"]')
              .val()
114 115 116
            data = {}
            data[abilityName] = {}
            data[abilityName].milestone_id = selected
117 118
            $loading
              .fadeIn()
119 120 121
            $.ajax(
              type: 'PUT'
              url: issueUpdateURL
122
              data: data
123
            ).done (data) ->
124
              $loading.fadeOut()
125
              $selectbox.hide()
126
              $milestoneLink = $value
127 128
                      .show()
                      .find('a')
J
Jacob Schatz 已提交
129 130 131 132 133 134
              if data.milestone?
                data.milestone.namespace = _this.currentProject.namespace
                data.milestone.path = _this.currentProject.path
                $value.html(milestoneLinkTemplate(data.milestone))
              else
                $value.html(milestoneLinkNoneTemplate)
P
Phil Hughes 已提交
135
      )