2017-01-03 11:31:43 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, no-var, one-var, prefer-rest-params, max-len, vars-on-top, wrap-iife, consistent-return, comma-dangle, one-var-declaration-per-line, quotes, no-return-assign, prefer-arrow-callback, prefer-template, no-shadow, no-else-return, max-len, object-shorthand */
|
2017-05-16 06:32:53 -04:00
|
|
|
import RefSelectDropdown from '~/ref_select_dropdown';
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
(function() {
|
|
|
|
this.NewBranchForm = (function() {
|
|
|
|
function NewBranchForm(form, availableRefs) {
|
2017-05-08 15:08:16 -04:00
|
|
|
this.validate = this.validate.bind(this);
|
2016-07-24 16:45:11 -04:00
|
|
|
this.branchNameError = form.find('.js-branch-name-error');
|
|
|
|
this.name = form.find('.js-branch-name');
|
|
|
|
this.ref = form.find('#ref');
|
2017-05-16 06:32:53 -04:00
|
|
|
new RefSelectDropdown($('.js-branch-select'), availableRefs); // eslint-disable-line no-new
|
2016-07-24 16:45:11 -04:00
|
|
|
this.setupRestrictions();
|
|
|
|
this.addBinding();
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
NewBranchForm.prototype.addBinding = function() {
|
|
|
|
return this.name.on('blur', this.validate);
|
|
|
|
};
|
|
|
|
|
|
|
|
NewBranchForm.prototype.init = function() {
|
2017-01-03 11:31:43 -05:00
|
|
|
if (this.name.length && this.name.val().length > 0) {
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.name.trigger('blur');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NewBranchForm.prototype.setupRestrictions = function() {
|
|
|
|
var endsWith, invalid, single, startsWith;
|
|
|
|
startsWith = {
|
|
|
|
pattern: /^(\/|\.)/g,
|
|
|
|
prefix: "can't start with",
|
|
|
|
conjunction: "or"
|
|
|
|
};
|
|
|
|
endsWith = {
|
|
|
|
pattern: /(\/|\.|\.lock)$/g,
|
|
|
|
prefix: "can't end in",
|
|
|
|
conjunction: "or"
|
|
|
|
};
|
|
|
|
invalid = {
|
|
|
|
pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g,
|
|
|
|
prefix: "can't contain",
|
|
|
|
conjunction: ", "
|
|
|
|
};
|
|
|
|
single = {
|
|
|
|
pattern: /^@+$/g,
|
|
|
|
prefix: "can't be",
|
|
|
|
conjunction: "or"
|
|
|
|
};
|
|
|
|
return this.restrictions = [startsWith, invalid, endsWith, single];
|
|
|
|
};
|
|
|
|
|
|
|
|
NewBranchForm.prototype.validate = function() {
|
|
|
|
var errorMessage, errors, formatter, unique, validator;
|
2017-05-08 15:25:12 -04:00
|
|
|
const indexOf = [].indexOf;
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
this.branchNameError.empty();
|
|
|
|
unique = function(values, value) {
|
2017-02-25 07:43:26 -05:00
|
|
|
if (indexOf.call(values, value) === -1) {
|
2016-07-24 16:45:11 -04:00
|
|
|
values.push(value);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
};
|
|
|
|
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 + "'";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return restriction.prefix + " " + (formatted.join(restriction.conjunction));
|
|
|
|
};
|
|
|
|
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) {
|
|
|
|
errorMessage = $("<span/>").text(errors.join(', '));
|
|
|
|
return this.branchNameError.append(errorMessage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return NewBranchForm;
|
|
|
|
})();
|
2017-02-10 01:50:50 -05:00
|
|
|
}).call(window);
|