2017-11-03 02:09:34 +02:00
|
|
|
const React = require('react'); const Component = React.Component;
|
2017-11-19 01:59:07 +02:00
|
|
|
const { View, Button } = require('react-native');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
const { Log } = require('lib/log.js');
|
|
|
|
const { NoteList } = require('lib/components/note-list.js');
|
|
|
|
const { Folder } = require('lib/models/folder.js');
|
|
|
|
const { Tag } = require('lib/models/tag.js');
|
|
|
|
const { Note } = require('lib/models/note.js');
|
|
|
|
const { Setting } = require('lib/models/setting.js');
|
|
|
|
const { themeStyle } = require('lib/components/global-style.js');
|
|
|
|
const { ScreenHeader } = require('lib/components/screen-header.js');
|
|
|
|
const { MenuOption, Text } = require('react-native-popup-menu');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { ActionButton } = require('lib/components/action-button.js');
|
|
|
|
const { dialogs } = require('lib/dialogs.js');
|
|
|
|
const DialogBox = require('react-native-dialogbox').default;
|
|
|
|
const { BaseScreenComponent } = require('lib/components/base-screen.js');
|
2017-05-12 22:23:54 +02:00
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
class NotesScreenComponent extends BaseScreenComponent {
|
2017-05-12 22:23:54 +02:00
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
static navigationOptions(options) {
|
2017-05-16 21:57:09 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
|
2017-07-25 20:09:01 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
await this.refreshNotes();
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentWillReceiveProps(newProps) {
|
2017-07-26 20:36:16 +02:00
|
|
|
if (newProps.notesOrder !== this.props.notesOrder ||
|
2017-07-25 20:36:52 +02:00
|
|
|
newProps.selectedFolderId != this.props.selectedFolderId ||
|
|
|
|
newProps.selectedTagId != this.props.selectedTagId ||
|
|
|
|
newProps.notesParentType != this.props.notesParentType) {
|
2017-07-25 20:09:01 +02:00
|
|
|
await this.refreshNotes(newProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshNotes(props = null) {
|
|
|
|
if (props === null) props = this.props;
|
|
|
|
|
|
|
|
let options = {
|
2017-07-26 20:36:16 +02:00
|
|
|
order: props.notesOrder,
|
|
|
|
uncompletedTodosOnTop: props.uncompletedTodosOnTop,
|
2017-07-25 20:09:01 +02:00
|
|
|
};
|
|
|
|
|
2017-07-25 20:36:52 +02:00
|
|
|
const parent = this.parentItem(props);
|
2017-07-31 22:03:12 +02:00
|
|
|
if (!parent) return;
|
2017-07-25 20:36:52 +02:00
|
|
|
|
2017-07-25 20:09:01 +02:00
|
|
|
const source = JSON.stringify({
|
|
|
|
options: options,
|
2017-07-25 20:36:52 +02:00
|
|
|
parentId: parent.id,
|
2017-07-25 20:09:01 +02:00
|
|
|
});
|
|
|
|
|
2017-07-25 22:24:30 +02:00
|
|
|
if (source == props.notesSource) return;
|
2017-07-25 20:09:01 +02:00
|
|
|
|
2017-07-25 20:36:52 +02:00
|
|
|
let notes = [];
|
|
|
|
if (props.notesParentType == 'Folder') {
|
|
|
|
notes = await Note.previews(props.selectedFolderId, options);
|
|
|
|
} else {
|
|
|
|
notes = await Tag.notes(props.selectedTagId); // TODO: should also return previews
|
|
|
|
}
|
2017-07-25 20:09:01 +02:00
|
|
|
|
|
|
|
this.props.dispatch({
|
2017-11-08 23:22:24 +02:00
|
|
|
type: 'NOTE_UPDATE_ALL',
|
2017-07-25 20:09:01 +02:00
|
|
|
notes: notes,
|
|
|
|
notesSource: source,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
deleteFolder_onPress(folderId) {
|
2017-07-13 01:01:15 +02:00
|
|
|
dialogs.confirm(this, _('Delete notebook?')).then((ok) => {
|
|
|
|
if (!ok) return;
|
2017-07-13 00:32:08 +02:00
|
|
|
|
2017-07-13 01:01:15 +02:00
|
|
|
Folder.delete(folderId).then(() => {
|
2017-07-25 20:09:01 +02:00
|
|
|
this.props.dispatch({
|
2017-07-25 20:41:53 +02:00
|
|
|
type: 'NAV_GO',
|
2017-07-25 20:09:01 +02:00
|
|
|
routeName: 'Welcome',
|
|
|
|
});
|
2017-07-13 01:01:15 +02:00
|
|
|
}).catch((error) => {
|
|
|
|
alert(error.message);
|
2017-05-16 22:25:19 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
editFolder_onPress(folderId) {
|
2017-05-16 22:25:19 +02:00
|
|
|
this.props.dispatch({
|
2017-07-25 20:41:53 +02:00
|
|
|
type: 'NAV_GO',
|
2017-05-16 22:25:19 +02:00
|
|
|
routeName: 'Folder',
|
|
|
|
folderId: folderId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
menuOptions() {
|
2017-07-25 20:36:52 +02:00
|
|
|
if (this.props.notesParentType == 'Folder') {
|
|
|
|
if (this.props.selectedFolderId == Folder.conflictFolderId()) return [];
|
|
|
|
|
|
|
|
return [
|
|
|
|
{ title: _('Delete notebook'), onPress: () => { this.deleteFolder_onPress(this.props.selectedFolderId); } },
|
|
|
|
{ title: _('Edit notebook'), onPress: () => { this.editFolder_onPress(this.props.selectedFolderId); } },
|
|
|
|
];
|
|
|
|
} else {
|
2017-07-25 22:24:30 +02:00
|
|
|
return []; // For tags - TODO
|
2017-07-25 20:36:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parentItem(props = null) {
|
|
|
|
if (!props) props = this.props;
|
|
|
|
|
|
|
|
let output = null;
|
|
|
|
if (props.notesParentType == 'Folder') {
|
|
|
|
output = Folder.byId(props.folders, props.selectedFolderId);
|
|
|
|
} else if (props.notesParentType == 'Tag') {
|
|
|
|
output = Tag.byId(props.tags, props.selectedTagId);
|
|
|
|
} else {
|
2017-07-31 22:03:12 +02:00
|
|
|
return null;
|
2017-07-25 20:36:52 +02:00
|
|
|
throw new Error('Invalid parent type: ' + props.notesParentType);
|
|
|
|
}
|
|
|
|
return output;
|
2017-05-16 22:25:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
render() {
|
2017-07-25 20:36:52 +02:00
|
|
|
const parent = this.parentItem();
|
2017-08-01 21:08:38 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-08-01 19:59:01 +02:00
|
|
|
|
|
|
|
let rootStyle = {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
}
|
|
|
|
|
2017-07-31 22:03:12 +02:00
|
|
|
if (!this.props.visible) {
|
|
|
|
rootStyle.flex = 0.001; // This is a bit of a hack but it seems to work fine - it makes the component invisible but without unmounting it
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:55:03 +02:00
|
|
|
if (!parent) {
|
|
|
|
return (
|
|
|
|
<View style={rootStyle}>
|
|
|
|
<ScreenHeader title={title} menuOptions={this.menuOptions()} />
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = parent ? parent.title : null;
|
|
|
|
const addFolderNoteButtons = this.props.selectedFolderId && this.props.selectedFolderId != Folder.conflictFolderId();
|
2017-11-23 20:47:51 +02:00
|
|
|
const thisComp = this;
|
2017-08-02 19:55:03 +02:00
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
return (
|
2017-07-31 22:03:12 +02:00
|
|
|
<View style={rootStyle}>
|
2017-11-23 20:47:51 +02:00
|
|
|
<ScreenHeader
|
|
|
|
title={title}
|
|
|
|
menuOptions={this.menuOptions()}
|
|
|
|
parentComponent={thisComp}
|
|
|
|
folderPickerOptions={{
|
|
|
|
enabled: this.props.noteSelectionEnabled,
|
|
|
|
mustSelect: true,
|
|
|
|
onValueChange: async (folderId, itemIndex) => {
|
|
|
|
if (!folderId) return;
|
|
|
|
const noteIds = this.props.selectedNoteIds;
|
|
|
|
if (!noteIds.length) return;
|
|
|
|
|
|
|
|
const folder = await Folder.load(folderId);
|
|
|
|
|
|
|
|
const ok = await dialogs.confirm(this, _('Move %d note(s) to notebook "%s"?', noteIds.length, folder.title));
|
|
|
|
if (!ok) return;
|
|
|
|
|
|
|
|
this.props.dispatch({ type: 'NOTE_SELECTION_END' });
|
|
|
|
for (let i = 0; i < noteIds.length; i++) {
|
|
|
|
await Note.moveToFolder(noteIds[i], folderId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
2017-07-25 19:49:31 +02:00
|
|
|
<NoteList style={{flex: 1}}/>
|
2017-07-15 17:54:19 +02:00
|
|
|
<ActionButton addFolderNoteButtons={addFolderNoteButtons} parentFolderId={this.props.selectedFolderId}></ActionButton>
|
2017-07-13 01:01:15 +02:00
|
|
|
<DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
|
2017-05-12 22:23:54 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const NotesScreen = connect(
|
|
|
|
(state) => {
|
2017-05-15 21:10:00 +02:00
|
|
|
return {
|
2017-05-16 22:25:19 +02:00
|
|
|
folders: state.folders,
|
2017-07-25 20:36:52 +02:00
|
|
|
tags: state.tags,
|
2017-05-16 22:25:19 +02:00
|
|
|
selectedFolderId: state.selectedFolderId,
|
2017-11-23 20:47:51 +02:00
|
|
|
selectedNoteIds: state.selectedNoteIds,
|
2017-07-25 20:36:52 +02:00
|
|
|
selectedTagId: state.selectedTagId,
|
|
|
|
notesParentType: state.notesParentType,
|
2017-07-25 20:09:01 +02:00
|
|
|
notes: state.notes,
|
|
|
|
notesOrder: state.notesOrder,
|
|
|
|
notesSource: state.notesSource,
|
2017-07-26 20:36:16 +02:00
|
|
|
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
|
2017-08-01 19:59:01 +02:00
|
|
|
theme: state.settings.theme,
|
2017-11-23 20:47:51 +02:00
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
2017-05-15 21:10:00 +02:00
|
|
|
};
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
)(NotesScreenComponent)
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { NotesScreen };
|