summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/milestone_select.js.coffee
blob: 8ab03ed93eec3f53623158ff9454b17a9624628f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class @MilestoneSelect
  constructor: (currentProject) ->
    if currentProject?
      _this = @
      @currentProject = JSON.parse(currentProject)
    $('.js-milestone-select').each (i, dropdown) ->
      $dropdown = $(dropdown)
      projectId = $dropdown.data('project-id')
      milestonesUrl = $dropdown.data('milestones')
      issueUpdateURL = $dropdown.data('issueUpdate')
      selectedMilestone = $dropdown.data('selected')
      showNo = $dropdown.data('show-no')
      showAny = $dropdown.data('show-any')
      showUpcoming = $dropdown.data('show-upcoming')
      useId = $dropdown.data('use-id')
      defaultLabel = $dropdown.data('default-label')
      issuableId = $dropdown.data('issuable-id')
      abilityName = $dropdown.data('ability-name')
      $selectbox = $dropdown.closest('.selectbox')
      $block = $selectbox.closest('.block')
      $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon')
      $value = $block.find('.value')
      $loading = $block.find('.block-loading').fadeOut()

      if issueUpdateURL
        milestoneLinkTemplate = _.template(
          '<a href="/<%- namespace %>/<%- path %>/milestones/<%- iid %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>'
        )

        milestoneLinkNoneTemplate = '<span class="no-value">None</span>'

        collapsedSidebarLabelTemplate = _.template(
          '<span class="has-tooltip" data-container="body" title="<%- remaining %>" data-placement="left">
            <%- title %>
          </span>'
        )

      $dropdown.glDropdown(
        data: (term, callback) ->
          $.ajax(
            url: milestonesUrl
          ).done (data) ->
            extraOptions = []
            if showAny
              extraOptions.push(
                id: 0
                name: ''
                title: 'Any Milestone'
              )

            if showNo
              extraOptions.push(
                id: -1
                name: 'No Milestone'
                title: 'No Milestone'
              )

            if showUpcoming
              extraOptions.push(
                id: -2
                name: '#upcoming'
                title: 'Upcoming'
              )

            if extraOptions.length > 2
              extraOptions.push 'divider'

            callback(extraOptions.concat(data))
        filterable: true
        search:
          fields: ['title']
        selectable: true
        toggleLabel: (selected) ->
          if selected && 'id' of selected
            selected.title
          else
            defaultLabel
        fieldName: $dropdown.data('field-name')
        text: (milestone) ->
          _.escape(milestone.title)
        id: (milestone) ->
          if !useId
            milestone.name
          else
            milestone.id
        isSelected: (milestone) ->
          milestone.name is selectedMilestone
        hidden: ->
          $selectbox.hide()

          # display:block overrides the hide-collapse rule
          $value.css('display', '')
        clicked: (selected) ->
          page = $('body').data 'page'
          isIssueIndex = page is 'projects:issues:index'
          isMRIndex = page is page is 'projects:merge_requests:index'

          if $dropdown.hasClass 'js-filter-bulk-update'
            return

          if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
            if selected.name?
              selectedMilestone = selected.name
            else
              selectedMilestone = ''
            Issuable.filterResults $dropdown.closest('form')
          else if $dropdown.hasClass('js-filter-submit')
            $dropdown.closest('form').submit()
          else
            selected = $selectbox
              .find('input[type="hidden"]')
              .val()
            data = {}
            data[abilityName] = {}
            data[abilityName].milestone_id = if selected? then selected else null
            $loading
              .fadeIn()
            $dropdown.trigger('loading.gl.dropdown')
            $.ajax(
              type: 'PUT'
              url: issueUpdateURL
              data: data
            ).done (data) ->
              $dropdown.trigger('loaded.gl.dropdown')
              $loading.fadeOut()
              $selectbox.hide()
              $value.css('display', '')
              if data.milestone?
                data.milestone.namespace = _this.currentProject.namespace
                data.milestone.path = _this.currentProject.path
                data.milestone.remaining = $.timefor data.milestone.due_date
                $value.html(milestoneLinkTemplate(data.milestone))
                $sidebarCollapsedValue.find('span').html(collapsedSidebarLabelTemplate(data.milestone))
              else
                $value.html(milestoneLinkNoneTemplate)
                $sidebarCollapsedValue.find('span').text('No')
      )