/** * "Who is this for?" picker, shared by the lesson-booking and group-class * registration forms. * * The list arrives from the server already ordered children-first, with the * account holder last, and this module preserves that order: a guardian's * default selection is their first child, never themselves. Booking for the * wrong child is a correctable mistake; quietly enrolling the parent in a class * meant for their kid is not. */ (function () { 'use strict'; function escHtml(str) { return String(str) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } /** * Read the server-rendered student list off a `data-students` attribute. * Anything unparseable degrades to an empty list, which renders no picker * and books for the signed-in user — the pre-guardian behaviour. */ function parseStudents(raw) { if (!raw) return []; try { const list = JSON.parse(raw); return Array.isArray(list) ? list : []; } catch (e) { return []; } } /** * The picker's markup, or an empty string when there is nothing to choose: * an account with only itself on the list never sees the question. */ function selectorHtml(students, id) { if (!students || students.length < 2) return ''; const options = students.map((s) => { // The account holder reads as "Myself" — their own name next to their // children's is ambiguous about which row is the parent. const label = s.is_self ? `Myself (${s.name})` : s.name; return ``; }).join(''); return `

`; } /** * The chosen student id, or 0 when no picker was rendered — the server * reads 0 as "the caller books for themselves". */ function selectedId(id) { const el = document.getElementById(id); return el ? Number(el.value) || 0 : 0; } window.usGuardian = { parseStudents, selectorHtml, selectedId }; }());