feat(exercise-ref): группировка вариантов упражнения с общей историей
Поле group в справочнике, заголовки типа и группы в списке, автодополнение названий групп, объединение прошлых подходов вариантов в статистике упражнения.
This commit is contained in:
@@ -1,20 +1,43 @@
|
||||
import { showModal, closeModal, stepperHtml } from './modal-base.js';
|
||||
import { formatShortDate, formatSet } from '../utils.js';
|
||||
import { showModal, closeModal, stepperHtml, weightStep } from './modal-base.js';
|
||||
import { formatSet, escapeHtml } from '../utils.js';
|
||||
|
||||
let _data = {};
|
||||
let _cb = null;
|
||||
|
||||
export function showSetDialog(existingSet, history, callback) {
|
||||
function formatHistoryDate(iso) {
|
||||
const d = new Date(iso);
|
||||
return d.toLocaleDateString('ru-RU', { day: '2-digit', month: '2-digit', weekday: 'short' });
|
||||
}
|
||||
|
||||
function formatHistoryRow(h, historyLabel) {
|
||||
const sets = h.sets.map((s, i) =>
|
||||
`<span class="history-hint-set">${i + 1}) ${escapeHtml(formatSet(s))}</span>`).join('');
|
||||
const isVariant = historyLabel &&
|
||||
String(h.name || '').trim().toLowerCase() !== String(historyLabel).trim().toLowerCase();
|
||||
const nameLabel = isVariant
|
||||
? `<div class="history-hint-name">${escapeHtml(h.name)}</div>` : '';
|
||||
return `<div class="history-hint-row">
|
||||
<span class="history-hint-date">${formatHistoryDate(h.date)}</span>
|
||||
<span class="history-hint-sets">${nameLabel}${sets}</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object|null} existingSet - редактируемый подход
|
||||
* @param {Array} history - прошлые выполнения [{date, name, sets}]
|
||||
* @param {Function} callback
|
||||
* @param {object} options - { prefill - скопировать значения прошлого подхода,
|
||||
* historyLabel - имя текущего упражнения для подписи вариантов }
|
||||
*/
|
||||
export function showSetDialog(existingSet, history, callback, options = {}) {
|
||||
_cb = callback;
|
||||
const isEdit = !!existingSet;
|
||||
const isDropset = existingSet?.isDropset || false;
|
||||
const source = existingSet || options.prefill || null; // «+ подход» стартует с прошлого веса
|
||||
const isDropset = source?.isDropset || false;
|
||||
|
||||
let historyHtml = '';
|
||||
if (history?.length) {
|
||||
const rows = history.map(h => `<div class="history-hint-row">
|
||||
<span class="history-hint-date">${formatShortDate(h.date)}</span>
|
||||
<span class="history-hint-sets">${h.sets.map(formatSet).join(', ')}</span>
|
||||
</div>`).join('');
|
||||
const rows = history.map(h => formatHistoryRow(h, options.historyLabel)).join('');
|
||||
historyHtml = `<div class="history-hint"><div class="history-hint-title">📊 Прошлые тренировки</div>${rows}</div>`;
|
||||
}
|
||||
|
||||
@@ -26,15 +49,15 @@ export function showSetDialog(existingSet, history, callback) {
|
||||
Это дроп-сет</label></div>
|
||||
<div id="m-set-normal" class="${isDropset ? 'hidden' : ''}">
|
||||
<div class="field-row">
|
||||
<div class="field-group"><label>Вес (кг)</label>${stepperHtml('m-set-weight', existingSet?.weight ?? '', 0.5, 'Вес')}</div>
|
||||
<div class="field-group"><label>Повторения</label>${stepperHtml('m-set-reps', existingSet?.reps ?? '', 1, 'Повт.', 'numeric')}</div>
|
||||
<div class="field-group"><label>Вес (кг)</label>${stepperHtml('m-set-weight', source?.weight ?? '', 0.5, 'Вес', 'decimal', true)}</div>
|
||||
<div class="field-group"><label>Повторения</label>${stepperHtml('m-set-reps', source?.reps ?? '', 1, 'Повт.', 'numeric')}</div>
|
||||
</div></div>
|
||||
<div id="m-set-dropset-block" class="${isDropset ? '' : 'hidden'}">
|
||||
<div id="m-subsets-list"></div>
|
||||
<button class="btn btn-ghost mt-10" style="width:100%;" onclick="SetModal.addSubset()">+ Сбрасывание</button></div>
|
||||
<div class="field-group"><label>Оценка (1-5)</label>
|
||||
<div class="rating-grid" id="m-rating-grid">
|
||||
${[1,2,3,4,5].map(r => `<button class="rating-btn ${existingSet?.rating===r?'active':''}" data-rating="${r}" onclick="SetModal.rating(${r})">${r}</button>`).join('')}
|
||||
${[1,2,3,4,5].map(r => `<button class="rating-btn" data-rating="${r}" onclick="SetModal.rating(${r})">${r}</button>`).join('')}
|
||||
</div></div>
|
||||
<div class="btn-row">
|
||||
<button class="btn btn-secondary" onclick="ModalBase.closeOnBg({target:document.querySelector('.modal-overlay')})">Отмена</button>
|
||||
@@ -42,8 +65,8 @@ export function showSetDialog(existingSet, history, callback) {
|
||||
</div>`);
|
||||
|
||||
_data = {
|
||||
isDropset, rating: existingSet?.rating || 3,
|
||||
subSets: existingSet?.subSets ? JSON.parse(JSON.stringify(existingSet.subSets)) : [],
|
||||
isDropset, rating: source?.rating || 5,
|
||||
subSets: source?.subSets ? JSON.parse(JSON.stringify(source.subSets)) : [],
|
||||
};
|
||||
if (isDropset) { if (!_data.subSets.length) addSubset(); else renderSubsets(); }
|
||||
rating(_data.rating);
|
||||
@@ -64,33 +87,38 @@ export function addSubset() {
|
||||
}
|
||||
|
||||
export function renderSubsets() {
|
||||
document.getElementById('m-subsets-list').innerHTML = _data.subSets.map((s, i) => `
|
||||
document.getElementById('m-subsets-list').innerHTML = _data.subSets.map((s, i) => {
|
||||
const wStep = weightStep(s.weight);
|
||||
return `
|
||||
<div class="stepper-row">
|
||||
<div class="stepper-field">
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'weight', -0.5)">−</button>
|
||||
<input type="number" id="m-sw-${i}" step="0.5" value="${s.weight || ''}" placeholder="Вес" inputmode="decimal" oninput="SetModal.updateSubset(${i}, 'weight', this.value)">
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'weight', 0.5)">+</button>
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'weight', -1)">−</button>
|
||||
<input type="number" id="m-sw-${i}" step="${wStep}" value="${s.weight || ''}" placeholder="Вес" inputmode="decimal"
|
||||
onfocus="ModalBase.focusInput(this)" oninput="SetModal.updateSubset(${i}, 'weight', this.value)">
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'weight', 1)">+</button>
|
||||
</div>
|
||||
<div class="stepper-field">
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'reps', -1)">−</button>
|
||||
<input type="number" id="m-sr-${i}" step="1" value="${s.reps || ''}" placeholder="Повт." inputmode="numeric" oninput="SetModal.updateSubset(${i}, 'reps', this.value)">
|
||||
<input type="number" id="m-sr-${i}" step="1" value="${s.reps || ''}" placeholder="Повт." inputmode="numeric"
|
||||
onfocus="ModalBase.focusInput(this)" oninput="SetModal.updateSubset(${i}, 'reps', this.value)">
|
||||
<button type="button" class="stepper-btn" onclick="SetModal.stepSubset(${i}, 'reps', 1)">+</button>
|
||||
</div>
|
||||
<button class="icon-btn danger" onclick="SetModal.removeSubset(${i})">✕</button>
|
||||
</div>
|
||||
`).join('');
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
export function updateSubset(i, field, value) {
|
||||
_data.subSets[i][field] = value;
|
||||
}
|
||||
|
||||
export function stepSubset(i, field, delta) {
|
||||
export function stepSubset(i, field, dir) {
|
||||
const current = parseFloat(_data.subSets[i][field]) || 0;
|
||||
const step = field === 'weight' ? 0.5 : 1;
|
||||
const newVal = Math.max(0, current + delta);
|
||||
// Вес в сбрасываниях — тоже адаптивный шаг
|
||||
const step = field === 'weight' ? weightStep(current) : 1;
|
||||
const newVal = Math.max(0, current + (dir > 0 ? step : -step));
|
||||
_data.subSets[i][field] = parseFloat((Math.round(newVal / step) * step).toFixed(4));
|
||||
renderSubsets(); // Перерисовываем, чтобы обновить значение в input
|
||||
renderSubsets(); // Перерисовываем, чтобы обновить значение и шаг в input
|
||||
}
|
||||
|
||||
export function removeSubset(i) {
|
||||
@@ -115,7 +143,8 @@ export function submit() {
|
||||
} else {
|
||||
const w = parseFloat(document.getElementById('m-set-weight').value);
|
||||
const r = parseInt(document.getElementById('m-set-reps').value);
|
||||
if (!w && !r) { alert('Укажите вес и повторения'); return; }
|
||||
// Пустой или нулевой должен быть и вес, и повторы — например «вес 0, 15 раз» с собой легитимен
|
||||
if (!(w > 0) && !(r > 0)) { alert('Укажите вес и повторения'); return; }
|
||||
closeModal(); _cb?.({ isDropset: false, weight: w||0, reps: r||0, rating: _data.rating });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user