@@ -10,21 +10,16 @@ function parseRgbString(rgbString) {
1010 return null ; // Or throw an error for invalid format
1111}
1212
13- function ensureRequire ( ) {
14- // Needed e.g. in Jupyter notebooks: if require is already available, return resolved promise
15- if ( typeof require !== 'undefined' ) {
16- return Promise . resolve ( require ) ;
17- }
18-
19- // Otherwise, dynamically load require.js
13+ function loadScript ( src ) {
2014 return new Promise ( ( resolve , reject ) => {
2115 const script = document . createElement ( 'script' ) ;
22- script . src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js' ;
23- script . onload = ( ) => resolve ( require ) ;
16+ script . src = src ;
17+ script . async = true ;
18+ script . onload = ( ) => resolve ( ) ;
2419 script . onerror = reject ;
2520 document . head . appendChild ( script ) ;
2621 } ) ;
27- } ;
22+ }
2823
2924function main_visualizer (
3025 d3 ,
@@ -1371,12 +1366,16 @@ function main_visualizer(
13711366 can be used to pass in the appropriate data
13721367*/
13731368
1374- ensureRequire ( )
1375- . then ( require => {
1376- require . config ( { paths : { d3 : 'https://d3js.org/d3.v7.min' } } ) ;
1377- require ( [ "d3" ] , function ( d3 ) {
1369+ if ( typeof d3 !== 'undefined' ) {
1370+ main_visualizer ( d3 , $divnum , $data , $width , $height , $y_axis , $edges , $condense_mutations , $label_mutations , $tree_highlighting , $title , $rotate_tip_labels , $plot_type , $preamble , $source , $save_filename )
1371+ } else {
1372+ loadScript ( 'https://d3js.org/d3.v7.min.js' )
1373+ . then ( ( ) => {
1374+ if ( typeof d3 === 'undefined' ) {
1375+ throw new Error ( 'D3 loaded but global "d3" is unavailable.' ) ;
1376+ }
13781377 main_visualizer ( d3 , $divnum , $data , $width , $height , $y_axis , $edges , $condense_mutations , $label_mutations , $tree_highlighting , $title , $rotate_tip_labels , $plot_type , $preamble , $source , $save_filename )
1379- } ) ;
1380- } )
1381- . catch ( err => console . error ( 'Failed to load require.js:' , err ) ) ;
1378+ } )
1379+ . catch ( err => console . error ( 'Failed to load d3 script:' , err ) ) ;
1380+ }
13821381
0 commit comments