|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var d3 = require('@plotly/d3'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Show a styled confirmation dialog before sharing a chart with Plotly Cloud. |
| 7 | + * |
| 8 | + * The dialog is appended to the plot's positioning container (.svg-container) |
| 9 | + * so it is centered over the plot rather than the whole viewport. It can be |
| 10 | + * dismissed by clicking Cancel, clicking the backdrop, or pressing Escape. |
| 11 | + * |
| 12 | + * @param {DOM node} gd - the graph div, used to scope the dialog to the plot |
| 13 | + * @param {string} serverUrl - destination shown in the dialog message |
| 14 | + * @param {function} onConfirm - called when the user confirms the upload |
| 15 | + */ |
| 16 | +module.exports = function confirmCloudDialog(gd, serverUrl, onConfirm) { |
| 17 | + var container = d3.select(gd._fullLayout._paperdiv.node()); |
| 18 | + |
| 19 | + // Never stack dialogs - drop any that is already open. |
| 20 | + container.selectAll('.plotly-cloud-dialog').remove(); |
| 21 | + |
| 22 | + var overlay = container |
| 23 | + .append('div') |
| 24 | + .classed('plotly-cloud-dialog', true); |
| 25 | + |
| 26 | + var dialog = overlay.append('div') |
| 27 | + .classed('plotly-cloud-dialog-box', true); |
| 28 | + |
| 29 | + dialog.append('div') |
| 30 | + .classed('plotly-cloud-dialog-title', true) |
| 31 | + .text('Share with Plotly Cloud'); |
| 32 | + |
| 33 | + dialog.append('div') |
| 34 | + .classed('plotly-cloud-dialog-message', true) |
| 35 | + .text('Your chart data will be sent to ' + serverUrl + '.'); |
| 36 | + |
| 37 | + var buttons = dialog.append('div') |
| 38 | + .classed('plotly-cloud-dialog-buttons', true); |
| 39 | + |
| 40 | + function close() { |
| 41 | + overlay.remove(); |
| 42 | + document.removeEventListener('keydown', onKeydown); |
| 43 | + } |
| 44 | + |
| 45 | + function onKeydown(e) { |
| 46 | + if(e.key === 'Escape' || e.keyCode === 27) close(); |
| 47 | + } |
| 48 | + document.addEventListener('keydown', onKeydown); |
| 49 | + |
| 50 | + // Clicking the backdrop (but not the dialog box) cancels. |
| 51 | + overlay.on('click', function() { |
| 52 | + if(d3.event.target === overlay.node()) close(); |
| 53 | + }); |
| 54 | + |
| 55 | + buttons.append('button') |
| 56 | + .classed('plotly-cloud-dialog-btn', true) |
| 57 | + .classed('plotly-cloud-dialog-btn--cancel', true) |
| 58 | + .text('Cancel') |
| 59 | + .on('click', close); |
| 60 | + |
| 61 | + buttons.append('button') |
| 62 | + .classed('plotly-cloud-dialog-btn', true) |
| 63 | + .classed('plotly-cloud-dialog-btn--confirm', true) |
| 64 | + .text('Share') |
| 65 | + .on('click', function() { |
| 66 | + close(); |
| 67 | + onConfirm(); |
| 68 | + }); |
| 69 | +}; |
0 commit comments