|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var Plotly = require('../plotly'); |
| 13 | +var Lib = require('../lib'); |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {object} gd figure Object |
| 17 | + * @param {object} opts option object |
| 18 | + * @param opts.format 'jpeg' | 'png' | 'webp' | 'svg' |
| 19 | + */ |
| 20 | +function toSnapshot(gd, opts) { |
| 21 | + |
| 22 | + var Snapshot = Plotly.Snapshot; |
| 23 | + var Lib = Plotly.Lib; |
| 24 | + |
| 25 | + // check for undefined opts |
| 26 | + opts = (opts) ? opts : {}; |
| 27 | + |
| 28 | + // default to png |
| 29 | + opts.format = (opts.format) ? opts.format : 'png'; |
| 30 | + |
| 31 | + if(Lib.isIE()) { |
| 32 | + Lib.notifier('Snapshotting is unavailable in Internet Explorer. ' + |
| 33 | + 'Consider exporting your images using the Plotly Cloud', 'long'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if(gd._snapshotInProgress) { |
| 38 | + Lib.notifier('Snapshotting is still in progress - please hold', 'long'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + gd._snapshotInProgress = true; |
| 43 | + Lib.notifier('Taking snapshot - this may take a few seconds', 'long'); |
| 44 | + |
| 45 | + var ev = Snapshot.toImage(gd, opts); |
| 46 | + |
| 47 | + var filename = gd.fn || 'newplot'; |
| 48 | + filename += '.' + opts.format; |
| 49 | + |
| 50 | + ev.once('success', function(result) { |
| 51 | + gd._snapshotInProgress = false; |
| 52 | + |
| 53 | + var downloadLink = document.createElement('a'); |
| 54 | + downloadLink.href = result; |
| 55 | + downloadLink.download = filename; // only supported by FF and Chrome |
| 56 | + |
| 57 | + document.body.appendChild(downloadLink); |
| 58 | + downloadLink.click(); |
| 59 | + document.body.removeChild(downloadLink); |
| 60 | + |
| 61 | + ev.clean(); |
| 62 | + }); |
| 63 | + |
| 64 | + ev.once('error', function(err) { |
| 65 | + gd._snapshotInProgress = false; |
| 66 | + |
| 67 | + Lib.notifier('Sorry there was a problem downloading your ' + format, 'long'); |
| 68 | + console.error(err); |
| 69 | + |
| 70 | + ev.clean(); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +module.exports = toSnapshot; |
0 commit comments