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

167 lines
5 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';
2021-11-18 22:05:49 +05:30
import toast from '~/vue_shared/plugins/global_toast';
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');
this.initTabs();
this.initMRBtnListeners();
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.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.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-11-18 22:05:49 +05:30
toast(__('Marked as ready. Merging is now allowed.'));
} else {
toast(__('Marked as draft. Can only be merged when marked as ready.'));
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;