@@ -18,12 +18,17 @@ export class PlotlyViaCDNModule {
18
18
private static _plotlyVersion : string = 'latest' ;
19
19
static plotlyBundleNames : PlotlyBundleName [ ] = [ 'basic' , 'cartesian' , 'geo' , 'gl3d' , 'gl2d' , 'mapbox' , 'finance' ] ;
20
20
21
+ constructor ( public plotlyService : PlotlyService ) {
22
+ PlotlyService . setModuleName ( 'ViaCDN' ) ;
23
+ }
24
+
21
25
static set plotlyVersion ( version : string ) {
22
26
const isOk = version === 'latest' || / ^ \d \. \d { 1 , 2 } \. \d { 1 , 2 } $ / . test ( version ) ;
23
27
if ( ! isOk ) {
24
28
throw new Error ( `Invalid plotly version. Please set 'latest' or version number (i.e.: 1.4.3)` ) ;
25
29
}
26
30
31
+ PlotlyViaCDNModule . loadViaCDN ( ) ;
27
32
PlotlyViaCDNModule . _plotlyVersion = version ;
28
33
}
29
34
@@ -39,33 +44,38 @@ export class PlotlyViaCDNModule {
39
44
40
45
static loadViaCDN ( ) {
41
46
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 ( ) ;
66
76
} ;
67
77
68
- fn ( ) ;
78
+ setTimeout ( init ) ;
69
79
}
70
80
71
81
static forRoot ( config : Partial < { version : string } > ) : never {
0 commit comments