debian-mirror-gitlab/app/assets/javascripts/merge_request.js

194 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
/* eslint-disable func-names, no-underscore-dangle, consistent-return */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2018-03-17 18:26:18 +05:30
import { __ } from '~/locale';
2021-01-03 14:25:43 +05:30
import eventHub from '~/vue_merge_request_widget/event_hub';
2021-03-11 19:13:27 +05:30
import axios from './lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import { addDelimiter } from './lib/utils/text_utility';
2021-01-03 14:25:43 +05:30
import { getParameterValues, setUrlParams } from './lib/utils/url_utility';
2021-03-11 19:13:27 +05:30
import MergeRequestTabs from './merge_request_tabs';
import TaskList from './task_list';
2018-03-17 18:26:18 +05:30
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');
2019-12-26 22:10:19 +05:30
this.$('.show-all-commits').on('click', () => this.showAllCommits());
2018-03-17 18:26:18 +05:30
this.initTabs();
this.initMRBtnListeners();
this.initCommitMessageListeners();
2020-03-13 15:44:24 +05:30
if ($('.description.js-task-list-container').length) {
2018-03-17 18:26:18 +05:30
this.taskList = new TaskList({
dataType: 'merge_request',
fieldName: 'description',
selector: '.detail-page-description',
2019-03-02 22:35:43 +05:30
lockVersion: this.$el.data('lockVersion'),
2021-03-08 18:12:59 +05:30
onSuccess: (result) => {
2018-03-17 18:26:18 +05:30
document.querySelector('#task_status').innerText = result.task_status;
document.querySelector('#task_status_short').innerText = result.task_status_short;
2018-12-13 13:39:08 +05:30
},
2019-03-02 22:35:43 +05:30
onError: () => {
2021-09-04 01:27:46 +05:30
createFlash({
message: __(
2019-03-02 22:35:43 +05:30
'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
),
2021-09-04 01:27:46 +05:30
});
2019-03-02 22:35:43 +05:30
},
2018-03-17 18:26:18 +05:30
});
}
}
// Local jQuery finder
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.$ = function (selector) {
2018-03-17 18:26:18 +05:30
return this.$el.find(selector);
};
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.initTabs = function () {
2018-03-17 18:26:18 +05:30
if (window.mrTabs) {
window.mrTabs.unbindEvents();
}
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
window.mrTabs = new MergeRequestTabs(this.opts);
};
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.showAllCommits = function () {
2018-03-17 18:26:18 +05:30
this.$('.first-commits').remove();
return this.$('.all-commits').removeClass('hide');
};
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.initMRBtnListeners = function () {
2019-12-26 22:10:19 +05:30
const _this = this;
2021-01-03 14:25:43 +05:30
const draftToggles = document.querySelectorAll('.js-draft-toggle-button');
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
if (draftToggles.length) {
2021-03-08 18:12:59 +05:30
draftToggles.forEach((draftToggle) => {
draftToggle.addEventListener('click', (e) => {
2021-01-03 14:25:43 +05:30
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-09-04 01:27:46 +05:30
createFlash({
message: __('Something went wrong. Please try again.'),
});
2021-01-03 14:25:43 +05:30
});
});
});
}
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
return $('.btn-close, .btn-reopen').on('click', function (e) {
2019-12-26 22:10:19 +05:30
const $this = $(this);
const shouldSubmit = $this.hasClass('btn-comment');
2018-03-17 18:26:18 +05:30
if (shouldSubmit && $this.data('submitted')) {
return;
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (shouldSubmit) {
if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
2017-08-17 22:00:37 +05:30
e.preventDefault();
2018-03-17 18:26:18 +05:30
e.stopImmediatePropagation();
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
_this.submitNoteForm($this.closest('form'), $this);
}
}
});
};
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.submitNoteForm = function (form, $button) {
2019-12-26 22:10:19 +05:30
const noteText = form.find('textarea.js-note-text').val();
2018-03-17 18:26:18 +05:30
if (noteText.trim().length > 0) {
form.submit();
$button.data('submitted', true);
return $button.trigger('click');
}
};
2021-03-08 18:12:59 +05:30
MergeRequest.prototype.initCommitMessageListeners = function () {
$(document).on('click', 'a.js-with-description-link', (e) => {
2019-12-26 22:10:19 +05:30
const textarea = $('textarea.js-commit-message');
2018-03-17 18:26:18 +05:30
e.preventDefault();
textarea.val(textarea.data('messageWithDescription'));
$('.js-with-description-hint').hide();
$('.js-without-description-hint').show();
});
2021-03-08 18:12:59 +05:30
$(document).on('click', 'a.js-without-description-link', (e) => {
2019-12-26 22:10:19 +05:30
const textarea = $('textarea.js-commit-message');
2018-03-17 18:26:18 +05:30
e.preventDefault();
textarea.val(textarea.data('messageWithoutDescription'));
$('.js-with-description-hint').show();
$('.js-without-description-hint').hide();
});
};
2021-03-08 18:12:59 +05:30
MergeRequest.decreaseCounter = function (by = 1) {
2018-03-17 18:26:18 +05:30
const $el = $('.js-merge-counter');
2018-12-13 13:39:08 +05:30
const count = Math.max(parseInt($el.text().replace(/[^\d]/, ''), 10) - by, 0);
2018-03-17 18:26:18 +05:30
$el.text(addDelimiter(count));
};
2021-03-08 18:12:59 +05:30
MergeRequest.hideCloseButton = function () {
2018-03-17 18:26:18 +05:30
const el = document.querySelector('.merge-request .js-issuable-actions');
// Dropdown for mobile screen
el.querySelector('li.js-close-item').classList.add('hidden');
};
2021-03-08 18:12:59 +05:30
MergeRequest.toggleDraftStatus = function (title, isReady) {
2021-01-03 14:25:43 +05:30
if (isReady) {
2021-09-04 01:27:46 +05:30
createFlash({
message: __('The merge request can now be merged.'),
type: 'notice',
});
2021-01-03 14:25:43 +05:30
}
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) {
2021-03-08 18:12:59 +05:30
draftToggles.forEach((el) => {
2021-01-03 14:25:43 +05:30
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');
});
}
};
2018-03-17 18:26:18 +05:30
export default MergeRequest;