issue.js 5.13 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, no-underscore-dangle, one-var-declaration-per-line, object-shorthand, no-unused-vars, no-new, comma-dangle, consistent-return, quotes, dot-notation, quote-props, prefer-arrow-callback, max-len */
2
import 'vendor/jquery.waitforimages';
3
import { addDelimiter } from './lib/utils/text_utility';
Phil Hughes's avatar
Phil Hughes committed
4
import Flash from './flash';
5
import TaskList from './task_list';
6
import CreateMergeRequestDropdown from './create_merge_request_dropdown';
7
import IssuablesHelper from './helpers/issuables_helper';
Fatih Acet's avatar
Fatih Acet committed
8

9
export default class Issue {
10 11
  constructor() {
    if ($('a.btn-close').length) {
12
      this.taskList = new TaskList({
13 14 15 16 17 18 19 20
        dataType: 'issue',
        fieldName: 'description',
        selector: '.detail-page-description',
        onSuccess: (result) => {
          document.querySelector('#task_status').innerText = result.task_status;
          document.querySelector('#task_status_short').innerText = result.task_status_short;
        }
      });
21
      this.initIssueBtnEventListeners();
Fatih Acet's avatar
Fatih Acet committed
22
    }
23 24

    Issue.$btnNewBranch = $('#new-branch');
25
    Issue.createMrDropdownWrap = document.querySelector('.create-mr-dropdown-wrap');
26

27 28
    Issue.initMergeRequests();
    Issue.initRelatedBranches();
29

30 31
    this.closeButtons = $('a.btn-close');
    this.reopenButtons = $('a.btn-reopen');
32

33
    this.initCloseReopenReport();
34

35 36 37
    if (Issue.createMrDropdownWrap) {
      this.createMergeRequestDropdown = new CreateMergeRequestDropdown(Issue.createMrDropdownWrap);
    }
38
  }
Fatih Acet's avatar
Fatih Acet committed
39

40
  initIssueBtnEventListeners() {
41 42
    const issueFailMessage = 'Unable to update this issue at this time.';

43
    return $(document).on('click', '.js-issuable-actions a.btn-close, .js-issuable-actions a.btn-reopen', (e) => {
44
      var $button, shouldSubmit, url;
45 46
      e.preventDefault();
      e.stopImmediatePropagation();
47 48
      $button = $(e.currentTarget);
      shouldSubmit = $button.hasClass('btn-comment');
49
      if (shouldSubmit) {
50
        Issue.submitNoteForm($button.closest('form'));
51
      }
52

53
      this.disableCloseReopenButton($button);
54

55
      url = $button.attr('href');
56 57
      return $.ajax({
        type: 'PUT',
58
        url: url
59 60 61
      })
      .fail(() => new Flash(issueFailMessage))
      .done((data) => {
62 63 64 65
        const isClosedBadge = $('div.status-box-closed');
        const isOpenBadge = $('div.status-box-open');
        const projectIssuesCounter = $('.issue_counter');

66
        if ('id' in data) {
67
          const isClosed = $button.hasClass('btn-close');
68 69 70
          isClosedBadge.toggleClass('hidden', !isClosed);
          isOpenBadge.toggleClass('hidden', isClosed);

71
          $(document).trigger('issuable:change', isClosed);
72
          this.toggleCloseReopenButton(isClosed);
73

74
          let numProjectIssues = Number(projectIssuesCounter.first().text().trim().replace(/[^\d]/, ''));
75
          numProjectIssues = isClosed ? numProjectIssues - 1 : numProjectIssues + 1;
76
          projectIssuesCounter.text(addDelimiter(numProjectIssues));
77 78 79 80 81 82 83 84 85 86

          if (this.createMergeRequestDropdown) {
            if (isClosed) {
              this.createMergeRequestDropdown.unavailable();
              this.createMergeRequestDropdown.disable();
            } else {
              // We should check in case a branch was created in another tab
              this.createMergeRequestDropdown.checkAbilityToCreateBranch();
            }
          }
87 88
        } else {
          new Flash(issueFailMessage);
89
        }
90 91
      })
      .then(() => {
92
        this.disableCloseReopenButton($button, false);
Fatih Acet's avatar
Fatih Acet committed
93
      });
94 95
    });
  }
Fatih Acet's avatar
Fatih Acet committed
96

97
  initCloseReopenReport() {
98
    this.closeReopenReportToggle = IssuablesHelper.initCloseReopenReport();
99

100 101
    if (this.closeButtons) this.closeButtons = this.closeButtons.not('.issuable-close-button');
    if (this.reopenButtons) this.reopenButtons = this.reopenButtons.not('.issuable-close-button');
102 103
  }

104 105 106 107 108 109 110 111 112
  disableCloseReopenButton($button, shouldDisable) {
    if (this.closeReopenReportToggle) {
      this.closeReopenReportToggle.setDisable(shouldDisable);
    } else {
      $button.prop('disabled', shouldDisable);
    }
  }

  toggleCloseReopenButton(isClosed) {
113 114 115
    if (this.closeReopenReportToggle) this.closeReopenReportToggle.updateButton(isClosed);
    this.closeButtons.toggleClass('hidden', isClosed);
    this.reopenButtons.toggleClass('hidden', !isClosed);
116 117
  }

118 119 120
  static submitNoteForm(form) {
    var noteText;
    noteText = form.find("textarea.js-note-text").val();
121
    if (noteText && noteText.trim().length > 0) {
122 123 124
      return form.submit();
    }
  }
Fatih Acet's avatar
Fatih Acet committed
125

126 127 128
  static initMergeRequests() {
    var $container;
    $container = $('#merge-requests');
129 130 131
    return $.getJSON($container.data('url')).fail(function() {
      return new Flash('Failed to load referenced merge requests');
    }).done(function(data) {
132 133 134 135 136
      if ('html' in data) {
        return $container.html(data.html);
      }
    });
  }
Fatih Acet's avatar
Fatih Acet committed
137

138 139 140
  static initRelatedBranches() {
    var $container;
    $container = $('#related-branches');
141 142 143
    return $.getJSON($container.data('url')).fail(function() {
      return new Flash('Failed to load related branches');
    }).done(function(data) {
144 145 146 147 148 149
      if ('html' in data) {
        return $container.html(data.html);
      }
    });
  }
}