Some additional refinements.

This commit is contained in:
Matej Zrimšek 2016-07-22 18:02:57 +02:00
parent d59fd0de95
commit b70f49f4e0
6 changed files with 36 additions and 43 deletions

View file

@ -334,10 +334,6 @@ function startFileUpload(ev, btn) {
showResultFormErrors($form, errors); showResultFormErrors($form, errors);
}); });
if(!noErrors) {
animateSpinner(null, false);
}
ev.preventDefault();
return noErrors; return noErrors;
} }

View file

@ -178,7 +178,6 @@ function formCallback($form) {
// Init ajax success/error for edit form // Init ajax success/error for edit form
function formEditAjax($form) { function formEditAjax($form) {
var selectedTabIndex;
$form $form
.on("ajax:beforeSend", function () { .on("ajax:beforeSend", function () {
$(".nested_step_checklists ul").each(function () { $(".nested_step_checklists ul").each(function () {
@ -210,7 +209,7 @@ function formEditAjax($form) {
$(this).remove(); $(this).remove();
$errInput = $form.find(".form-group.has-error").first().find("input"); $errInput = $form.find(".form-group.has-error").first().find("input");
renderFormError($errInput); renderFormError(e, $errInput);
formCallback($form); formCallback($form);
initEditableHandsOnTable($form); initEditableHandsOnTable($form);
@ -256,7 +255,7 @@ function formNewAjax($form) {
$(this).remove(); $(this).remove();
$errInput = $form.find(".form-group.has-error").first().find("input"); $errInput = $form.find(".form-group.has-error").first().find("input");
renderFormError($errInput); renderFormError(e, $errInput);
formCallback($form); formCallback($form);
formNewAjax($form); formNewAjax($form);
@ -590,11 +589,11 @@ function stepValidator(ev, editMode, forS3) {
removeBlankFileForms($form); removeBlankFileForms($form);
var $fileInputs = $form.find("input[type=file]"); var $fileInputs = $form.find("input[type=file]");
var filesValid = filesValidator($fileInputs); var filesValid = filesValidator(ev, $fileInputs);
var $checklists = $form.find(".nested_step_checklists"); var $checklists = $form.find(".nested_step_checklists");
var checklistsValid = checklistsValidator($checklists, editMode); var checklistsValid = checklistsValidator(ev, $checklists, editMode);
var $nameInput = $form.find("#step_name"); var $nameInput = $form.find("#step_name");
var nameValid = nameValidator($nameInput); var nameValid = nameValidator(ev, $nameInput);
if(filesValid && checklistsValid && nameValid) { if(filesValid && checklistsValid && nameValid) {
if(forS3) { if(forS3) {
@ -606,9 +605,6 @@ function stepValidator(ev, editMode, forS3) {
// (startFileUpload already calls it) // (startFileUpload already calls it)
animateSpinner(); animateSpinner();
} }
} else {
// Don't submit form
ev.preventDefault();
} }
} }
@ -691,20 +687,20 @@ function startFileUpload(ev, btn) {
processFile(); processFile();
}, function (errors) { }, function (errors) {
var assetError; var assetErrorMsg;
for (var c in errors) { for (var c in errors) {
if (/^asset\./.test(c)) { if (/^asset\./.test(c)) {
assetError = errors[c]; assetErrorMsg = errors[c];
break; break;
} }
} }
if (assetError) { if (assetErrorMsg) {
var el = $form.find("input[type=file]").get(inputPointer - 1); var el = $form.find("input[type=file]").get(inputPointer - 1);
var $el = $(el); var $el = $(el);
$form.clear_form_errors(); $form.clear_form_errors();
renderFormError($el, assetError); renderFormError(e, $el, assetErrorMsg);
} else { } else {
tabsPropagateErrorClass($form); tabsPropagateErrorClass($form);
} }
@ -712,9 +708,5 @@ function startFileUpload(ev, btn) {
} }
var noErrors = processFile(); var noErrors = processFile();
if(!noErrors) {
animateSpinner(null, false);
}
ev.preventDefault();
return noErrors; return noErrors;
} }

View file

@ -43,7 +43,7 @@ $.fn.render_form_errors_no_clear = function(model_name, errors, input_group) {
// NOTE: Similar to $.fn.render_form_errors, except here we process // NOTE: Similar to $.fn.render_form_errors, except here we process
// one error at a time, which is not read from the form but is // one error at a time, which is not read from the form but is
// specified manually. // specified manually.
function renderFormError(nameInput, errMsg, errAttributes) { function renderFormError(ev, nameInput, errMsg, errAttributes) {
if(!_.isUndefined(errMsg)) { if(!_.isUndefined(errMsg)) {
var $errMsgSpan = $(nameInput).next(".help-block"); var $errMsgSpan = $(nameInput).next(".help-block");
errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes; errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes;
@ -67,7 +67,11 @@ function renderFormError(nameInput, errMsg, errAttributes) {
goToFormElement(nameInput); goToFormElement(nameInput);
} }
event.preventDefault(); // Don't submit form
ev.preventDefault();
ev.stopPropagation();
// Remove spinner if present
animateSpinner(null, false);
} }
// If any of tabs (if exist) has errors, mark parent tab // If any of tabs (if exist) has errors, mark parent tab

View file

@ -1,4 +1,7 @@
function nameValidator($nameInput) { // Form validators. They'll find, render and focus error/s and
// prevent form submission.
function nameValidator(ev, $nameInput) {
var nameTooShort = $nameInput.val().length === 0; var nameTooShort = $nameInput.val().length === 0;
var nameTooLong = $nameInput.val().length > 50; var nameTooLong = $nameInput.val().length > 50;
var errMsg; var errMsg;
@ -10,12 +13,12 @@ function nameValidator($nameInput) {
var hasErrors = !_.isUndefined(errMsg); var hasErrors = !_.isUndefined(errMsg);
if (hasErrors) { if (hasErrors) {
renderFormError($nameInput, errMsg); renderFormError(ev, $nameInput, errMsg);
} }
return !hasErrors; return !hasErrors;
} }
function checklistsValidator(checklists, editMode) { function checklistsValidator(ev, checklists, editMode) {
var noErrors = true; var noErrors = true;
// For every visible (i.e. not removed) checklist // For every visible (i.e. not removed) checklist
checklists.each(function() { checklists.each(function() {
@ -39,7 +42,7 @@ function checklistsValidator(checklists, editMode) {
if (anyChecklistItemFilled || editMode) { if (anyChecklistItemFilled || editMode) {
// In edit mode, checklist's name can't be blank // In edit mode, checklist's name can't be blank
var errMsg = I18n.t("devise.names.not_blank"); var errMsg = I18n.t("devise.names.not_blank");
renderFormError($checklistNameInput, errMsg); renderFormError(ev, $checklistNameInput, errMsg);
noErrors = false; noErrors = false;
} else { } else {
// Hide empty checklist (remove would break logic) // Hide empty checklist (remove would break logic)
@ -59,16 +62,17 @@ $.fn.files_validator = function(callback) {
var $form = $(this); var $form = $(this);
if ($form.length) { if ($form.length) {
$form.submit(function (ev) { $form.submit(function (ev) {
$form.clear_form_errors();
var $fileInputs = $form.find("input[type=file]"); var $fileInputs = $form.find("input[type=file]");
filesValidator($fileInputs, callback); filesValidator(ev, $fileInputs, callback);
}); });
} }
}; };
function filesValidator(fileInputs, callback) { function filesValidator(ev, fileInputs, callback) {
var filesSizeValid = true; var filesSizeValid = true;
if (fileInputs.length) { if (fileInputs.length) {
var filesSizeValid = filesSizeValidator(fileInputs); var filesSizeValid = filesSizeValidator(ev, fileInputs);
// TODO File content check // TODO File content check
if (!filesSizeValid && callback) { if (!filesSizeValid && callback) {
@ -78,7 +82,7 @@ function filesValidator(fileInputs, callback) {
return filesSizeValid; return filesSizeValid;
} }
function filesSizeValidator(fileInputs) { function filesSizeValidator(ev, fileInputs) {
function getFileTooBigError(file) { function getFileTooBigError(file) {
if (!file) { if (!file) {
@ -98,19 +102,19 @@ function filesSizeValidator(fileInputs) {
var assetError = getFileTooBigError(file); var assetError = getFileTooBigError(file);
var input = $(fileInput); var input = $(fileInput);
if (assetError) { if (assetError) {
renderFormError(input, assetError, "data-error='file-size'"); renderFormError(ev, input, assetError, "data-error='file-size'");
fileSizeValid = false; fileSizeValid = false;
} }
}); });
if(fileSizeValid) { if(fileSizeValid) {
// Check if there is enough free space for the files // Check if there is enough free space for the files
fileSizeValid = enaughSpaceValidator(fileInputs); fileSizeValid = enaughSpaceValidator(ev, fileInputs);
} }
return fileSizeValid; return fileSizeValid;
} }
// Overriden in billing module for checking // Overriden in billing module for checking
// whether enough organization space is free // whether enough organization space is free
function enaughSpaceValidator(fileInputs) { function enaughSpaceValidator(ev, fileInputs) {
return true; return true;
} }

View file

@ -1,5 +1,6 @@
// Scroll to and focus on element // Scroll to and focus on element
function goToFormElement(input) { function goToFormElement(input) {
$("html, body").stop();
$("html, body").animate( $("html, body").animate(
{ {
scrollTop: $(input).closest(".form-group").offset().top scrollTop: $(input).closest(".form-group").offset().top

View file

@ -89,28 +89,24 @@ function startFileUpload(ev, btn) {
}, function (errors) { }, function (errors) {
$form.render_form_errors("user", errors); $form.render_form_errors("user", errors);
var avatarError; var avatarErrorMsg;
animateSpinner($form, false); animateSpinner($form, false);
for (var c in errors) { for (var c in errors) {
if (/^avatar/.test(c)) { if (/^avatar/.test(c)) {
avatarError = errors[c]; avatarErrorMsg = errors[c];
break; break;
} }
} }
if (avatarError) { if (avatarErrorMsg) {
var $el = $form.find("input[type=file]"); var $el = $form.find("input[type=file]");
$form.clear_form_errors(); $form.clear_form_errors();
renderFormError($el, avatarError); renderFormError(ev, $el, avatarErrorMsg);
} }
}, "avatar"); }, "avatar");
if(!noErrors) {
animateSpinner(null, false);
}
ev.preventDefault();
return noErrors; return noErrors;
} }