Skip to content

Commit

Permalink
Added template configuration for plugins in the plugins example
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarto committed Nov 16, 2016
1 parent 2dc95e1 commit 86918a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions web/client/examples/plugins/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const startApp = () => {
return Object.keys(plugins).map((plugin) => {
const pluginName = plugin.substring(0, plugin.length - 6);
return (<PluginConfigurator key={pluginName} pluginName={pluginName} pluginsCfg={pluginsCfg.standard}
pluginImpl={plugins[plugin][plugin]}
onToggle={togglePlugin.bind(null, pluginName, callback)}
onApplyCfg={configurePlugin.bind(null, plugin, callback)}
pluginConfig={userCfg[pluginName + 'Plugin'] && JSON.stringify(userCfg[pluginName + 'Plugin'], null, 2) || "{}"}
Expand Down
44 changes: 40 additions & 4 deletions web/client/examples/plugins/components/PluginConfigurator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ require('codemirror/lib/codemirror.css');

require('codemirror/mode/javascript/javascript');

const assign = require('object-assign');

const PluginConfigurator = React.createClass({
propTypes: {
pluginName: React.PropTypes.string,
pluginsCfg: React.PropTypes.array,
onToggle: React.PropTypes.func,
onApplyCfg: React.PropTypes.func,
pluginConfig: React.PropTypes.string
pluginConfig: React.PropTypes.string,
pluginImpl: React.PropTypes.object
},
getInitialState() {
return {
Expand All @@ -40,14 +42,33 @@ const PluginConfigurator = React.createClass({
});
}
},
getPropValue(type) {
if (type === React.PropTypes.string || type === React.PropTypes.string.isRequired) {
return '';
}
if (type === React.PropTypes.number || type === React.PropTypes.number.isRequired) {
return 0;
}
if (type === React.PropTypes.bool || type === React.PropTypes.bool.isRequired) {
return false;
}
if (type === React.PropTypes.object || type === React.PropTypes.object.isRequired) {
return {};
}
if (type === React.PropTypes.array || type === React.PropTypes.array.isRequired) {
return [];
}
return null;
},
renderCfg() {
return this.state.configVisible ? [
<label key="config-label">Enter a JSON object to configure plugin properties</label>,
<Codemirror key="code-mirror" value={this.state.code} onChange={this.updateCode} options={{
mode: {name: "javascript", json: true},
lineNumbers: true
}}/>,
<Button key="apply-cfg" onClick={this.applyCfg}>Apply</Button>
<Button key="apply-cfg" onClick={this.applyCfg}>Apply</Button>,
<Button key="help-cfg" onClick={this.showProps}><Glyphicon glyph="question-sign"/></Button>
] : null;
},
render() {
Expand All @@ -58,11 +79,26 @@ const PluginConfigurator = React.createClass({
checked={this.props.pluginsCfg.indexOf(this.props.pluginName) !== -1}
label={this.props.pluginName}
onChange={this.props.onToggle}/>

{this.renderCfg()}
</li>);
},
updateCode: function(newCode) {
showProps() {
if (this.props.pluginImpl) {
const plugin = this.props.pluginImpl;
const pluginProps = plugin.WrappedComponent && plugin.WrappedComponent.propTypes || plugin.propTypes;

const propsValues = (plugin.WrappedComponent && plugin.WrappedComponent.getDefaultProps && plugin.WrappedComponent.getDefaultProps()) ||
(plugin.getDefaultProps && plugin.getDefaultProps()) || {};

const props = Object.keys(pluginProps || {}).reduce((previous, current) => {
return assign(previous, {[current]: this.getPropValue(pluginProps[current])}, propsValues);
}, {});
this.setState({
code: JSON.stringify(props, null, 4)
});
}
},
updateCode(newCode) {
this.setState({
code: newCode
});
Expand Down

0 comments on commit 86918a2

Please sign in to comment.