From c26423b7d419c91bde386689cfadae0f03ca864b Mon Sep 17 00:00:00 2001 From: Maxim Dolgolyov Date: Sat, 30 May 2026 09:04:31 +0300 Subject: [PATCH] =?UTF-8?q?fix(phys9=20legacy):=20null-guard=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20themeBtn=20=D0=B8=20refToggle=20=D0=B2=20=D0=B4?= =?UTF-8?q?=D0=BE=D1=87=D0=B5=D1=80=D0=BD=D0=B8=D1=85=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=86=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit На страницах 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 --- frontend/js/phys9_legacy.js | 62 +++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/frontend/js/phys9_legacy.js b/frontend/js/phys9_legacy.js index be4a32e..d75d4f1 100644 --- a/frontend/js/phys9_legacy.js +++ b/frontend/js/phys9_legacy.js @@ -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 ? '' : ''; - localStorage.setItem('phys9_theme', d ? '1' : ''); -}); -if (localStorage.getItem('phys9_theme') === '1') { - document.body.classList.add('dark'); - themeBtn.innerHTML = ''; +if (themeBtn) { + themeBtn.addEventListener('click', () => { + const d = document.body.classList.toggle('dark'); + themeBtn.innerHTML = d ? '' : ''; + localStorage.setItem('phys9_theme', d ? '1' : ''); + }); + if (localStorage.getItem('phys9_theme') === '1') { + document.body.classList.add('dark'); + themeBtn.innerHTML = ''; + } } // ══════════════════════════════════════════════ @@ -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) {} +})();