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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, no-return-assign, @gitlab/require-i18n-strings */
import $ from 'jquery';
2017-11-20 20:28:29 +00:00
import RefSelectDropdown from './ref_select_dropdown';
2017-11-20 20:28:29 +00:00
export default class NewBranchForm {
constructor(form, availableRefs) {
this.validate = this.validate.bind(this);
this.branchNameError = form.querySelector('.js-branch-name-error');
this.name = form.querySelector('.js-branch-name');
this.ref = form.querySelector('#ref');
2017-11-20 20:28:29 +00:00
new RefSelectDropdown($('.js-branch-select'), availableRefs); // eslint-disable-line no-new
this.setupRestrictions();
this.addBinding();
this.init();
}
addBinding() {
this.name.addEventListener('blur', this.validate);
2017-11-20 20:28:29 +00:00
}
init() {
if (this.name != null && this.name.value.length > 0) {
const event = new CustomEvent('blur');
this.name.dispatchEvent(event);
2016-07-24 20:45:11 +00:00
}
2017-11-20 20:28:29 +00:00
}
2016-07-24 20:45:11 +00:00
2017-11-20 20:28:29 +00:00
setupRestrictions() {
const startsWith = {
2017-11-20 20:28:29 +00:00
pattern: /^(\/|\.)/g,
prefix: "can't start with",
conjunction: 'or',
2016-07-24 20:45:11 +00:00
};
const endsWith = {
2017-11-20 20:28:29 +00:00
pattern: /(\/|\.|\.lock)$/g,
prefix: "can't end in",
conjunction: 'or',
2016-07-24 20:45:11 +00:00
};
const invalid = {
2017-11-20 20:28:29 +00:00
pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g,
prefix: "can't contain",
conjunction: ', ',
2017-11-20 20:28:29 +00:00
};
const single = {
2017-11-20 20:28:29 +00:00
pattern: /^@+$/g,
prefix: "can't be",
conjunction: 'or',
2016-07-24 20:45:11 +00:00
};
return (this.restrictions = [startsWith, invalid, endsWith, single]);
2017-11-20 20:28:29 +00:00
}
2016-07-24 20:45:11 +00:00
2017-11-20 20:28:29 +00:00
validate() {
const { indexOf } = [];
2017-05-08 19:25:12 +00:00
this.branchNameError.innerHTML = '';
const unique = function (values, value) {
2017-11-20 20:28:29 +00:00
if (indexOf.call(values, value) === -1) {
values.push(value);
2016-07-24 20:45:11 +00:00
}
2017-11-20 20:28:29 +00:00
return values;
2016-07-24 20:45:11 +00:00
};
const formatter = function (values, restriction) {
const formatted = values.map((value) => {
2017-11-20 20:28:29 +00:00
switch (false) {
case !/\s/.test(value):
return 'spaces';
case !/\/{2,}/g.test(value):
return 'consecutive slashes';
default:
return `'${value}'`;
2017-11-20 20:28:29 +00:00
}
});
return `${restriction.prefix} ${formatted.join(restriction.conjunction)}`;
2017-11-20 20:28:29 +00:00
};
const validator = (errors, restriction) => {
const matched = this.name.value.match(restriction.pattern);
if (matched) {
return errors.concat(formatter(matched.reduce(unique, []), restriction));
}
return errors;
};
const errors = this.restrictions.reduce(validator, []);
2017-11-20 20:28:29 +00:00
if (errors.length > 0) {
this.branchNameError.textContent = errors.join(', ');
2017-11-20 20:28:29 +00:00
}
}
}