-
Notifications
You must be signed in to change notification settings - Fork 2
✨ first version of layoutUpdater #9
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
Draft
jonasvdd
wants to merge
1
commit into
master
Choose a base branch
from
layoutUpdater
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { getGraphDiv, removeApplyRelayoutEvent } from '../utils/updaterUtils'; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * LayoutUpdater is a component which updates the layout of a plotly graph. | ||
| */ | ||
| export default class LayoutUpdater extends Component { | ||
| static #previousLayout = {}; | ||
|
|
||
| shouldComponentUpdate({ updateData }) { | ||
| return typeof updateData == 'object' && LayoutUpdater.#previousLayout !== updateData; | ||
| } | ||
|
|
||
| render() { | ||
| const { id, gdID, updateData, triggerRelayout } = this.props; | ||
| const idDiv = <div id={id}></div>; | ||
| if (!this.shouldComponentUpdate(this.props)) { | ||
| console.log("LayoutUpdater " + gdID + ": no update required"); | ||
| console.log("LayoutUpdater no update: updateData: " + updateData); | ||
| return idDiv; | ||
| } | ||
| console.log("LayoutUpdater " + gdID + ": update required"); | ||
| console.log("LayoutUpdater update: updateData: " + updateData); | ||
|
|
||
| LayoutUpdater.#previousLayout = updateData; | ||
| let trgrRelayout = triggerRelayout; | ||
| if (updateData.hasOwnProperty('triggerRelayout')) { | ||
| trgrRelayout =updateData.triggerRelayout; | ||
| delete updateData.triggerRelayout; | ||
| } | ||
|
|
||
| let graphDiv = getGraphDiv(gdID); | ||
| if (trgrRelayout === true) { Plotly.relayout(graphDiv, updateData); } | ||
| else { removeApplyRelayoutEvent(graphDiv, updateData); } | ||
| return idDiv; | ||
| } | ||
| } | ||
|
|
||
| LayoutUpdater.defaultProps = { | ||
| triggerRelayout: false, | ||
| }; | ||
|
|
||
| LayoutUpdater.propTypes = { | ||
| /** | ||
| * The ID used to identify this component in Dash callbacks. | ||
| */ | ||
| id: PropTypes.string, | ||
|
|
||
| /** | ||
| * The id of the graph-div whose layout will be updated. | ||
| * | ||
| * .. Note: | ||
| * | ||
| * * if you use multiple graphs; each graph MUST have a unique id; otherwise we | ||
| * cannot guarantee that resampling will work correctly. | ||
| * * LayoutUpdater will determine the html-graph-div by performing partial matching | ||
| * on the "id" property (using `gdID`) of all divs with classname="dash-graph". | ||
| * It will select the first item of that match list; so if multiple same | ||
| * graph-div IDs are used, or one graph-div-ID is a subset of the other (partial | ||
| * matching) there is no guarantee that the correct div will be selected. | ||
| */ | ||
| gdID: PropTypes.string.isRequired, | ||
|
|
||
| /** | ||
| * Whether a Relayout will be triggered after the update or not. | ||
| */ | ||
| triggerRelayout: PropTypes.bool, | ||
|
|
||
| /** | ||
| * The layout update data dict. | ||
| * If this contains the `triggerRelayout` key, it will be used to determine whether | ||
| * a relayout-event will be triggered or not. | ||
| */ | ||
| updateData: PropTypes.object, | ||
|
|
||
| /** | ||
| * Dash-assigned callback that should be called to report property changes | ||
| * to Dash, to make them available for callbacks. | ||
| */ | ||
| setProps: PropTypes.func | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| /* eslint-disable import/prefer-default-export */ | ||
| import TraceUpdater from './components/TraceUpdater.react'; | ||
| import LayoutUpdater from './components/LayoutUpdater.react'; | ||
|
|
||
| export { | ||
| TraceUpdater | ||
| TraceUpdater, | ||
| LayoutUpdater | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { isElement } from "lodash"; | ||
|
|
||
| export function getGraphDiv(gdID) { | ||
| // see this link for more information https://stackoverflow.com/a/34002028 | ||
| let graphDiv = document?.querySelectorAll('div[id*="' + gdID + '"][class*="dash-graph"]'); | ||
| if (graphDiv.length > 1) { | ||
| throw new SyntaxError("LayoutUpdater: multiple graphs with ID=\"" + gdID + "\" found; n=" + graphDiv.length + " \n(either multiple graphs with same ID's or current ID is a str-subset of other graph IDs)"); | ||
| } else if (graphDiv.length < 1) { | ||
| throw new SyntaxError("LayoutUpdater: no graphs with ID=\"" + gdID + "\" found"); | ||
| } | ||
| graphDiv = graphDiv?.[0]?.getElementsByClassName('js-plotly-plot')?.[0]; | ||
| if (!isElement(graphDiv)) { | ||
| throw new Error(`Invalid gdID '${gdID}'`); | ||
| } | ||
| return graphDiv; | ||
| } | ||
|
|
||
|
|
||
| Function.prototype.clone = function () { | ||
| let cloneObj = this; | ||
| if (this.__isClone) { | ||
| cloneObj = this.__clonedFrom; | ||
| } | ||
| let temp = function () { return cloneObj.apply(this, arguments); }; | ||
| for (let key in this) { | ||
| temp[key] = this[key]; | ||
| } | ||
| temp.__isClone = true; | ||
| temp.__clonedFrom = cloneObj; | ||
| return temp; | ||
| }; | ||
|
|
||
| export async function removeApplyRelayoutEvent(graphDiv, updateData) { | ||
| let relayout_func = graphDiv._ev._events.plotly_relayout; | ||
| if (Array.isArray(relayout_func)) { relayout_func = relayout_func[0]; }; | ||
| if (relayout_func == undefined) { | ||
| console.log('no relayout_func found for ' + graphDiv.parentNode.id); | ||
| return idDiv; | ||
| } | ||
| relayout_func = relayout_func.clone(); | ||
| // console.log("relayout_func get: ", graphDiv.parentNode.id, relayout_func); | ||
| await graphDiv.removeAllListeners('plotly_relayout'); | ||
| await Plotly.relayout(graphDiv, updateData); | ||
| console.log("relayout_func set: ", graphDiv.parentNode.id, " ", relayout_func); | ||
| await graphDiv.on('plotly_relayout', relayout_func); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it possible to distinguish between multiple matches versus multiple unique IDs? If I clone a graph and want the update to apply to the clone as well as the original, the clone would have the same ID.