2017-11-12 02:44:26 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { reg } = require('lib/registry.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2017-11-12 02:44:26 +02:00
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
|
|
|
const { Header } = require('./Header.min.js');
|
|
|
|
const { themeStyle } = require('../theme.js');
|
2017-12-08 23:51:59 +02:00
|
|
|
const pathUtils = require('lib/path-utils.js');
|
2017-11-12 02:44:26 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
|
|
|
|
class ConfigScreenComponent extends React.Component {
|
|
|
|
|
2017-12-08 00:29:02 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
settings: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this.setState({ settings: this.props.settings });
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:30:32 +02:00
|
|
|
keyValueToArray(kv) {
|
|
|
|
let output = [];
|
|
|
|
for (let k in kv) {
|
|
|
|
if (!kv.hasOwnProperty(k)) continue;
|
|
|
|
output.push({
|
|
|
|
key: k,
|
|
|
|
label: kv[k],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
output.sort((a, b) => {
|
|
|
|
return a.label.toLowerCase() < b.label.toLowerCase() ? -1 : +1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-11-12 02:44:26 +02:00
|
|
|
settingToComponent(key, value) {
|
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
|
|
|
|
let output = null;
|
|
|
|
|
|
|
|
const rowStyle = {
|
|
|
|
marginBottom: 10,
|
|
|
|
};
|
|
|
|
|
|
|
|
const labelStyle = Object.assign({}, theme.textStyle, {
|
|
|
|
display: 'inline-block',
|
|
|
|
marginRight: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const controlStyle = {
|
|
|
|
display: 'inline-block',
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateSettingValue = (key, value) => {
|
2017-12-08 00:29:02 +02:00
|
|
|
const settings = Object.assign({}, this.state.settings);
|
|
|
|
settings[key] = value;
|
|
|
|
this.setState({ settings: settings });
|
2017-11-12 02:44:26 +02:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:36:26 +02:00
|
|
|
// Component key needs to be key+value otherwise it doesn't update when the settings change.
|
|
|
|
|
2017-11-12 02:44:26 +02:00
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
|
|
|
|
if (md.isEnum) {
|
|
|
|
let items = [];
|
|
|
|
const settingOptions = md.options();
|
2017-12-17 13:30:32 +02:00
|
|
|
let array = this.keyValueToArray(settingOptions);
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
const e = array[i];
|
|
|
|
items.push(<option value={e.key.toString()} key={e.key}>{settingOptions[e.key]}</option>);
|
2017-11-12 02:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-12-15 09:31:57 +02:00
|
|
|
<div key={key} style={rowStyle}>
|
2017-11-12 02:44:26 +02:00
|
|
|
<div style={labelStyle}><label>{md.label()}</label></div>
|
|
|
|
<select value={value} style={controlStyle} onChange={(event) => { updateSettingValue(key, event.target.value) }}>
|
|
|
|
{items}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (md.type === Setting.TYPE_BOOL) {
|
2017-12-08 00:29:02 +02:00
|
|
|
const onCheckboxClick = (event) => {
|
|
|
|
updateSettingValue(key, !value)
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:31:49 +02:00
|
|
|
// Hack: The {key+value.toString()} is needed as otherwise the checkbox doesn't update when the state changes.
|
|
|
|
// There's probably a better way to do this but can't figure it out.
|
|
|
|
|
2017-11-12 02:44:26 +02:00
|
|
|
return (
|
2018-01-23 20:31:49 +02:00
|
|
|
<div key={key+value.toString()} style={rowStyle}>
|
2017-11-12 02:44:26 +02:00
|
|
|
<div style={controlStyle}>
|
2018-01-23 20:31:49 +02:00
|
|
|
<input id={'setting_checkbox_' + key} type="checkbox" checked={!!value} onChange={(event) => { onCheckboxClick(event) }}/><label onClick={(event) => { onCheckboxClick(event) }} style={labelStyle} htmlFor={'setting_checkbox_' + key}>{md.label()}</label>
|
2017-11-12 02:44:26 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2017-12-08 00:29:02 +02:00
|
|
|
} else if (md.type === Setting.TYPE_STRING) {
|
|
|
|
const onTextChange = (event) => {
|
2018-01-23 20:31:49 +02:00
|
|
|
updateSettingValue(key, event.target.value);
|
2017-12-08 00:29:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-26 00:44:09 +02:00
|
|
|
const inputType = md.secure === true ? 'password' : 'text';
|
|
|
|
|
2017-12-08 00:29:02 +02:00
|
|
|
return (
|
2017-12-15 09:31:57 +02:00
|
|
|
<div key={key} style={rowStyle}>
|
2017-12-08 00:29:02 +02:00
|
|
|
<div style={labelStyle}><label>{md.label()}</label></div>
|
2018-01-26 00:44:09 +02:00
|
|
|
<input type={inputType} style={controlStyle} value={this.state.settings[key]} onChange={(event) => {onTextChange(event)}} />
|
2017-12-08 00:29:02 +02:00
|
|
|
</div>
|
|
|
|
);
|
2018-01-19 14:27:44 +02:00
|
|
|
} else if (md.type === Setting.TYPE_INT) {
|
|
|
|
const onNumChange = (event) => {
|
2018-01-23 20:31:49 +02:00
|
|
|
updateSettingValue(key, event.target.value);
|
2018-01-19 14:27:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2018-01-23 20:31:49 +02:00
|
|
|
<div key={key} style={rowStyle}>
|
|
|
|
<div style={labelStyle}><label>{md.label()}</label></div>
|
|
|
|
<input type="number" style={controlStyle} value={this.state.settings[key]} onChange={(event) => {onNumChange(event)}} min={md.minimum} max={md.maximum} step={md.step}/>
|
|
|
|
</div>
|
2018-01-19 14:27:44 +02:00
|
|
|
);
|
2017-12-08 00:29:02 +02:00
|
|
|
} else {
|
|
|
|
console.warn('Type not implemented: ' + key);
|
2017-11-12 02:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-12-08 00:29:02 +02:00
|
|
|
onSaveClick() {
|
|
|
|
for (let n in this.state.settings) {
|
|
|
|
if (!this.state.settings.hasOwnProperty(n)) continue;
|
|
|
|
Setting.setValue(n, this.state.settings[n]);
|
|
|
|
}
|
|
|
|
this.props.dispatch({ type: 'NAV_BACK' });
|
|
|
|
}
|
|
|
|
|
|
|
|
onCancelClick() {
|
|
|
|
this.props.dispatch({ type: 'NAV_BACK' });
|
|
|
|
}
|
|
|
|
|
2017-11-12 02:44:26 +02:00
|
|
|
render() {
|
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
const style = this.props.style;
|
2017-12-08 00:29:02 +02:00
|
|
|
const settings = this.state.settings;
|
2017-11-12 02:44:26 +02:00
|
|
|
|
|
|
|
const headerStyle = {
|
|
|
|
width: style.width,
|
|
|
|
};
|
|
|
|
|
|
|
|
const containerStyle = {
|
|
|
|
padding: 10,
|
|
|
|
};
|
|
|
|
|
2017-12-08 00:29:02 +02:00
|
|
|
const buttonStyle = {
|
|
|
|
display: this.state.settings === this.props.settings ? 'none' : 'inline-block',
|
|
|
|
marginRight: 10,
|
|
|
|
}
|
|
|
|
|
2017-11-12 02:44:26 +02:00
|
|
|
let settingComps = [];
|
|
|
|
let keys = Setting.keys(true, 'desktop');
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!(key in settings)) {
|
|
|
|
console.warn('Missing setting: ' + key);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-08 00:29:02 +02:00
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
if (md.show && !md.show(settings)) continue;
|
2017-11-12 02:44:26 +02:00
|
|
|
const comp = this.settingToComponent(key, settings[key]);
|
|
|
|
if (!comp) continue;
|
|
|
|
settingComps.push(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={style}>
|
|
|
|
<Header style={headerStyle} />
|
|
|
|
<div style={containerStyle}>
|
2017-12-08 23:51:59 +02:00
|
|
|
<div style={Object.assign({}, theme.textStyle, {marginBottom: 20})}>
|
|
|
|
{_('Notes and settings are stored in: %s', pathUtils.toSystemSlashes(Setting.value('profileDir'), process.platform))}
|
|
|
|
</div>
|
2017-11-12 02:44:26 +02:00
|
|
|
{ settingComps }
|
2017-12-08 00:29:02 +02:00
|
|
|
<button onClick={() => {this.onSaveClick()}} style={buttonStyle}>{_('Save')}</button>
|
|
|
|
<button onClick={() => {this.onCancelClick()}} style={buttonStyle}>{_('Cancel')}</button>
|
2017-11-12 02:44:26 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
return {
|
|
|
|
theme: state.settings.theme,
|
|
|
|
settings: state.settings,
|
|
|
|
locale: state.settings.locale,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const ConfigScreen = connect(mapStateToProps)(ConfigScreenComponent);
|
|
|
|
|
|
|
|
module.exports = { ConfigScreen };
|