-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from BrainMaestro/add-plugin-settings
Add plugin settings
- Loading branch information
Showing
24 changed files
with
388 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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 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 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,3 @@ | ||
import config from 'lib/config' | ||
|
||
export default (pluginName) => config.get('plugins')[pluginName] |
This file contains 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,4 @@ | ||
import get from './get' | ||
import validate from './validate' | ||
|
||
export default { get, validate } |
This file contains 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,24 @@ | ||
import { every } from 'lodash/fp' | ||
|
||
const VALID_TYPES = new Set([ | ||
'array', | ||
'string', | ||
'number', | ||
'bool', | ||
'option', | ||
]) | ||
|
||
const validSetting = ({ type, options }) => { | ||
// General validation of settings | ||
if (!type || !VALID_TYPES.has(type)) return false | ||
|
||
// Type-specific validations | ||
if (type === 'option') return Array.isArray(options) && options.length | ||
|
||
return true | ||
} | ||
|
||
export default ({ settings }) => { | ||
if (!settings) return true | ||
return every(validSetting)(settings) | ||
} |
This file contains 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 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,28 @@ | ||
import React, { PropTypes } from 'react' | ||
import styles from './styles.css' | ||
|
||
const Checkbox = ({ label, value, onChange, description }) => ( | ||
<div className={styles.item}> | ||
<div className={styles.itemValueWithoutLabel}> | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={value} | ||
onChange={({ target }) => onChange(target.checked)} | ||
className={styles.checkbox} | ||
/> | ||
{label} | ||
</label> | ||
<div className={styles.itemNotice}>{description}</div> | ||
</div> | ||
</div> | ||
) | ||
|
||
Checkbox.propTypes = { | ||
label: PropTypes.string, | ||
value: PropTypes.bool, | ||
onChange: PropTypes.func.isRequired, | ||
description: PropTypes.string, | ||
} | ||
|
||
export default Checkbox |
This file contains 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,41 @@ | ||
import React, { PropTypes } from 'react' | ||
import ReactSelect, { Creatable } from 'react-select' | ||
import Wrapper from './Wrapper' | ||
|
||
const Select = ({ label, value, onChange, description, options, multi, clearable, creatable }) => { | ||
const Component = creatable ? Creatable : ReactSelect | ||
return ( | ||
<Wrapper label={label} description={description}> | ||
<Component | ||
multi={multi} | ||
value={value} | ||
clearable={clearable} | ||
options={options} | ||
onChange={newValue => { | ||
if (!newValue) { | ||
return newValue | ||
} | ||
const changedValue = multi ? newValue.map(val => val.value) : newValue.value | ||
onChange(changedValue) | ||
}} | ||
/> | ||
</Wrapper> | ||
) | ||
} | ||
|
||
Select.propTypes = { | ||
label: PropTypes.string, | ||
value: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.number, | ||
PropTypes.array, | ||
]), | ||
onChange: PropTypes.func.isRequired, | ||
description: PropTypes.string, | ||
options: PropTypes.array.isRequired, | ||
multi: PropTypes.bool, | ||
clearable: PropTypes.bool, | ||
creatable: PropTypes.bool | ||
} | ||
|
||
export default Select |
This file contains 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,27 @@ | ||
import React, { PropTypes } from 'react' | ||
import Wrapper from './Wrapper' | ||
import styles from './styles.css' | ||
|
||
const Input = ({ label, value, onChange, description, type }) => ( | ||
<Wrapper label={label} description={description}> | ||
<input | ||
type={type} | ||
value={value || ''} | ||
className={styles.input} | ||
onChange={({ target }) => onChange(target.value)} | ||
/> | ||
</Wrapper> | ||
) | ||
|
||
Input.propTypes = { | ||
label: PropTypes.string, | ||
value: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.number, | ||
]), | ||
onChange: PropTypes.func.isRequired, | ||
description: PropTypes.string, | ||
type: PropTypes.string.isRequired, | ||
} | ||
|
||
export default Input |
This file contains 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,20 @@ | ||
import React, { PropTypes } from 'react' | ||
import styles from './styles.css' | ||
|
||
const Wrapper = ({ label, description, children }) => ( | ||
<div className={styles.item}> | ||
{label && <label className={styles.label}>{label}:</label>} | ||
<div className={label ? styles.itemValue : styles.itemValueWithoutLabel}> | ||
{children} | ||
<div className={styles.itemNotice}>{description}</div> | ||
</div> | ||
</div> | ||
) | ||
|
||
Wrapper.propTypes = { | ||
label: PropTypes.string, | ||
description: PropTypes.string, | ||
children: PropTypes.any | ||
} | ||
|
||
export default Wrapper |
This file contains 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,7 @@ | ||
import Checkbox from './Checkbox' | ||
import Text from './Text' | ||
import Select from './Select' | ||
|
||
export { | ||
Checkbox, Text, Select | ||
} |
This file contains 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,48 @@ | ||
.item { | ||
display: flex; | ||
flex-direction: row; | ||
width: 250px; | ||
margin-top: 20px; | ||
&:first-child { | ||
margin-top: 0; | ||
} | ||
} | ||
|
||
.itemValue { | ||
display: flex; | ||
flex-direction: column; | ||
flex-grow: 2; | ||
} | ||
|
||
.itemValueWithoutLabel { | ||
composes: itemValue; | ||
margin-left: 75px; | ||
} | ||
|
||
.itemNotice{ | ||
color: var(--secondary-font-color); | ||
font-size: .8em; | ||
margin-top: 5px; | ||
} | ||
|
||
.label { | ||
margin-right: 15px; | ||
margin-top: 8px; | ||
min-width: 60px; | ||
max-width: 60px; | ||
} | ||
|
||
.input { | ||
font-size: 16px; | ||
line-height: 34px; | ||
padding: 0 10px; | ||
box-sizing: border-box; | ||
width: 100%; | ||
border-color: #d9d9d9 #ccc #b3b3b3; | ||
border-radius: 4px; | ||
border: 1px solid #ccc; | ||
} | ||
|
||
.checkbox { | ||
margin-right: 5px; | ||
} |
This file contains 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,23 @@ | ||
import React, { PropTypes } from 'react' | ||
import { Select, Text, Checkbox } from 'main/components/Form' | ||
|
||
const components = { | ||
bool: Checkbox, | ||
option: Select, | ||
array: Select, | ||
} | ||
|
||
const FormItem = ({ type, ...props }) => { | ||
const Component = components[type] || Text | ||
|
||
return ( | ||
<Component type={type} {...props} /> | ||
) | ||
} | ||
|
||
FormItem.propTypes = { | ||
value: PropTypes.any, | ||
type: PropTypes.string.isRequired, | ||
} | ||
|
||
export default FormItem |
This file contains 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,59 @@ | ||
import React, { PropTypes, Component } from 'react' | ||
import config from 'lib/config' | ||
import FormItem from './FormItem' | ||
import styles from './styles.css' | ||
|
||
export default class Settings extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
values: config.get('plugins')[props.name], | ||
} | ||
this.renderSetting = this.renderSetting.bind(this) | ||
this.changeSetting = this.changeSetting.bind(this) | ||
} | ||
|
||
changeSetting(plugin, label, value) { | ||
const values = { | ||
...this.state.values, | ||
[label]: value, | ||
} | ||
|
||
this.setState({ values }) | ||
config.set('plugins', { | ||
...config.get('plugins'), | ||
[this.props.name]: values, | ||
}) | ||
} | ||
|
||
renderSetting(key) { | ||
const setting = this.props.settings[key] | ||
const { defaultValue, label, ...restProps } = setting | ||
const value = key in this.state.values ? this.state.values[key] : defaultValue | ||
|
||
return ( | ||
<FormItem | ||
key={key} | ||
label={label || key} | ||
value={value} | ||
onChange={newValue => this.changeSetting(this.props.name, key, newValue)} | ||
{...restProps} | ||
/> | ||
) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className={styles.settingsWrapper}> | ||
{ | ||
Object.keys(this.props.settings).map(this.renderSetting) | ||
} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
Settings.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
settings: PropTypes.object.isRequired, | ||
} |
This file contains 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
Oops, something went wrong.