gitlab-org--gitlab-foss/app/assets/javascripts/issuable_form.js

109 lines
4.0 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-use-before-define, no-useless-escape, no-new, quotes, object-shorthand, no-unused-vars, comma-dangle, no-alert, consistent-return, no-else-return, prefer-template, one-var, one-var-declaration-per-line, curly, max-len */
/* global GitLab */
/* global Autosave */
/* global dateFormat */
import Pikaday from 'pikaday';
2017-05-12 08:15:28 +00:00
import UsersSelect from './users_select';
import GfmAutoComplete from './gfm_auto_complete';
2017-06-30 21:37:31 +00:00
import ZenMode from './zen_mode';
2017-05-12 08:15:28 +00:00
2016-07-24 20:45:11 +00:00
(function() {
this.IssuableForm = (function() {
IssuableForm.prototype.wipRegex = /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i;
function IssuableForm(form) {
2017-01-06 14:43:21 +00:00
var $issuableDueDate, calendar;
2016-07-24 20:45:11 +00: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);
new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources).setup();
2016-07-24 20:45:11 +00:00
new UsersSelect();
new ZenMode();
this.titleField = this.form.find("input[name*='[title]']");
this.descriptionField = this.form.find("textarea[name*='[description]']");
if (!(this.titleField.length && this.descriptionField.length)) {
return;
}
this.initAutosave();
this.form.on("submit", this.handleSubmit);
this.form.on("click", ".btn-cancel", this.resetAutosave);
this.initWip();
$issuableDueDate = $('#issuable-due-date');
if ($issuableDueDate.length) {
2017-01-06 14:43:21 +00:00
calendar = new Pikaday({
field: $issuableDueDate.get(0),
2017-04-21 15:05:38 +00:00
theme: 'gitlab-theme animate-picker',
2017-02-14 21:45:36 +00:00
format: 'yyyy-mm-dd',
2017-04-21 15:05:38 +00:00
container: $issuableDueDate.parent().get(0),
onSelect: function(dateText) {
$issuableDueDate.val(dateFormat(new Date(dateText), 'yyyy-mm-dd'));
2016-07-24 20:45:11 +00:00
}
2017-01-04 13:16:14 +00:00
});
2017-02-14 21:45:36 +00:00
calendar.setDate(new Date($issuableDueDate.val()));
2016-07-24 20:45:11 +00:00
}
}
IssuableForm.prototype.initAutosave = function() {
new Autosave(this.titleField, [document.location.pathname, document.location.search, "title"]);
return new Autosave(this.descriptionField, [document.location.pathname, document.location.search, "description"]);
};
IssuableForm.prototype.handleSubmit = function() {
return this.resetAutosave();
};
IssuableForm.prototype.resetAutosave = function() {
this.titleField.data("autosave").reset();
return this.descriptionField.data("autosave").reset();
};
IssuableForm.prototype.initWip = function() {
this.$wipExplanation = this.form.find(".js-wip-explanation");
this.$noWipExplanation = this.form.find(".js-no-wip-explanation");
if (!(this.$wipExplanation.length && this.$noWipExplanation.length)) {
return;
}
this.form.on("click", ".js-toggle-wip", this.toggleWip);
this.titleField.on("keyup blur", this.renderWipExplanation);
return this.renderWipExplanation();
};
IssuableForm.prototype.workInProgress = function() {
return this.wipRegex.test(this.titleField.val());
};
IssuableForm.prototype.renderWipExplanation = function() {
if (this.workInProgress()) {
this.$wipExplanation.show();
return this.$noWipExplanation.hide();
} else {
this.$wipExplanation.hide();
return this.$noWipExplanation.show();
}
};
IssuableForm.prototype.toggleWip = function(event) {
event.preventDefault();
if (this.workInProgress()) {
this.removeWip();
} else {
this.addWip();
}
return this.renderWipExplanation();
};
IssuableForm.prototype.removeWip = function() {
return this.titleField.val(this.titleField.val().replace(this.wipRegex, ""));
};
IssuableForm.prototype.addWip = function() {
return this.titleField.val("WIP: " + (this.titleField.val()));
};
return IssuableForm;
})();
}).call(window);