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

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-07-23 00:52:24 +02:00
import React, { Component } from 'react';
import { connect } from 'react-redux'
2017-07-25 20:36:52 +02:00
import { ListView, Text, TouchableHighlight, View, StyleSheet } from 'react-native';
2017-07-23 00:52:24 +02:00
import { Log } from 'lib/log.js';
import { _ } from 'lib/locale.js';
import { Checkbox } from 'lib/components/checkbox.js';
import { reg } from 'lib/registry.js';
import { Note } from 'lib/models/note.js';
import { time } from 'lib/time-utils.js';
2017-08-01 19:59:01 +02:00
import { globalStyle, themeStyle } from '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({
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,
backgroundColor: theme.backgroundColor,
},
listItemText: {
flex: 1,
color: theme.color,
fontSize: theme.fontSize,
},
};
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
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;
this.props.dispatch({
type: 'NAV_GO',
routeName: 'Note',
noteId: this.props.note.id,
});
}
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-08-19 23:59:08 +02:00
let checkboxStyle = !isTodo ? { display: 'none' } : { color: theme.color };
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;
const rootStyle = isTodo && checkboxChecked ? {opacity: 0.4} : {};
2017-07-23 20:26:50 +02:00
2017-07-23 00:52:24 +02:00
return (
2017-08-19 23:59:08 +02:00
<TouchableHighlight onPress={() => this.onPress()} underlayColor="#0066FF" style={rootStyle}>
2017-07-23 20:26:50 +02:00
<View style={ listItemStyle }>
2017-07-25 20:36:52 +02:00
<Checkbox
style={checkboxStyle}
checked={checkboxChecked}
onChange={(checked) => this.todoCheckbox_change(checked)}
/>
2017-08-19 23:59:08 +02:00
<Text style={listItemTextStyle}>{note.title}</Text>
2017-07-23 00:52:24 +02:00
</View>
</TouchableHighlight>
);
}
}
const NoteItem = connect(
(state) => {
2017-08-01 19:59:01 +02:00
return {
theme: state.settings.theme,
};
2017-07-23 00:52:24 +02:00
}
)(NoteItemComponent)
export { NoteItem }