scinote-web/app/assets/javascripts/users/registrations/edit.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
/**
* Toggle the view/edit form visibility.
* @param form - The jQuery form selector.
* @param edit - True to set form to edit mode;
* false to set form to view mode.
*/
function toggleFormVisibility(form, edit) {
if (edit) {
form.find("[data-part='view']").hide();
form.find("[data-part='edit']").show();
form.find("[data-part='edit'] input:not([type='file']):not([type='submit']):first").focus();
} else {
form.find("[data-part='view']").show();
form.find("[data-part='edit'] input").blur();
form.find("[data-part='edit']").hide();
// Clear all errors on the parent form
form.clearFormErrors();
2016-02-12 23:52:43 +08:00
// Clear any neccesary fields
form.find("input[data-role='clear']").val("");
// Copy field data
var val = form.find("input[data-role='src']").val();
form.find("input[data-role='edit']").val(val);
}
}
var forms = $("form[data-for]");
// Add "edit form" listeners
forms
.find("[data-action='edit']").click(function() {
var $form = $(this).closest("form");
2016-02-12 23:52:43 +08:00
// First, hide all form edits
_.each(forms, function(form) {
toggleFormVisibility($form, false);
2016-02-12 23:52:43 +08:00
});
// Then, edit the current form
toggleFormVisibility($form, true);
2016-02-12 23:52:43 +08:00
});
// Add "cancel form" listeners
forms
.find("[data-action='cancel']").click(function() {
var $form = $(this).closest("form");
2016-02-12 23:52:43 +08:00
// Hide the edit portion of the form
toggleFormVisibility($form, false);
2016-02-12 23:52:43 +08:00
});
// Add form submit listeners
forms
.not("[data-for='avatar']")
2016-02-12 23:52:43 +08:00
.on("ajax:success", function(ev, data, status) {
// Simply reload the page
location.reload();
})
.on("ajax:error", function(ev, data, status) {
// Render form errors
$(this).renderFormErrors("user", data.responseJSON);
2016-02-12 23:52:43 +08:00
});
2016-12-08 18:26:13 +08:00
function processFile(ev) {
var $form = $(ev.target.form);
$form.clearFormErrors();
2016-02-12 23:52:43 +08:00
var $fileInput = $form.find("input[type=file]");
if(filesValidator(ev, $fileInput, FileTypeEnum.AVATAR)) {
2016-12-08 18:26:13 +08:00
// Local file uploading
animateSpinner();
}
2016-02-12 23:52:43 +08:00
}