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

120 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-05-16 21:57:09 +02:00
import React, { Component } from 'react';
import { connect } from 'react-redux'
2017-05-16 22:25:19 +02:00
import { View, Text, Button, StyleSheet } from 'react-native';
2017-05-16 21:57:09 +02:00
import { Log } from 'src/log.js';
2017-05-16 22:25:19 +02:00
import { Menu, MenuOptions, MenuOption, MenuTrigger } from 'react-native-popup-menu';
2017-05-16 21:57:09 +02:00
import { _ } from 'src/locale.js';
2017-05-16 23:46:21 +02:00
import { Setting } from 'src/models/setting.js';
2017-05-16 21:57:09 +02:00
2017-05-16 22:25:19 +02:00
const styles = StyleSheet.create({
divider: {
marginVertical: 5,
marginHorizontal: 2,
borderBottomWidth: 1,
borderColor: '#ccc'
},
});
2017-05-16 21:57:09 +02:00
class ScreenHeaderComponent extends Component {
2017-05-16 22:25:19 +02:00
static defaultProps = {
menuOptions: [],
};
2017-05-16 21:57:09 +02:00
showBackButton() {
// Note: this is hardcoded for now because navigation.state doesn't tell whether
// it's possible to go back or not. Maybe it's possible to get this information
// from somewhere else.
return this.props.navState.routeName != 'Folders';
}
2017-05-24 21:27:13 +02:00
sideMenuButton_press = () => {
this.props.dispatch({ type: 'SIDE_MENU_TOGGLE' });
}
2017-05-16 21:57:09 +02:00
backButton_press = () => {
this.props.dispatch({ type: 'Navigation/BACK' });
}
2017-05-16 22:25:19 +02:00
menu_select = (value) => {
if (typeof(value) == 'function') {
value();
}
}
2017-05-16 23:46:21 +02:00
menu_login = () => {
this.props.dispatch({
type: 'Navigation/NAVIGATE',
routeName: 'Login',
});
}
menu_logout = () => {
let user = { email: null, session: null };
Setting.setObject('user', user);
this.props.dispatch({
type: 'USER_SET',
user: user,
});
}
2017-05-16 21:57:09 +02:00
render() {
2017-05-16 23:46:21 +02:00
let key = 0;
2017-05-16 22:25:19 +02:00
let menuOptionComponents = [];
for (let i = 0; i < this.props.menuOptions.length; i++) {
let o = this.props.menuOptions[i];
menuOptionComponents.push(
2017-05-16 23:46:21 +02:00
<MenuOption value={o.onPress} key={'menuOption_' + key++}>
2017-05-16 22:25:19 +02:00
<Text>{o.title}</Text>
2017-05-16 23:46:21 +02:00
</MenuOption>);
}
if (menuOptionComponents.length) {
menuOptionComponents.push(<View key={'menuOption_' + key++} style={styles.divider}/>);
2017-05-16 22:25:19 +02:00
}
2017-05-16 23:46:21 +02:00
if (this.props.user && this.props.user.session) {
menuOptionComponents.push(
<MenuOption value={this.menu_logout} key={'menuOption_' + key++}>
<Text>{_('Logout')}</Text>
</MenuOption>);
} else {
menuOptionComponents.push(
<MenuOption value={this.menu_login} key={'menuOption_' + key++}>
<Text>{_('Login')}</Text>
</MenuOption>);
}
menuOptionComponents.push(
<MenuOption value={1} key={'menuOption_' + key++}>
<Text>{_('Configuration')}</Text>
</MenuOption>);
2017-05-16 22:25:19 +02:00
let title = 'title' in this.props && this.props.title !== null ? this.props.title : _(this.props.navState.routeName);
2017-05-16 21:57:09 +02:00
return (
<View style={{ flexDirection: 'row', padding: 10, backgroundColor: '#ffffff', alignItems: 'center' }} >
2017-05-24 21:27:13 +02:00
<Button title="☰" onPress={this.sideMenuButton_press} />
2017-05-16 21:57:09 +02:00
<Button disabled={!this.showBackButton()} title="<" onPress={this.backButton_press}></Button>
2017-05-16 22:25:19 +02:00
<Text style={{ flex:1, marginLeft: 10 }} >{title}</Text>
<Menu onSelect={this.menu_select}>
<MenuTrigger>
<Text style={{ fontSize: 20 }}> &#8942; </Text>
</MenuTrigger>
<MenuOptions>
{ menuOptionComponents }
</MenuOptions>
</Menu>
2017-05-16 21:57:09 +02:00
</View>
);
}
}
const ScreenHeader = connect(
(state) => {
2017-05-16 23:46:21 +02:00
return { user: state.user };
2017-05-16 21:57:09 +02:00
}
)(ScreenHeaderComponent)
export { ScreenHeader };