debian-mirror-gitlab/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

2022-08-13 15:12:31 +05:30
/* eslint-disable func-names, 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);
2022-08-13 15:12:31 +05:30
this.branchNameError = form.querySelector('.js-branch-name-error');
this.name = form.querySelector('.js-branch-name');
this.ref = form.querySelector('#ref');
2018-03-17 18:26:18 +05:30
new RefSelectDropdown($('.js-branch-select'), availableRefs); // eslint-disable-line no-new
this.setupRestrictions();
this.addBinding();
this.init();
}
addBinding() {
2022-08-13 15:12:31 +05:30
this.name.addEventListener('blur', this.validate);
2018-03-17 18:26:18 +05:30
}
init() {
2022-08-13 15:12:31 +05:30
if (this.name != null && this.name.value.length > 0) {
const event = new CustomEvent('blur');
this.name.dispatchEvent(event);
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
2022-08-13 15:12:31 +05:30
this.branchNameError.innerHTML = '';
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) => {
2022-08-13 15:12:31 +05:30
const matched = this.name.value.match(restriction.pattern);
2019-12-26 22:10:19 +05:30
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) {
2022-08-13 15:12:31 +05:30
this.branchNameError.textContent = errors.join(', ');
2018-03-17 18:26:18 +05:30
}
}
}