Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Remove jquery dependency, this is not needed anymore

Roland Geider88f0a63a6b4d
diff --git a/package-lock.json b/package-lock.json
index decf8778e..4949ca281 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,9 +11,8 @@
"dependencies": {
"@popperjs/core": "^2.11.8",
"bootstrap": "5.3.8",
- "datatables.net-bs5": "^2.3.8",
- "htmx.org": "^2.0.8",
- "jquery": "^4.0.0"
+ "datatables.net-bs5": "^3.0.0",
+ "htmx.org": "^2.0.8"
},
"devDependencies": {
"@wger-project/react-components": "26.7.24"
@@ -1667,6 +1666,16 @@
"node": ">=10"
}
},
+ "node_modules/cosmiconfig/node_modules/yaml": {
+ "version": "1.10.3",
+ "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.3.tgz",
+ "integrity": "sha512-vIYeF1u3CjlhAFekPPAk2h/Kv4T3mAkMox5OymRiJQB0spDP10LHvt+K7G9Ny6NuuMAb25/6n1qyUjAcGNf/AA==",
+ "dev": true,
+ "license": "ISC",
+ "engines": {
+ "node": ">= 6"
+ }
+ },
"node_modules/css-box-model": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/css-box-model/-/css-box-model-1.2.1.tgz",
@@ -1824,22 +1833,18 @@
}
},
"node_modules/datatables.net": {
- "version": "2.3.8",
- "resolved": "https://registry.npmjs.org/datatables.net/-/datatables.net-2.3.8.tgz",
- "integrity": "sha512-uhViowhlDlheAuo5a8TrkQqADsjrtGeOyvrigvr4t0+K3MyAWqClORXWAYIcN9VLX6iIX0C8O9gwJNd01hITRg==",
- "license": "MIT",
- "dependencies": {
- "jquery": ">=1.7"
- }
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/datatables.net/-/datatables.net-3.0.0.tgz",
+ "integrity": "sha512-6p19AYaAZEghnlTI3fLNrGFrJ8aioC47E0BQmLT3YnNcL1cZHfIlc2EsN5OL0bljIZRDIXJYoaIr7nB2AZQy1Q==",
+ "license": "MIT"
},
"node_modules/datatables.net-bs5": {
- "version": "2.3.8",
- "resolved": "https://registry.npmjs.org/datatables.net-bs5/-/datatables.net-bs5-2.3.8.tgz",
- "integrity": "sha512-TbFH99QSWm93Kn3teHLFKeyOqYbaiddlHvRFdXUwAvh/fjTMhACWmHG+I43ss8d23OEFHV0WIbN4lpPusZm5zw==",
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/datatables.net-bs5/-/datatables.net-bs5-3.0.0.tgz",
+ "integrity": "sha512-GkvSGsYfhO4lmaM1uyDOCNK5UY1VTr93km5Z6+t0iGM1l1PU4DGrjxFdFB+N5eTKRgGWvRsNng92sdIUfmkTsw==",
"license": "MIT",
"dependencies": {
- "datatables.net": "2.3.8",
- "jquery": ">=1.7"
+ "datatables.net": "3.0.0"
}
},
"node_modules/debug": {
@@ -2410,7 +2415,9 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-4.0.0.tgz",
"integrity": "sha512-TXCHVR3Lb6TZdtw1l3RTLf8RBWVGexdxL6AC8/e0xZKEpBflBsjh9/8LXw+dkNFuOyW9B7iB3O1sP7hS0Kiacg==",
- "license": "MIT"
+ "dev": true,
+ "license": "MIT",
+ "peer": true
},
"node_modules/js-tokens": {
"version": "4.0.0",
@@ -3688,16 +3695,6 @@
}
}
},
- "node_modules/yaml": {
- "version": "1.10.3",
- "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.3.tgz",
- "integrity": "sha512-vIYeF1u3CjlhAFekPPAk2h/Kv4T3mAkMox5OymRiJQB0spDP10LHvt+K7G9Ny6NuuMAb25/6n1qyUjAcGNf/AA==",
- "dev": true,
- "license": "ISC",
- "engines": {
- "node": ">= 6"
- }
- },
"node_modules/yup": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/yup/-/yup-1.7.1.tgz",
diff --git a/package.json b/package.json
index a7c92e656..3f8799155 100644
--- a/package.json
+++ b/package.json
@@ -12,9 +12,8 @@
"dependencies": {
"@popperjs/core": "^2.11.8",
"bootstrap": "5.3.8",
- "datatables.net-bs5": "^2.3.8",
- "htmx.org": "^2.0.8",
- "jquery": "^4.0.0"
+ "datatables.net-bs5": "^3.0.0",
+ "htmx.org": "^2.0.8"
},
"devDependencies": {
"@wger-project/react-components": "26.7.24"
diff --git a/wger/core/templates/rest_framework/api.html b/wger/core/templates/rest_framework/api.html
index 0ac26ecf4..5b3e5011d 100644
--- a/wger/core/templates/rest_framework/api.html
+++ b/wger/core/templates/rest_framework/api.html
@@ -2,12 +2,11 @@
{% load rest_framework wger_extras django_bootstrap_breadcrumbs static crispy_forms_tags i18n %}
{% block header %}
- <script src="{% static 'node/jquery/dist/jquery.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'rest_framework/css/prettify.css' %}"/>
<script src="{% static 'rest_framework/js/prettify-min.js' %}"></script>
<script>
- $(document).ready(function () {
+ document.addEventListener('DOMContentLoaded', function () {
prettyPrint();
});
</script>
diff --git a/wger/core/templates/user/overview.html b/wger/core/templates/user/overview.html
index 1a04a4072..97bd270d4 100644
--- a/wger/core/templates/user/overview.html
+++ b/wger/core/templates/user/overview.html
@@ -1,12 +1,8 @@
{% extends "base.html" %}
-{% load i18n static wger_extras django_bootstrap_breadcrumbs %}
+{% load i18n wger_extras django_bootstrap_breadcrumbs %}
{% block title %}{{current_user|format_username}}{% endblock %}
-{% block header %}
- <script src="{% static 'node/jquery/dist/jquery.js' %}"></script>
-{% endblock %}
-
{% block breadcrumbs %}
{% if current_user.userprofile.gym %}
{{ block.super }}
diff --git a/wger/gym/templates/gym/partial_user_list.html b/wger/gym/templates/gym/partial_user_list.html
index 5a8338cf0..6fc722458 100644
--- a/wger/gym/templates/gym/partial_user_list.html
+++ b/wger/gym/templates/gym/partial_user_list.html
@@ -1,19 +1,22 @@
{% load i18n static %}
<link rel="stylesheet" type="text/css" href="{% static 'node/datatables.net-bs5/css/dataTables.bootstrap5.css' %}">
-<script src="{% static 'node/jquery/dist/jquery.js' %}"></script>
<script src="{% static 'node/datatables.net/js/dataTables.js' %}" ></script>
<script src="{% static 'node/datatables.net-bs5/js/dataTables.bootstrap5.js' %}" ></script>
<script>
- $(document).ready(function () {
- $('#main_member_list').DataTable({
+ document.addEventListener('DOMContentLoaded', function () {
+ const title = document.createElement('h4');
+ title.textContent = '{% translate "Members" %}';
+
+ new DataTable('#main_member_list', {
paging: false,
- bFilter: true,
- bInfo: false,
- dom: '<"datatable-header d-flex justify-content-between align-items-center"f>t<"datatable-footer"ip>', // Layout of elements
- initComplete: function () {
- const title = $('<div class="datatable-title"><h4>Members</h4></div>');
- $(".datatable-header").prepend(title);
+ searching: true,
+ info: false,
+ layout: {
+ topStart: title,
+ topEnd: 'search',
+ bottomStart: null,
+ bottomEnd: null
}
});
});
diff --git a/wger/nutrition/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.js
index 71a38db4d..fc04861b8 100644
--- a/wger/nutrition/static/js/nutrition.js
+++ b/wger/nutrition/static/js/nutrition.js
@@ -20,41 +20,57 @@
'use strict';
+function wgerGetCsrfToken() {
+ return document.querySelector('[name=csrfmiddlewaretoken]').value;
+}
+
function updateIngredientValue(url) {
- let formData = $('#nutritional-values-form').serializeArray();
- $.get(url, formData, 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-energy-kjoule').html(parseFloat(data.energy * 4.184).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-fiber').html(parseFloat(data.fiber).toFixed(2));
- $('#value-sodium').html(parseFloat(data.sodium).toFixed(2));
- });
+ let form = document.getElementById('nutritional-values-form');
+ let params = new URLSearchParams(new FormData(form));
+
+ fetch(url + '?' + params.toString())
+ .then((response) => response.json())
+ .then(function (data) {
+ // Show any validation errors
+ let errorContainer = document.getElementById('calculator-errors');
+ errorContainer.innerHTML = '';
+ if (data.errors) {
+ Object.values(data.errors).forEach(function (value) {
+ errorContainer.innerHTML += '<p class="ym-message">' + value + '</p>';
+ });
+ }
+
+ // Replace the nutritional values. Fields without a value (e.g. sugar or
+ // sodium) have no element in the page, those are simply skipped.
+ let setValue = function (id, value) {
+ let element = document.getElementById(id);
+ if (element) {
+ element.textContent = parseFloat(value).toFixed(2);
+ }
+ };
+ setValue('value-energy', data.energy);
+ setValue('value-energy-kjoule', data.energy * 4.184);
+ setValue('value-protein', data.protein);
+ setValue('value-carbohydrates', data.carbohydrates);
+ setValue('value-carbohydrates-sugar', data.carbohydrates_sugar);
+ setValue('value-fat', data.fat);
+ setValue('value-fat-saturated', data.fat_saturated);
+ setValue('value-fiber', data.fiber);
+ setValue('value-sodium', data.sodium);
+ });
}
function wgerInitIngredientDetail(url) {
// Prevent the form from being sent
- $('#nutritional-values-form').submit(function (e) {
+ document.getElementById('nutritional-values-form').addEventListener('submit', function (e) {
e.preventDefault();
});
- $('#id_amount').keyup(function () {
+ document.getElementById('id_amount').addEventListener('keyup', function () {
updateIngredientValue(url);
});
- $('#id_unit').change(function () {
+ document.getElementById('id_unit').addEventListener('change', function () {
updateIngredientValue(url);
});
}
@@ -64,65 +80,64 @@ function wgerInitIngredientDetail(url) {
* Calories calculator
*/
function wgerInitCaloriesCalculator() {
- $('#form-transfer-calories').click(function (e) {
- let baseCalories;
+ document.getElementById('form-transfer-calories').addEventListener('click', function (e) {
e.preventDefault();
- baseCalories = Number($('#id_base_calories').html());
- $('#id_calories').val(baseCalories);
+ let baseCalories = Number(document.getElementById('id_base_calories').textContent);
+ document.getElementById('id_calories').value = baseCalories;
});
- $('#add-calories-total').click(function (e) {
- let additionalCalories;
- let baseCalories;
+ document.getElementById('add-calories-total').addEventListener('click', function (e) {
e.preventDefault();
- baseCalories = Number($('#id_base_calories').html());
- additionalCalories = Number($('#id_additional_calories').val());
- $('#id_calories').val(baseCalories + additionalCalories);
+ let baseCalories = Number(document.getElementById('id_base_calories').textContent);
+ let additionalCalories = Number(document.getElementById('id_additional_calories').value);
+ document.getElementById('id_calories').value = baseCalories + additionalCalories;
});
- $('#form-update-calories').click(function (e) {
+ document.getElementById('form-update-calories').addEventListener('click', function (e) {
e.preventDefault();
- // Get own ID and update the user profile
- $.get('/api/v2/userprofile', function () {
- }).done(function (userprofile) {
- let totalCalories = $('#id_calories')[0].value;
- $.ajax({
- url: '/api/v2/userprofile/' + userprofile.results[0].user + '/',
- type: 'PATCH',
- data: {calories: totalCalories}
- });
+ // The userprofile endpoint always operates on the current user's profile
+ // and updates are done with a POST request, no ID needed
+ fetch('/api/v2/userprofile/', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'X-CSRFToken': wgerGetCsrfToken()
+ },
+ body: new URLSearchParams({calories: document.getElementById('id_calories').value})
});
});
- $('.calories-autoform').click(function (e) {
- let $bmrForm;
- let bmrUrl;
- e.preventDefault();
-
- // BMR
- $bmrForm = $('#bmr-form');
- bmrUrl = $bmrForm.attr('action');
- $.post(bmrUrl,
- $bmrForm.serialize(),
- function (data) {
- let $activitiesForm;
- let activitiesUrl;
- $('#bmr-result-container').show();
- $('#bmr-result-value').html(data.bmr);
-
- // Activities
- $activitiesForm = $('#activities-form');
- activitiesUrl = $activitiesForm.attr('action');
- $.post(activitiesUrl,
- $activitiesForm.serialize(),
- function (activitiesData) {
- $('#activities-result-container').show();
- $('#activities-result-value').html(activitiesData.factor);
-
- // Total calories
- $('#id_base_calories').html(activitiesData.activities);
- });
- });
+ document.querySelectorAll('.calories-autoform').forEach(function (form) {
+ form.addEventListener('click', function (e) {
+ e.preventDefault();
+
+ // BMR
+ let bmrForm = document.getElementById('bmr-form');
+ fetch(bmrForm.getAttribute('action'), {
+ method: 'POST',
+ body: new URLSearchParams(new FormData(bmrForm))
+ })
+ .then((response) => response.json())
+ .then(function (data) {
+ document.getElementById('bmr-result-container').style.display = '';
+ document.getElementById('bmr-result-value').innerHTML = data.bmr;
+
+ // Activities
+ let activitiesForm = document.getElementById('activities-form');
+ fetch(activitiesForm.getAttribute('action'), {
+ method: 'POST',
+ body: new URLSearchParams(new FormData(activitiesForm))
+ })
+ .then((response) => response.json())
+ .then(function (activitiesData) {
+ document.getElementById('activities-result-container').style.display = '';
+ document.getElementById('activities-result-value').innerHTML = activitiesData.factor;
+
+ // Total calories
+ document.getElementById('id_base_calories').innerHTML = activitiesData.activities;
+ });
+ });
+ });
});
}
diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.html
index 8195a283b..841f2a3b3 100644
--- a/wger/nutrition/templates/ingredient/view.html
+++ b/wger/nutrition/templates/ingredient/view.html
@@ -4,9 +4,8 @@
{% block title %}{{ingredient.name}}{% endblock %}
{% block header %}
-<script src="{% static 'node/jquery/dist/jquery.js' %}"></script>
<script>
-$(document).ready(function() {
+document.addEventListener('DOMContentLoaded', function() {
wgerInitIngredientDetail("{% url 'api-ingredient-get-values' ingredient.id %}");
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(el => new bootstrap.Tooltip(el));
});
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 9ecd626dd..8566be942 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -4,9 +4,8 @@
{% load crispy_forms_tags %}
{% block header %}
-<script src="{% static 'node/jquery/dist/jquery.js' %}"></script>
<script>
-$(document).ready(function() {
+document.addEventListener('DOMContentLoaded', function() {
wgerInitCaloriesCalculator();
});
</script>