-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Share chart button #7802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Share chart button #7802
Changes from all commits
42c9601
8ce5512
ad9ddfc
100da7b
82f5437
fddd50e
ba91b11
717fc26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| 'use strict'; | ||
|
|
||
| var d3 = require('@plotly/d3'); | ||
|
|
||
| /** | ||
| * Show a styled confirmation dialog before sharing a chart with Plotly Cloud. | ||
| * | ||
| * The dialog is appended to the plot's positioning container (.svg-container) | ||
| * so it is centered over the plot rather than the whole viewport. It can be | ||
| * dismissed by clicking Cancel, clicking the backdrop, or pressing Escape. | ||
| * | ||
| * @param {DOM node} gd - the graph div, used to scope the dialog to the plot | ||
| * @param {string} serverUrl - destination shown in the dialog message | ||
| * @param {function} onConfirm - called when the user confirms the upload | ||
| */ | ||
| module.exports = function confirmCloudDialog(gd, serverUrl, onConfirm) { | ||
| var container = d3.select(gd._fullLayout._paperdiv.node()); | ||
|
|
||
| // Never stack dialogs - drop any that is already open. | ||
| container.selectAll('.plotly-cloud-dialog').remove(); | ||
|
|
||
| var overlay = container | ||
| .append('div') | ||
| .classed('plotly-cloud-dialog', true); | ||
|
|
||
| var dialog = overlay.append('div') | ||
| .classed('plotly-cloud-dialog-box', true); | ||
|
|
||
| dialog.append('div') | ||
| .classed('plotly-cloud-dialog-title', true) | ||
| .text('Share with Plotly Cloud'); | ||
|
|
||
| dialog.append('div') | ||
| .classed('plotly-cloud-dialog-message', true) | ||
| .text('Your chart data will be sent to ' + serverUrl + '.'); | ||
|
|
||
| var buttons = dialog.append('div') | ||
| .classed('plotly-cloud-dialog-buttons', true); | ||
|
|
||
| function close() { | ||
| overlay.remove(); | ||
| document.removeEventListener('keydown', onKeydown); | ||
| } | ||
|
|
||
| function onKeydown(e) { | ||
| if(e.key === 'Escape' || e.keyCode === 27) close(); | ||
| } | ||
| document.addEventListener('keydown', onKeydown); | ||
|
|
||
| // Clicking the backdrop (but not the dialog box) cancels. | ||
| overlay.on('click', function() { | ||
| if(d3.event.target === overlay.node()) close(); | ||
| }); | ||
|
|
||
| buttons.append('button') | ||
| .classed('plotly-cloud-dialog-btn', true) | ||
| .classed('plotly-cloud-dialog-btn--cancel', true) | ||
| .text('Cancel') | ||
| .on('click', close); | ||
|
|
||
| buttons.append('button') | ||
| .classed('plotly-cloud-dialog-btn', true) | ||
| .classed('plotly-cloud-dialog-btn--confirm', true) | ||
| .text('Share') | ||
| .on('click', function() { | ||
| close(); | ||
| onConfirm(); | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| .plotly-cloud-dialog { | ||
| font-family: 'Open Sans', verdana, arial, sans-serif; // Because not all contexts have this specified. | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| z-index: 1001; | ||
|
|
||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| background-color: rgba(0, 0, 0, 0.4); | ||
|
|
||
| .plotly-cloud-dialog-box { | ||
| box-sizing: border-box; | ||
| min-width: 300px; | ||
| max-width: 420px; | ||
| padding: 20px 24px; | ||
|
|
||
| background-color: $color-bg-light; | ||
| border: 1px solid $color-bg-darker; | ||
| border-radius: 4px; | ||
| box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25); | ||
|
|
||
| font-size: 13px; | ||
| color: #2a3f5f; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-title { | ||
| font-size: 16px; | ||
| font-weight: bold; | ||
| margin-bottom: 12px; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-message { | ||
| line-height: 1.5; | ||
| overflow-wrap: break-word; | ||
| word-wrap: break-word; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-buttons { | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| margin-top: 20px; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn { | ||
| font-family: inherit; | ||
| font-size: 13px; | ||
| padding: 7px 16px; | ||
| margin-left: 8px; | ||
|
|
||
| border-radius: 3px; | ||
| border: 1px solid transparent; | ||
| cursor: pointer; | ||
|
|
||
| &:focus-visible { | ||
| outline: 2px solid $color-brand-primary; | ||
| outline-offset: 1px; | ||
| } | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn--cancel { | ||
| background-color: $color-bg-light; | ||
| border-color: $color-bg-darker; | ||
| color: $color-muted-text; | ||
|
|
||
| &:hover { | ||
| background-color: $color-bg-base; | ||
| } | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn--confirm { | ||
| background-color: $color-brand-primary; | ||
| color: $color-bg-light; | ||
|
|
||
| &:hover { | ||
| background-color: $color-brand-accent; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,14 +33,10 @@ var configAttributes = { | |||||||||||||||||
|
|
||||||||||||||||||
| plotlyServerURL: { | ||||||||||||||||||
| valType: 'string', | ||||||||||||||||||
| dflt: '', | ||||||||||||||||||
| dflt: 'https://cloud.plotly.com/upload', | ||||||||||||||||||
| description: [ | ||||||||||||||||||
| 'When set it determines base URL for', | ||||||||||||||||||
| 'the \'Edit in Chart Studio\' `showEditInChartStudio`/`showSendToCloud` mode bar button', | ||||||||||||||||||
| 'and the showLink/sendData on-graph link.', | ||||||||||||||||||
| 'To enable sending your data to Chart Studio Cloud, you need to', | ||||||||||||||||||
| 'set both `plotlyServerURL` to \'https://chart-studio.plotly.com\' and', | ||||||||||||||||||
| 'also set `showSendToCloud` to true.' | ||||||||||||||||||
| 'Sets the URL for the `sendChartToCloud` modebar button.', | ||||||||||||||||||
| 'When clicked, the button will send the chart data to this URL.', | ||||||||||||||||||
| ].join(' ') | ||||||||||||||||||
| }, | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -275,24 +271,12 @@ var configAttributes = { | |||||||||||||||||
| }, | ||||||||||||||||||
| showSendToCloud: { | ||||||||||||||||||
| valType: 'boolean', | ||||||||||||||||||
| dflt: false, | ||||||||||||||||||
| description: [ | ||||||||||||||||||
| 'Should we include a ModeBar button, labeled "Edit in Chart Studio",', | ||||||||||||||||||
| 'that sends this chart to chart-studio.plotly.com (formerly plot.ly) or another plotly server', | ||||||||||||||||||
| 'as specified by `plotlyServerURL` for editing, export, etc? Prior to version 1.43.0', | ||||||||||||||||||
| 'this button was included by default, now it is opt-in using this flag.', | ||||||||||||||||||
| 'Note that this button can (depending on `plotlyServerURL` being set) send your data', | ||||||||||||||||||
| 'to an external server. However that server does not persist your data', | ||||||||||||||||||
| 'until you arrive at the Chart Studio and explicitly click "Save".' | ||||||||||||||||||
| ].join(' ') | ||||||||||||||||||
| }, | ||||||||||||||||||
| showEditInChartStudio: { | ||||||||||||||||||
| valType: 'boolean', | ||||||||||||||||||
| dflt: false, | ||||||||||||||||||
| dflt: true, | ||||||||||||||||||
| description: [ | ||||||||||||||||||
| 'Same as `showSendToCloud`, but use a pencil icon instead of a floppy-disk.', | ||||||||||||||||||
| 'Note that if both `showSendToCloud` and `showEditInChartStudio` are turned,', | ||||||||||||||||||
| 'only `showEditInChartStudio` will be honored.' | ||||||||||||||||||
| 'Should we include a modebar button that sends this chart to a URL', | ||||||||||||||||||
| 'specified by `plotlyServerURL`, for sharing the chart with others?', | ||||||||||||||||||
| 'Note that this button can (depending on `plotlyServerURL` being set)', | ||||||||||||||||||
| 'send your data to an external server.' | ||||||||||||||||||
|
Comment on lines
+276
to
+279
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| ].join(' ') | ||||||||||||||||||
| }, | ||||||||||||||||||
| modeBarButtonsToRemove: { | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ var formatLocale = require('d3-format').formatLocale; | |||||
| var isNumeric = require('fast-isnumeric'); | ||||||
| var b64encode = require('base64-arraybuffer'); | ||||||
|
|
||||||
| var version = require('../version').version; | ||||||
| var Registry = require('../registry'); | ||||||
| var PlotSchema = require('../plot_api/plot_schema'); | ||||||
| var Template = require('../plot_api/plot_template'); | ||||||
|
|
@@ -202,35 +203,55 @@ function positionPlayWithData(gd, container) { | |||||
|
|
||||||
| plots.sendDataToCloud = function(gd) { | ||||||
| var baseUrl = (window.PLOTLYENV || {}).BASE_URL || gd._context.plotlyServerURL; | ||||||
| if(!baseUrl) return; | ||||||
| if(!baseUrl) { | ||||||
| console.error('No destination URL provided (plotlyServerURL is not set)'); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Plotly Cloud origin, used to validate incoming messages and to target outgoing ones. | ||||||
| // `baseUrl` (plotlyServerURL) is the upload page that handles login and signals | ||||||
| // back when authentication succeeds. | ||||||
| var cloudOrigin; | ||||||
| try { | ||||||
| cloudOrigin = new URL(baseUrl).origin; | ||||||
| } catch(e) { | ||||||
| console.error('Invalid plotlyServerURL: ' + baseUrl); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| gd.emit('plotly_beforeexport'); | ||||||
|
|
||||||
| var hiddenformDiv = d3.select(gd) | ||||||
| .append('div') | ||||||
| .attr('id', 'hiddenform') | ||||||
| .style('display', 'none'); | ||||||
| // Build the request body: the chart JSON plus the plotly.js version used to | ||||||
| // generate it, so Cloud can host the chart with a compatible plotly.js version. | ||||||
| var chart = JSON.parse(plots.graphJson(gd, false, 'keepdata')); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can avoid serializing and de-serializing by doing this I think
Suggested change
|
||||||
| chart.version = version; | ||||||
|
|
||||||
| // Open the Cloud login page in a new tab. We keep a reference so we can post | ||||||
| // the chart back to it once Cloud reports that authentication succeeded. | ||||||
| var cloudWindow = window.open(baseUrl, '_blank'); | ||||||
| if(!cloudWindow) { | ||||||
| console.error('Unable to open Plotly Cloud (the popup may have been blocked)'); | ||||||
| gd.emit('plotly_afterexport'); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May not want to emit |
||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| var handleMessage = function(event) { | ||||||
| // Only trust messages coming from the Cloud origin. | ||||||
| if(event.origin !== cloudOrigin) return; | ||||||
|
|
||||||
| var hiddenform = hiddenformDiv | ||||||
| .append('form') | ||||||
| .attr({ | ||||||
| action: baseUrl + '/external', | ||||||
| method: 'post', | ||||||
| target: '_blank' | ||||||
| }); | ||||||
| if(event.data && event.data.type === 'authenticated') { | ||||||
| cloudWindow.postMessage({ | ||||||
| type: 'chart', | ||||||
| chart: chart | ||||||
| }, cloudOrigin); | ||||||
|
|
||||||
| var hiddenformInput = hiddenform | ||||||
| .append('input') | ||||||
| .attr({ | ||||||
| type: 'text', | ||||||
| name: 'data' | ||||||
| }); | ||||||
| window.removeEventListener('message', handleMessage); | ||||||
| gd.emit('plotly_afterexport'); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| hiddenformInput.node().value = plots.graphJson(gd, false, 'keepdata'); | ||||||
| hiddenform.node().submit(); | ||||||
| hiddenformDiv.remove(); | ||||||
| window.addEventListener('message', handleMessage); | ||||||
|
|
||||||
| gd.emit('plotly_afterexport'); | ||||||
| return false; | ||||||
| }; | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these strings should (eventually) be wrapped in the localization function (search for
Lib._(or_(to find where this is done elsewhere in the codebase)