Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

First version of a more intelligent form handling in modal dialogs

Roland Geiderbe021bd0aa2c
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.js
index b17e25605..1791e9f08 100644
--- a/manager/static/js/workout-manager.js
+++ b/manager/static/js/workout-manager.js
@@ -255,6 +255,52 @@ function form_modal_dialog()
// Set its title and open the dialog
$("#ajax-info").dialog({title: $(responseText).find("#main-content h2").html()});
//$("#ajax-info").dialog("open");
+
+
+ // If there is a form in the modal dialog (there usually is) prevent the submit
+ // button from submitting it and do it here with an AJAX request. If there
+ // are errors (there is an element with the class 'ym-error' in the result)
+ // reload the content back into the dialog so the user can correct the entries.
+ // If there isn't assume all was saved correctly and load that result into the
+ // page's main DIV (#main-content). All this must be done like this because there
+ // doesn't seem to be any reliable and easy way to detect redirects with AJAX.
+ if ($(responseText).find(".ym-form").length > 0)
+ {
+ form = $("#ajax-info").find(".ym-form");
+ submit = $(form).find("#form-save");
+ console.log('found form in modal dialog');
+
+ submit.click(function(e) {
+ e.preventDefault();
+ console.log('clicked on submit');
+ form_action = form.attr('action');
+
+ // OK, we did the POST, what do we do with the result?
+ $.post(form_action, form.serialize(), function(data) {
+
+ if($(data).find('.ym-form .ym-error').length > 0)
+ {
+ // TODO: we must do the same again here, binding the click-event,
+ // checking for errors, etc, so move this out to a function
+ // and call itself here again.
+
+ console.log('There was an error in the form');
+ $("#ajax-info .ym-form").html($(data).find('.ym-form'));
+ }
+ else
+ {
+ // TODO: we must re-initialise all JS from #main-content again
+
+ console.log('No errors found, yipeee');
+ $("#ajax-info").dialog("close");
+ $("#main-content").html($(data).find('#main-content'));
+ }
+
+
+
+ });
+ });
+ }
});
});
}