2018-03-09 22:59:12 +02:00
|
|
|
const React = require('react'); const Component = React.Component;
|
2018-07-20 15:59:55 +02:00
|
|
|
const { AppState, View, Button, Text } = require('react-native');
|
2018-03-09 22:59:12 +02:00
|
|
|
const { stateUtils } = require('lib/reducer.js');
|
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { reg } = require('lib/registry.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 } = 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 {
|
2018-03-09 22:59:12 +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
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2018-07-20 15:59:55 +02:00
|
|
|
this.onAppStateChange_ = async () => {
|
|
|
|
// Force an update to the notes list when app state changes
|
|
|
|
let newProps = Object.assign({}, this.props);
|
|
|
|
newProps.notesSource = '';
|
|
|
|
await this.refreshNotes(newProps);
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
this.sortButton_press = async () => {
|
|
|
|
const buttons = [];
|
2018-03-09 22:59:12 +02:00
|
|
|
const sortNoteOptions = Setting.enumOptions('notes.sortOrder.field');
|
2018-02-22 20:58:15 +02:00
|
|
|
|
|
|
|
const makeCheckboxText = function(selected, sign, label) {
|
2018-03-09 22:59:12 +02:00
|
|
|
const s = sign === 'tick' ? '✓' : '⬤'
|
|
|
|
return (selected ? (s + ' ') : '') + label;
|
|
|
|
}
|
2018-02-22 20:58:15 +02:00
|
|
|
|
|
|
|
for (let field in sortNoteOptions) {
|
|
|
|
if (!sortNoteOptions.hasOwnProperty(field)) continue;
|
|
|
|
buttons.push({
|
2018-03-09 22:59:12 +02:00
|
|
|
text: makeCheckboxText(Setting.value('notes.sortOrder.field') === field, 'bullet', sortNoteOptions[field]),
|
|
|
|
id: { name: 'notes.sortOrder.field', value: field },
|
2018-02-22 20:58:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons.push({
|
2018-03-09 22:59:12 +02:00
|
|
|
text: makeCheckboxText(Setting.value('notes.sortOrder.reverse'), 'tick', '[ ' + Setting.settingMetadata('notes.sortOrder.reverse').label() + ' ]'),
|
|
|
|
id: { name: 'notes.sortOrder.reverse', value: !Setting.value('notes.sortOrder.reverse') },
|
2018-02-22 20:58:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
buttons.push({
|
2018-03-09 22:59:12 +02:00
|
|
|
text: makeCheckboxText(Setting.value('uncompletedTodosOnTop'), 'tick', '[ ' + Setting.settingMetadata('uncompletedTodosOnTop').label() + ' ]'),
|
|
|
|
id: { name: 'uncompletedTodosOnTop', value: !Setting.value('uncompletedTodosOnTop') },
|
2018-02-22 20:58:15 +02:00
|
|
|
});
|
|
|
|
|
2018-05-09 22:00:05 +02:00
|
|
|
buttons.push({
|
|
|
|
text: makeCheckboxText(Setting.value('showCompletedTodos'), 'tick', '[ ' + Setting.settingMetadata('showCompletedTodos').label() + ' ]'),
|
|
|
|
id: { name: 'showCompletedTodos', value: !Setting.value('showCompletedTodos') },
|
|
|
|
});
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const r = await dialogs.pop(this, Setting.settingMetadata('notes.sortOrder.field').label(), buttons);
|
2018-02-22 20:58:15 +02:00
|
|
|
if (!r) return;
|
|
|
|
|
|
|
|
Setting.setValue(r.name, r.value);
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2018-02-22 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 20:09:01 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
await this.refreshNotes();
|
2018-07-20 15:59:55 +02:00
|
|
|
AppState.addEventListener('change', this.onAppStateChange_);
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentWillUnmount() {
|
|
|
|
AppState.removeEventListener('change', this.onAppStateChange_);
|
2017-07-25 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:38:19 +02:00
|
|
|
async UNSAFE_componentWillReceiveProps(newProps) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (newProps.notesOrder !== this.props.notesOrder ||
|
|
|
|
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,
|
2018-05-09 22:00:05 +02:00
|
|
|
showCompletedTodos: props.showCompletedTodos,
|
2018-02-22 20:58:15 +02:00
|
|
|
caseInsensitive: true,
|
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 = [];
|
2018-03-09 22:59:12 +02:00
|
|
|
if (props.notesParentType == 'Folder') {
|
2017-07-25 20:36:52 +02:00
|
|
|
notes = await Note.previews(props.selectedFolderId, options);
|
|
|
|
} else {
|
2019-01-18 19:56:56 +02:00
|
|
|
notes = await Tag.notes(props.selectedTagId, options);
|
2017-07-25 20:36:52 +02:00
|
|
|
}
|
2017-07-25 20:09:01 +02:00
|
|
|
|
|
|
|
this.props.dispatch({
|
2018-03-09 22:59:12 +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) {
|
2018-05-09 10:53:47 +02:00
|
|
|
dialogs.confirm(this, _('Delete notebook? All notes and sub-notebooks within this notebook will also be deleted.')).then((ok) => {
|
2017-07-13 01:01:15 +02:00
|
|
|
if (!ok) return;
|
2017-07-13 00:32:08 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
Folder.delete(folderId).then(() => {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Welcome',
|
2017-07-25 20:09:01 +02:00
|
|
|
});
|
2018-03-09 22:59:12 +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({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Folder',
|
2017-05-16 22:25:19 +02:00
|
|
|
folderId: folderId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
menuOptions() {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.props.notesParentType == 'Folder') {
|
2017-07-25 20:36:52 +02:00
|
|
|
if (this.props.selectedFolderId == Folder.conflictFolderId()) return [];
|
|
|
|
|
2017-12-28 21:57:21 +02:00
|
|
|
const folder = this.parentItem();
|
2018-01-06 21:37:42 +02:00
|
|
|
if (!folder) return [];
|
2017-12-28 21:57:21 +02:00
|
|
|
|
|
|
|
let output = [];
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!folder.encryption_applied) output.push({ title: _('Edit notebook'), onPress: () => { this.editFolder_onPress(this.props.selectedFolderId); } });
|
|
|
|
output.push({ title: _('Delete notebook'), onPress: () => { this.deleteFolder_onPress(this.props.selectedFolderId); } });
|
2017-12-28 21:57:21 +02:00
|
|
|
|
|
|
|
return output;
|
2017-07-25 20:36:52 +02:00
|
|
|
} 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;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (props.notesParentType == 'Folder') {
|
2017-07-25 20:36:52 +02:00
|
|
|
output = Folder.byId(props.folders, props.selectedFolderId);
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (props.notesParentType == 'Tag') {
|
2017-07-25 20:36:52 +02:00
|
|
|
output = Tag.byId(props.tags, props.selectedTagId);
|
|
|
|
} else {
|
2017-07-31 22:03:12 +02:00
|
|
|
return null;
|
2018-03-09 22:59:12 +02:00
|
|
|
throw new Error('Invalid parent type: ' + props.notesParentType);
|
2017-07-25 20:36:52 +02:00
|
|
|
}
|
|
|
|
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,
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2017-08-01 19:59:01 +02:00
|
|
|
|
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>
|
2018-03-09 22:59:12 +02:00
|
|
|
)
|
2017-08-02 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-12-15 02:45:35 +02:00
|
|
|
const actionButtonComp = this.props.noteSelectionEnabled || !this.props.visible ? null : <ActionButton addFolderNoteButtons={addFolderNoteButtons} parentFolderId={this.props.selectedFolderId}></ActionButton>
|
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}
|
2018-02-22 20:58:15 +02:00
|
|
|
sortButton_press={this.sortButton_press}
|
2017-11-23 20:47:51 +02:00
|
|
|
folderPickerOptions={{
|
|
|
|
enabled: this.props.noteSelectionEnabled,
|
|
|
|
mustSelect: true,
|
|
|
|
}}
|
|
|
|
/>
|
2018-03-09 22:59:12 +02:00
|
|
|
<NoteList style={{flex: 1}}/>
|
|
|
|
{ actionButtonComp }
|
|
|
|
<DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
|
2017-05-12 22:23:54 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const NotesScreen = connect(
|
|
|
|
(state) => {
|
|
|
|
return {
|
|
|
|
folders: state.folders,
|
|
|
|
tags: state.tags,
|
|
|
|
selectedFolderId: state.selectedFolderId,
|
|
|
|
selectedNoteIds: state.selectedNoteIds,
|
|
|
|
selectedTagId: state.selectedTagId,
|
|
|
|
notesParentType: state.notesParentType,
|
|
|
|
notes: state.notes,
|
|
|
|
notesSource: state.notesSource,
|
|
|
|
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
|
2018-05-09 22:00:05 +02:00
|
|
|
showCompletedTodos: state.settings.showCompletedTodos,
|
2018-03-09 22:59:12 +02:00
|
|
|
theme: state.settings.theme,
|
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
notesOrder: stateUtils.notesOrder(state.settings),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)(NotesScreenComponent)
|
|
|
|
|
2018-07-20 15:59:55 +02:00
|
|
|
module.exports = { NotesScreen };
|