@@ -13,24 +13,26 @@ var toImage = require('../plot_api/to_image');
1313var Lib = require ( '../lib' ) ; // for isIE
1414var fileSaver = require ( './filesaver' ) ;
1515
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 }
2324 */
2425function downloadImage ( gd , opts ) {
26+ var _gd ;
27+ if ( ! Lib . isPlainObject ( gd ) ) _gd = Lib . getGraphDiv ( gd ) ;
2528
2629 // check for undefined opts
2730 opts = opts || { } ;
28-
2931 // default to png
3032 opts . format = opts . format || 'png' ;
3133
3234 return new Promise ( function ( resolve , reject ) {
33- if ( gd . _snapshotInProgress ) {
35+ if ( _gd && _gd . _snapshotInProgress ) {
3436 reject ( new Error ( 'Snapshotting already in progress.' ) ) ;
3537 }
3638
@@ -43,19 +45,19 @@ function downloadImage(gd, opts) {
4345 reject ( new Error ( 'Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.' ) ) ;
4446 }
4547
46- gd . _snapshotInProgress = true ;
48+ _gd . _snapshotInProgress = true ;
4749 var promise = toImage ( gd , opts ) ;
4850
4951 var filename = opts . filename || gd . fn || 'newplot' ;
5052 filename += '.' + opts . format ;
5153
5254 promise . then ( function ( result ) {
53- gd . _snapshotInProgress = false ;
55+ _gd . _snapshotInProgress = false ;
5456 return fileSaver ( result , filename ) ;
5557 } ) . then ( function ( name ) {
5658 resolve ( name ) ;
5759 } ) . catch ( function ( err ) {
58- gd . _snapshotInProgress = false ;
60+ _gd . _snapshotInProgress = false ;
5961 reject ( err ) ;
6062 } ) ;
6163 } ) ;
0 commit comments