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

96 lines
2.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, no-var, one-var, consistent-return, no-return-assign, prefer-arrow-callback, prefer-template, no-shadow, no-else-return, @gitlab/i18n/no-non-i18n-strings */
import $ from 'jquery';
2017-11-20 15:28:29 -05:00
import RefSelectDropdown from './ref_select_dropdown';
2017-11-20 15:28:29 -05:00
export default class NewBranchForm {
constructor(form, availableRefs) {
this.validate = this.validate.bind(this);
this.branchNameError = form.find('.js-branch-name-error');
this.name = form.find('.js-branch-name');
this.ref = form.find('#ref');
new RefSelectDropdown($('.js-branch-select'), availableRefs); // eslint-disable-line no-new
this.setupRestrictions();
this.addBinding();
this.init();
}
addBinding() {
return this.name.on('blur', this.validate);
}
init() {
if (this.name.length && this.name.val().length > 0) {
return this.name.trigger('blur');
2016-07-24 16:45:11 -04:00
}
2017-11-20 15:28:29 -05:00
}
2016-07-24 16:45:11 -04:00
2017-11-20 15:28:29 -05:00
setupRestrictions() {
var endsWith, invalid, single, startsWith;
startsWith = {
pattern: /^(\/|\.)/g,
prefix: "can't start with",
conjunction: 'or',
2016-07-24 16:45:11 -04:00
};
2017-11-20 15:28:29 -05:00
endsWith = {
pattern: /(\/|\.|\.lock)$/g,
prefix: "can't end in",
conjunction: 'or',
2016-07-24 16:45:11 -04:00
};
2017-11-20 15:28:29 -05:00
invalid = {
pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g,
prefix: "can't contain",
conjunction: ', ',
2017-11-20 15:28:29 -05:00
};
single = {
pattern: /^@+$/g,
prefix: "can't be",
conjunction: 'or',
2016-07-24 16:45:11 -04:00
};
return (this.restrictions = [startsWith, invalid, endsWith, single]);
2017-11-20 15:28:29 -05:00
}
2016-07-24 16:45:11 -04:00
2017-11-20 15:28:29 -05:00
validate() {
var errorMessage, errors, formatter, unique, validator;
const { indexOf } = [];
2017-05-08 15:25:12 -04:00
2017-11-20 15:28:29 -05:00
this.branchNameError.empty();
unique = function(values, value) {
if (indexOf.call(values, value) === -1) {
values.push(value);
2016-07-24 16:45:11 -04:00
}
2017-11-20 15:28:29 -05:00
return values;
2016-07-24 16:45:11 -04:00
};
2017-11-20 15:28:29 -05:00
formatter = function(values, restriction) {
var formatted;
formatted = values.map(function(value) {
switch (false) {
case !/\s/.test(value):
return 'spaces';
case !/\/{2,}/g.test(value):
return 'consecutive slashes';
default:
return "'" + value + "'";
}
});
return restriction.prefix + ' ' + formatted.join(restriction.conjunction);
2017-11-20 15:28:29 -05:00
};
validator = (function(_this) {
return function(errors, restriction) {
var matched;
matched = _this.name.val().match(restriction.pattern);
if (matched) {
return errors.concat(formatter(matched.reduce(unique, []), restriction));
} else {
return errors;
}
};
})(this);
errors = this.restrictions.reduce(validator, []);
if (errors.length > 0) {
errorMessage = $('<span/>').text(errors.join(', '));
2017-11-20 15:28:29 -05:00
return this.branchNameError.append(errorMessage);
}
}
}