docs: explain calculator form helpers

This commit is contained in:
nyan404 committed 2026-08-25 04:22:57 +03:00
1 parent 3989cdc4e3
commit 6118b20448
1 file changed
+12
+12
View File
@@ -219,6 +219,7 @@
let mode = 'coaching'; let mode = 'coaching';
const extraKittens = document.querySelector('#extraKittens'); const extraKittens = document.querySelector('#extraKittens');
const statusHelp = document.querySelector('#statusHelp'); const statusHelp = document.querySelector('#statusHelp');
// Adds one repeatable kitten row and optionally fills it from saved values.
function addExtraKitten(values = {}) { function addExtraKitten(values = {}) {
const row = document.createElement('div'); row.className = 'extra-kitten'; const row = document.createElement('div'); row.className = 'extra-kitten';
row.innerHTML = `<input data-extra="name" placeholder="Имя котёнка" required> row.innerHTML = `<input data-extra="name" placeholder="Имя котёнка" required>
@@ -233,7 +234,9 @@
extraKittens.append(row); extraKittens.append(row);
} }
document.querySelector('#addKitten').addEventListener('click', () => addExtraKitten()); document.querySelector('#addKitten').addEventListener('click', () => addExtraKitten());
// Returns all independence controls participating in the current coaching form.
function statusFields() { return [document.querySelector('#coaching [name="kitten_independence"]'), ...extraKittens.querySelectorAll('[data-extra="independence"]')]; } function statusFields() { return [document.querySelector('#coaching [name="kitten_independence"]'), ...extraKittens.querySelectorAll('[data-extra="independence"]')]; }
// Enables or locks independence controls according to the selected skill.
function updateStatusRules(event) { function updateStatusRules(event) {
const fields = statusFields(); const fields = statusFields();
const isZu = document.querySelector('#coaching [name="skill"]').value === 'ЗУ'; const isZu = document.querySelector('#coaching [name="skill"]').value === 'ЗУ';
@@ -245,6 +248,7 @@
statusHelp.textContent = 'Для ЛУ можно указать не более одного несамостоятельного котёнка.'; statusHelp.textContent = 'Для ЛУ можно указать не более одного несамостоятельного котёнка.';
} }
} }
// Returns a user-facing validation error when kitten statuses conflict.
function validateStatusRules() { function validateStatusRules() {
const fields = statusFields(); const fields = statusFields();
const skill = document.querySelector('#coaching [name="skill"]').value; const skill = document.querySelector('#coaching [name="skill"]').value;
@@ -252,6 +256,7 @@
if (skill === 'ЛУ' && fields.filter(field => field.value === 'несамостоятельно').length > 1) return 'Для ЛУ допускается только один несамостоятельный котёнок.'; if (skill === 'ЛУ' && fields.filter(field => field.value === 'несамостоятельно').length > 1) return 'Для ЛУ допускается только один несамостоятельный котёнок.';
return ''; return '';
} }
// Switches the visible calculator panel and disables fields in hidden panels.
function selectMode(nextMode) { function selectMode(nextMode) {
mode = nextMode; mode = nextMode;
document.querySelectorAll('[data-mode]').forEach(item => item.classList.toggle('active', item.dataset.mode === mode)); document.querySelectorAll('[data-mode]').forEach(item => item.classList.toggle('active', item.dataset.mode === mode));
@@ -267,6 +272,7 @@
document.querySelectorAll('[data-mode]').forEach(button => button.addEventListener('click', () => selectMode(button.dataset.mode))); document.querySelectorAll('[data-mode]').forEach(button => button.addEventListener('click', () => selectMode(button.dataset.mode)));
document.querySelector('#coaching [name="skill"]').addEventListener('change', updateStatusRules); document.querySelector('#coaching [name="skill"]').addEventListener('change', updateStatusRules);
document.querySelector('#coaching [name="kitten_independence"]').addEventListener('change', updateStatusRules); document.querySelector('#coaching [name="kitten_independence"]').addEventListener('change', updateStatusRules);
// Writes a value into a named field inside a calculator panel.
function setField(panel, name, value) { function setField(panel, name, value) {
const field = document.querySelector(`#${panel} [name="${name}"]`); const field = document.querySelector(`#${panel} [name="${name}"]`);
if (field && value !== undefined && value !== null) field.value = value; if (field && value !== undefined && value !== null) field.value = value;
@@ -328,6 +334,7 @@
identityDialog.close(); identityStatus.textContent = 'Связка добавлена в текущий процесс.'; identityDialog.close(); identityStatus.textContent = 'Связка добавлена в текущий процесс.';
}); });
document.querySelector('#closeIdentityDialog').addEventListener('click', () => identityDialog.close()); document.querySelector('#closeIdentityDialog').addEventListener('click', () => identityDialog.close());
// Serializes the active calculator form into the API payload shape.
function payload() { function payload() {
const data = {}; const data = {};
document.querySelectorAll(`#${mode} [name]`).forEach(field => { data[field.name] = field.value; }); document.querySelectorAll(`#${mode} [name]`).forEach(field => { data[field.name] = field.value; });
@@ -349,6 +356,7 @@
} }
return data; return data;
} }
// Uploads selected screenshots and returns attachment descriptors for the API.
async function uploadScreenshots() { async function uploadScreenshots() {
const files = [...(screenshots.files || [])]; const files = [...(screenshots.files || [])];
const uploaded = []; const uploaded = [];
@@ -392,6 +400,7 @@
const applicationFeathers = document.querySelector('#applicationFeathers'); const applicationFeathers = document.querySelector('#applicationFeathers');
let applicationType = 'feather_donation'; let applicationType = 'feather_donation';
// Changes between reports and applications while optionally updating the URL.
function setSection(nextSection, updateUrl = true) { function setSection(nextSection, updateUrl = true) {
const isApplications = nextSection === 'applications'; const isApplications = nextSection === 'applications';
reportsSection.classList.toggle('hidden', isApplications); reportsSection.classList.toggle('hidden', isApplications);
@@ -411,6 +420,7 @@
sectionTabs.forEach(tab => tab.addEventListener('click', () => setSection(tab.dataset.section))); sectionTabs.forEach(tab => tab.addEventListener('click', () => setSection(tab.dataset.section)));
window.addEventListener('popstate', () => setSection(new URLSearchParams(window.location.search).get('section') === 'applications' ? 'applications' : 'reports', false)); window.addEventListener('popstate', () => setSection(new URLSearchParams(window.location.search).get('section') === 'applications' ? 'applications' : 'reports', false));
// Adds and initializes one feather row in the application form.
function addApplicationFeather(values = {}) { function addApplicationFeather(values = {}) {
const row = document.createElement('div'); const row = document.createElement('div');
row.className = 'feather'; row.className = 'feather';
@@ -421,11 +431,13 @@
} }
document.querySelector('#applicationAddFeather').addEventListener('click', () => addApplicationFeather()); document.querySelector('#applicationAddFeather').addEventListener('click', () => addApplicationFeather());
// Toggles visibility and disabled state for an application subsection.
function setApplicationFieldsVisible(selector, visible) { function setApplicationFieldsVisible(selector, visible) {
const section = document.querySelector(selector); const section = document.querySelector(selector);
section.classList.toggle('hidden', !visible); section.classList.toggle('hidden', !visible);
section.querySelectorAll('input, select, textarea, button').forEach(field => { field.disabled = !visible; }); section.querySelectorAll('input, select, textarea, button').forEach(field => { field.disabled = !visible; });
} }
// Synchronizes application subsection visibility with the selected type.
function syncApplicationForm() { function syncApplicationForm() {
const isFeather = applicationType === 'feather_donation'; const isFeather = applicationType === 'feather_donation';
setApplicationFieldsVisible('#applicationFeatherFields', isFeather); setApplicationFieldsVisible('#applicationFeatherFields', isFeather);