@@ -18,12 +18,17 @@ export class PlotlyViaCDNModule {
1818 private static _plotlyVersion : string = 'latest' ;
1919 static plotlyBundleNames : PlotlyBundleName [ ] = [ 'basic' , 'cartesian' , 'geo' , 'gl3d' , 'gl2d' , 'mapbox' , 'finance' ] ;
2020
21+ constructor ( public plotlyService : PlotlyService ) {
22+ PlotlyService . setModuleName ( 'ViaCDN' ) ;
23+ }
24+
2125 static set plotlyVersion ( version : string ) {
2226 const isOk = version === 'latest' || / ^ \d \. \d { 1 , 2 } \. \d { 1 , 2 } $ / . test ( version ) ;
2327 if ( ! isOk ) {
2428 throw new Error ( `Invalid plotly version. Please set 'latest' or version number (i.e.: 1.4.3)` ) ;
2529 }
2630
31+ PlotlyViaCDNModule . loadViaCDN ( ) ;
2732 PlotlyViaCDNModule . _plotlyVersion = version ;
2833 }
2934
@@ -39,33 +44,38 @@ export class PlotlyViaCDNModule {
3944
4045 static loadViaCDN ( ) {
4146 PlotlyService . setPlotly ( 'waiting' ) ;
42- const src = PlotlyViaCDNModule . _plotlyBundle == null
43- ? `https://cdn.plot.ly/plotly-${ PlotlyViaCDNModule . _plotlyVersion } .min.js`
44- : `https://cdn.plot.ly/plotly-${ PlotlyViaCDNModule . _plotlyBundle } -${ PlotlyViaCDNModule . _plotlyBundle } .min.js` ;
45-
46- const script : HTMLScriptElement = document . createElement ( 'script' ) ;
47- script . type = 'text/javascript' ;
48- script . src = src ;
49- script . onerror = ( ) => console . error ( `Error loading plotly.js library from ${ src } ` ) ;
50-
51- const head : HTMLHeadElement = document . getElementsByTagName ( 'head' ) [ 0 ] ;
52- head . appendChild ( script ) ;
53-
54- let counter = 200 ; // equivalent of 10 seconds...
55-
56- const fn = ( ) => {
57- const plotly = ( window as any ) . Plotly ;
58- if ( plotly ) {
59- PlotlyService . setPlotly ( plotly ) ;
60- } else if ( counter > 0 ) {
61- counter -- ;
62- setTimeout ( fn , 50 ) ;
63- } else {
64- throw new Error ( `Error loading plotly.js library from ${ src } . Timeout.` ) ;
65- }
47+
48+ const init = ( ) => {
49+ const src = PlotlyViaCDNModule . _plotlyBundle == null
50+ ? `https://cdn.plot.ly/plotly-${ PlotlyViaCDNModule . _plotlyVersion } .min.js`
51+ : `https://cdn.plot.ly/plotly-${ PlotlyViaCDNModule . _plotlyBundle } -${ PlotlyViaCDNModule . _plotlyVersion } .min.js` ;
52+
53+ const script : HTMLScriptElement = document . createElement ( 'script' ) ;
54+ script . type = 'text/javascript' ;
55+ script . src = src ;
56+ script . onerror = ( ) => console . error ( `Error loading plotly.js library from ${ src } ` ) ;
57+
58+ const head : HTMLHeadElement = document . getElementsByTagName ( 'head' ) [ 0 ] ;
59+ head . appendChild ( script ) ;
60+
61+ let counter = 200 ; // equivalent of 10 seconds...
62+
63+ const fn = ( ) => {
64+ const plotly = ( window as any ) . Plotly ;
65+ if ( plotly ) {
66+ PlotlyService . setPlotly ( plotly ) ;
67+ } else if ( counter > 0 ) {
68+ counter -- ;
69+ setTimeout ( fn , 50 ) ;
70+ } else {
71+ throw new Error ( `Error loading plotly.js library from ${ src } . Timeout.` ) ;
72+ }
73+ } ;
74+
75+ fn ( ) ;
6676 } ;
6777
68- fn ( ) ;
78+ setTimeout ( init ) ;
6979 }
7080
7181 static forRoot ( config : Partial < { version : string } > ) : never {
0 commit comments