Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SPORTS,
calculate,
calculateComparison,
missingComparisonFields,
sportDescriptions,
validateComparison,
validateMethod,
Expand Down Expand Up @@ -81,6 +82,10 @@ function renderComparison(results, sport, { scroll = false } = {}) {
if (scroll) $('#comparison').scrollIntoView({ behavior: 'smooth', block: 'start' });
}

function showAllInputs() {
['max', 'hrr', 'lthr'].forEach((name) => $(`#inputs-${name}`).classList.remove('hidden'));
}

function switchInputs(method) {
['max', 'hrr', 'lthr'].forEach((name) => $(`#inputs-${name}`).classList.toggle('hidden', name !== method));
const help = {
Expand All @@ -99,9 +104,17 @@ function syncComparisonSport() {

function runComparison({ scroll = true } = {}) {
const values = formValues();
const missing = missingComparisonFields(values);
if (missing.length) {
showAllInputs();
showError(`对比三种方法需要完整数据,请先填写:${missing.join('、')}。`);
$('#calculator-form').scrollIntoView({ behavior: 'smooth', block: 'start' });
return;
}

const issues = validateComparison(values);
if (issues.length) {
showError(`对比前请先补全数据:${issues.map(({ method, error }) => `${METHODS[method]}:${error}`).join(' ')}`);
showError(`对比三种方法的数据有误:${issues.map(({ method, error }) => `${METHODS[method]}:${error}`).join(' ')}`);
$('#calculator-form').scrollIntoView({ behavior: 'smooth', block: 'start' });
return;
}
Expand Down
13 changes: 13 additions & 0 deletions calculations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ export function validateComparison(values = {}) {
.filter(({ error }) => error);
}

export const COMPARISON_FIELDS = [
{ key: 'age', label: '年龄' },
{ key: 'maxHr', label: '最大心率' },
{ key: 'restingHr', label: '静息心率' },
{ key: 'lthr', label: '乳酸阈值心率' },
];

export function missingComparisonFields(values = {}) {
return COMPARISON_FIELDS
.filter(({ key }) => Number.isNaN(toNumber(values[key])))
.map(({ label }) => label);
}

function basisFor(method, values, maxHrBase, base) {
if (method === 'max') {
return [
Expand Down
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ <h1>让每一次训练,<br /><em>都在正确的区间。</em></h1>
<div class="input-panel">
<div class="input-intro"><span class="step">01</span><div><h3>输入你的数据</h3><p id="method-help">使用年龄估算最大心率。推荐使用 Tanaka 公式:208 − 0.7 × 年龄。</p></div></div>
<div class="inputs" id="inputs-max">
<label class="field"><span>年龄</span><div class="input-wrap"><input id="age" type="number" min="13" max="100" value="30" inputmode="numeric" /><span>岁</span></div></label>
<label class="field"><span>年龄</span><div class="input-wrap"><input id="age" type="number" min="13" max="100" placeholder="如 30" inputmode="numeric" /><span>岁</span></div></label>
<label class="field"><span>最大心率公式</span><select id="max-formula"><option value="tanaka">Tanaka · 208 − 0.7 × 年龄</option><option value="fox">Fox · 220 − 年龄</option></select></label>
</div>
<div class="inputs hidden" id="inputs-hrr">
<label class="field"><span>最大心率</span><div class="input-wrap"><input id="max-hr" type="number" min="100" max="240" value="187" inputmode="numeric" /><span>bpm</span></div></label>
<label class="field"><span>静息心率</span><div class="input-wrap"><input id="resting-hr" type="number" min="30" max="120" value="58" inputmode="numeric" /><span>bpm</span></div></label>
<label class="field"><span>最大心率</span><div class="input-wrap"><input id="max-hr" type="number" min="100" max="240" placeholder="如 187" inputmode="numeric" /><span>bpm</span></div></label>
<label class="field"><span>静息心率</span><div class="input-wrap"><input id="resting-hr" type="number" min="30" max="120" placeholder="如 58" inputmode="numeric" /><span>bpm</span></div></label>
</div>
<div class="inputs hidden" id="inputs-lthr">
<label class="field"><span>乳酸阈值心率</span><div class="input-wrap"><input id="lthr" type="number" min="100" max="230" value="168" inputmode="numeric" /><span>bpm</span></div></label>
<label class="field"><span>乳酸阈值心率</span><div class="input-wrap"><input id="lthr" type="number" min="100" max="230" placeholder="如 168" inputmode="numeric" /><span>bpm</span></div></label>
<div class="field field-note"><span>没有测试数据?</span><p>可用 30 分钟全力测试的平均心率作为近似值。</p></div>
</div>
<div class="action-row"><button class="primary-button" type="submit">计算我的训练区间 <span>→</span></button><button class="secondary-button" id="compare-button" type="button">对比三种方法 <span>+</span></button></div>
Expand Down
Loading
Loading