All files / app/assets/javascripts merge_request.js

36.84% Statements 28/76
27.02% Branches 10/37
37.5% Functions 6/16
36.84% Lines 28/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                        43x 43x 43x   43x 43x   43x 4x           3x 3x   3x       3x         1x                     6x       6x 43x 41x     43x     6x 43x 43x   43x                                                                             43x                                   6x                 6x 1x 1x   1x     6x                                                              
/* eslint-disable func-names, no-underscore-dangle, consistent-return */
 
import $ from 'jquery';
import { createAlert } from '~/alert';
import { TYPE_MERGE_REQUEST } from '~/issues/constants';
import toast from '~/vue_shared/plugins/global_toast';
import { __ } from '~/locale';
import { loadingIconForLegacyJS } from '~/loading_icon_for_legacy_js';
import axios from './lib/utils/axios_utils';
import { addDelimiter } from './lib/utils/text_utility';
import { getParameterValues, setUrlParams } from './lib/utils/url_utility';
import MergeRequestTabs from './merge_request_tabs';
import TaskList from './task_list';
 
function MergeRequest(opts) {
  // Initialize MergeRequest behavior
  //
  // Options:
  //   action - String, current controller action
  //
  this.opts = opts != null ? opts : {};
  this.submitNoteForm = this.submitNoteForm.bind(this);
  this.$el = $('.merge-request');
 
  this.initTabs();
  this.initMRBtnListeners();
 
  if ($('.description.js-task-list-container').length) {
    this.taskList = new TaskList({
      dataType: TYPE_MERGE_REQUEST,
      fieldName: 'description',
      selector: '.detail-page-description',
      lockVersion: this.$el.data('lockVersion'),
      onSuccess: (result) => {
        const taskStatus = document.querySelector('#task_status');
        const taskStatusShort = document.querySelector('#task_status_short');
 
        Iif (taskStatus) {
          taskStatus.innerText = result.task_status;
        }
 
        Iif (taskStatusShort) {
          document.querySelector('#task_status_short').innerText = result.task_status_short;
        }
      },
      onError: () => {
        createAlert({
          message: __(
            'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
          ),
        });
      },
    });
  }
}
 
// Local jQuery finder
MergeRequest.prototype.$ = function (selector) {
  return this.$el.find(selector);
};
 
MergeRequest.prototype.initTabs = function () {
  if (window.mrTabs) {
    window.mrTabs.unbindEvents();
  }
 
  window.mrTabs = new MergeRequestTabs(this.opts);
};
 
MergeRequest.prototype.initMRBtnListeners = function () {
  const _this = this;
  const draftToggles = document.querySelectorAll('.js-draft-toggle-button');
 
  Iif (draftToggles.length) {
    draftToggles.forEach((draftToggle) => {
      draftToggle.addEventListener('click', (e) => {
        e.preventDefault();
        e.stopImmediatePropagation();
 
        const url = draftToggle.href;
        const wipEvent = getParameterValues('merge_request[wip_event]', url)[0];
        const mobileDropdown = draftToggle.closest('.dropdown.show');
 
        const loader = loadingIconForLegacyJS({ inline: true, classes: ['gl-mr-3'] });
 
        if (mobileDropdown) {
          $(mobileDropdown.firstElementChild).dropdown('toggle');
        }
 
        draftToggle.setAttribute('disabled', 'disabled');
        draftToggle.prepend(loader);
 
        axios
          .put(draftToggle.href, null, { params: { format: 'json' } })
          .then(({ data }) => {
            draftToggle.removeAttribute('disabled');
 
            MergeRequest.toggleDraftStatus(data.title, wipEvent === 'ready');
          })
          .catch(() => {
            createAlert({
              message: __('Something went wrong. Please try again.'),
            });
          })
          .finally(() => {
            draftToggle.removeAttribute('disabled');
            loader.remove();
          });
      });
    });
  }
 
  return $('.btn-close, .btn-reopen').on('click', function (e) {
    const $this = $(this);
    const shouldSubmit = $this.hasClass('btn-comment');
    if (shouldSubmit && $this.data('submitted')) {
      return;
    }
 
    if (shouldSubmit) {
      if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
        e.preventDefault();
        e.stopImmediatePropagation();
 
        _this.submitNoteForm($this.closest('form'), $this);
      }
    }
  });
};
 
MergeRequest.prototype.submitNoteForm = function (form, $button) {
  const noteText = form.find('textarea.js-note-text').val();
  if (noteText.trim().length > 0) {
    form.submit();
    $button.data('submitted', true);
    return $button.trigger('click');
  }
};
 
MergeRequest.decreaseCounter = function (by = 1) {
  const $el = $('.js-merge-counter');
  const count = Math.max(parseInt($el.first().text().replace(/[^\d]/, ''), 10) - by, 0);
 
  $el.text(addDelimiter(count));
};
 
MergeRequest.toggleDraftStatus = function (title, isReady) {
  if (isReady) {
    toast(__('Marked as ready. Merging is now allowed.'));
  } else {
    toast(__('Marked as draft. Can only be merged when marked as ready.'));
  }
  const titleEl = document.querySelector(`.merge-request .detail-page-header .title`);
 
  if (titleEl) {
    titleEl.textContent = title;
  }
 
  const draftToggles = document.querySelectorAll('.js-draft-toggle-button');
 
  if (draftToggles.length) {
    draftToggles.forEach((el) => {
      const draftToggle = el;
      const url = setUrlParams(
        { 'merge_request[wip_event]': isReady ? 'draft' : 'ready' },
        draftToggle.href,
      );
 
      draftToggle.setAttribute('href', url);
      draftToggle.querySelector('.gl-dropdown-item-text-wrapper').textContent = isReady
        ? __('Mark as draft')
        : __('Mark as ready');
    });
  }
};
 
export default MergeRequest;