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

171 lines
5.0 KiB
JavaScript
Raw Normal View History

2017-07-22 17:55:09 +02:00
import React, { Component } from 'react';
import { TouchableOpacity , Button, Text, Image, StyleSheet, ScrollView, View } from 'react-native';
2017-05-24 21:27:13 +02:00
import { connect } from 'react-redux'
2017-07-22 17:55:09 +02:00
import Icon from 'react-native-vector-icons/Ionicons';
2017-06-24 20:06:28 +02:00
import { Log } from 'lib/log.js';
import { Note } from 'lib/models/note.js';
2017-07-24 23:52:30 +02:00
import { Setting } from 'lib/models/setting.js';
2017-07-15 18:14:15 +02:00
import { FoldersScreenUtils } from 'lib/components/screens/folders-utils.js'
import { Synchronizer } from 'lib/synchronizer.js';
2017-07-06 21:48:17 +02:00
import { reg } from 'lib/registry.js';
import { _ } from 'lib/locale.js';
2017-07-21 23:40:02 +02:00
import { globalStyle } from 'lib/components/global-style.js';
2017-05-24 21:27:13 +02:00
2017-07-22 17:55:09 +02:00
const styleObject = {
2017-05-24 21:27:13 +02:00
menu: {
flex: 1,
2017-07-21 23:40:02 +02:00
backgroundColor: globalStyle.backgroundColor,
2017-07-22 17:55:09 +02:00
},
2017-05-24 21:27:13 +02:00
name: {
position: 'absolute',
left: 70,
top: 20,
},
item: {
fontSize: 14,
fontWeight: '300',
},
2017-07-22 17:55:09 +02:00
button: {
2017-07-15 20:56:24 +02:00
flex: 1,
2017-07-22 17:55:09 +02:00
flexDirection: 'row',
2017-07-21 23:40:02 +02:00
height: 36,
2017-07-22 17:55:09 +02:00
alignItems: 'center',
paddingLeft: globalStyle.marginLeft,
paddingRight: globalStyle.marginRight,
2017-07-15 20:56:24 +02:00
},
2017-07-22 17:55:09 +02:00
buttonText: {
2017-07-21 23:40:02 +02:00
flex: 1,
2017-07-22 17:55:09 +02:00
color: globalStyle.color,
paddingLeft: 10,
2017-07-15 20:56:24 +02:00
},
2017-07-22 17:55:09 +02:00
syncStatus: {
paddingLeft: globalStyle.marginLeft,
paddingRight: globalStyle.marginRight,
color: globalStyle.colorFaded,
},
};
styleObject.folderButton = Object.assign({}, styleObject.button);
styleObject.folderButtonText = Object.assign({}, styleObject.buttonText);
2017-07-22 18:36:55 +02:00
styleObject.folderIcon = Object.assign({}, globalStyle.icon);
2017-07-22 19:21:39 +02:00
styleObject.folderIcon.color = '#0072d5';
2017-07-22 17:55:09 +02:00
styleObject.syncButton = Object.assign({}, styleObject.button);
styleObject.syncButtonText = Object.assign({}, styleObject.buttonText);
styleObject.folderButtonSelected = Object.assign({}, styleObject.folderButton);
styleObject.folderButtonSelected.backgroundColor = globalStyle.selectedColor;
const styles = StyleSheet.create(styleObject);
2017-05-24 21:27:13 +02:00
class SideMenuContentComponent extends Component {
2017-07-06 21:48:17 +02:00
constructor() {
super();
this.state = { syncReportText: '' };
}
2017-05-24 21:27:13 +02:00
folder_press(folder) {
2017-07-08 00:25:03 +02:00
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
2017-05-24 22:11:37 +02:00
2017-07-25 20:09:01 +02:00
this.props.dispatch({
type: 'NAV_GO',
2017-07-25 20:09:01 +02:00
routeName: 'Notes',
folderId: folder.id,
});
2017-05-24 21:27:13 +02:00
}
2017-07-06 21:48:17 +02:00
async synchronize_press() {
2017-07-24 23:52:30 +02:00
if (Setting.value('sync.target') == Setting.SYNC_TARGET_ONEDRIVE && !reg.oneDriveApi().auth()) {
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
this.props.dispatch({
type: 'NAV_GO',
2017-07-24 23:52:30 +02:00
routeName: 'OneDriveLogin',
});
return;
}
const sync = await reg.synchronizer(Setting.value('sync.target'))
if (this.props.syncStarted) {
sync.cancel();
2017-07-06 21:48:17 +02:00
} else {
2017-07-24 23:52:30 +02:00
reg.scheduleSync(0);
2017-07-06 21:48:17 +02:00
}
}
2017-07-22 17:55:09 +02:00
folderItem(folder, selected) {
2017-07-22 18:36:55 +02:00
const iconComp = selected ? <Icon name='md-folder-open' style={styles.folderIcon} /> : <Icon name='md-folder' style={styles.folderIcon} />;
2017-07-22 17:55:09 +02:00
const folderButtonStyle = selected ? styles.folderButtonSelected : styles.folderButton;
return (
<TouchableOpacity key={folder.id} onPress={() => { this.folder_press(folder) }}>
<View style={folderButtonStyle}>
{ iconComp }
<Text numberOfLines={1} style={styles.folderButtonText}>{folder.title}</Text>
</View>
</TouchableOpacity>
);
}
synchronizeButton(state) {
const title = state == 'sync' ? _('Synchronize') : _('Cancel synchronization');
2017-07-22 18:36:55 +02:00
const iconComp = state == 'sync' ? <Icon name='md-sync' style={globalStyle.icon} /> : <Icon name='md-close' style={globalStyle.icon} />;
2017-07-22 17:55:09 +02:00
return (
<TouchableOpacity key={'synchronize_button'} onPress={() => { this.synchronize_press() }}>
<View style={styles.syncButton}>
{ iconComp }
<Text style={styles.syncButtonText}>{title}</Text>
</View>
</TouchableOpacity>
);
}
2017-05-24 21:27:13 +02:00
render() {
2017-07-06 21:48:17 +02:00
let items = [];
2017-07-22 17:55:09 +02:00
// HACK: inner height of ScrollView doesn't appear to be calculated correctly when
// using padding. So instead creating blank elements for padding bottom and top.
items.push(<View style={{ height: globalStyle.marginTop }} key='bottom_top_hack'/>);
2017-05-24 21:27:13 +02:00
for (let i = 0; i < this.props.folders.length; i++) {
2017-07-22 17:55:09 +02:00
let folder = this.props.folders[i];
items.push(this.folderItem(folder, this.props.selectedFolderId == folder.id));
2017-05-24 21:27:13 +02:00
}
2017-07-22 17:55:09 +02:00
if (items.length) items.push(<View style={{ height: 30, flex: -1 }} key='divider_1'></View>); // DIVIDER
2017-07-06 21:48:17 +02:00
let lines = Synchronizer.reportToLines(this.props.syncReport);
const syncReportText = lines.join("\n");
2017-07-22 17:55:09 +02:00
items.push(this.synchronizeButton(this.props.syncStarted ? 'cancel' : 'sync'));
items.push(<Text key='sync_report' style={styles.syncStatus}>{syncReportText}</Text>);
2017-07-06 21:48:17 +02:00
2017-07-22 17:55:09 +02:00
items.push(<View style={{ height: globalStyle.marginBottom }} key='bottom_padding_hack'/>);
2017-07-06 21:48:17 +02:00
2017-05-24 21:27:13 +02:00
return (
2017-07-22 17:55:09 +02:00
<View style={{flex:1}}>
<View style={{flexDirection:'row'}}>
<Image style={{flex:1, height: 150}} source={require('../images/SideMenuHeader.png')} />
</View>
<ScrollView scrollsToTop={false} style={styles.menu}>
{ items }
</ScrollView>
</View>
2017-05-24 21:27:13 +02:00
);
}
};
const SideMenuContent = connect(
(state) => {
return {
folders: state.folders,
syncStarted: state.syncStarted,
syncReport: state.syncReport,
2017-07-22 17:55:09 +02:00
selectedFolderId: state.selectedFolderId,
2017-05-24 21:27:13 +02:00
};
}
)(SideMenuContentComponent)
export { SideMenuContent };