Files
gym-bro/frontend/js/modals/cardio-modal.js
Иван Овчинников a38ebbb941 fix(modal): блокировка прокрутки фона, фокус и адаптивный шаг весов
Модальные окна фиксируют прокрутку страницы, числовые поля получают фокус и адаптивный шаг веса, кардио и строки подходов получают сортировку и время подхода.
2026-11-20 00:51:20 +03:00

69 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { showModal, closeModal, stepperHtml } from './modal-base.js';
import { state } from '../state.js';
let _cb = null;
export function showCardioDialog(existing, callback) {
_cb = callback;
const e = existing || {};
const types = [
['вело', '🚴 Вело'],
['ходьба', '🚶 Ходьба'],
['бег', '🏃 Бег'],
['эллипс', '⚡ Эллипс'],
['гребля', '🚣 Гребля'],
];
// Популярные типы — выше: считаем, сколько раз каждый уже использовался
const counts = {};
state.workouts.forEach(w => (w.cardio || []).forEach(c => {
counts[c.type] = (counts[c.type] || 0) + 1;
}));
const orderedTypes = [...types].sort((a, b) => (counts[b[0]] || 0) - (counts[a[0]] || 0));
showModal(`<div class="modal-content">
<h3 class="modal-title">${existing ? 'Редактировать' : 'Новое'} кардио</h3>
<div class="field-row">
<div class="field-group"><label>Тип</label><select id="m-ct">
${orderedTypes.map(([v, label]) => `<option value="${v}" ${e.type === v ? 'selected' : ''}>${label}</option>`).join('')}
</select></div>
<div class="field-group"><label>Назначение</label><select id="m-cp">
<option value="warmup" ${e.purpose==='warmup'?'selected':''}>🔥 Разминка</option>
<option value="cooldown" ${e.purpose==='cooldown'?'selected':''}>🧘 Заминка</option>
<option value="separate" ${e.purpose==='separate'?'selected':''}>➕ Отдельная</option>
</select></div></div>
<div class="field-row-3">
<div class="field-group"><label>Минут</label>${stepperHtml('m-cd', e.duration??'', 1, 'мин', 'numeric')}</div>
<div class="field-group"><label>Дист.</label>${stepperHtml('m-cdi', e.distance??'', 0.1, 'км')}</div>
<div class="field-group"><label>Скор.</label>${stepperHtml('m-cs', e.speed??'', 0.5, 'км/ч')}</div></div>
<div class="field-row">
<div class="field-group"><label>Наклон</label>${stepperHtml('m-ci', e.incline??'', 0.5, '—')}</div>
<div class="field-group"><label>Сопр.</label>${stepperHtml('m-cr', e.resistance??'', 1, '—', 'numeric')}</div></div>
<div class="field-group"><label>Заметка</label><input type="text" id="m-cn" value="${e.notes||''}"></div>
<div class="btn-row">
<button class="btn btn-secondary" onclick="ModalBase.closeOnBg({target:document.querySelector('.modal-overlay')})">Отмена</button>
<button class="btn btn-success" onclick="CardioModal.submit()">Сохранить</button></div>
</div>`);
}
export function submit() {
// 1. Сначала считываем все значения, пока модалка ещё в DOM
const result = {
type: document.getElementById('m-ct').value,
purpose: document.getElementById('m-cp').value,
duration: parseInt(document.getElementById('m-cd').value) || 0,
distance: parseFloat(document.getElementById('m-cdi').value) || 0,
speed: parseFloat(document.getElementById('m-cs').value) || 0,
incline: parseFloat(document.getElementById('m-ci').value) || 0,
resistance: parseInt(document.getElementById('m-cr').value) || 0,
notes: document.getElementById('m-cn').value.trim(),
};
// 2. Только теперь закрываем модалку
closeModal();
// 3. Отдаём результат
_cb?.(result);
}
window.CardioModal = { submit };