summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issues_bulk_assignment.js.es6
blob: 9697fb33566b3a534c9950a79a8764e4811287cc (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
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-disable */
((global) => {

  class IssuableBulkActions {
    constructor({ container, form, issues } = {}) {
      this.container = container || $('.content'),
      this.form = form || this.getElement('.bulk-update');
      this.issues = issues || this.getElement('.issues-list .issue');
      this.form.data('bulkActions', this);
      this.willUpdateLabels = false;
      this.bindEvents();
      // Fixes bulk-assign not working when navigating through pages
      Issuable.initChecks();
    }

    getElement(selector) {
      return this.container.find(selector);
    }

    bindEvents() {
      return this.form.off('submit').on('submit', this.onFormSubmit.bind(this));
    }

    onFormSubmit(e) {
      e.preventDefault();
      return this.submit();
    }

    submit() {
      const _this = this;
      const xhr = $.ajax({
        url: this.form.attr('action'),
        method: this.form.attr('method'),
        dataType: 'JSON',
        data: this.getFormDataAsObject()
      });
      xhr.done(() => window.location.reload());
      xhr.fail(() => new Flash("Issue update failed"));
      return xhr.always(this.onFormSubmitAlways.bind(this));
    }

    onFormSubmitAlways() {
      return this.form.find('[type="submit"]').enable();
    }

    getSelectedIssues() {
      return this.issues.has('.selected_issue:checked');
    }

    getLabelsFromSelection() {
      const labels = [];
      this.getSelectedIssues().map(function() {
        const labelsData = $(this).data('labels');
        if (labelsData) {
          return labelsData.map(function(labelId) {
            if (labels.indexOf(labelId) === -1) {
              return labels.push(labelId);
            }
          });
        }
      });
      return labels;
    }


    /**
     * Will return only labels that were marked previously and the user has unmarked
     * @return {Array} Label IDs
     */

    getUnmarkedIndeterminedLabels() {
      const result = [];
      const labelsToKeep = [];

      this.getElement('.labels-filter .is-indeterminate')
        .each((i, el) => labelsToKeep.push($(el).data('labelId')));

      this.getLabelsFromSelection().forEach((id) => {
        if (labelsToKeep.indexOf(id) === -1) {
          result.push(id);
        }
      });

      return result;
    }


    /**
     * Simple form serialization, it will return just what we need
     * Returns key/value pairs from form data
     */

    getFormDataAsObject() {
      const formData = {
        update: {
          state_event: this.form.find('input[name="update[state_event]"]').val(),
          assignee_id: this.form.find('input[name="update[assignee_id]"]').val(),
          milestone_id: this.form.find('input[name="update[milestone_id]"]').val(),
          issuable_ids: this.form.find('input[name="update[issuable_ids]"]').val(),
          subscription_event: this.form.find('input[name="update[subscription_event]"]').val(),
          add_label_ids: [],
          remove_label_ids: []
        }
      };
      if (this.willUpdateLabels) {
        this.getLabelsToApply().map(function(id) {
          return formData.update.add_label_ids.push(id);
        });
        this.getLabelsToRemove().map(function(id) {
          return formData.update.remove_label_ids.push(id);
        });
      }
      return formData;
    }

    getLabelsToApply() {
      const labelIds = [];
      const $labels = this.form.find('.labels-filter input[name="update[label_ids][]"]');
      $labels.each(function(k, label) {
        if (label) {
          return labelIds.push(parseInt($(label).val()));
        }
      });
      return labelIds;
    }


    /**
     * Returns Label IDs that will be removed from issue selection
     * @return {Array} Array of labels IDs
     */

    getLabelsToRemove() {
      const result = [];
      const indeterminatedLabels = this.getUnmarkedIndeterminedLabels();
      const labelsToApply = this.getLabelsToApply();
      indeterminatedLabels.map(function(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
        if (labelsToApply.indexOf(id) === -1) {
          return result.push(id);
        }
      });
      return result;
    }
  }

  global.IssuableBulkActions = IssuableBulkActions;

})(window.gl || (window.gl = {}));