@@ -13,24 +13,26 @@ var toImage = require('../plot_api/to_image');
13
13
var Lib = require ( '../lib' ) ; // for isIE
14
14
var fileSaver = require ( './filesaver' ) ;
15
15
16
- /**
17
- * @param {object } gd figure Object
18
- * @param {object } opts option object
19
- * @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
20
- * @param opts.width width of snapshot in px
21
- * @param opts.height height of snapshot in px
22
- * @param opts.filename name of file excluding extension
16
+ /** Plotly.downloadImage
17
+ *
18
+ * @param {object | string | HTML div } gd
19
+ * can either be a data/layout/config object
20
+ * or an existing graph <div>
21
+ * or an id to an existing graph <div>
22
+ * @param {object } opts (see ../plot_api/to_image)
23
+ * @return {promise }
23
24
*/
24
25
function downloadImage ( gd , opts ) {
26
+ var _gd ;
27
+ if ( ! Lib . isPlainObject ( gd ) ) _gd = Lib . getGraphDiv ( gd ) ;
25
28
26
29
// check for undefined opts
27
30
opts = opts || { } ;
28
-
29
31
// default to png
30
32
opts . format = opts . format || 'png' ;
31
33
32
34
return new Promise ( function ( resolve , reject ) {
33
- if ( gd . _snapshotInProgress ) {
35
+ if ( _gd && _gd . _snapshotInProgress ) {
34
36
reject ( new Error ( 'Snapshotting already in progress.' ) ) ;
35
37
}
36
38
@@ -43,19 +45,19 @@ function downloadImage(gd, opts) {
43
45
reject ( new Error ( 'Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.' ) ) ;
44
46
}
45
47
46
- gd . _snapshotInProgress = true ;
48
+ _gd . _snapshotInProgress = true ;
47
49
var promise = toImage ( gd , opts ) ;
48
50
49
51
var filename = opts . filename || gd . fn || 'newplot' ;
50
52
filename += '.' + opts . format ;
51
53
52
54
promise . then ( function ( result ) {
53
- gd . _snapshotInProgress = false ;
55
+ _gd . _snapshotInProgress = false ;
54
56
return fileSaver ( result , filename ) ;
55
57
} ) . then ( function ( name ) {
56
58
resolve ( name ) ;
57
59
} ) . catch ( function ( err ) {
58
- gd . _snapshotInProgress = false ;
60
+ _gd . _snapshotInProgress = false ;
59
61
reject ( err ) ;
60
62
} ) ;
61
63
} ) ;
0 commit comments