2017-11-03 02:09:34 +02:00
|
|
|
const React = require('react'); const Component = React.Component;
|
|
|
|
const { connect } = require('react-redux');
|
2017-11-23 20:47:51 +02:00
|
|
|
const { ListView, Text, TouchableOpacity , View, StyleSheet } = require('react-native');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { Log } = require('lib/log.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { Checkbox } = require('lib/components/checkbox.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
const { Note } = require('lib/models/note.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { globalStyle, themeStyle } = require('lib/components/global-style.js');
|
2017-07-23 00:52:24 +02:00
|
|
|
|
|
|
|
class NoteItemComponent extends Component {
|
|
|
|
|
2017-08-01 19:59:01 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.styles_ = {};
|
|
|
|
}
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
noteItem_press(noteId) {
|
|
|
|
this.props.dispatch({
|
2017-07-25 20:41:53 +02:00
|
|
|
type: 'NAV_GO',
|
2017-07-23 00:52:24 +02:00
|
|
|
routeName: 'Note',
|
|
|
|
noteId: noteId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:59:01 +02:00
|
|
|
styles() {
|
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
|
|
|
|
if (this.styles_[this.props.theme]) return this.styles_[this.props.theme];
|
|
|
|
this.styles_ = {};
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
listItem: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
//height: 40,
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
borderBottomColor: theme.dividerColor,
|
2017-08-19 23:59:08 +02:00
|
|
|
alignItems: 'flex-start',
|
2017-08-01 19:59:01 +02:00
|
|
|
paddingLeft: theme.marginLeft,
|
|
|
|
paddingRight: theme.marginRight,
|
|
|
|
paddingTop: theme.itemMarginTop,
|
|
|
|
paddingBottom: theme.itemMarginBottom,
|
2017-11-23 20:47:51 +02:00
|
|
|
//backgroundColor: theme.backgroundColor,
|
2017-08-01 19:59:01 +02:00
|
|
|
},
|
|
|
|
listItemText: {
|
|
|
|
flex: 1,
|
|
|
|
color: theme.color,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
},
|
2017-11-23 20:47:51 +02:00
|
|
|
selectionWrapper: {
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
},
|
2017-08-01 19:59:01 +02:00
|
|
|
};
|
|
|
|
|
2017-08-19 23:59:08 +02:00
|
|
|
styles.listItemWithCheckbox = Object.assign({}, styles.listItem);
|
|
|
|
delete styles.listItemWithCheckbox.paddingTop;
|
|
|
|
delete styles.listItemWithCheckbox.paddingBottom;
|
|
|
|
delete styles.listItemWithCheckbox.paddingLeft;
|
|
|
|
|
|
|
|
styles.listItemTextWithCheckbox = Object.assign({}, styles.listItemText);
|
|
|
|
styles.listItemTextWithCheckbox.marginTop = styles.listItem.paddingTop - 1;
|
|
|
|
styles.listItemTextWithCheckbox.marginBottom = styles.listItem.paddingBottom;
|
2017-08-01 19:59:01 +02:00
|
|
|
|
2017-11-23 20:47:51 +02:00
|
|
|
styles.selectionWrapperSelected = Object.assign({}, styles.selectionWrapper);
|
|
|
|
styles.selectionWrapperSelected.backgroundColor = theme.selectedColor;
|
|
|
|
|
2017-08-01 19:59:01 +02:00
|
|
|
this.styles_[this.props.theme] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[this.props.theme];
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:36:52 +02:00
|
|
|
async todoCheckbox_change(checked) {
|
|
|
|
if (!this.props.note) return;
|
|
|
|
|
|
|
|
const newNote = {
|
|
|
|
id: this.props.note.id,
|
|
|
|
todo_completed: checked ? time.unixMs() : 0,
|
|
|
|
}
|
|
|
|
await Note.save(newNote);
|
|
|
|
}
|
|
|
|
|
|
|
|
onPress() {
|
|
|
|
if (!this.props.note) return;
|
|
|
|
|
2017-11-23 20:47:51 +02:00
|
|
|
if (this.props.noteSelectionEnabled) {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NOTE_SELECTION_TOGGLE',
|
|
|
|
id: this.props.note.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Note',
|
|
|
|
noteId: this.props.note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onLongPress() {
|
|
|
|
if (!this.props.note) return;
|
|
|
|
|
2017-07-25 20:36:52 +02:00
|
|
|
this.props.dispatch({
|
2017-11-23 20:41:35 +02:00
|
|
|
type: this.props.noteSelectionEnabled ? 'NOTE_SELECTION_TOGGLE' : 'NOTE_SELECTION_START',
|
2017-11-23 20:47:51 +02:00
|
|
|
id: this.props.note.id,
|
2017-07-25 20:36:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
render() {
|
|
|
|
const note = this.props.note ? this.props.note : {};
|
2017-08-19 23:59:08 +02:00
|
|
|
const isTodo = !!Number(note.is_todo);
|
2017-07-23 00:52:24 +02:00
|
|
|
const onPress = this.props.onPress;
|
|
|
|
const onCheckboxChange = this.props.onCheckboxChange;
|
2017-08-01 19:59:01 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2017-11-19 02:03:42 +02:00
|
|
|
// IOS: display: none crashes the app
|
2017-11-19 02:23:18 +02:00
|
|
|
let checkboxStyle = !isTodo ? { display: 'none' } : { color: theme.color };
|
2017-08-19 23:59:08 +02:00
|
|
|
|
|
|
|
if (isTodo) {
|
|
|
|
checkboxStyle.paddingRight = 10;
|
|
|
|
checkboxStyle.paddingTop = theme.itemMarginTop;
|
|
|
|
checkboxStyle.paddingBottom = theme.itemMarginBottom;
|
|
|
|
checkboxStyle.paddingLeft = theme.marginLeft;
|
|
|
|
}
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
const checkboxChecked = !!Number(note.todo_completed);
|
|
|
|
|
2017-08-19 23:59:08 +02:00
|
|
|
const listItemStyle = isTodo ? this.styles().listItemWithCheckbox : this.styles().listItem;
|
|
|
|
const listItemTextStyle = isTodo ? this.styles().listItemTextWithCheckbox : this.styles().listItemText;
|
2017-11-23 20:41:35 +02:00
|
|
|
const opacityStyle = isTodo && checkboxChecked ? {opacity: 0.4} : {};
|
2017-11-23 20:47:51 +02:00
|
|
|
const isSelected = this.props.noteSelectionEnabled && this.props.selectedNoteIds.indexOf(note.id) >= 0;
|
|
|
|
|
|
|
|
const selectionWrapperStyle = isSelected ? this.styles().selectionWrapperSelected : this.styles().selectionWrapper;
|
2017-07-23 20:26:50 +02:00
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
return (
|
2017-11-23 20:41:35 +02:00
|
|
|
<TouchableOpacity onPress={() => this.onPress()} onLongPress={() => this.onLongPress()}>
|
2017-11-23 20:47:51 +02:00
|
|
|
<View style={ selectionWrapperStyle }>
|
2017-11-23 20:41:35 +02:00
|
|
|
<View style={ opacityStyle }>
|
|
|
|
<View style={ listItemStyle }>
|
|
|
|
<Checkbox
|
|
|
|
style={checkboxStyle}
|
|
|
|
checked={checkboxChecked}
|
|
|
|
onChange={(checked) => this.todoCheckbox_change(checked)}
|
|
|
|
/>
|
|
|
|
<Text style={listItemTextStyle}>{note.title}</Text>
|
|
|
|
</View>
|
2017-11-23 20:47:51 +02:00
|
|
|
</View>
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
2017-11-23 20:47:51 +02:00
|
|
|
</TouchableOpacity>
|
2017-07-23 00:52:24 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const NoteItem = connect(
|
|
|
|
(state) => {
|
2017-08-01 19:59:01 +02:00
|
|
|
return {
|
|
|
|
theme: state.settings.theme,
|
2017-11-23 20:47:51 +02:00
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
selectedNoteIds: state.selectedNoteIds,
|
2017-08-01 19:59:01 +02:00
|
|
|
};
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
)(NoteItemComponent)
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { NoteItem };
|