2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-08-09 07:57:45 -04:00
|
|
|
import Pikaday from 'pikaday';
|
2017-10-12 12:31:29 -04:00
|
|
|
import Autosave from './autosave';
|
2017-05-12 04:15:28 -04:00
|
|
|
import UsersSelect from './users_select';
|
2017-05-16 14:42:06 -04:00
|
|
|
import GfmAutoComplete from './gfm_auto_complete';
|
2017-06-30 17:37:31 -04:00
|
|
|
import ZenMode from './zen_mode';
|
2018-01-12 07:59:08 -05:00
|
|
|
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
|
2018-10-24 06:51:33 -04:00
|
|
|
import { parsePikadayDate, pikadayToString } from './lib/utils/datetime_utility';
|
2017-05-12 04:15:28 -04:00
|
|
|
|
2017-10-26 16:21:55 -04:00
|
|
|
export default class IssuableForm {
|
|
|
|
constructor(form) {
|
|
|
|
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);
|
|
|
|
this.wipRegex = /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i;
|
|
|
|
|
2018-10-24 07:30:39 -04:00
|
|
|
this.gfmAutoComplete = new GfmAutoComplete(
|
|
|
|
gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources,
|
|
|
|
).setup();
|
|
|
|
this.usersSelect = new UsersSelect();
|
|
|
|
this.zenMode = new ZenMode();
|
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
|
|
|
}
|
|
|
|
|
2017-10-26 16:21:55 -04:00
|
|
|
this.initAutosave();
|
2018-05-29 20:51:30 -04:00
|
|
|
this.form.on('submit', this.handleSubmit);
|
2017-10-26 16:21:55 -04:00
|
|
|
this.form.on('click', '.btn-cancel', this.resetAutosave);
|
|
|
|
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),
|
|
|
|
parse: dateString => parsePikadayDate(dateString),
|
|
|
|
toString: date => pikadayToString(date),
|
|
|
|
onSelect: dateText => $issuableDueDate.val(calendar.toString(dateText)),
|
|
|
|
});
|
|
|
|
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() {
|
2018-10-24 07:30:39 -04:00
|
|
|
this.autosave = new Autosave(this.titleField, [
|
|
|
|
document.location.pathname,
|
|
|
|
document.location.search,
|
|
|
|
'title',
|
|
|
|
]);
|
|
|
|
return new Autosave(this.descriptionField, [
|
|
|
|
document.location.pathname,
|
|
|
|
document.location.search,
|
|
|
|
'description',
|
|
|
|
]);
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit() {
|
|
|
|
return this.resetAutosave();
|
|
|
|
}
|
|
|
|
|
|
|
|
resetAutosave() {
|
|
|
|
this.titleField.data('autosave').reset();
|
|
|
|
return this.descriptionField.data('autosave').reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return this.wipRegex.test(this.titleField.val());
|
|
|
|
}
|
|
|
|
|
|
|
|
renderWipExplanation() {
|
|
|
|
if (this.workInProgress()) {
|
|
|
|
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() {
|
|
|
|
return this.titleField.val(this.titleField.val().replace(this.wipRegex, ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
addWip() {
|
2018-10-24 07:30:39 -04:00
|
|
|
this.titleField.val(`WIP: ${this.titleField.val()}`);
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|
2018-01-12 07:25:18 -05:00
|
|
|
|
|
|
|
initTargetBranchDropdown() {
|
|
|
|
this.$targetBranchSelect.select2({
|
2018-01-12 07:59:08 -05:00
|
|
|
...AutoWidthDropdownSelect.selectOptions('js-target-branch-select'),
|
2018-01-12 07:25:18 -05:00
|
|
|
ajax: {
|
|
|
|
url: this.$targetBranchSelect.data('endpoint'),
|
|
|
|
dataType: 'JSON',
|
|
|
|
quietMillis: 250,
|
|
|
|
data(search) {
|
|
|
|
return {
|
|
|
|
search,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
results(data) {
|
|
|
|
return {
|
2018-01-12 07:59:08 -05:00
|
|
|
// `data` keys are translated so we can't just access them with a string based key
|
2018-01-12 07:25:18 -05:00
|
|
|
results: data[Object.keys(data)[0]].map(name => ({
|
|
|
|
id: name,
|
|
|
|
text: name,
|
|
|
|
})),
|
|
|
|
};
|
2018-01-16 04:18:23 -05:00
|
|
|
},
|
2018-01-12 07:25:18 -05:00
|
|
|
},
|
|
|
|
initSelection(el, callback) {
|
|
|
|
const val = el.val();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
id: val,
|
|
|
|
text: val,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-10-26 16:21:55 -04:00
|
|
|
}
|