Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Further work on moving JS function to individual apps

Roland Geider5a2f68ad203a
diff --git a/wger/exercises/static/js/exercises.js b/wger/exercises/static/js/exercises.js
new file mode 100644
index 000000000..ec8b74f3f
--- /dev/null
+++ b/wger/exercises/static/js/exercises.js
@@ -0,0 +1,207 @@
+/*
+ * This file is part of wger Workout Manager.
+ *
+ * wger Workout Manager is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * wger Workout Manager is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ */
+
+
+/*
+ * wger exercise functions
+ */
+
+"use strict";
+
+
+
+/*
+ * Highlight a muscle in the overview
+ */
+function highlight_muscle(element, perms_exercises)
+{
+ var div_id = $(element).data('target');
+ var is_front = ($(element).data('isFront') == 'True') ? 'front' : 'back';
+ var muscle_id = div_id.match(/\d+/);
+ var muscle_name = $(element).data('name');
+
+ // Reset all other highlighted muscles
+ $('.muscle').removeClass('muscle-active');
+ $('.muscle').addClass('muscle-inactive');
+
+ // Hightligh the current one
+ $(element).removeClass('muscle-inactive');
+ $(element).addClass('muscle-active');
+
+ if (perms_exercises)
+ {
+ $('#muscle-edit-box').show();
+ $('.muscle-edit-name').html(muscle_name);
+ $('#muscle-edit-edit-link').attr("href", "/exercise/muscle/" + muscle_id + "/edit/");
+ $('#muscle-delete-edit-link').attr("href", "/exercise/muscle/" + muscle_id + "/delete/");
+ }
+
+ // Set the corresponding background
+ $('#muscle-system').css('background-image',
+ 'url(/static/images/muscles/main/muscle-' + muscle_id + '.svg),'
+ + 'url(/static/images/muscles/muscular_system_' + is_front + '.svg)');
+
+ // Show the corresponding exercises
+ $('.exercise-list').hide();
+ $('#' + div_id).show();
+}
+
+/*
+ *
+ * D3js functions
+ *
+ */
+
+function weight_log_chart(data, div_id, reps_i18n, width_factor)
+{
+
+ // Calculate the size
+ var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
+ var height_factor = width_factor / 600 * 200;
+
+ var margin = {top: 20, right: 80, bottom: 30, left: 50},
+ width = width_factor - margin.left - margin.right,
+ height = height_factor - margin.top - margin.bottom;
+
+ var parseDate = d3.time.format("%Y-%m-%d").parse;
+
+ var x = d3.time.scale()
+ .range([0, width]);
+
+ var y = d3.scale.linear()
+ .range([height, 0]);
+
+ var color = d3.scale.category10();
+
+ var xAxis = d3.svg.axis()
+ .scale(x)
+ .ticks(6)
+ .orient("bottom");
+
+ var yAxis = d3.svg.axis()
+ .scale(y)
+ .ticks(6)
+ .orient("left");
+
+ var line = d3.svg.line()
+ .interpolate("cardinal")
+ .tension(0.6)
+ .x(function(d) { return x(d.date); })
+ .y(function(d) { return y(d.weight); });
+
+ var svg = d3.select("#svg-" + div_id).append("svg")
+ .attr("width", width + margin.left + margin.right)
+ .attr("height", height + margin.top + margin.bottom)
+ .append("g")
+ .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
+
+
+ color.domain(d3.keys(data[0]).filter(function(key){
+ //console.log(key);
+ return ($.inArray(key, ['date', 'id']) == -1);
+ })
+ );
+
+ data.forEach(function(d) {
+ d.date = parseDate(d.date);
+ });
+
+
+ var reps = color.domain().map(function(name) {
+
+ var temp_values = data.filter(function(d) {
+ return(+d[name] > 0);
+ });
+
+ var filtered_values = temp_values.map(function(d) {
+ return {date: d.date,
+ weight: +d[name],
+ log_id: d.id};
+ });
+
+ return {
+ name: name,
+ values: filtered_values
+ };
+ });
+
+ //console.log(reps);
+
+ x.domain(d3.extent(data, function(d) { return d.date; }));
+
+ // Add 1 kg of "breathing room" on the min value, so the diagrams don't look
+ // too flat
+ y.domain([
+ d3.min(reps, function(c) { return d3.min(c.values, function(v) { return v.weight - 1; }); }),
+ d3.max(reps, function(c) { return d3.max(c.values, function(v) { return v.weight; }); })
+ ]);
+
+ svg.append("g")
+ .attr("class", "x axis")
+ .attr("transform", "translate(0," + height + ")")
+ .call(xAxis);
+
+ svg.append("g")
+ .attr("class", "y axis")
+ .call(yAxis)
+ .append("text")
+ .attr("transform", "rotate(-90)")
+ .attr("y", 6)
+ .attr("dy", ".71em");
+ //.style("text-anchor", "end")
+ //.text("Weight");
+
+ var log_series = svg.selectAll(".log_series")
+ .data(reps)
+ .enter().append("g")
+ .attr("class", "log_series");
+
+ log_series.append("path")
+ .attr("class", "line")
+ .attr("d", function(d) { return line(d.values); })
+ .style("stroke", function(d) { return color(d.name); });
+
+ reps.forEach(function(d){
+ var color_name = d.name
+ var temp_name = hex_random();
+ var color_class = 'color-' + color(color_name).replace('#', '');
+
+ svg.selectAll(".dot" + temp_name)
+ .data(d.values)
+ .enter().append("circle")
+ .attr("class", "dot modal-dialog " + color_class)
+ .attr("cx", line.x())
+ .attr("cy", line.y())
+ .attr("id", function(d) { return d.log_id; })
+ .attr("href", function(d) { return '/' + get_current_language() + '/workout/log/edit-entry/' + d.log_id.match(/\d+/); })
+ .attr("r", 5)
+ .style("stroke", function(d) {
+ return color(color_name);
+ });
+ });
+
+
+ log_series.append("text")
+ .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
+ .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.weight) + ")"; })
+ .attr("x", 6)
+ .attr("dy", ".35em")
+ .text(function(d) { return d.name + " " + reps_i18n; });
+
+ // Make the circles clickable: open their edit dialog
+ form_modal_dialog();
+}
+
diff --git a/wger/exercises/templates/mobile/muscles/overview.html b/wger/exercises/templates/mobile/muscles/overview.html
index c4da7c6fa..44621db2a 100644
--- a/wger/exercises/templates/mobile/muscles/overview.html
+++ b/wger/exercises/templates/mobile/muscles/overview.html
@@ -10,7 +10,9 @@
{% block title %}{% trans "Muscle overview" %}{% endblock %}
-{% block header %}{% endblock %}
+{% block header %}
+<script src="{% static 'js/exercise.js' %}"></script>
+{% endblock %}
{% block scripts %}
diff --git a/wger/exercises/templates/mobile/view.html b/wger/exercises/templates/mobile/view.html
index 99a35c735..64cfb44f3 100644
--- a/wger/exercises/templates/mobile/view.html
+++ b/wger/exercises/templates/mobile/view.html
@@ -7,6 +7,7 @@
{% block header %}
<script src="{% static 'js/tinymce/tiny_mce.js' %}"></script>
+<script src="{% static 'js/exercises.js' %}"></script>
<script>
function custom_modal_init()
diff --git a/wger/exercises/templates/muscles/overview.html b/wger/exercises/templates/muscles/overview.html
index e19a29271..853b15d67 100644
--- a/wger/exercises/templates/muscles/overview.html
+++ b/wger/exercises/templates/muscles/overview.html
@@ -11,6 +11,8 @@
{% block header %}
+<script src="{% static 'js/exercises.js' %}"></script>
+
<script>
$(document).ready(function() {
//Prefetch the background images
diff --git a/wger/exercises/templates/view.html b/wger/exercises/templates/view.html
index 4bf7abf38..b6c8aa168 100644
--- a/wger/exercises/templates/view.html
+++ b/wger/exercises/templates/view.html
@@ -7,6 +7,7 @@
{% block header %}
<script src="{% static 'js/tinymce/tiny_mce.js' %}"></script>
+<script src="{% static 'js/exercises.js' %}"></script>
<script>
function custom_modal_init()
diff --git a/wger/manager/static/js/workout-manager.js b/wger/manager/static/js/workout-manager.js
index fb14cbcd6..d66146dc6 100644
--- a/wger/manager/static/js/workout-manager.js
+++ b/wger/manager/static/js/workout-manager.js
@@ -278,42 +278,6 @@ function modal_dialog_form_edit()
-function init_ingredient_autocompleter()
-{
- // Init the autocompleter
- $("#id_ingredient_searchfield").autocomplete({
- source: '/' + get_current_language() + "/nutrition/ingredient/search/",
- minLength: 2,
- select: function(event, ui) {
-
- // After clicking on a result set the value of the hidden field
- $('#id_ingredient').val(ui.item.id);
- $('#exercise_name').html(ui.item.label);
-
- // See if the ingredient has any units and set the values for the forms
- $.get('/' + get_current_language() + '/nutrition/ingredient/' + ui.item.id + '/get-units',
- function(data){
-
- // Remove any old units, if any
- var options = $('#id_weight_unit').find('option');
- $.each(options, function(index, option_obj) {
- if (option_obj.value != '')
- {
- $(option_obj).remove();
- }
- });
-
- // Add new units, if any
- $.each(data, function(index, value) {
- $('#id_unit').append(new Option(value.name, value.id));
- $('#id_weight_unit').append(new Option(value.name_model, value.id));
- });
- });
- }
- });
-}
-
-
/*
* Returns a random hex string. This is useful, e.g. to add a unique ID to generated
* HTML elements
@@ -476,262 +440,6 @@ function init_weight_log_datepicker()
}
-/*
- *
- * D3js functions
- *
- */
-
-function weight_log_chart(data, div_id, reps_i18n, width_factor)
-{
-
- // Calculate the size
- var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
- var height_factor = width_factor / 600 * 200;
-
- var margin = {top: 20, right: 80, bottom: 30, left: 50},
- width = width_factor - margin.left - margin.right,
- height = height_factor - margin.top - margin.bottom;
-
- var parseDate = d3.time.format("%Y-%m-%d").parse;
-
- var x = d3.time.scale()
- .range([0, width]);
-
- var y = d3.scale.linear()
- .range([height, 0]);
-
- var color = d3.scale.category10();
-
- var xAxis = d3.svg.axis()
- .scale(x)
- .ticks(6)
- .orient("bottom");
-
- var yAxis = d3.svg.axis()
- .scale(y)
- .ticks(6)
- .orient("left");
-
- var line = d3.svg.line()
- .interpolate("cardinal")
- .tension(0.6)
- .x(function(d) { return x(d.date); })
- .y(function(d) { return y(d.weight); });
-
- var svg = d3.select("#svg-" + div_id).append("svg")
- .attr("width", width + margin.left + margin.right)
- .attr("height", height + margin.top + margin.bottom)
- .append("g")
- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
-
-
- color.domain(d3.keys(data[0]).filter(function(key){
- //console.log(key);
- return ($.inArray(key, ['date', 'id']) == -1);
- })
- );
-
- data.forEach(function(d) {
- d.date = parseDate(d.date);
- });
-
-
- var reps = color.domain().map(function(name) {
-
- var temp_values = data.filter(function(d) {
- return(+d[name] > 0);
- });
-
- var filtered_values = temp_values.map(function(d) {
- return {date: d.date,
- weight: +d[name],
- log_id: d.id};
- });
-
- return {
- name: name,
- values: filtered_values
- };
- });
-
- //console.log(reps);
-
- x.domain(d3.extent(data, function(d) { return d.date; }));
-
- // Add 1 kg of "breathing room" on the min value, so the diagrams don't look
- // too flat
- y.domain([
- d3.min(reps, function(c) { return d3.min(c.values, function(v) { return v.weight - 1; }); }),
- d3.max(reps, function(c) { return d3.max(c.values, function(v) { return v.weight; }); })
- ]);
-
- svg.append("g")
- .attr("class", "x axis")
- .attr("transform", "translate(0," + height + ")")
- .call(xAxis);
-
- svg.append("g")
- .attr("class", "y axis")
- .call(yAxis)
- .append("text")
- .attr("transform", "rotate(-90)")
- .attr("y", 6)
- .attr("dy", ".71em");
- //.style("text-anchor", "end")
- //.text("Weight");
-
- var log_series = svg.selectAll(".log_series")
- .data(reps)
- .enter().append("g")
- .attr("class", "log_series");
-
- log_series.append("path")
- .attr("class", "line")
- .attr("d", function(d) { return line(d.values); })
- .style("stroke", function(d) { return color(d.name); });
-
- reps.forEach(function(d){
- var color_name = d.name
- var temp_name = hex_random();
- var color_class = 'color-' + color(color_name).replace('#', '');
-
- svg.selectAll(".dot" + temp_name)
- .data(d.values)
- .enter().append("circle")
- .attr("class", "dot modal-dialog " + color_class)
- .attr("cx", line.x())
- .attr("cy", line.y())
- .attr("id", function(d) { return d.log_id; })
- .attr("href", function(d) { return '/' + get_current_language() + '/workout/log/edit-entry/' + d.log_id.match(/\d+/); })
- .attr("r", 5)
- .style("stroke", function(d) {
- return color(color_name);
- });
- });
-
-
- log_series.append("text")
- .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
- .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.weight) + ")"; })
- .attr("x", 6)
- .attr("dy", ".35em")
- .text(function(d) { return d.name + " " + reps_i18n; });
-
- // Make the circles clickable: open their edit dialog
- form_modal_dialog();
-}
-
-
-/*
- * Draw the BMI chart
- */
-function render_bmi(width_factor)
-{
- // Delete the other diagrams
- d3.selectAll('svg').remove();
-
- // Calculate the size
- var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
- var height_factor = width_factor / 600 * 300;
-
- var margin = {top: 20, right: 80, bottom: 30, left: 50},
- width = width_factor - margin.left - margin.right,
- height = height_factor - margin.top - margin.bottom;
-
- var x = d3.scale.linear()
- .range([0, width]);
-
- var y = d3.scale.linear()
- .range([height, 0]);
-
- var z = d3.scale.ordinal().range(['#000080', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff7f2a', '#ff0000', '#800000']);
-
- var xAxis = d3.svg.axis()
- .scale(x)
- .orient("bottom");
-
- var yAxis = d3.svg.axis()
- .scale(y)
- .orient("left");
-
- var stack = d3.layout.stack()
- .offset("zero")
- .values(function(d) { return d.values; })
- .x(function(d) { return d.height; })
- .y(function(d) { return d.weight; });
-
- var nest = d3.nest()
- .key(function(d) { return d.key; });
-
- var area = d3.svg.area()
- .interpolate("linear")
- .x(function(d) { return x(d.height); })
- .y0(function(d) { return y(d.y0); })
- .y1(function(d) { return y(d.y); });
-
-
- var svg = d3.select("#bmi-chart").append("svg")
- .attr("width", width + margin.left + margin.right)
- .attr("height", height + margin.top + margin.bottom)
- .append("g")
- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
-
- // Clip path, drawings outside are removed
- svg.append("defs").append("clipPath")
- .attr("id", "clip")
- .append("rect")
- .attr("width", width)
- .attr("height", height);
-
-
- d3.json("/nutrition/calculator/bmi/chart-data", function(data) {
-
- var layers = stack(nest.entries(data));
-
- // Manually set the domains
- x.domain([150, 200]); // height, in cm
- y.domain([30, 160]); // weight, in kg
-
- svg.selectAll(".layer")
- .data(layers)
- .enter().append("path")
- .attr("class", "layer")
- .attr("id", function(d, i) { return "key-" + d.key; })
- .attr("clip-path", "url(#clip)")
- .attr("d", function(d) { return area(d.values); })
- .style("fill", function(d, i) { return z(i); })
- .style("opacity", 0.6);
-
- svg.append("g")
- .attr("class", "x axis")
- .attr("transform", "translate(0," + height + ")")
- .call(xAxis);
-
- svg.append("g")
- //.attr("transform", "translate(" + width + ",0)")
- .attr("class", "y axis")
- .call(yAxis);
-
- var url = $("#bmi-form").attr( 'action' );
-
- $.post(url,
- $("#bmi-form").serialize(),
- function(data) {
- $('#bmi-result-container').show();
- $('#bmi-result-value').html(data.bmi);
- svg.append("circle")
- .attr("cx", x(data.height))
- .attr("cy", y(data.weight))
- .attr("fill", "black")
- .attr("r", 5);
- });
- });
-
-}
-
-
-
function toggle_weight_log_table()
{
@@ -739,7 +447,7 @@ function toggle_weight_log_table()
e.preventDefault();
var target = $(this).data('toggleTarget');
$('#' + target).toggle({effect: 'blind', duration: 600});
- });
+ });
}
/*
@@ -778,94 +486,3 @@ function prefetch_images(imageArray)
});
}
-
-
-/*
- * Highlight a muscle in the overview
- */
-function highlight_muscle(element, perms_exercises)
-{
- var div_id = $(element).data('target');
- var is_front = ($(element).data('isFront') == 'True') ? 'front' : 'back';
- var muscle_id = div_id.match(/\d+/);
- var muscle_name = $(element).data('name');
-
- // Reset all other highlighted muscles
- $('.muscle').removeClass('muscle-active');
- $('.muscle').addClass('muscle-inactive');
-
- // Hightligh the current one
- $(element).removeClass('muscle-inactive');
- $(element).addClass('muscle-active');
-
- if (perms_exercises)
- {
- $('#muscle-edit-box').show();
- $('.muscle-edit-name').html(muscle_name);
- $('#muscle-edit-edit-link').attr("href", "/exercise/muscle/" + muscle_id + "/edit/");
- $('#muscle-delete-edit-link').attr("href", "/exercise/muscle/" + muscle_id + "/delete/");
- }
-
- // Set the corresponding background
- $('#muscle-system').css('background-image',
- 'url(/static/images/muscles/main/muscle-' + muscle_id + '.svg),'
- + 'url(/static/images/muscles/muscular_system_' + is_front + '.svg)');
-
- // Show the corresponding exercises
- $('.exercise-list').hide();
- $('#' + div_id).show();
-}
-
-
-/*
- * Calories calculator
- */
-function init_calories_calculator()
-{
- $("#form-transfer-calories").click(function(e){
- e.preventDefault();
- var base_calories = Number($('#id_base_calories').html());
- $('#id_calories').val(base_calories);
- });
-
- $("#add-calories-total").click(function(e){
- e.preventDefault();
- var base_calories = Number($('#id_base_calories').html());
- var additional_calories = Number($('#id_additional_calories').val());
- $('#id_calories').val(base_calories + additional_calories);
- });
-
- $("#form-update-calories").click(function(e){
- e.preventDefault();
- var form_url = $("#total-calories-form").attr('action');
- $.post(form_url,
- $("#total-calories-form").serialize(),
- function(data) {
- });
- });
-
- $(".calories-autoform").click(function(e){
- e.preventDefault();
-
- // BMR
- var bmr_url = $("#bmr-form").attr('action');
- $.post(bmr_url,
- $("#bmr-form").serialize(),
- function(data) {
- $('#bmr-result-container').show();
- $('#bmr-result-value').html(data.bmr);
-
- // Activities
- var activities_url = $("#activities-form").attr('action');
- $.post(activities_url,
- $("#activities-form").serialize(),
- function(data) {
- $('#activities-result-container').show();
- $('#activities-result-value').html(data.factor);
-
- // Total calories
- $('#id_base_calories').html(data.activities);
- });
- });
- });
-}
diff --git a/wger/manager/templates/mobile/workout/log.html b/wger/manager/templates/mobile/workout/log.html
index 7346f9fd7..a8d86867f 100644
--- a/wger/manager/templates/mobile/workout/log.html
+++ b/wger/manager/templates/mobile/workout/log.html
@@ -7,7 +7,9 @@
{% block title %}{% trans "Weight log for workout" %}{% endblock %}
-{% block header %}{% endblock %}
+{% block header %}
+<script src="{% static 'js/exercises.js' %}"></script>
+{% endblock %}
{% block scripts %}
diff --git a/wger/manager/templates/workout/log.html b/wger/manager/templates/workout/log.html
index cca3770c8..dab21a090 100644
--- a/wger/manager/templates/workout/log.html
+++ b/wger/manager/templates/workout/log.html
@@ -8,6 +8,7 @@
{% block header %}
<script src="{% static 'js/d3.js' %}"></script>
+<script src="{% static 'js/exercises.js' %}"></script>
<script type="text/javascript">
// Initialise the datepicker for the modal dialog
diff --git a/wger/nutrition/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.js
index 7be877ac2..2deafdd09 100644
--- a/wger/nutrition/static/js/nutrition.js
+++ b/wger/nutrition/static/js/nutrition.js
@@ -18,6 +18,8 @@
* wger Nutriton functions
*/
+"use strict";
+
function update_ingredient_value(url)
{
form_data = $('#nutritional-values-form').serializeArray();
@@ -88,3 +90,201 @@ function init_english_ingredients()
$("#ajax-info").load('/' + get_current_language() + "/workout/api/user-preferences?do=set_english-ingredients&show=" + switch_to);
});
}
+
+
+function init_ingredient_autocompleter()
+{
+ // Init the autocompleter
+ $("#id_ingredient_searchfield").autocomplete({
+ source: '/' + get_current_language() + "/nutrition/ingredient/search/",
+ minLength: 2,
+ select: function(event, ui) {
+
+ // After clicking on a result set the value of the hidden field
+ $('#id_ingredient').val(ui.item.id);
+ $('#exercise_name').html(ui.item.label);
+
+ // See if the ingredient has any units and set the values for the forms
+ $.get('/' + get_current_language() + '/nutrition/ingredient/' + ui.item.id + '/get-units',
+ function(data){
+
+ // Remove any old units, if any
+ var options = $('#id_weight_unit').find('option');
+ $.each(options, function(index, option_obj) {
+ if (option_obj.value != '')
+ {
+ $(option_obj).remove();
+ }
+ });
+
+ // Add new units, if any
+ $.each(data, function(index, value) {
+ $('#id_unit').append(new Option(value.name, value.id));
+ $('#id_weight_unit').append(new Option(value.name_model, value.id));
+ });
+ });
+ }
+ });
+}
+
+
+/*
+ * Draw the BMI chart
+ */
+function render_bmi(width_factor)
+{
+ // Delete the other diagrams
+ d3.selectAll('svg').remove();
+
+ // Calculate the size
+ var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
+ var height_factor = width_factor / 600 * 300;
+
+ var margin = {top: 20, right: 80, bottom: 30, left: 50},
+ width = width_factor - margin.left - margin.right,
+ height = height_factor - margin.top - margin.bottom;
+
+ var x = d3.scale.linear()
+ .range([0, width]);
+
+ var y = d3.scale.linear()
+ .range([height, 0]);
+
+ var z = d3.scale.ordinal().range(['#000080', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff7f2a', '#ff0000', '#800000']);
+
+ var xAxis = d3.svg.axis()
+ .scale(x)
+ .orient("bottom");
+
+ var yAxis = d3.svg.axis()
+ .scale(y)
+ .orient("left");
+
+ var stack = d3.layout.stack()
+ .offset("zero")
+ .values(function(d) { return d.values; })
+ .x(function(d) { return d.height; })
+ .y(function(d) { return d.weight; });
+
+ var nest = d3.nest()
+ .key(function(d) { return d.key; });
+
+ var area = d3.svg.area()
+ .interpolate("linear")
+ .x(function(d) { return x(d.height); })
+ .y0(function(d) { return y(d.y0); })
+ .y1(function(d) { return y(d.y); });
+
+
+ var svg = d3.select("#bmi-chart").append("svg")
+ .attr("width", width + margin.left + margin.right)
+ .attr("height", height + margin.top + margin.bottom)
+ .append("g")
+ .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
+
+ // Clip path, drawings outside are removed
+ svg.append("defs").append("clipPath")
+ .attr("id", "clip")
+ .append("rect")
+ .attr("width", width)
+ .attr("height", height);
+
+
+ d3.json("/nutrition/calculator/bmi/chart-data", function(data) {
+
+ var layers = stack(nest.entries(data));
+
+ // Manually set the domains
+ x.domain([150, 200]); // height, in cm
+ y.domain([30, 160]); // weight, in kg
+
+ svg.selectAll(".layer")
+ .data(layers)
+ .enter().append("path")
+ .attr("class", "layer")
+ .attr("id", function(d, i) { return "key-" + d.key; })
+ .attr("clip-path", "url(#clip)")
+ .attr("d", function(d) { return area(d.values); })
+ .style("fill", function(d, i) { return z(i); })
+ .style("opacity", 0.6);
+
+ svg.append("g")
+ .attr("class", "x axis")
+ .attr("transform", "translate(0," + height + ")")
+ .call(xAxis);
+
+ svg.append("g")
+ //.attr("transform", "translate(" + width + ",0)")
+ .attr("class", "y axis")
+ .call(yAxis);
+
+ var url = $("#bmi-form").attr( 'action' );
+
+ $.post(url,
+ $("#bmi-form").serialize(),
+ function(data) {
+ $('#bmi-result-container').show();
+ $('#bmi-result-value').html(data.bmi);
+ svg.append("circle")
+ .attr("cx", x(data.height))
+ .attr("cy", y(data.weight))
+ .attr("fill", "black")
+ .attr("r", 5);
+ });
+ });
+
+}
+
+
+/*
+ * Calories calculator
+ */
+function init_calories_calculator()
+{
+ $("#form-transfer-calories").click(function(e){
+ e.preventDefault();
+ var base_calories = Number($('#id_base_calories').html());
+ $('#id_calories').val(base_calories);
+ });
+
+ $("#add-calories-total").click(function(e){
+ e.preventDefault();
+ var base_calories = Number($('#id_base_calories').html());
+ var additional_calories = Number($('#id_additional_calories').val());
+ $('#id_calories').val(base_calories + additional_calories);
+ });
+
+ $("#form-update-calories").click(function(e){
+ e.preventDefault();
+ var form_url = $("#total-calories-form").attr('action');
+ $.post(form_url,
+ $("#total-calories-form").serialize(),
+ function(data) {
+ });
+ });
+
+ $(".calories-autoform").click(function(e){
+ e.preventDefault();
+
+ // BMR
+ var bmr_url = $("#bmr-form").attr('action');
+ $.post(bmr_url,
+ $("#bmr-form").serialize(),
+ function(data) {
+ $('#bmr-result-container').show();
+ $('#bmr-result-value').html(data.bmr);
+
+ // Activities
+ var activities_url = $("#activities-form").attr('action');
+ $.post(activities_url,
+ $("#activities-form").serialize(),
+ function(data) {
+ $('#activities-result-container').show();
+ $('#activities-result-value').html(data.factor);
+
+ // Total calories
+ $('#id_base_calories').html(data.activities);
+ });
+ });
+ });
+}
diff --git a/wger/nutrition/templates/bmi/form.html b/wger/nutrition/templates/bmi/form.html
index 0c351c50a..d8b6c0fcc 100644
--- a/wger/nutrition/templates/bmi/form.html
+++ b/wger/nutrition/templates/bmi/form.html
@@ -5,6 +5,9 @@
{% block header %}
<script src="{% static 'js/d3.js' %}"></script>
+<script src="{% static 'js/nutrition.js' %}"></script>
+
+
<style>
div.table {
display:table;
diff --git a/wger/nutrition/templates/meal_item/edit.html b/wger/nutrition/templates/meal_item/edit.html
index b8e694c61..5f5d525b5 100644
--- a/wger/nutrition/templates/meal_item/edit.html
+++ b/wger/nutrition/templates/meal_item/edit.html
@@ -9,6 +9,8 @@
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
+
<script type="text/javascript">
$(document).ready(function() {
// Init the autocompleter
diff --git a/wger/nutrition/templates/mobile/meal_item/edit.html b/wger/nutrition/templates/mobile/meal_item/edit.html
index da0aa8b32..b0d4d03f9 100644
--- a/wger/nutrition/templates/mobile/meal_item/edit.html
+++ b/wger/nutrition/templates/mobile/meal_item/edit.html
@@ -9,6 +9,8 @@
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
+
<script type="text/javascript">
$(document).ready(function() {
// Init the autocompleter
diff --git a/wger/nutrition/templates/mobile/rate/form.html b/wger/nutrition/templates/mobile/rate/form.html
index 7c7659672..1b92f9e1b 100644
--- a/wger/nutrition/templates/mobile/rate/form.html
+++ b/wger/nutrition/templates/mobile/rate/form.html
@@ -4,6 +4,8 @@
{% load wger_extras %}
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
+
<script>
$(document).ready(function() {
init_calories_calculator();
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 63aa36598..cd0b113de 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -4,6 +4,8 @@
{% load wger_extras %}
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
+
<script>
$(document).ready(function() {
init_calories_calculator();
diff --git a/wger/weight/static/js/weight.js b/wger/weight/static/js/weight.js
index e3ca482ba..f1ce3df03 100644
--- a/wger/weight/static/js/weight.js
+++ b/wger/weight/static/js/weight.js
@@ -18,6 +18,9 @@
* wger weight functions
*/
+"use strict";
+
+
// Simple helper function that simply returns the y component of an entry
function y_value(d) { return d.y; }
diff --git a/wger/weight/templates/edit.html b/wger/weight/templates/edit.html
index 12a6f582a..9ae67aa44 100644
--- a/wger/weight/templates/edit.html
+++ b/wger/weight/templates/edit.html
@@ -9,6 +9,8 @@
{% block header %}
+<script src="{% static 'js/weight.js' %}"></script>
+
<script>
$(document).ready(function () {
init_weight_datepicker();