@@ -8888,14 +8888,43 @@ var ScenarioPlanner = (function () {
88888888 }
88898889 ] ;
88908890
8891+ // DOMUtil.escapeHtml is provided by dom-utils.js (loaded before this module in build order)
8892+ var escapeHtml = DOMUtil . escapeHtml ;
8893+
88918894 var running = false ;
88928895 var timers = [ ] ;
88938896
8897+ // Cached DOM references — resolved once in init(), avoids
8898+ // repeated getElementById calls in startPlanning/resetPlanner.
8899+ var _els = null ;
8900+
8901+ function _getEls ( ) {
8902+ if ( ! _els ) {
8903+ _els = {
8904+ workspace : document . getElementById ( 'scenarioWorkspace' ) ,
8905+ goal : document . getElementById ( 'scenarioGoal' ) ,
8906+ status : document . getElementById ( 'scenarioAgentStatus' ) ,
8907+ phases : document . getElementById ( 'scenarioPhases' ) ,
8908+ summary : document . getElementById ( 'scenarioSummary' ) ,
8909+ customInput : document . getElementById ( 'scenarioCustomInput' ) ,
8910+ customBtn : document . getElementById ( 'scenarioCustomBtn' ) ,
8911+ resetBtn : document . getElementById ( 'scenarioResetBtn' )
8912+ } ;
8913+ }
8914+ return _els ;
8915+ }
8916+
8917+ /** Schedule a timeout and register it for cleanup on reset. */
8918+ function schedule ( fn , ms ) {
8919+ var id = setTimeout ( fn , ms ) ;
8920+ timers . push ( id ) ;
8921+ return id ;
8922+ }
8923+
88948924 function init ( ) {
88958925 var presetBtns = document . querySelectorAll ( '.scenario-preset-btn' ) ;
8896- var customBtn = document . getElementById ( 'scenarioCustomBtn' ) ;
8897- var customInput = document . getElementById ( 'scenarioCustomInput' ) ;
8898- var resetBtn = document . getElementById ( 'scenarioResetBtn' ) ;
8926+ // Eagerly resolve and cache DOM refs
8927+ var els = _getEls ( ) ;
88998928
89008929 if ( ! presetBtns . length ) return ;
89018930
@@ -8908,22 +8937,22 @@ var ScenarioPlanner = (function () {
89088937 } ) ;
89098938 } ) ;
89108939
8911- if ( customBtn && customInput ) {
8912- customBtn . addEventListener ( 'click' , function ( ) {
8913- var val = customInput . value . trim ( ) ;
8940+ if ( els . customBtn && els . customInput ) {
8941+ els . customBtn . addEventListener ( 'click' , function ( ) {
8942+ var val = els . customInput . value . trim ( ) ;
89148943 if ( ! val || running ) return ;
89158944 presetBtns . forEach ( function ( b ) { b . setAttribute ( 'aria-pressed' , 'false' ) ; } ) ;
89168945 startPlanning ( val ) ;
89178946 } ) ;
8918- customInput . addEventListener ( 'keydown' , function ( e ) {
8947+ els . customInput . addEventListener ( 'keydown' , function ( e ) {
89198948 if ( e . key === 'Enter' ) {
8920- customBtn . click ( ) ;
8949+ els . customBtn . click ( ) ;
89218950 }
89228951 } ) ;
89238952 }
89248953
8925- if ( resetBtn ) {
8926- resetBtn . addEventListener ( 'click' , function ( ) { resetPlanner ( ) ; } ) ;
8954+ if ( els . resetBtn ) {
8955+ els . resetBtn . addEventListener ( 'click' , function ( ) { resetPlanner ( ) ; } ) ;
89278956 }
89288957 }
89298958
@@ -8938,34 +8967,24 @@ var ScenarioPlanner = (function () {
89388967 return customPlan ;
89398968 }
89408969
8941- function escapeHtml ( str ) {
8942- var el = document . createElement ( 'span' ) ;
8943- el . textContent = str ;
8944- return el . innerHTML ;
8945- }
8946-
89478970 function startPlanning ( scenario ) {
89488971 running = true ;
89498972 clearTimers ( ) ;
89508973
8951- var workspace = document . getElementById ( 'scenarioWorkspace' ) ;
8952- var goalEl = document . getElementById ( 'scenarioGoal' ) ;
8953- var statusEl = document . getElementById ( 'scenarioAgentStatus' ) ;
8954- var phasesEl = document . getElementById ( 'scenarioPhases' ) ;
8955- var summaryEl = document . getElementById ( 'scenarioSummary' ) ;
8974+ var els = _getEls ( ) ;
89568975
89578976 // Reset
8958- phasesEl . innerHTML = '' ;
8959- summaryEl . hidden = true ;
8960- summaryEl . innerHTML = '' ;
8961- statusEl . className = 'scenario-agent-status' ;
8962- workspace . hidden = false ;
8977+ els . phases . innerHTML = '' ;
8978+ els . summary . hidden = true ;
8979+ els . summary . innerHTML = '' ;
8980+ els . status . className = 'scenario-agent-status' ;
8981+ els . workspace . hidden = false ;
89638982
89648983 var safeScenario = escapeHtml ( scenario . length > 100 ? scenario . substring ( 0 , 97 ) + '...' : scenario ) ;
8965- goalEl . innerHTML = '\u{1F3AF} <strong>Goal:</strong> ' + safeScenario ;
8984+ els . goal . innerHTML = '\u{1F3AF} <strong>Goal:</strong> ' + safeScenario ;
89668985
89678986 var plan = getPlan ( scenario ) ;
8968- animatePhases ( plan , phasesEl , statusEl , summaryEl ) ;
8987+ animatePhases ( plan , els . phases , els . status , els . summary ) ;
89698988 }
89708989
89718990 function animatePhases ( plan , phasesEl , statusEl , summaryEl ) {
@@ -8991,21 +9010,18 @@ var ScenarioPlanner = (function () {
89919010 phasesEl . appendChild ( phaseEl ) ;
89929011
89939012 // Trigger visibility transition
8994- var tid1 = setTimeout ( function ( ) { phaseEl . classList . add ( 'visible' ) ; } , 50 ) ;
8995- timers . push ( tid1 ) ;
9013+ schedule ( function ( ) { phaseEl . classList . add ( 'visible' ) ; } , 50 ) ;
89969014
89979015 var stepsContainer = phaseEl . querySelector ( '.scenario-phase-steps' ) ;
89989016 animateSteps ( phase . steps , stepsContainer , phaseIndex , function ( ) {
89999017 var phaseStatusEl = document . getElementById ( 'phaseStatus' + phaseIndex ) ;
90009018 if ( phaseStatusEl ) phaseStatusEl . textContent = '\u2705 complete' ;
90019019 phaseIndex ++ ;
9002- var tid2 = setTimeout ( nextPhase , 500 ) ;
9003- timers . push ( tid2 ) ;
9020+ schedule ( nextPhase , 500 ) ;
90049021 } ) ;
90059022 }
90069023
9007- var tid0 = setTimeout ( nextPhase , 400 ) ;
9008- timers . push ( tid0 ) ;
9024+ schedule ( nextPhase , 400 ) ;
90099025 }
90109026
90119027 function animateSteps ( steps , container , phaseIdx , onDone ) {
@@ -9025,12 +9041,9 @@ var ScenarioPlanner = (function () {
90259041 '<span>' + escapeHtml ( step . text ) + '</span>' ;
90269042 container . appendChild ( stepEl ) ;
90279043
9028- var tid1 = setTimeout ( function ( ) {
9029- stepEl . classList . add ( 'visible' ) ;
9030- } , 50 ) ;
9031- timers . push ( tid1 ) ;
9044+ schedule ( function ( ) { stepEl . classList . add ( 'visible' ) ; } , 50 ) ;
90329045
9033- var tid2 = setTimeout ( function ( ) {
9046+ schedule ( function ( ) {
90349047 var icon = stepEl . querySelector ( '.scenario-step-icon' ) ;
90359048 if ( icon ) {
90369049 icon . textContent = '\u2713' ;
@@ -9039,7 +9052,6 @@ var ScenarioPlanner = (function () {
90399052 stepIndex ++ ;
90409053 nextStep ( ) ;
90419054 } , step . delay ) ;
9042- timers . push ( tid2 ) ;
90439055 }
90449056
90459057 nextStep ( ) ;
@@ -9070,21 +9082,17 @@ var ScenarioPlanner = (function () {
90709082 clearTimers ( ) ;
90719083 running = false ;
90729084
9073- var workspace = document . getElementById ( 'scenarioWorkspace' ) ;
9074- var phasesEl = document . getElementById ( 'scenarioPhases' ) ;
9075- var summaryEl = document . getElementById ( 'scenarioSummary' ) ;
9076- var statusEl = document . getElementById ( 'scenarioAgentStatus' ) ;
9077- var customInput = document . getElementById ( 'scenarioCustomInput' ) ;
9085+ var els = _getEls ( ) ;
90789086 var presetBtns = document . querySelectorAll ( '.scenario-preset-btn' ) ;
90799087
9080- if ( workspace ) workspace . hidden = true ;
9081- if ( phasesEl ) phasesEl . innerHTML = '' ;
9082- if ( summaryEl ) { summaryEl . hidden = true ; summaryEl . innerHTML = '' ; }
9083- if ( statusEl ) {
9084- statusEl . textContent = '\u{1F916} AgentBox is thinking...' ;
9085- statusEl . className = 'scenario-agent-status' ;
9088+ if ( els . workspace ) els . workspace . hidden = true ;
9089+ if ( els . phases ) els . phases . innerHTML = '' ;
9090+ if ( els . summary ) { els . summary . hidden = true ; els . summary . innerHTML = '' ; }
9091+ if ( els . status ) {
9092+ els . status . textContent = '\u{1F916} AgentBox is thinking...' ;
9093+ els . status . className = 'scenario-agent-status' ;
90869094 }
9087- if ( customInput ) customInput . value = '' ;
9095+ if ( els . customInput ) els . customInput . value = '' ;
90889096 presetBtns . forEach ( function ( b ) { b . setAttribute ( 'aria-pressed' , 'false' ) ; } ) ;
90899097 }
90909098
0 commit comments