2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-08-09 07:57:45 -04:00
|
|
|
import Pikaday from 'pikaday';
|
2019-04-23 15:18:07 -04:00
|
|
|
import GfmAutoComplete from 'ee_else_ce/gfm_auto_complete';
|
2021-12-07 10:15:03 -05:00
|
|
|
import Autosave from '~/autosave';
|
|
|
|
import AutoWidthDropdownSelect from '~/issuable/auto_width_dropdown_select';
|
|
|
|
import { loadCSSFile } from '~/lib/utils/css_utils';
|
|
|
|
import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility';
|
|
|
|
import { select2AxiosTransport } from '~/lib/utils/select2_utils';
|
|
|
|
import { queryToObject, objectToQuery } from '~/lib/utils/url_utility';
|
|
|
|
import UsersSelect from '~/users_select';
|
|
|
|
import ZenMode from '~/zen_mode';
|
2019-11-21 04:06:16 -05:00
|
|
|
|
2020-01-15 01:08:54 -05:00
|
|
|
const MR_SOURCE_BRANCH = 'merge_request[source_branch]';
|
|
|
|
const MR_TARGET_BRANCH = 'merge_request[target_branch]';
|
2022-03-22 08:07:28 -04:00
|
|
|
const DATA_ISSUES_NEW_PATH = 'data-new-issue-path';
|
2020-01-15 01:08:54 -05:00
|
|
|
|
2019-11-21 04:06:16 -05:00
|
|
|
function organizeQuery(obj, isFallbackKey = false) {
|
2020-01-15 01:08:54 -05:00
|
|
|
if (!obj[MR_SOURCE_BRANCH] && !obj[MR_TARGET_BRANCH]) {
|
|
|
|
return obj;
|
|
|
|
}
|
2019-11-21 04:06:16 -05:00
|
|
|
|
|
|
|
if (isFallbackKey) {
|
|
|
|
return {
|
2020-01-15 01:08:54 -05:00
|
|
|
[MR_SOURCE_BRANCH]: obj[MR_SOURCE_BRANCH],
|
2019-11-21 04:06:16 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-01-15 01:08:54 -05:00
|
|
|
[MR_SOURCE_BRANCH]: obj[MR_SOURCE_BRANCH],
|
|
|
|
[MR_TARGET_BRANCH]: obj[MR_TARGET_BRANCH],
|
2019-11-21 04:06:16 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function format(searchTerm, isFallbackKey = false) {
|
2021-06-11 11:09:58 -04:00
|
|
|
const queryObject = queryToObject(searchTerm, { legacySpacesDecode: true });
|
2019-11-21 04:06:16 -05:00
|
|
|
const organizeQueryObject = organizeQuery(queryObject, isFallbackKey);
|
|
|
|
const formattedQuery = objectToQuery(organizeQueryObject);
|
|
|
|
|
|
|
|
return formattedQuery;
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:12:12 -04:00
|
|
|
function getSearchTerm(newIssuePath) {
|
|
|
|
const { search, pathname } = document.location;
|
|
|
|
return newIssuePath === pathname ? '' : format(search);
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:06:16 -05:00
|
|
|
function getFallbackKey() {
|
|
|
|
const searchTerm = format(document.location.search, true);
|
|
|
|
return ['autosave', document.location.pathname, searchTerm].join('/');
|
|
|
|
}
|
2017-05-12 04:15:28 -04:00
|
|
|
|
2017-10-26 16:21:55 -04:00
|
|
|
export default class IssuableForm {
|
2022-09-09 08:12:03 -04:00
|
|
|
static addAutosave(map, id, $input, searchTerm, fallbackKey) {
|
|
|
|
if ($input.length) {
|
|
|
|
map.set(
|
|
|
|
id,
|
|
|
|
new Autosave($input, [document.location.pathname, searchTerm, id], `${fallbackKey}=${id}`),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 16:21:55 -04:00
|
|
|
constructor(form) {
|
2022-05-18 14:08:05 -04:00
|
|
|
if (form.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-26 16:21:55 -04:00
|
|
|
this.form = form;
|
|
|
|
this.toggleWip = this.toggleWip.bind(this);
|
|
|
|
this.renderWipExplanation = this.renderWipExplanation.bind(this);
|
|
|
|
this.resetAutosave = this.resetAutosave.bind(this);
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
2021-08-16 20:10:22 -04:00
|
|
|
// prettier-ignore
|
|
|
|
this.draftRegex = new RegExp(
|
2020-07-17 17:09:23 -04:00
|
|
|
'^\\s*(' + // Line start, then any amount of leading whitespace
|
2021-10-17 20:09:33 -04:00
|
|
|
'\\[draft\\]\\s*' + // [Draft] and any following whitespace
|
2021-08-16 20:10:22 -04:00
|
|
|
'|draft:\\s*' + // Draft: and any following whitespace
|
2020-12-23 07:10:26 -05:00
|
|
|
'|\\(draft\\)\\s*' + // (Draft) and any following whitespace
|
2021-08-16 20:10:22 -04:00
|
|
|
')+' + // At least one repeated match of the preceding parenthetical
|
|
|
|
'\\s*', // Any amount of trailing whitespace
|
2020-07-17 17:09:23 -04:00
|
|
|
'i', // Match any case(s)
|
|
|
|
);
|
2017-10-26 16:21:55 -04:00
|
|
|
|
2018-10-24 07:30:39 -04:00
|
|
|
this.gfmAutoComplete = new GfmAutoComplete(
|
|
|
|
gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources,
|
|
|
|
).setup();
|
2022-06-28 14:08:20 -04:00
|
|
|
this.usersSelect = new UsersSelect();
|
2020-09-14 08:09:34 -04:00
|
|
|
this.reviewersSelect = new UsersSelect(undefined, '.js-reviewer-search');
|
2018-10-24 07:30:39 -04:00
|
|
|
this.zenMode = new ZenMode();
|
2017-10-26 16:21:55 -04:00
|
|
|
|
2022-09-07 05:12:12 -04:00
|
|
|
this.searchTerm = getSearchTerm(form[0].getAttribute(DATA_ISSUES_NEW_PATH));
|
|
|
|
this.fallbackKey = getFallbackKey();
|
2017-10-26 16:21:55 -04:00
|
|
|
this.titleField = this.form.find('input[name*="[title]"]');
|
|
|
|
this.descriptionField = this.form.find('textarea[name*="[description]"]');
|
|
|
|
if (!(this.titleField.length && this.descriptionField.length)) {
|
|
|
|
return;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2022-09-09 08:12:03 -04:00
|
|
|
this.autosaves = this.initAutosave();
|
2018-05-29 20:51:30 -04:00
|
|
|
this.form.on('submit', this.handleSubmit);
|
2022-06-22 23:08:49 -04:00
|
|
|
this.form.on('click', '.btn-cancel, .js-reset-autosave', this.resetAutosave);
|
2022-02-14 10:13:32 -05:00
|
|
|
this.form.find('.js-unwrap-on-load').unwrap();
|
2017-10-26 16:21:55 -04:00
|
|
|
this.initWip();
|
|
|
|
|
|
|
|
const $issuableDueDate = $('#issuable-due-date');
|
|
|
|
|
|
|
|
if ($issuableDueDate.length) {
|
|
|
|
const calendar = new Pikaday({
|
|
|
|
field: $issuableDueDate.get(0),
|
|
|
|
theme: 'gitlab-theme animate-picker',
|
|
|
|
format: 'yyyy-mm-dd',
|
|
|
|
container: $issuableDueDate.parent().get(0),
|
2020-12-23 19:10:25 -05:00
|
|
|
parse: (dateString) => parsePikadayDate(dateString),
|
|
|
|
toString: (date) => pikadayToString(date),
|
2022-09-09 08:12:03 -04:00
|
|
|
onSelect: (dateText) => {
|
|
|
|
$issuableDueDate.val(calendar.toString(dateText));
|
|
|
|
if (this.autosaves.has('due_date')) this.autosaves.get('due_date').save();
|
|
|
|
},
|
2018-11-06 16:16:49 -05:00
|
|
|
firstDay: gon.first_day_of_week,
|
2017-10-26 16:21:55 -04:00
|
|
|
});
|
|
|
|
calendar.setDate(parsePikadayDate($issuableDueDate.val()));
|
|
|
|
}
|
2018-01-12 07:25:18 -05:00
|
|
|
|
|
|
|
this.$targetBranchSelect = $('.js-target-branch-select', this.form);
|
|
|
|
|
|
|
|
if (this.$targetBranchSelect.length) {
|
|
|
|
this.initTargetBranchDropdown();
|
|
|
|
}
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
initAutosave() {
|
2022-09-09 08:12:03 -04:00
|
|
|
const autosaveMap = new Map();
|
|
|
|
IssuableForm.addAutosave(
|
|
|
|
autosaveMap,
|
|
|
|
'title',
|
|
|
|
this.form.find('input[name*="[title]"]'),
|
|
|
|
this.searchTerm,
|
|
|
|
this.fallbackKey,
|
2019-11-21 04:06:16 -05:00
|
|
|
);
|
2022-09-09 08:12:03 -04:00
|
|
|
IssuableForm.addAutosave(
|
|
|
|
autosaveMap,
|
|
|
|
'description',
|
|
|
|
this.form.find('textarea[name*="[description]"]'),
|
|
|
|
this.searchTerm,
|
|
|
|
this.fallbackKey,
|
|
|
|
);
|
|
|
|
IssuableForm.addAutosave(
|
|
|
|
autosaveMap,
|
|
|
|
'due_date',
|
|
|
|
this.form.find('input[name*="[due_date]"]'),
|
|
|
|
this.searchTerm,
|
|
|
|
this.fallbackKey,
|
2019-11-21 04:06:16 -05:00
|
|
|
);
|
2022-09-09 08:12:03 -04:00
|
|
|
|
|
|
|
return autosaveMap;
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit() {
|
|
|
|
return this.resetAutosave();
|
|
|
|
}
|
|
|
|
|
|
|
|
resetAutosave() {
|
2022-09-09 08:12:03 -04:00
|
|
|
this.autosaves.forEach((autosaveItem) => {
|
|
|
|
autosaveItem?.reset();
|
|
|
|
});
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
initWip() {
|
|
|
|
this.$wipExplanation = this.form.find('.js-wip-explanation');
|
|
|
|
this.$noWipExplanation = this.form.find('.js-no-wip-explanation');
|
|
|
|
if (!(this.$wipExplanation.length && this.$noWipExplanation.length)) {
|
2018-10-24 07:30:39 -04:00
|
|
|
return undefined;
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
this.form.on('click', '.js-toggle-wip', this.toggleWip);
|
|
|
|
this.titleField.on('keyup blur', this.renderWipExplanation);
|
|
|
|
return this.renderWipExplanation();
|
|
|
|
}
|
|
|
|
|
|
|
|
workInProgress() {
|
2021-08-16 20:10:22 -04:00
|
|
|
return this.draftRegex.test(this.titleField.val());
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
renderWipExplanation() {
|
|
|
|
if (this.workInProgress()) {
|
2020-07-17 17:09:23 -04:00
|
|
|
// These strings are not "translatable" (the code is hard-coded to look for them)
|
2021-05-17 05:10:26 -04:00
|
|
|
this.$wipExplanation.find('code')[0].textContent =
|
|
|
|
'Draft'; /* eslint-disable-line @gitlab/require-i18n-strings */
|
2017-10-26 16:21:55 -04:00
|
|
|
this.$wipExplanation.show();
|
|
|
|
return this.$noWipExplanation.hide();
|
|
|
|
}
|
2018-10-24 07:30:39 -04:00
|
|
|
this.$wipExplanation.hide();
|
|
|
|
return this.$noWipExplanation.show();
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleWip(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
if (this.workInProgress()) {
|
|
|
|
this.removeWip();
|
|
|
|
} else {
|
|
|
|
this.addWip();
|
|
|
|
}
|
|
|
|
return this.renderWipExplanation();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeWip() {
|
2021-08-16 20:10:22 -04:00
|
|
|
return this.titleField.val(this.titleField.val().replace(this.draftRegex, ''));
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addWip() {
|
2020-07-17 17:09:23 -04:00
|
|
|
this.titleField.val(`Draft: ${this.titleField.val()}`);
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
2018-01-12 07:25:18 -05:00
|
|
|
|
|
|
|
initTargetBranchDropdown() {
|
2019-01-29 04:35:53 -05:00
|
|
|
import(/* webpackChunkName: 'select2' */ 'select2/select2')
|
|
|
|
.then(() => {
|
2020-11-18 04:09:02 -05:00
|
|
|
// eslint-disable-next-line promise/no-nesting
|
|
|
|
loadCSSFile(gon.select2_css_path)
|
|
|
|
.then(() => {
|
|
|
|
this.$targetBranchSelect.select2({
|
|
|
|
...AutoWidthDropdownSelect.selectOptions('js-target-branch-select'),
|
|
|
|
ajax: {
|
|
|
|
url: this.$targetBranchSelect.data('endpoint'),
|
|
|
|
dataType: 'JSON',
|
|
|
|
quietMillis: 250,
|
|
|
|
data(search) {
|
|
|
|
return {
|
|
|
|
search,
|
|
|
|
};
|
|
|
|
},
|
2021-03-11 22:08:56 -05:00
|
|
|
results({ results }) {
|
2020-11-18 04:09:02 -05:00
|
|
|
return {
|
|
|
|
// `data` keys are translated so we can't just access them with a string based key
|
2021-03-11 22:08:56 -05:00
|
|
|
results: results[Object.keys(results)[0]].map((name) => ({
|
2020-11-18 04:09:02 -05:00
|
|
|
id: name,
|
|
|
|
text: name,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
},
|
2021-03-11 22:08:56 -05:00
|
|
|
transport: select2AxiosTransport,
|
2020-11-18 04:09:02 -05:00
|
|
|
},
|
|
|
|
initSelection(el, callback) {
|
|
|
|
const val = el.val();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
id: val,
|
|
|
|
text: val,
|
|
|
|
});
|
|
|
|
},
|
2019-01-29 04:35:53 -05:00
|
|
|
});
|
2020-11-18 04:09:02 -05:00
|
|
|
})
|
|
|
|
.catch(() => {});
|
2019-01-29 04:35:53 -05:00
|
|
|
})
|
|
|
|
.catch(() => {});
|
2018-01-12 07:25:18 -05:00
|
|
|
}
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|