2019-11-18 10:06:27 -05:00
|
|
|
/* eslint-disable func-names, no-underscore-dangle, consistent-return */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2021-06-04 14:10:08 -04:00
|
|
|
import createFlash from '~/flash';
|
2021-10-12 17:09:47 -04:00
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2018-01-17 09:09:59 -05:00
|
|
|
import { __ } from '~/locale';
|
2020-10-01 08:10:14 -04:00
|
|
|
import eventHub from '~/vue_merge_request_widget/event_hub';
|
2021-02-01 10:08:56 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-11-13 04:11:54 -05:00
|
|
|
import { addDelimiter } from './lib/utils/text_utility';
|
2020-10-01 08:10:14 -04:00
|
|
|
import { getParameterValues, setUrlParams } from './lib/utils/url_utility';
|
2021-02-14 13:09:20 -05:00
|
|
|
import MergeRequestTabs from './merge_request_tabs';
|
|
|
|
import TaskList from './task_list';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
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();
|
|
|
|
|
2020-01-31 13:09:11 -05:00
|
|
|
if ($('.description.js-task-list-container').length) {
|
2017-12-15 07:57:08 -05:00
|
|
|
this.taskList = new TaskList({
|
|
|
|
dataType: 'merge_request',
|
|
|
|
fieldName: 'description',
|
|
|
|
selector: '.detail-page-description',
|
2019-01-24 18:31:14 -05:00
|
|
|
lockVersion: this.$el.data('lockVersion'),
|
2020-12-23 16:10:24 -05:00
|
|
|
onSuccess: (result) => {
|
2017-12-15 07:57:08 -05:00
|
|
|
document.querySelector('#task_status').innerText = result.task_status;
|
|
|
|
document.querySelector('#task_status_short').innerText = result.task_status_short;
|
2018-10-10 03:13:34 -04:00
|
|
|
},
|
2019-02-06 18:18:07 -05:00
|
|
|
onError: () => {
|
2021-06-04 14:10:08 -04:00
|
|
|
createFlash({
|
|
|
|
message: __(
|
2019-02-06 18:18:07 -05:00
|
|
|
'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
|
|
|
|
),
|
2021-06-04 14:10:08 -04:00
|
|
|
});
|
2019-02-06 18:18:07 -05:00
|
|
|
},
|
2017-12-15 07:57:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local jQuery finder
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.prototype.$ = function (selector) {
|
2017-12-15 07:57:08 -05:00
|
|
|
return this.$el.find(selector);
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.prototype.initTabs = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
if (window.mrTabs) {
|
|
|
|
window.mrTabs.unbindEvents();
|
|
|
|
}
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
window.mrTabs = new MergeRequestTabs(this.opts);
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.prototype.initMRBtnListeners = function () {
|
2019-11-18 10:06:27 -05:00
|
|
|
const _this = this;
|
2020-10-01 08:10:14 -04:00
|
|
|
const draftToggles = document.querySelectorAll('.js-draft-toggle-button');
|
2020-09-01 20:10:29 -04:00
|
|
|
|
2020-10-01 08:10:14 -04:00
|
|
|
if (draftToggles.length) {
|
2020-12-23 16:10:24 -05:00
|
|
|
draftToggles.forEach((draftToggle) => {
|
|
|
|
draftToggle.addEventListener('click', (e) => {
|
2020-10-01 08:10:14 -04:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
|
|
|
|
const url = draftToggle.href;
|
|
|
|
const wipEvent = getParameterValues('merge_request[wip_event]', url)[0];
|
|
|
|
const mobileDropdown = draftToggle.closest('.dropdown.show');
|
|
|
|
|
|
|
|
if (mobileDropdown) {
|
|
|
|
$(mobileDropdown.firstElementChild).dropdown('toggle');
|
|
|
|
}
|
|
|
|
|
|
|
|
draftToggle.setAttribute('disabled', 'disabled');
|
|
|
|
|
|
|
|
axios
|
|
|
|
.put(draftToggle.href, null, { params: { format: 'json' } })
|
|
|
|
.then(({ data }) => {
|
|
|
|
draftToggle.removeAttribute('disabled');
|
|
|
|
eventHub.$emit('MRWidgetUpdateRequested');
|
|
|
|
MergeRequest.toggleDraftStatus(data.title, wipEvent === 'unwip');
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
draftToggle.removeAttribute('disabled');
|
2021-06-04 14:10:08 -04:00
|
|
|
createFlash({
|
|
|
|
message: __('Something went wrong. Please try again.'),
|
|
|
|
});
|
2020-10-01 08:10:14 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-09-01 20:10:29 -04:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
return $('.btn-close, .btn-reopen').on('click', function (e) {
|
2019-11-18 10:06:27 -05:00
|
|
|
const $this = $(this);
|
|
|
|
const shouldSubmit = $this.hasClass('btn-comment');
|
2017-12-15 07:57:08 -05:00
|
|
|
if (shouldSubmit && $this.data('submitted')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-28 06:30:25 -05:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
if (shouldSubmit) {
|
|
|
|
if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
|
2016-11-28 06:30:25 -05:00
|
|
|
e.preventDefault();
|
2017-12-15 07:57:08 -05:00
|
|
|
e.stopImmediatePropagation();
|
2016-11-28 06:30:25 -05:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
_this.submitNoteForm($this.closest('form'), $this);
|
2017-09-07 16:25:50 -04:00
|
|
|
}
|
2017-12-15 07:57:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.prototype.submitNoteForm = function (form, $button) {
|
2019-11-18 10:06:27 -05:00
|
|
|
const noteText = form.find('textarea.js-note-text').val();
|
2017-12-15 07:57:08 -05:00
|
|
|
if (noteText.trim().length > 0) {
|
|
|
|
form.submit();
|
|
|
|
$button.data('submitted', true);
|
|
|
|
return $button.trigger('click');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.decreaseCounter = function (by = 1) {
|
2018-01-15 12:06:38 -05:00
|
|
|
const $el = $('.js-merge-counter');
|
2018-10-10 03:13:34 -04:00
|
|
|
const count = Math.max(parseInt($el.text().replace(/[^\d]/, ''), 10) - by, 0);
|
2017-12-15 07:57:08 -05:00
|
|
|
|
|
|
|
$el.text(addDelimiter(count));
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.hideCloseButton = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
const el = document.querySelector('.merge-request .js-issuable-actions');
|
|
|
|
// Dropdown for mobile screen
|
|
|
|
el.querySelector('li.js-close-item').classList.add('hidden');
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
MergeRequest.toggleDraftStatus = function (title, isReady) {
|
2020-10-01 08:10:14 -04:00
|
|
|
if (isReady) {
|
2021-10-12 17:09:47 -04:00
|
|
|
toast(__('Marked as ready. Merging is now allowed.'));
|
|
|
|
} else {
|
|
|
|
toast(__('Marked as draft. Can only be merged when marked as ready.'));
|
2020-10-01 08:10:14 -04:00
|
|
|
}
|
|
|
|
const titleEl = document.querySelector('.merge-request .detail-page-description .title');
|
|
|
|
|
|
|
|
if (titleEl) {
|
|
|
|
titleEl.textContent = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
const draftToggles = document.querySelectorAll('.js-draft-toggle-button');
|
|
|
|
|
|
|
|
if (draftToggles.length) {
|
2020-12-23 16:10:24 -05:00
|
|
|
draftToggles.forEach((el) => {
|
2020-10-01 08:10:14 -04:00
|
|
|
const draftToggle = el;
|
|
|
|
const url = setUrlParams(
|
|
|
|
{ 'merge_request[wip_event]': isReady ? 'wip' : 'unwip' },
|
|
|
|
draftToggle.href,
|
|
|
|
);
|
|
|
|
|
|
|
|
draftToggle.setAttribute('href', url);
|
|
|
|
draftToggle.textContent = isReady ? __('Mark as draft') : __('Mark as ready');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
export default MergeRequest;
|