|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import Form from 'react-jsonschema-form'; |
| 4 | + |
| 5 | +import './styles.css'; |
| 6 | + |
| 7 | +import defaultConfig from '../src/components/Graph/config'; |
| 8 | +import { Graph } from '../src'; |
| 9 | +import mock from './miserables'; |
| 10 | +import Utils from './utils'; |
| 11 | +import ReactD3GraphUtils from '../src/utils'; |
| 12 | + |
| 13 | +export default class Sandbox extends React.Component { |
| 14 | + constructor(props) { |
| 15 | + super(props); |
| 16 | + |
| 17 | + const schemaProps = Utils.generateFormSchema(defaultConfig, '', {}); |
| 18 | + |
| 19 | + const schema = { |
| 20 | + type: 'object', |
| 21 | + properties: schemaProps |
| 22 | + }; |
| 23 | + |
| 24 | + const uiSchema = { |
| 25 | + height: {'ui:readonly': 'true'}, |
| 26 | + width: {'ui:readonly': 'true'} |
| 27 | + }; |
| 28 | + |
| 29 | + this.uiSchema = uiSchema; |
| 30 | + |
| 31 | + this.state = { |
| 32 | + config: defaultConfig, |
| 33 | + generatedConfig: {}, |
| 34 | + schema |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + onClickNode = (id) => window.alert(`clicked node ${id}`); |
| 39 | + |
| 40 | + onClickLink = (source, target) => window.alert(`clicked link between ${source} and ${target}`); |
| 41 | + |
| 42 | + onMouseOverNode = () => { |
| 43 | + // Do something with the node identifier ... |
| 44 | + } |
| 45 | + |
| 46 | + onMouseOutNode = () => { |
| 47 | + // Do something with the node identifier ... |
| 48 | + } |
| 49 | + |
| 50 | + pauseGraphSimulation = () => this.refs.graph.pauseSimulation(); |
| 51 | + |
| 52 | + restartGraphSimulation = () => this.refs.graph.restartSimulation(); |
| 53 | + |
| 54 | + resetNodesPositions = () => this.refs.graph.resetNodesPositions(); |
| 55 | + |
| 56 | + _buildGraphConfig = (data) => { |
| 57 | + let config = {}; |
| 58 | + let schemaPropsValues = {}; |
| 59 | + |
| 60 | + for(let k of Object.keys(data.formData)) { |
| 61 | + // Set value mapping correctly for config object of react-d3-graph |
| 62 | + Utils.setValue(config, k, data.formData[k]); |
| 63 | + // Set new values for schema of jsonform |
| 64 | + schemaPropsValues[k] = {}; |
| 65 | + schemaPropsValues[k]['default'] = data.formData[k] |
| 66 | + } |
| 67 | + |
| 68 | + return {config, schemaPropsValues}; |
| 69 | + } |
| 70 | + |
| 71 | + refreshGraph = (data) => { |
| 72 | + const {config, schemaPropsValues} = this._buildGraphConfig(data); |
| 73 | + |
| 74 | + this.state.schema.properties = ReactD3GraphUtils.merge(this.state.schema.properties, schemaPropsValues); |
| 75 | + |
| 76 | + this.setState({ |
| 77 | + config |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // Generate graph configuration file ready to use! |
| 82 | + onSubmit = (data) => { |
| 83 | + const {config, schemaPropsValues} = this._buildGraphConfig(data); |
| 84 | + |
| 85 | + this.setState({ |
| 86 | + generatedConfig: config |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + onClickSubmit = () => { |
| 91 | + // Hack for allow submit button to live outside jsonform |
| 92 | + document.body.querySelector('.invisible-button').click(); |
| 93 | + } |
| 94 | + |
| 95 | + resetGraphConfig = () => { |
| 96 | + const schemaProps = Utils.generateFormSchema(defaultConfig, '', {}); |
| 97 | + |
| 98 | + const schema = { |
| 99 | + type: 'object', |
| 100 | + properties: schemaProps |
| 101 | + }; |
| 102 | + |
| 103 | + this.setState({ |
| 104 | + config: defaultConfig, |
| 105 | + schema |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + render() { |
| 110 | + const graphProps = { |
| 111 | + id: 'graph', |
| 112 | + data: mock, |
| 113 | + config: this.state.config, |
| 114 | + onClickNode: this.onClickNode, |
| 115 | + onClickLink: this.onClickLink, |
| 116 | + onMouseOverNode: this.onMouseOverNode, |
| 117 | + onMouseOutNode: this.onMouseOutNode |
| 118 | + }; |
| 119 | + |
| 120 | + const btnStyle = { |
| 121 | + cursor: this.state.config.staticGraph ? 'not-allowed' : 'pointer' |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className='container'> |
| 126 | + <div className='container__graph'> |
| 127 | + <button onClick={this.restartGraphSimulation} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>▶️</button> |
| 128 | + <button onClick={this.pauseGraphSimulation} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>⏸️</button> |
| 129 | + <button onClick={this.resetNodesPositions} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>Unstick nodes</button> |
| 130 | + <Graph ref='graph' {...graphProps}/> |
| 131 | + </div> |
| 132 | + <div className='container__form'> |
| 133 | + <h4>Graph configurations</h4> |
| 134 | + <Form className='form-wrapper' |
| 135 | + schema={this.state.schema} |
| 136 | + uiSchema={this.uiSchema} |
| 137 | + onChange={this.refreshGraph} |
| 138 | + onSubmit={this.onSubmit}> |
| 139 | + <button className='invisible-button' type='submit'></button> |
| 140 | + </Form> |
| 141 | + <button className='submit-button btn btn-primary' onClick={this.onClickSubmit}>Generate config</button> |
| 142 | + <button className='reset-button btn btn-danger' onClick={this.resetGraphConfig}>Reset config</button> |
| 143 | + </div> |
| 144 | + <div className='container__graph-config'> |
| 145 | + <h4>Your config</h4> |
| 146 | + <JSONContainer data={this.state.generatedConfig} /> |
| 147 | + </div> |
| 148 | + <div className='container__graph-data'> |
| 149 | + <h4>Graph data</h4> |
| 150 | + <JSONContainer data={mock} /> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +class JSONContainer extends React.Component { |
| 158 | + shouldComponentUpdate(nextProps, nextState) { |
| 159 | + return JSON.stringify(nextProps.data) !== JSON.stringify(this.props.data); |
| 160 | + } |
| 161 | + |
| 162 | + render() { |
| 163 | + return ( |
| 164 | + <pre className='json-data-container'>{JSON.stringify(this.props.data, null, 2)}</pre> |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments