@@ -9,72 +9,76 @@ const TOO_MANY_ANTS = 64;
9
9
* @param {string } s Selector
10
10
* @returns {HTMLElement }
11
11
*/
12
- const $ = s => document . querySelector ( s ) ;
12
+ const safe$ = selector => {
13
+ var elem = document . querySelector ( selector ) ;
14
+ if ( ! elem ) throw new Error ( "can't find " + selector ) ;
15
+ return elem ;
16
+ } ;
13
17
14
18
/**
15
19
* @type {HTMLCanvasElement }
16
20
*/
17
- const playfield = $ ( '#playfield' ) ;
21
+ const playfield = safe $( '#playfield' ) ;
18
22
/**
19
23
* @type {HTMLButtonElement }
20
24
*/
21
- const startStopBtn = $ ( '#startstop' ) ;
25
+ const startStopBtn = safe $( '#startstop' ) ;
22
26
/**
23
27
* @type {HTMLButtonElement }
24
28
*/
25
- const stepBtn = $ ( '#step' ) ;
29
+ const stepBtn = safe $( '#step' ) ;
26
30
/**
27
31
* @type {HTMLOutputElement }
28
32
*/
29
- const stepCounter = $ ( '#stepnum' ) ;
33
+ const stepCounter = safe $( '#stepnum' ) ;
30
34
/**
31
35
* @type {HTMLInputElement }
32
36
*/
33
- const speedSlider = $ ( '#speedslider' ) ;
37
+ const speedSlider = safe $( '#speedslider' ) ;
34
38
/**
35
39
* @type {HTMLInputElement }
36
40
*/
37
- const speedBox = $ ( '#speedbox' ) ;
41
+ const speedBox = safe $( '#speedbox' ) ;
38
42
/**
39
43
* @type {HTMLInputElement }
40
44
*/
41
- const muteCheckbox = $ ( '#mutecheck' ) ;
45
+ const muteCheckbox = safe $( '#mutecheck' ) ;
42
46
/**
43
47
* @type {HTMLOutputElement }
44
48
*/
45
- const antsCounter = $ ( '#antscount' ) ;
49
+ const antsCounter = safe $( '#antscount' ) ;
46
50
/**
47
51
* @type {HTMLButtonElement }
48
52
*/
49
- const loadBtn = $ ( '#loadbtn' ) ;
53
+ const loadBtn = safe $( '#loadbtn' ) ;
50
54
/**
51
55
* @type {HTMLButtonElement }
52
56
*/
53
- const dumpBtn = $ ( '#dumpbtn' ) ;
57
+ const dumpBtn = safe $( '#dumpbtn' ) ;
54
58
/**
55
59
* @type {HTMLOutputElement }
56
60
*/
57
- const statusBar = $ ( '#statusbar' ) ;
61
+ const statusBar = safe $( '#statusbar' ) ;
58
62
/**
59
63
* @type {HTMLButtonElement }
60
64
*/
61
- const fitBtn = $ ( '#fit' ) ;
65
+ const fitBtn = safe $( '#fit' ) ;
62
66
/**
63
67
* @type {HTMLInputElement }
64
68
*/
65
- const autoFit = $ ( '#autofit' ) ;
69
+ const autoFit = safe $( '#autofit' ) ;
66
70
/**
67
71
* @type {HTMLSelectElement }
68
72
*/
69
- const followSelector = $ ( '#follow' ) ;
73
+ const followSelector = safe $( '#follow' ) ;
70
74
/**
71
75
* @type {HTMLSelectElement }
72
76
*/
73
- const actionsSelector = $ ( '#actions' ) ;
77
+ const actionsSelector = safe $( '#actions' ) ;
74
78
/**
75
79
* @type {HTMLDivElement }
76
80
*/
77
- const debugBar = $ ( '#debugbar' ) ;
81
+ const debugBar = safe $( '#debugbar' ) ;
78
82
79
83
ace . config . set ( 'basePath' , 'https://cdn.jsdelivr.net/npm/[email protected] /src-noconflict/' ) ;
80
84
const textbox = ace . edit ( 'textbox' , { mode : 'ace/mode/xml' } ) ;
@@ -106,7 +110,7 @@ var world = new World();
106
110
/**
107
111
* @type {CanvasToolsManager }
108
112
*/
109
- var canvasTools = new CanvasToolsManager ( playfield , $ ( '#toolselect' ) , $ ( '#tooloption' ) , [
113
+ var canvasTools = new CanvasToolsManager ( playfield , safe $( '#toolselect' ) , safe $( '#tooloption' ) , [
110
114
new DragTool ( ) ,
111
115
new DrawCellsTool ( world ) ,
112
116
new DrawAntsTool ( world , breeder , ants ) ,
@@ -403,8 +407,8 @@ dumpBtn.addEventListener('click', () => actions.trigger('dump'));
403
407
*/
404
408
function fitace ( ) {
405
409
setTimeout ( ( ) => {
406
- var rect = $ ( '#textbox' ) . parentElement . getBoundingClientRect ( ) ;
407
- $ ( '#textbox' ) . setAttribute ( 'style' , `width:${ rect . width } px;height:${ rect . height } px` ) ;
410
+ var rect = safe $( '#textbox' ) . parentElement . getBoundingClientRect ( ) ;
411
+ safe $( '#textbox' ) . setAttribute ( 'style' , `width:${ rect . width } px;height:${ rect . height } px` ) ;
408
412
textbox . resize ( true ) ;
409
413
} , 0 ) ;
410
414
}
@@ -419,7 +423,7 @@ window.addEventListener('hashchange', () => {
419
423
where = '#dumpstatuswrapper' ;
420
424
textbox . setTheme ( DARK_MODE ? 'ace/theme/pastel_on_dark' : 'ace/theme/chrome' ) ;
421
425
}
422
- $ ( where ) . append ( statusBar ) ;
426
+ safe $( where ) . append ( statusBar ) ;
423
427
fitace ( ) ;
424
428
} ) ;
425
429
location . hash = "#" ; // Don't have editor open by default
@@ -466,17 +470,18 @@ if ("serviceWorker" in navigator) {
466
470
}
467
471
468
472
// Dev version indicator
473
+ const heading = safe$ ( "#foohead" ) ;
469
474
if ( location . host . indexOf ( 'localhost' ) != - 1 ) {
470
475
document . title += ' - localhost version' ;
471
- $ ( 'main . heading' ) . textContent += ' - localhost version' ;
476
+ heading . textContent += ' - localhost version' ;
472
477
}
473
478
else if ( location . host . indexOf ( '.github.dev' ) != - 1 ) {
474
479
document . title += ' - codespace version' ;
475
- $ ( 'main . heading' ) . textContent += ' - codespace version' ;
480
+ heading . textContent += ' - codespace version' ;
476
481
}
477
482
else if ( location . protocol . indexOf ( 'file' ) != - 1 ) {
478
483
document . title += ' - file:// version' ;
479
- $ ( 'main . heading' ) . textContent += ' - file:// version (some features unavailable)' ;
484
+ heading . textContent += ' - file:// version (some features unavailable)' ;
480
485
}
481
486
else {
482
487
// we are in the full web version
0 commit comments