summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issues-bulk-assignment.js.coffee
blob: b454f9389dd789326d75dafd26303ceed476d509 (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
class @IssuableBulkActions
  constructor: (opts = {}) ->
    # Set defaults
    {
      @container = $('.content')
      @form = @getElement('.bulk-update')
      @issues = @getElement('.issues-list .issue')
    } = opts

    @bindEvents()

    # Fixes bulk-assign not working when navigating through pages
    Issuable.initChecks();

  getElement: (selector) ->
    @container.find selector

  bindEvents: ->
    @form.off('submit').on('submit', @onFormSubmit.bind(@))

  onFormSubmit: (e) ->
    e.preventDefault()
    @submit()

  submit: ->
    _this = @

    xhr = $.ajax
            url: @form.attr 'action'
            method: @form.attr 'method'
            dataType: 'JSON',
            data: @getFormDataAsObject()

    xhr.done (response, status, xhr) ->
      location.reload()

    xhr.fail ->
      new Flash("Issue update failed")

    xhr.always @onFormSubmitAlways.bind(@)

  onFormSubmitAlways: ->
    @form.find('[type="submit"]').enable()

  getSelectedIssues: ->
    @issues.has('.selected_issue:checked')

  getLabelsFromSelection: ->
    labels = []

    @getSelectedIssues().map ->
      _labels = $(@).data('labels')
      if _labels
        _labels.map (labelId) ->
          labels.push(labelId) if labels.indexOf(labelId) is -1

    labels

  ###*
   * Will return only labels that were marked previously and the user has unmarked
   * @return {Array} Label IDs
  ###
  getUnmarkedIndeterminedLabels: ->
    result = []
    labelsToKeep = []

    for el in @getElement('.labels-filter .is-indeterminate')
      labelsToKeep.push $(el).data('labelId')

    for id in @getLabelsFromSelection()
      # Only the ones that we are not going to keep
      result.push(id) if labelsToKeep.indexOf(id) is -1

    result

  ###*
   * Simple form serialization, it will return just what we need
   * Returns key/value pairs from form data
  ###
  getFormDataAsObject: ->
    formData =
      update:
        state_event       : @form.find('input[name="update[state_event]"]').val()
        assignee_id       : @form.find('input[name="update[assignee_id]"]').val()
        milestone_id      : @form.find('input[name="update[milestone_id]"]').val()
        issues_ids        : @form.find('input[name="update[issues_ids]"]').val()
        add_label_ids     : []
        remove_label_ids  : []

    @getLabelsToApply().map (id) ->
      formData.update.add_label_ids.push id

    @getLabelsToRemove().map (id) ->
      formData.update.remove_label_ids.push id

    formData

  getLabelsToApply: ->
    labelIds = []
    $labels = @form.find('.labels-filter input[name="update[label_ids][]"]')

    $labels.each (k, label) ->
      labelIds.push parseInt($(label).val()) if label

    labelIds

  ###*
   * Returns Label IDs that will be removed from issue selection
   * @return {Array} Array of labels IDs
  ###
  getLabelsToRemove: ->
    result = []
    indeterminatedLabels = @getUnmarkedIndeterminedLabels()
    labelsToApply = @getLabelsToApply()

    indeterminatedLabels.map (id) ->
      # We need to exclude label IDs that will be applied
      # By not doing this will cause issues from selection to not add labels at all
      result.push(id) if labelsToApply.indexOf(id) is -1

    result