debian-mirror-gitlab/app/assets/javascripts/new_branch_form.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

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