2018-12-13 13:39:08 +05:30
|
|
|
/* eslint-disable func-names, no-var, one-var, consistent-return, no-return-assign, prefer-arrow-callback, prefer-template, no-shadow, no-else-return */
|
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() {
|
|
|
|
var endsWith, invalid, single, startsWith;
|
|
|
|
startsWith = {
|
|
|
|
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
|
|
|
};
|
2018-03-17 18:26:18 +05:30
|
|
|
endsWith = {
|
|
|
|
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
|
|
|
};
|
2018-03-17 18:26:18 +05:30
|
|
|
invalid = {
|
|
|
|
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
|
|
|
};
|
|
|
|
single = {
|
|
|
|
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() {
|
|
|
|
var errorMessage, errors, formatter, unique, validator;
|
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();
|
|
|
|
unique = function(values, value) {
|
|
|
|
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
|
|
|
};
|
2018-03-17 18:26:18 +05:30
|
|
|
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 + "'";
|
|
|
|
}
|
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
return restriction.prefix + ' ' + formatted.join(restriction.conjunction);
|
2018-03-17 18:26:18 +05:30
|
|
|
};
|
|
|
|
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) {
|
2018-12-13 13:39:08 +05:30
|
|
|
errorMessage = $('<span/>').text(errors.join(', '));
|
2018-03-17 18:26:18 +05:30
|
|
|
return this.branchNameError.append(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|