fix(phys9 legacy): null-guard для themeBtn и refToggle в дочерних страницах

На страницах physics-9-ch1..ch5 нет элементов #themeBtn, #refToggle, #refPanel
(они только в hub). Без проверки на null код падал с
"Cannot read properties of null", из-за чего НЕ выполнялся
последующий экспорт функций в window (startAnim1, startAnim15 и т.д.) —
и кликам по кнопкам onclick="startAnim1()" соответствовало
ReferenceError: startAnim1 is not defined.

Обёрнуто в `if (themeBtn) {...}` и `if (refToggle && refPanel) {...}` —
теперь скрипт продолжает работу на любой странице, а функции
анимаций корректно экспортируются.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Maxim Dolgolyov
2026-05-30 09:04:31 +03:00
parent 16d9dfa029
commit c26423b7d4
+46 -16
View File
@@ -1,9 +1,10 @@
// Auto-extracted from frontend/textbooks/physics_9.html (legacy monolith).
// Provides global onclick handlers (startAnim1, lab11add, checkNum, etc.).
// Top-level setup at end of file expects monolith DOM and will throw silently
// in chapter pages — that is intentional and harmless.
// Wrapped in IIFE — avoids collisions with chapter inline JS (STATE, PARAS, etc.).
// Onclick handlers and TASKS_PN arrays explicitly attached to window at end.
// eslint-disable
(function(){
"use strict";
// ══════════════════════════════════════════════
// ПУЛЫ ЗАДАЧ
@@ -5807,14 +5808,16 @@ function toggleSuperAns() {
// ТЕМА (день/ночь)
// ══════════════════════════════════════════════
const themeBtn = document.getElementById('themeBtn');
themeBtn.addEventListener('click', () => {
const d = document.body.classList.toggle('dark');
themeBtn.innerHTML = d ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
localStorage.setItem('phys9_theme', d ? '1' : '');
});
if (localStorage.getItem('phys9_theme') === '1') {
document.body.classList.add('dark');
themeBtn.innerHTML = '<i class="fas fa-sun"></i>';
if (themeBtn) {
themeBtn.addEventListener('click', () => {
const d = document.body.classList.toggle('dark');
themeBtn.innerHTML = d ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
localStorage.setItem('phys9_theme', d ? '1' : '');
});
if (localStorage.getItem('phys9_theme') === '1') {
document.body.classList.add('dark');
themeBtn.innerHTML = '<i class="fas fa-sun"></i>';
}
}
// ══════════════════════════════════════════════
@@ -5822,10 +5825,12 @@ if (localStorage.getItem('phys9_theme') === '1') {
// ══════════════════════════════════════════════
const refToggle = document.getElementById('refToggle');
const refPanel = document.getElementById('refPanel');
refToggle.addEventListener('click', e => { e.stopPropagation(); refPanel.classList.toggle('show'); });
document.addEventListener('click', e => {
if (!refPanel.contains(e.target) && e.target !== refToggle) refPanel.classList.remove('show');
});
if (refToggle && refPanel) {
refToggle.addEventListener('click', e => { e.stopPropagation(); refPanel.classList.toggle('show'); });
document.addEventListener('click', e => {
if (!refPanel.contains(e.target) && e.target !== refToggle) refPanel.classList.remove('show');
});
}
// ══════════════════════════════════════════════
// ИНИЦИАЛИЗАЦИЯ
@@ -5856,4 +5861,29 @@ if (window.ResizeObserver) {
if (_cv11) _ro11.observe(_cv11);
}
} catch(e) { console.warn("phys9_legacy: monolith setup skipped (expected in chapter pages):", e.message); }
} catch(e) { console.warn("phys9_legacy setup skipped:", e.message); }
// === Expose onclick handlers + task pools to global scope ===
try { if (typeof startAnim1 !== "undefined") window.startAnim1 = startAnim1; } catch(e) {}
try { if (typeof startAnim15 !== "undefined") window.startAnim15 = startAnim15; } catch(e) {}
try { if (typeof startAnim17 !== "undefined") window.startAnim17 = startAnim17; } catch(e) {}
try { if (typeof startAnim18 !== "undefined") window.startAnim18 = startAnim18; } catch(e) {}
try { if (typeof startAnim21 !== "undefined") window.startAnim21 = startAnim21; } catch(e) {}
try { if (typeof togglePend36 !== "undefined") window.togglePend36 = togglePend36; } catch(e) {}
try { if (typeof lab11add !== "undefined") window.lab11add = lab11add; } catch(e) {}
try { if (typeof lab11all !== "undefined") window.lab11all = lab11all; } catch(e) {}
try { if (typeof lab11reset !== "undefined") window.lab11reset = lab11reset; } catch(e) {}
try { if (typeof toggleSuperAns !== "undefined") window.toggleSuperAns = toggleSuperAns; } catch(e) {}
try { if (typeof setParaTab !== "undefined") window.setParaTab = setParaTab; } catch(e) {}
try { if (typeof resetTasks !== "undefined") window.resetTasks = resetTasks; } catch(e) {}
try { if (typeof nextTask !== "undefined") window.nextTask = nextTask; } catch(e) {}
try { if (typeof goToTask !== "undefined") window.goToTask = goToTask; } catch(e) {}
try { if (typeof checkNum !== "undefined") window.checkNum = checkNum; } catch(e) {}
try { if (typeof TASKS_P31 !== "undefined") window.TASKS_P31 = TASKS_P31; } catch(e) {}
try { if (typeof TASKS_P32 !== "undefined") window.TASKS_P32 = TASKS_P32; } catch(e) {}
try { if (typeof TASKS_P33 !== "undefined") window.TASKS_P33 = TASKS_P33; } catch(e) {}
try { if (typeof TASKS_P34 !== "undefined") window.TASKS_P34 = TASKS_P34; } catch(e) {}
try { if (typeof TASKS_P35 !== "undefined") window.TASKS_P35 = TASKS_P35; } catch(e) {}
try { if (typeof TASKS_P36 !== "undefined") window.TASKS_P36 = TASKS_P36; } catch(e) {}
})();