2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable func-names, consistent-return, no-return-assign, @gitlab/require-i18n-strings */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-11-20 15:28:29 -05:00
|
|
|
import RefSelectDropdown from './ref_select_dropdown';
|
2017-05-16 06:32:53 -04:00
|
|
|
|
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() {
|
2019-11-15 04:06:30 -05:00
|
|
|
const startsWith = {
|
2017-11-20 15:28:29 -05:00
|
|
|
pattern: /^(\/|\.)/g,
|
|
|
|
prefix: "can't start with",
|
2018-10-10 03:13:34 -04:00
|
|
|
conjunction: 'or',
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2019-11-15 04:06:30 -05:00
|
|
|
const endsWith = {
|
2017-11-20 15:28:29 -05:00
|
|
|
pattern: /(\/|\.|\.lock)$/g,
|
|
|
|
prefix: "can't end in",
|
2018-10-10 03:13:34 -04:00
|
|
|
conjunction: 'or',
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2019-11-15 04:06:30 -05:00
|
|
|
const invalid = {
|
2017-11-20 15:28:29 -05:00
|
|
|
pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g,
|
|
|
|
prefix: "can't contain",
|
2018-10-10 03:13:34 -04:00
|
|
|
conjunction: ', ',
|
2017-11-20 15:28:29 -05:00
|
|
|
};
|
2019-11-15 04:06:30 -05:00
|
|
|
const single = {
|
2017-11-20 15:28:29 -05:00
|
|
|
pattern: /^@+$/g,
|
|
|
|
prefix: "can't be",
|
2018-10-10 03:13:34 -04:00
|
|
|
conjunction: 'or',
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2018-10-10 03:13:34 -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() {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { indexOf } = [];
|
2017-05-08 15:25:12 -04:00
|
|
|
|
2017-11-20 15:28:29 -05:00
|
|
|
this.branchNameError.empty();
|
2019-11-15 04:06:30 -05:00
|
|
|
const unique = function(values, value) {
|
2017-11-20 15:28:29 -05:00
|
|
|
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
|
|
|
};
|
2019-11-15 04:06:30 -05:00
|
|
|
const formatter = function(values, restriction) {
|
|
|
|
const formatted = values.map(value => {
|
2017-11-20 15:28:29 -05:00
|
|
|
switch (false) {
|
|
|
|
case !/\s/.test(value):
|
|
|
|
return 'spaces';
|
|
|
|
case !/\/{2,}/g.test(value):
|
|
|
|
return 'consecutive slashes';
|
|
|
|
default:
|
2019-09-24 08:06:20 -04:00
|
|
|
return `'${value}'`;
|
2017-11-20 15:28:29 -05:00
|
|
|
}
|
|
|
|
});
|
2019-09-24 08:06:20 -04:00
|
|
|
return `${restriction.prefix} ${formatted.join(restriction.conjunction)}`;
|
2017-11-20 15:28:29 -05:00
|
|
|
};
|
2019-11-15 10:06:12 -05:00
|
|
|
const validator = (errors, restriction) => {
|
|
|
|
const matched = this.name.val().match(restriction.pattern);
|
|
|
|
if (matched) {
|
|
|
|
return errors.concat(formatter(matched.reduce(unique, []), restriction));
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return errors;
|
2019-11-15 10:06:12 -05:00
|
|
|
};
|
2019-11-15 04:06:30 -05:00
|
|
|
const errors = this.restrictions.reduce(validator, []);
|
2017-11-20 15:28:29 -05:00
|
|
|
if (errors.length > 0) {
|
2019-11-15 04:06:30 -05:00
|
|
|
const errorMessage = $('<span/>').text(errors.join(', '));
|
2017-11-20 15:28:29 -05:00
|
|
|
return this.branchNameError.append(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|