1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/components/screens/config.js

174 lines
4.7 KiB
JavaScript
Raw Normal View History

const React = require('react'); const Component = React.Component;
const { View, Switch, Slider, StyleSheet, Text, Button, ScrollView } = require('react-native');
const { connect } = require('react-redux');
const { ScreenHeader } = require('lib/components/screen-header.js');
const { _, setLocale } = require('lib/locale.js');
const { BaseScreenComponent } = require('lib/components/base-screen.js');
const { Dropdown } = require('lib/components/Dropdown.js');
const { themeStyle } = require('lib/components/global-style.js');
const { Setting } = require('lib/models/setting.js');
2017-07-23 20:26:50 +02:00
2017-08-01 19:59:01 +02:00
class ConfigScreenComponent extends BaseScreenComponent {
static navigationOptions(options) {
return { header: null };
2017-07-30 23:04:26 +02:00
}
2017-07-23 20:26:50 +02:00
2017-08-01 19:59:01 +02:00
constructor() {
super();
this.styles_ = {};
}
2017-07-30 23:04:26 +02:00
2017-08-01 19:59:01 +02:00
styles() {
const themeId = this.props.theme;
const theme = themeStyle(themeId);
if (this.styles_[themeId]) return this.styles_[themeId];
this.styles_ = {};
let styles = {
body: {
flex: 1,
justifyContent: 'flex-start',
flexDirection: 'column',
},
2017-08-01 19:59:01 +02:00
settingContainer: {
flex: 1,
flexDirection: 'row',
2017-08-01 19:59:01 +02:00
borderBottomWidth: 1,
borderBottomColor: theme.dividerColor,
paddingTop: theme.marginTop,
paddingBottom: theme.marginBottom,
paddingLeft: theme.marginLeft,
paddingRight: theme.marginRight,
},
settingText: {
fontWeight: 'bold',
color: theme.color,
fontSize: theme.fontSize,
flex: 1,
2017-08-01 19:59:01 +02:00
},
settingControl: {
color: theme.color,
flex: 1,
2017-08-01 19:59:01 +02:00
},
}
2017-08-01 19:59:01 +02:00
styles.switchSettingText = Object.assign({}, styles.settingText);
styles.switchSettingText.width = '80%';
2017-08-01 19:59:01 +02:00
styles.switchSettingContainer = Object.assign({}, styles.settingContainer);
styles.switchSettingContainer.flexDirection = 'row';
styles.switchSettingContainer.justifyContent = 'space-between';
2017-07-23 20:26:50 +02:00
2017-08-01 19:59:01 +02:00
styles.switchSettingControl = Object.assign({}, styles.settingControl);
delete styles.switchSettingControl.color;
styles.switchSettingControl.width = '20%';
this.styles_[themeId] = StyleSheet.create(styles);
return this.styles_[themeId];
2017-07-23 20:26:50 +02:00
}
2017-07-31 22:51:24 +02:00
settingToComponent(key, value) {
const themeId = this.props.theme;
const theme = themeStyle(themeId);
2017-07-23 20:26:50 +02:00
let output = null;
const updateSettingValue = (key, value) => {
2017-07-31 22:51:24 +02:00
Setting.setValue(key, value);
2017-07-23 20:26:50 +02:00
}
2017-07-31 22:51:24 +02:00
const md = Setting.settingMetadata(key);
if (md.isEnum) {
value = value.toString();
2017-07-23 20:26:50 +02:00
let items = [];
2017-07-31 22:51:24 +02:00
const settingOptions = md.options();
2017-07-23 20:26:50 +02:00
for (let k in settingOptions) {
if (!settingOptions.hasOwnProperty(k)) continue;
items.push({ label: settingOptions[k], value: k.toString() });
2017-07-23 20:26:50 +02:00
}
return (
2017-08-01 19:59:01 +02:00
<View key={key} style={this.styles().settingContainer}>
<Text key="label" style={this.styles().settingText}>{md.label()}</Text>
<Dropdown
key="control"
style={this.styles().settingControl}
items={items}
selectedValue={value}
itemListStyle={{
backgroundColor: theme.backgroundColor,
}}
headerStyle={{
color: theme.color,
fontSize: theme.fontSize,
}}
itemStyle={{
color: theme.color,
fontSize: theme.fontSize,
}}
onValueChange={(itemValue, itemIndex) => { updateSettingValue(key, itemValue); }}
/>
2017-07-23 20:26:50 +02:00
</View>
);
2017-07-31 22:51:24 +02:00
} else if (md.type == Setting.TYPE_BOOL) {
return (
2017-08-01 19:59:01 +02:00
<View key={key} style={this.styles().switchSettingContainer}>
<Text key="label" style={this.styles().switchSettingText}>{md.label()}</Text>
<Switch key="control" style={this.styles().switchSettingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
</View>
);
2017-07-31 22:51:24 +02:00
} else if (md.type == Setting.TYPE_INT) {
2017-07-25 23:55:26 +02:00
return (
2017-08-01 19:59:01 +02:00
<View key={key} style={this.styles().settingContainer}>
<Text key="label" style={this.styles().settingText}>{md.label()}</Text>
<Slider key="control" style={this.styles().settingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
2017-07-25 23:55:26 +02:00
</View>
);
} else {
2017-07-23 20:26:50 +02:00
//throw new Error('Unsupported setting type: ' + setting.type);
}
return output;
}
render() {
2017-07-31 22:51:24 +02:00
const settings = this.props.settings;
2017-07-23 20:26:50 +02:00
let settingComps = [];
for (let key in settings) {
2017-07-24 23:29:40 +02:00
if (key == 'sync.target') continue;
2017-07-23 20:26:50 +02:00
if (!settings.hasOwnProperty(key)) continue;
2017-07-31 22:51:24 +02:00
if (!Setting.isPublic(key)) continue;
2017-07-23 20:26:50 +02:00
const comp = this.settingToComponent(key, settings[key]);
if (!comp) continue;
settingComps.push(comp);
}
//style={this.styles().body}
2017-07-23 20:26:50 +02:00
return (
2017-08-01 19:59:01 +02:00
<View style={this.rootStyle(this.props.theme).root}>
2017-07-24 23:58:14 +02:00
<ScreenHeader title={_('Configuration')}/>
<ScrollView >
2017-07-23 20:26:50 +02:00
{ settingComps }
</ScrollView>
2017-07-23 20:26:50 +02:00
</View>
);
}
}
const ConfigScreen = connect(
(state) => {
2017-08-01 19:59:01 +02:00
return {
settings: state.settings,
theme: state.settings.theme,
};
2017-07-23 20:26:50 +02:00
}
)(ConfigScreenComponent)
2017-11-03 02:13:17 +02:00
module.exports = { ConfigScreen };