Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move the JS functions to external file, do some fixes on the in-place editing
Roland Geiderf5019d5e135f
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.jsnew file mode 100644index 000000000..0bbecb3ea--- /dev/null+++ b/manager/static/js/workout-manager.js@@ -0,0 +1,145 @@+/*+ * Own functions+ *+ */+++/*+ * Setup JQuery sortables to make the sets sortable+ */+function setup_sortable()+{+ $(".workout-table tbody").sortable({+ handle: 'td.ym-g10',+ revert: true,+ update : function (event, ui) {+ // Monkey around the HTML, till we find the IDs of the set and the day+ day_element = ui.item.parent().parent().find('tr').first().attr('id'); //day-xy+ day_id = day_element.match(/\d+/);++ // returns something in the form "set-1,set-2,set-3,"+ order = $( this ).sortable('toArray');++ //$("#ajax-info").show();+ //$("#ajax-info").addClass('success');+ $.get("/workout/api/edit-set" + "?do=set_order&day_id=" + day_id + "&order=" + order)+++ // TODO: it seems to be necessary to call the view two times before it returns+ // current data.+ $.get("/workout/day/view/" + day_id);+ $("#div-day-" + day_id).load("/workout/day/view/" + day_id);+ }++ })++ // Allow the settings within an exercise to be sortable+ $(".settings-list").sortable({+ placeholder: 'sortable-settings',+ revert: true,+ tolerance: 'pointer',+ helper: function(event, ui) {+ //return ui;+ return $('<div class="sortable-settings-drag">' + ui.html() + '</div>');+ },+ update : function (event, ui) {+ // returns something in the form "setting-1,setting-2,setting-3,"+ order = $( this ).sortable('toArray');++ // Load the day-ID+ day_element = ui.item.parents('table').find('tr').attr('id'); //day-xy+ day_id = day_element.match(/\d+/);++ //$("#ajax-info").show();+ //$("#ajax-info").addClass('success');+ $("#ajax-info").load("/workout/api/edit-settting?do=set_order&order=" + order);++ // TODO: it seems to be necessary to call the view two times before it returns+ // current data.+ $.get("/workout/day/view/" + day_id);+ $("#div-day-" + day_id).load("/workout/day/view/" + day_id);+ }+ });+}+++/*+ * Setup JQuery calls to edit the sets+ */+function setup_ajax_set_edit()+{+ $(".ajax-set-edit").click(function(e) {+ e.preventDefault();++ set_element = $(this).parents('tr').attr('id');+ set_id = set_element.match(/\d+/);++ exercise_id = $(this).parents('li').attr('id').match(/\d+/);+++ $($(this).parents('li')).load("/workout/api/edit-set?do=edit_set&set=" + set_id + "&exercise=" + exercise_id);+ });+}++function toggle_comments()+{+ $("#exercise-comments-toggle").click(function(e) {+ e.preventDefault();+++ if ( showComment == 0 )+ {+ $('.exercise-comments').show();+ showComment = 1;+ }+ else if ( showComment == 1 )+ {+ $('.exercise-comments').hide();+ showComment = 0;+ }++ $("#ajax-info").load("/workout/api/user-preferences?do=set_show-comments&show=" + showComment);+ });+}++function setup_inplace_editing()+{+ $(".ajax-form-cancel").each(function(index) {+++ exercise_id = $(this).parents('li').attr('id').match(/\d+/);+ day_id = $(this).parents('table').attr('id').match(/\d+/);+ set_id = $(this).parents('tr').attr('id').match(/\d+/);++ // Editing of set+ $(".ajax-form-cancel").click(function(e) {+ e.preventDefault();+ $("#div-day-" + day_id).load("/workout/day/view/" + day_id);++ setup_sortable();+ setup_ajax_set_edit();+ })++ // Send the Form+ $('.ajax-form-set-edit').submit(function(e) {+ e.preventDefault();++ url = "/workout/api/edit-set?do=edit_set&set=" + set_id + "&exercise=" + exercise_id+ form_data = $(this).serialize();+ $.post( url, form_data);++ $("#div-day-" + day_id).load("/workout/day/view/" + day_id);+ });++ // Init the autocompleter+ $(".ajax-form-exercise-list").autocomplete({+ source: "{% url exercises.views.exercise_search %}",+ minLength: 2,+ select: function(event, ui) {++ // After clicking on a result set the value of the hidden field+ $('#set-' + set_id + '-exercercise-id-hidden').val(ui.item.id);+ }+ });+ });+}\ No newline at end of filediff --git a/manager/templates/setting/ajax_edit.html b/manager/templates/setting/ajax_edit.htmlindex 0ca3cc5f7..d940b42fe 100644--- a/manager/templates/setting/ajax_edit.html+++ b/manager/templates/setting/ajax_edit.html@@ -6,39 +6,9 @@ $(document).ready(function() {// Call the setup function to allow the sets to be sortable again after loading this// view via AJAX- // Editing of set- $(".ajax-form-cancel").click(function(e) {- e.preventDefault();- $("#div-day-" + {{set.exerciseday.id}}).load("/workout/day/view/" + {{set.exerciseday.id}});- alert(1);-- setup_ajax_set_edit();- })-- // Send the Form- $('.ajax-form-set-edit').submit(function(e) {- e.preventDefault();-- url = "/workout/api/edit-set?do=edit_set&set=" + {{set.id}} + "&exercise=" + {{exercise.id}}- form_data = $(this).serialize();- $.post( url, form_data);-- $("#div-day-" + {{set.exerciseday.id}}).load("/workout/day/view/" + {{set.exerciseday.id}});- //alert(1);-- setup_ajax_set_edit();- });-- // Init the autocompleter- $(".ajax-form-exercise-list").autocomplete({- source: "{% url exercises.views.exercise_search %}",- minLength: 2,- select: function(event, ui) {-- // After clicking on a result set the value of the hidden field- $('#set-{{set.id}}-exercercise-id-hidden').val(ui.item.id);- }- });+ setup_inplace_editing();++})</script>diff --git a/manager/templates/tags/render_day.html b/manager/templates/tags/render_day.htmlindex b46edbb70..f1af1142d 100644--- a/manager/templates/tags/render_day.html+++ b/manager/templates/tags/render_day.html@@ -7,6 +7,8 @@ $(document).ready(function() {// Call the setup function to allow the sets to be sortable again after loading this// view via AJAXsetup_sortable();+ setup_ajax_set_edit();+ setup_inplace_editing();})</script>diff --git a/manager/templates/workout/view.html b/manager/templates/workout/view.htmlindex 04a59c82e..be0c6ae9c 100644--- a/manager/templates/workout/view.html+++ b/manager/templates/workout/view.html@@ -7,81 +7,9 @@{% block header %}<script src="{% static "js/jquery-ui.js" %}"></script>+<script src="{% static "js/workout-manager.js" %}"></script><script>-function setup_sortable()-{- // Allow the sets to be sortable- $(".workout-table tbody").sortable({- handle: 'td.ym-g10',- revert: true,- update : function (event, ui) {-- // Monkey around the HTML, till we find the IDs of the set and the day- day_element = ui.item.parent().parent().find('tr').first().attr('id'); //day-xy- day_id = day_element.match(/\d+/);-- // returns something in the form "set-1,set-2,set-3,"- order = $( this ).sortable('toArray');-- //$("#ajax-info").show();- //$("#ajax-info").addClass('success');- $.get("/workout/api/edit-set" + "?do=set_order&day_id=" + day_id + "&order=" + order)--- // TODO: it seems to be necessary to call the view two times before it returns- // current data.- $.get("/workout/day/view/" + day_id);- $("#div-day-" + day_id).load("/workout/day/view/" + day_id);- }-- })-- // Allow the settings within an exercise to be sortable- $(".settings-list").sortable({- placeholder: 'sortable-settings',- revert: true,- tolerance: 'pointer',- helper: function(event, ui) {- //return ui;- return $('<div class="sortable-settings-drag">' + ui.html() + '</div>');- },- update : function (event, ui) {- // returns something in the form "setting-1,setting-2,setting-3,"- order = $( this ).sortable('toArray');-- // Load the day-ID- day_element = ui.item.parents('table').find('tr').attr('id'); //day-xy- day_id = day_element.match(/\d+/);-- //$("#ajax-info").show();- //$("#ajax-info").addClass('success');- $("#ajax-info").load("/workout/api/edit-settting?do=set_order&order=" + order);-- // TODO: it seems to be necessary to call the view two times before it returns- // current data.- $.get("/workout/day/view/" + day_id);- $("#div-day-" + day_id).load("/workout/day/view/" + day_id);- }- });-}--// Editing of sets-function setup_ajax_set_edit()-{- $(".ajax-set-edit").click(function(e) {- e.preventDefault();-- set_element = $(this).parents('tr').attr('id');- set_id = set_element.match(/\d+/);-- exercise_id = $(this).parents('li').attr('id').match(/\d+/);--- $($(this).parents('li')).load("/workout/api/edit-set?do=edit_set&set=" + set_id + "&exercise=" + exercise_id);-- //alert(1);- });-}+$(document).ready(function() {@@ -90,9 +18,11 @@ $(document).ready(function() {// Allow the sets to be sortablesetup_sortable();- // Editing of setssetup_ajax_set_edit();+ // Editing of sets+ //setup_ajax_set_edit();+// Show/Hide the comments to the exercises{% if user.userprofile.show_comments %}showComment = 1;@@ -102,23 +32,7 @@ $(document).ready(function() {$(".exercise-comments").hide();{% endif %}- $("#exercise-comments-toggle").click(function(e) {- e.preventDefault();--- if ( showComment == 0 )- {- $('.exercise-comments').show();- showComment = 1;- }- else if ( showComment == 1 )- {- $('.exercise-comments').hide();- showComment = 0;- }-- $("#ajax-info").load("/workout/api/user-preferences?do=set_show-comments&show=" + showComment);- });+ toggle_comments();});</script>{% endblock %}diff --git a/manager/views.py b/manager/views.pyindex 0ab0b61e1..8ede06487 100644--- a/manager/views.py+++ b/manager/views.py@@ -521,6 +521,9 @@ def api_edit_set(request):set_form = SetForm(request.POST, instance=workout_set)if set_form.is_valid():++ # TODO: when there is more than one exercise per set, we need to+ # manually set them here, otherwise they get lost!set_form.save()else: