Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Move JS functions to external files, clean up where possible

Roland Geidere3208c0869d1
diff --git a/wger/exercises/views/__init__.py b/wger/exercises/views/__init__.py
index e69de29bb..40a96afc6 100644
--- a/wger/exercises/views/__init__.py
+++ b/wger/exercises/views/__init__.py
@@ -0,0 +1 @@
+# -*- coding: utf-8 -*-
diff --git a/wger/manager/static/js/workout-manager.js b/wger/manager/static/js/workout-manager.js
index 753f47b63..fb14cbcd6 100644
--- a/wger/manager/static/js/workout-manager.js
+++ b/wger/manager/static/js/workout-manager.js
@@ -1,7 +1,24 @@
+/*
+ * 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
+ */
+
/*
* Own functions
*
*/
+
"use strict";
function get_current_language()
@@ -86,26 +103,6 @@ function toggle_comments()
});
}
-function set_english_ingredients()
-{
- $("#ajax-english-ingredients").click(function(e) {
- e.preventDefault();
-
-
- if ( useEnglishIngredients == 0 )
- {
- $('#english-ingredients-status').attr("src", "/static/images/icons/status-on.svg");
- useEnglishIngredients = 1;
- }
- else if ( useEnglishIngredients == 1 )
- {
- $('#english-ingredients-status').attr("src", "/static/images/icons/status-off.svg");
- useEnglishIngredients = 0;
- }
-
- $("#ajax-info").load('/' + get_current_language() + "/workout/api/user-preferences?do=set_english-ingredients&show=" + useEnglishIngredients);
- });
-}
/*
@@ -484,190 +481,6 @@ function init_weight_log_datepicker()
* D3js functions
*
*/
-// Simple helper function that simply returns the y component of an entry
-function y_value(d) { return d.y; }
-
-function getDate(d) {
- return new Date(d);
-}
-
-function weight_chart(data, width_factor)
-{
- // Return if there is no data to process
- if(data == '')
- {
- return;
- }
-
-
-
- // Calculate the size
- var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
- var height_factor = width_factor / 600 * 400;
- var height2_factor = width_factor / 600 * 390;
- var bottom_factor = width_factor / 600 * 150;
- var top_factor = width_factor / 600 * 290;
-
- var minDate = getDate(data[0].x),
- maxDate = getDate(data[data.length-1].x);
-
- var margin = {top: 10, right: 10, bottom: bottom_factor, left: 40},
- margin2 = {top: top_factor, right: 10, bottom: 50, left: 40},
- width = width_factor - margin.left - margin.right,
- height = height_factor - margin.top - margin.bottom,
- height2 = height2_factor - margin2.top - margin2.bottom;
-
- var x = d3.time.scale()
- .domain([minDate, maxDate])
- .range([0, width]);
- var x2 = d3.time.scale()
- .domain([minDate, maxDate])
- .range([0, width]);
-
- var min_y_value = d3.min(data, y_value) - 1;
- var max_y_value = d3.max(data, y_value) + 1;
-
-
- var y = d3.scale.linear()
- .domain([min_y_value, max_y_value])
- .range([height, 0]);
- var y2 = d3.scale.linear()
- .domain([min_y_value, max_y_value])
- .range([height2, 0]);
-
- var xAxis = d3.svg.axis()
- .scale(x)
- .ticks(6)
- .orient("bottom");
-
- var xAxis2 = d3.svg.axis()
- .scale(x2)
- .ticks(6)
- .orient("bottom");
-
- var yAxis = d3.svg.axis()
- .scale(y)
- .orient("left");
-
- var brush = d3.svg.brush()
- .x(x2)
- .on("brush", brush);
-
- var line = d3.svg.line()
- .x(function(d) { return x(getDate(d.x)); })
- .y(function(d) { return y(d.y); })
- .interpolate('cardinal');
-
- var line2 = d3.svg.line()
- .x(function(d) { return x2(getDate(d.x)); })
- .y(function(d) { return y2(d.y); })
- .interpolate('cardinal');
-
- var area = d3.svg.area()
- .x(line.x())
- .y1(line.y())
- .y0(y(min_y_value))
- .interpolate('cardinal');
-
- var area2 = d3.svg.area()
- .x(line2.x())
- .y1(line2.y())
- .y0(y2(min_y_value))
- .interpolate('cardinal');
-
- // Reset the content of weight_diagram, otherwise if there is a filter
- // a new SVG will be appended to it
- $("#weight_diagram").html("");
-
- var svg = d3.select("#weight_diagram").append("svg")
- .datum(data)
- .attr("width", width + margin.left + margin.right)
- .attr("height", height + margin.top + margin.bottom);
-
- svg.append("defs").append("clipPath")
- .attr("id", "clip")
- .append("rect")
- .attr("width", width)
- .attr("height", height);
-
- var focus = svg.append("g")
- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
-
- var context = svg.append("g")
- .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
-
- focus.append("path")
- .attr("class", "area")
- .attr("clip-path", "url(#clip)")
- .attr("d", area);
-
- focus.append("g")
- .attr("class", "x axis")
- .attr("transform", "translate(0," + height + ")")
- .call(xAxis);
-
- focus.append("g")
- .attr("class", "y axis")
- .call(yAxis);
-
- focus.append("path")
- .attr("class", "line")
- .attr("clip-path", "url(#clip)")
- .attr("d", line);
-
- focus.selectAll(".dot")
- .data(data.filter(function(d) { return d.y; }))
- .enter().append("circle")
- .attr("clip-path", "url(#clip)")
- .attr("class", "dot modal-dialog")
- .attr("href", function(d) { return '/' + get_current_language() + '/weight/' + d.id + '/edit/'; })
- .attr("id", function(d) { return d.id; })
- .attr("cx", line.x())
- .attr("cy", line.y())
- .attr("r", 0)
- .transition() // Animate the data points, "opening" them one after another
- .duration(1000)
- .delay(function(d, i) { return i / data.length * 1600; })
- .attr("r", function(d) { return 5; });
-
-
- context.append("path")
- .attr("class", "area")
- .attr("d", area2);
-
- context.append("path")
- .attr("class", "line")
- .attr("d", line2);
-
- context.append("g")
- .attr("class", "x axis")
- .attr("transform", "translate(0," + height2 + ")")
- .call(xAxis2);
-
- context.append("g")
- .attr("class", "x brush")
- .call(brush)
- .selectAll("rect")
- .attr("y", -6)
- .attr("height", height2 + 7);
-
- function brush() {
- x.domain(brush.empty() ? x2.domain() : brush.extent());
- focus.select("path").attr("d", area);
- focus.select(".line").attr("d", line);
-
- focus.selectAll(".dot")
- .attr("cx", line.x())
- .attr("cy", line.y());
-
- focus.select(".x.axis").call(xAxis);
- }
-
- // Make the circles clickable: open their edit dialog
- form_modal_dialog();
-}
-
-
function weight_log_chart(data, div_id, reps_i18n, width_factor)
{
@@ -1001,4 +814,58 @@ function highlight_muscle(element, perms_exercises)
// Show the corresponding exercises
$('.exercise-list').hide();
$('#' + div_id).show();
-}
\ No newline at end of file
+}
+
+
+/*
+ * 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/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.js
new file mode 100644
index 000000000..7be877ac2
--- /dev/null
+++ b/wger/nutrition/static/js/nutrition.js
@@ -0,0 +1,90 @@
+/*
+ * 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 Nutriton functions
+ */
+
+function update_ingredient_value(url)
+{
+ form_data = $('#nutritional-values-form').serializeArray();
+ $.post(url, form_data, function(data) {
+
+ console.log(data);
+ // Show any validation errors
+ $('#calculator-errors').html('');
+ if (data['errors'])
+ {
+ $.each(data['errors'], function(index, value) {
+ $('#calculator-errors').append('<p class="ym-message">'+value+'</p>');
+ });
+ }
+
+ // Replace the nutritional values
+ $('#value-energy').html(parseFloat(data['energy']).toFixed(2));
+ $('#value-protein').html(parseFloat(data['protein']).toFixed(2));
+ $('#value-carbohydrates').html(parseFloat(data['carbohydrates']).toFixed(2));
+ $('#value-carbohydrates-sugar').html(parseFloat(data['carbohydrates_sugar']).toFixed(2));
+ $('#value-fat').html(parseFloat(data['fat']).toFixed(2));
+ $('#value-fat-saturated').html(parseFloat(data['fat_saturated']).toFixed(2));
+ $('#value-fibres').html(parseFloat(data['fibres']).toFixed(2));
+ $('#value-sodium').html(parseFloat(data['sodium']).toFixed(2));
+ });
+}
+
+function init_ingredient_detail(url)
+{
+ // Prevent the form from being sent
+ $('#nutritional-values-form').submit(function(e) {
+ e.preventDefault();
+ });
+
+ $('#id_amount').keyup(function(){
+ update_ingredient_value(url);
+ });
+
+ $('#id_unit').change(function(){
+ update_ingredient_value(url);
+ });
+}
+
+
+
+/*
+ * Update the user's preferences
+ */
+function init_english_ingredients()
+{
+ $("#ajax-english-ingredients").click(function(e) {
+ e.preventDefault();
+
+ // Update the status icon and the data attribute
+ switch_to = $('#english-ingredients-status').data('showIngredients') == 'true' ? 0 : 1;
+ if ( switch_to == 1 )
+ {
+ $('#english-ingredients-status').attr("src", "/static/images/icons/status-on.svg");
+ $('#english-ingredients-status').data('showIngredients', 'true');
+ }
+ else if ( switch_to == 0 )
+ {
+ $('#english-ingredients-status').attr("src", "/static/images/icons/status-off.svg");
+ $('#english-ingredients-status').data('showIngredients', 'false');
+ }
+
+ // Update the settings
+ $("#ajax-info").load('/' + get_current_language() + "/workout/api/user-preferences?do=set_english-ingredients&show=" + switch_to);
+ });
+}
diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.html
index b5710aec6..68b36f851 100644
--- a/wger/nutrition/templates/ingredient/view.html
+++ b/wger/nutrition/templates/ingredient/view.html
@@ -7,47 +7,10 @@
{% block title %}{{ingredient.name}}{% endblock %}
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
<script>
-function update_value() {
- form_data = $('#nutritional-values-form').serializeArray();
- $.post("{% url 'ingredient-get-values' ingredient.id %}", form_data, function(data) {
-
- // Show any validation errors
- $('#calculator-errors').html('');
- if (data['errors'])
- {
- $.each(data['errors'], function(index, value) {
- $('#calculator-errors').append('<p class="ym-message">'+value+'</p>');
- });
- }
-
- // Replace the nutritional values
- $('#value-energy').html(parseFloat(data['energy']).toFixed(2));
- $('#value-protein').html(parseFloat(data['protein']).toFixed(2));
- $('#value-carbohydrates').html(parseFloat(data['carbohydrates']).toFixed(2));
- $('#value-carbohydrates-sugar').html(parseFloat(data['carbohydrates_sugar']).toFixed(2));
- $('#value-fat').html(parseFloat(data['fat']).toFixed(2));
- $('#value-fat-saturated').html(parseFloat(data['fat_saturated']).toFixed(2));
- $('#value-fibres').html(parseFloat(data['fibres']).toFixed(2));
- $('#value-sodium').html(parseFloat(data['sodium']).toFixed(2));
- });
- }
-
$(document).ready(function() {
-
- // Prevent the form from being sent
- $('#nutritional-values-form').submit(function(e) {
- e.preventDefault();
- });
-
- $('#id_amount').keyup(function(){
- update_value();
- });
-
- $('#id_unit').change(function(){
- update_value();
- });
-
+ init_ingredient_detail("{% url 'ingredient-get-values' ingredient.id %}");
});
</script>
{% endblock %}
diff --git a/wger/nutrition/templates/mobile/ingredient/view.html b/wger/nutrition/templates/mobile/ingredient/view.html
index 6f6847595..8a125b918 100644
--- a/wger/nutrition/templates/mobile/ingredient/view.html
+++ b/wger/nutrition/templates/mobile/ingredient/view.html
@@ -7,47 +7,10 @@
{% block title %}{{ingredient.name}}{% endblock %}
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
<script>
-function update_value() {
- form_data = $('#nutritional-values-form').serializeArray();
- $.post("{% url 'ingredient-get-values' ingredient.id %}", form_data, function(data) {
-
- // Show any validation errors
- $('#calculator-errors').html('');
- if (data['errors'])
- {
- $.each(data['errors'], function(index, value) {
- $('#calculator-errors').append('<p class="ym-message">'+value+'</p>');
- });
- }
-
- // Replace the nutritional values
- $('#value-energy').html(parseFloat(data['energy']).toFixed(2));
- $('#value-protein').html(parseFloat(data['protein']).toFixed(2));
- $('#value-carbohydrates').html(parseFloat(data['carbohydrates']).toFixed(2));
- $('#value-carbohydrates-sugar').html(parseFloat(data['carbohydrates_sugar']).toFixed(2));
- $('#value-fat').html(parseFloat(data['fat']).toFixed(2));
- $('#value-fat-saturated').html(parseFloat(data['fat_saturated']).toFixed(2));
- $('#value-fibres').html(parseFloat(data['fibres']).toFixed(2));
- $('#value-sodium').html(parseFloat(data['sodium']).toFixed(2));
- });
- }
-
$(document).on('pageinit', function(){
-
- // Prevent the form from being sent
- $('#nutritional-values-form').submit(function(e) {
- e.preventDefault();
- });
-
- $('#id_amount').keyup(function(){
- update_value();
- });
-
- $('#id_unit').change(function(){
- update_value();
- });
-
+ init_ingredient_detail("{% url 'ingredient-get-values' ingredient.id %}");
});
</script>
{% endblock %}
diff --git a/wger/nutrition/templates/mobile/plan/view.html b/wger/nutrition/templates/mobile/plan/view.html
index c2e6144da..b440f7c17 100644
--- a/wger/nutrition/templates/mobile/plan/view.html
+++ b/wger/nutrition/templates/mobile/plan/view.html
@@ -7,6 +7,8 @@
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
+
<script>
function custom_modal_init()
{
@@ -17,15 +19,7 @@ function custom_modal_init()
$(document).ready(function() {
// Check if the user wants to see ingredients in english
- set_english_ingredients();
-
- {% if user.userprofile.show_english_ingredients %}
- useEnglishIngredients = 1;
- $('#english-ingredients-status').attr("src", "{% static 'images/icons/status-on.svg' %}");
- {% else %}
- useEnglishIngredients = 0;
- $('#english-ingredients-status').attr("src", "{% static 'images/icons/status-off.svg' %}");
- {% endif %}
+ init_english_ingredients();
/*
* At the moment (Oct. 2013), firefox OS has no possibility to download
@@ -186,11 +180,21 @@ $(document).ready(function() {
<p>
{% trans "If you find the ingredient list too short, you might want to activate the preference to also show English ingredients." %}
<a href="{% url 'preferences' %}" id="ajax-english-ingredients">{% trans "That's done here" %}</a>:
+ {% if user.userprofile.show_english_ingredients %}
<img src="{% static 'images/icons/status-on.svg' %}"
width="20"
height="20"
- alt="{% trans 'Delete nutrition plan' %}"
- id="english-ingredients-status">
+ alt=""
+ id="english-ingredients-status"
+ data-show-ingredients="true">
+ {% else %}
+ <img src="{% static 'images/icons/status-off.svg' %}"
+ width="20"
+ height="20"
+ alt=""
+ id="english-ingredients-status"
+ data-show-ingredients="false">
+ {% endif %}
</p>
{% endif %}
diff --git a/wger/nutrition/templates/mobile/rate/form.html b/wger/nutrition/templates/mobile/rate/form.html
index 6e8950460..7c7659672 100644
--- a/wger/nutrition/templates/mobile/rate/form.html
+++ b/wger/nutrition/templates/mobile/rate/form.html
@@ -6,54 +6,7 @@
{% block header %}
<script>
$(document).ready(function() {
-
- $("#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);
- });
- });
- });
-
+ init_calories_calculator();
});
</script>
{% endblock %}
diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.html
index 15d38ead2..229cbb7af 100644
--- a/wger/nutrition/templates/plan/view.html
+++ b/wger/nutrition/templates/plan/view.html
@@ -7,6 +7,7 @@
{% block header %}
+<script src="{% static 'js/nutrition.js' %}"></script>
<script>
function custom_modal_init()
{
@@ -17,15 +18,7 @@ function custom_modal_init()
$(document).ready(function() {
// Check if the user wants to see ingredients in english
- set_english_ingredients();
-
- {% if user.userprofile.show_english_ingredients %}
- var useEnglishIngredients = 1;
- $('#english-ingredients-status').attr("src", "{% static 'images/icons/status-on.svg' %}");
- {% else %}
- var useEnglishIngredients = 0;
- $('#english-ingredients-status').attr("src", "{% static 'images/icons/status-off.svg' %}");
- {% endif %}
+ init_english_ingredients();
});
</script>
{% endblock %}
@@ -153,11 +146,21 @@ $(document).ready(function() {
<p>
{% trans "If you find the ingredient list too short, you might want to activate the preference to also show English ingredients." %}
<a href="{% url 'preferences' %}" id="ajax-english-ingredients">{% trans "That's done here" %}</a>:
+ {% if user.userprofile.show_english_ingredients %}
<img src="{% static 'images/icons/status-on.svg' %}"
width="20"
height="20"
- alt="{% trans 'Delete nutrition plan' %}"
- id="english-ingredients-status">
+ alt=""
+ id="english-ingredients-status"
+ data-show-ingredients="true">
+ {% else %}
+ <img src="{% static 'images/icons/status-off.svg' %}"
+ width="20"
+ height="20"
+ alt=""
+ id="english-ingredients-status"
+ data-show-ingredients="false">
+ {% endif %}
</p>
{% endif %}
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 0fc11e666..63aa36598 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -6,54 +6,7 @@
{% block header %}
<script>
$(document).ready(function() {
-
- $("#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);
- });
- });
- });
-
+ init_calories_calculator();
});
</script>
{% endblock %}
diff --git a/wger/weight/static/js/weight.js b/wger/weight/static/js/weight.js
new file mode 100644
index 000000000..e3ca482ba
--- /dev/null
+++ b/wger/weight/static/js/weight.js
@@ -0,0 +1,203 @@
+/*
+ * 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 weight functions
+ */
+
+// Simple helper function that simply returns the y component of an entry
+function y_value(d) { return d.y; }
+
+function getDate(d) {
+ return new Date(d);
+}
+
+function weight_chart(data, width_factor)
+{
+ // Return if there is no data to process
+ if(data == '')
+ {
+ return;
+ }
+
+
+
+ // Calculate the size
+ var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;
+ var height_factor = width_factor / 600 * 400;
+ var height2_factor = width_factor / 600 * 390;
+ var bottom_factor = width_factor / 600 * 150;
+ var top_factor = width_factor / 600 * 290;
+
+ var minDate = getDate(data[0].x),
+ maxDate = getDate(data[data.length-1].x);
+
+ var margin = {top: 10, right: 10, bottom: bottom_factor, left: 40},
+ margin2 = {top: top_factor, right: 10, bottom: 50, left: 40},
+ width = width_factor - margin.left - margin.right,
+ height = height_factor - margin.top - margin.bottom,
+ height2 = height2_factor - margin2.top - margin2.bottom;
+
+ var x = d3.time.scale()
+ .domain([minDate, maxDate])
+ .range([0, width]);
+ var x2 = d3.time.scale()
+ .domain([minDate, maxDate])
+ .range([0, width]);
+
+ var min_y_value = d3.min(data, y_value) - 1;
+ var max_y_value = d3.max(data, y_value) + 1;
+
+
+ var y = d3.scale.linear()
+ .domain([min_y_value, max_y_value])
+ .range([height, 0]);
+ var y2 = d3.scale.linear()
+ .domain([min_y_value, max_y_value])
+ .range([height2, 0]);
+
+ var xAxis = d3.svg.axis()
+ .scale(x)
+ .ticks(6)
+ .orient("bottom");
+
+ var xAxis2 = d3.svg.axis()
+ .scale(x2)
+ .ticks(6)
+ .orient("bottom");
+
+ var yAxis = d3.svg.axis()
+ .scale(y)
+ .orient("left");
+
+ var brush = d3.svg.brush()
+ .x(x2)
+ .on("brush", brush);
+
+ var line = d3.svg.line()
+ .x(function(d) { return x(getDate(d.x)); })
+ .y(function(d) { return y(d.y); })
+ .interpolate('cardinal');
+
+ var line2 = d3.svg.line()
+ .x(function(d) { return x2(getDate(d.x)); })
+ .y(function(d) { return y2(d.y); })
+ .interpolate('cardinal');
+
+ var area = d3.svg.area()
+ .x(line.x())
+ .y1(line.y())
+ .y0(y(min_y_value))
+ .interpolate('cardinal');
+
+ var area2 = d3.svg.area()
+ .x(line2.x())
+ .y1(line2.y())
+ .y0(y2(min_y_value))
+ .interpolate('cardinal');
+
+ // Reset the content of weight_diagram, otherwise if there is a filter
+ // a new SVG will be appended to it
+ $("#weight_diagram").html("");
+
+ var svg = d3.select("#weight_diagram").append("svg")
+ .datum(data)
+ .attr("width", width + margin.left + margin.right)
+ .attr("height", height + margin.top + margin.bottom);
+
+ svg.append("defs").append("clipPath")
+ .attr("id", "clip")
+ .append("rect")
+ .attr("width", width)
+ .attr("height", height);
+
+ var focus = svg.append("g")
+ .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
+
+ var context = svg.append("g")
+ .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
+
+ focus.append("path")
+ .attr("class", "area")
+ .attr("clip-path", "url(#clip)")
+ .attr("d", area);
+
+ focus.append("g")
+ .attr("class", "x axis")
+ .attr("transform", "translate(0," + height + ")")
+ .call(xAxis);
+
+ focus.append("g")
+ .attr("class", "y axis")
+ .call(yAxis);
+
+ focus.append("path")
+ .attr("class", "line")
+ .attr("clip-path", "url(#clip)")
+ .attr("d", line);
+
+ focus.selectAll(".dot")
+ .data(data.filter(function(d) { return d.y; }))
+ .enter().append("circle")
+ .attr("clip-path", "url(#clip)")
+ .attr("class", "dot modal-dialog")
+ .attr("href", function(d) { return '/' + get_current_language() + '/weight/' + d.id + '/edit/'; })
+ .attr("id", function(d) { return d.id; })
+ .attr("cx", line.x())
+ .attr("cy", line.y())
+ .attr("r", 0)
+ .transition() // Animate the data points, "opening" them one after another
+ .duration(1000)
+ .delay(function(d, i) { return i / data.length * 1600; })
+ .attr("r", function(d) { return 5; });
+
+
+ context.append("path")
+ .attr("class", "area")
+ .attr("d", area2);
+
+ context.append("path")
+ .attr("class", "line")
+ .attr("d", line2);
+
+ context.append("g")
+ .attr("class", "x axis")
+ .attr("transform", "translate(0," + height2 + ")")
+ .call(xAxis2);
+
+ context.append("g")
+ .attr("class", "x brush")
+ .call(brush)
+ .selectAll("rect")
+ .attr("y", -6)
+ .attr("height", height2 + 7);
+
+ function brush() {
+ x.domain(brush.empty() ? x2.domain() : brush.extent());
+ focus.select("path").attr("d", area);
+ focus.select(".line").attr("d", line);
+
+ focus.selectAll(".dot")
+ .attr("cx", line.x())
+ .attr("cy", line.y());
+
+ focus.select(".x.axis").call(xAxis);
+ }
+
+ // Make the circles clickable: open their edit dialog
+ form_modal_dialog();
+}
+
diff --git a/wger/weight/templates/mobile/weight_overview.html b/wger/weight/templates/mobile/weight_overview.html
index 82f803820..0f13c9127 100644
--- a/wger/weight/templates/mobile/weight_overview.html
+++ b/wger/weight/templates/mobile/weight_overview.html
@@ -14,23 +14,13 @@
{% block header %}
<script src="{% static 'js/d3.js' %}"></script>
<script src="{% static 'js/jquery-globalize/lib/globalize.js' %}"></script>
-
+<script src="{% static 'js/weight.js' %}"></script>
<script type="text/javascript">
$(document).on('pageinit', function () {
- // Load the chart data, without arguments this returns all weight entries
- var date_min = $("#weight_filter_from").val();
- var date_max = $("#weight_filter_to").val();
- var filter_data = {}
- if( date_min && date_max)
- {
- filter_data = {'date_min': date_min,
- 'date_max': date_max}
- }
-
- $.getJSON("/weight/api/get_weight_data/", filter_data, function(json) {
+ $.getJSON("/weight/api/get_weight_data/", function(json) {
weight_chart(json, $('#content').width() * 0.95);
});
});
diff --git a/wger/weight/templates/weight_overview.html b/wger/weight/templates/weight_overview.html
index b5277e2a3..6b6accf68 100644
--- a/wger/weight/templates/weight_overview.html
+++ b/wger/weight/templates/weight_overview.html
@@ -14,7 +14,7 @@
{% block header %}
<script src="{% static 'js/d3.js' %}"></script>
<script src="{% static 'js/jquery-globalize/lib/globalize.js' %}"></script>
-
+<script src="{% static 'js/weight.js' %}"></script>
<script type="text/javascript">
@@ -28,44 +28,9 @@ function custom_modal_init()
$(document).ready(function () {
- // Load the chart data, without arguments this returns all weight entries
- var date_min = $("#weight_filter_from").val();
- var date_max = $("#weight_filter_to").val();
- var filter_data = {}
- if( date_min && date_max)
- {
- filter_data = {'date_min': date_min,
- 'date_max': date_max}
- }
-
- $.getJSON("/weight/api/get_weight_data/", filter_data, function(json) {
+ $.getJSON("/weight/api/get_weight_data/", function(json) {
weight_chart(json);
});
-
- // Add datepickers to the filter elements
-
- $("#weight_filter_from").datepicker({ dateFormat: "yy-mm-dd",
- {% if min_date %}minDate: {{ min_date }}{% endif %}
- });
- $("#weight_filter_to").datepicker({ dateFormat: "yy-mm-dd",
- {% if max_date %}maxDate: {{max_date}}{% endif %}
- });
-
- // Process the filter
- $("#weight_diagram_refresh").click(function(e) {
-
- e.preventDefault();
-
- var date_min = $("#weight_filter_from").val();
- var date_max = $("#weight_filter_to").val();
-
- $.getJSON("/weight/api/get_weight_data/",
- {'date_min': date_min,
- 'date_max': date_max},
- function(json) {
- weight_chart(json);
- });
- });
});
</script>
{% endblock %}