Skip to content

Commit

Permalink
Merge pull request #21 from fioreale/13-trim-text-when-adding-new-wor…
Browse files Browse the repository at this point in the history
…kout

fixe workout uplaod and format
  • Loading branch information
fioreale authored Feb 22, 2024
2 parents 8a3d0bc + 76c0b5a commit b25f0c2
Showing 1 changed file with 60 additions and 40 deletions.
100 changes: 60 additions & 40 deletions app/public/assets/JS/sendWorkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ document.addEventListener("DOMContentLoaded", () => {
const sendWorkoutButton = document.querySelector(".sendWorkout");
const buttonExercise = document.querySelector(".addExerciseButton");
const buttonScheda = document.querySelector(".addSchedaButton");
const workoutNameInput = document.querySelector("#input-workout-name");
const workoutNameInput = document.querySelector("#input-workout-name")
const alertText = document.querySelector("#alert-text");
const modalAddWorkout = $("#modalAddWorkout");
const alertModal = $("#alert-modal");

const inputGroupHTML = `
<div class="input-group input-text-exercise mb-1">
<input type="text" aria-label="esercizio" class="form-control" placeholder="esercizio">
<input type="text" aria-label="serie" class="form-control" placeholder="serie">
<input type="text" aria-label="reps" class="form-control" placeholder="reps">
<button type="button" class="btn btn-danger delete-row">Delete</button>
<button type="button" class="btn btn-secondary move-up">&#8593;</button>
<button type="button" class="btn btn-secondary move-down">&#8595;</button>
let currentSchedaName = "A"; // Track the current scheda name

function inputGroupHTML(schedaName) {
return `
<div class="input-group input-text-exercise mb-1" data-scheda="${schedaName}">
<input type="text" aria-label="esercizio" class="form-control" placeholder="esercizio">
<input type="text" aria-label="serie" class="form-control" placeholder="serie">
<input type="text" aria-label="reps" class="form-control" placeholder="reps">
<button type="button" class="btn btn-danger delete-row">Delete</button>
<button type="button" class="btn btn-secondary move-up">&#8593;</button>
<button type="button" class="btn btn-secondary move-down">&#8595;</button>
</div>`;
}

buttonExercise.addEventListener("click", () => {
buttonExercise.insertAdjacentHTML("beforebegin", inputGroupHTML);
// Insert the input group with the current scheda name
buttonExercise.insertAdjacentHTML("beforebegin", inputGroupHTML(currentSchedaName));
buttonScheda.removeAttribute("hidden");
});

buttonScheda.addEventListener("click", () => {
buttonExercise.insertAdjacentHTML("beforebegin", "<hr>");
buttonExercise.insertAdjacentHTML("beforebegin", `<div class="scheda"></div>`);
// Insert a divider and update the current scheda name for future inputs
buttonExercise.insertAdjacentHTML("beforebegin", "<hr class='scheda-divider'>");
currentSchedaName = getNextSchedaName(currentSchedaName);
});

document.addEventListener("click", event => {
const target = event.target;
if (target.classList.contains("delete-row")) {
Expand All @@ -38,11 +42,21 @@ document.addEventListener("DOMContentLoaded", () => {
}
});

document.addEventListener("click", event => {
const target = event.target;
if (target.classList.contains("delete-row")) {
target.closest(".input-group").remove();
} else if (target.classList.contains("move-up") || target.classList.contains("move-down")) {
const isMovingUp = target.classList.contains("move-up");
moveElement(target.closest(".input-group"), isMovingUp);
}
});

sendWorkoutButton.addEventListener("click", async () => {
const listInputWorkouts = document.querySelectorAll(".input-text-exercise");
const workoutJSON = createSchede(listInputWorkouts);

if (!workoutNameInput.value.trim() || !workoutJSON) {
if (!workoutJSON || !workoutNameInput.value.trim().replace(/[^a-zA-Z0-9]/g, '_')) {
alertText.textContent = "Valori Mancanti!";
alertModal.modal("show");
return;
Expand Down Expand Up @@ -76,39 +90,39 @@ document.addEventListener("DOMContentLoaded", () => {

function createSchede(listInputElements) {
let workout = {
name: workoutNameInput.value.trim(),
name: workoutNameInput.value.trim().replace(/[^a-zA-Z0-9]/g, '_'),
schede: [...processSchede(listInputElements)],
};
return JSON.stringify(workout);
}

function processSchede(elements) {
const schede = []; // Array to hold all schede
let currentSchedaExercises = []; // Temp array for current scheda exercises
let schedaName = "A"; // Starting name for scheda, increment alphabetically for each new scheda

elements.forEach((element, index) => {
const exercise = createEx_JSON(element);
currentSchedaExercises.push(exercise);

// If the next element is a divider (e.g., <hr>) or the last element in the list,
// it indicates the end of the current scheda.
const isLastElement = index === elements.length - 1;
const isNextDivider = elements[index + 1] && elements[index + 1].matches(".scheda-divider");
if (isLastElement || isNextDivider) {
schede.push({
name: schedaName,
esercizi: currentSchedaExercises,
});
// Reset for the next scheda
currentSchedaExercises = [];
schedaName = getNextSchedaName(schedaName);
const schedeMap = {}; // Use an object to map scheda names to their exercises

elements.forEach((element) => {
// Only process elements that are exercise inputs
if (element.classList.contains("input-text-exercise")) {
const schedaName = element.dataset.scheda;
const exercise = createEx_JSON(element);

// If the scheda does not exist in the map, create it
if (!schedeMap[schedaName]) {
schedeMap[schedaName] = [];
}

// Add the exercise to the correct scheda in the map
schedeMap[schedaName].push(exercise);
}
});

// Convert the map into an array of schede objects
const schede = Object.keys(schedeMap).map((name) => ({
name: name,
esercizi: schedeMap[name],
}));

return schede;
}

function createEx_JSON(exerciseElement) {
const inputs = exerciseElement.querySelectorAll("input");
const titleEx = inputs[0].value.trim();
Expand All @@ -134,11 +148,17 @@ document.addEventListener("DOMContentLoaded", () => {
return String.fromCharCode(currentName.charCodeAt(0) + 1);
}


function moveElement(element, isMovingUp) {
let swapElement = isMovingUp ? element.previousElementSibling : element.nextElementSibling;
if (swapElement) {
isMovingUp ? swapElement.before(element) : element.before(swapElement);
if (swapElement && swapElement.classList.contains("input-text-exercise")) {
// Perform the swap
isMovingUp ? element.parentNode.insertBefore(element, swapElement) : element.parentNode.insertBefore(swapElement, element);

// Update `data-scheda` attribute if moving across different scheda
if (element.dataset.scheda !== swapElement.dataset.scheda) {
element.dataset.scheda = swapElement.dataset.scheda;
// Optionally, adjust the currentSchedaName if needed
}
}
}
});

0 comments on commit b25f0c2

Please sign in to comment.