2017-11-06 20:35:04 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
2017-11-06 22:54:58 +02:00
|
|
|
const { Header } = require('./Header.min.js');
|
2017-11-06 20:35:04 +02:00
|
|
|
const { SideBar } = require('./SideBar.min.js');
|
|
|
|
const { NoteList } = require('./NoteList.min.js');
|
|
|
|
const { NoteText } = require('./NoteText.min.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { PromptDialog } = require('./PromptDialog.min.js');
|
|
|
|
const { Setting } = require('lib/models/setting.js');
|
2017-11-17 20:57:27 +02:00
|
|
|
const { BaseModel } = require('lib/base-model.js');
|
2017-11-12 01:13:14 +02:00
|
|
|
const { Tag } = require('lib/models/tag.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { Note } = require('lib/models/note.js');
|
2017-11-17 20:57:27 +02:00
|
|
|
const { uuid } = require('lib/uuid.js');
|
2017-11-08 23:22:24 +02:00
|
|
|
const { Folder } = require('lib/models/folder.js');
|
2017-11-06 22:54:58 +02:00
|
|
|
const { themeStyle } = require('../theme.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2017-11-07 23:11:14 +02:00
|
|
|
const layoutUtils = require('lib/layout-utils.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
2017-11-30 01:03:10 +02:00
|
|
|
const eventManager = require('../eventManager');
|
2017-11-06 20:35:04 +02:00
|
|
|
|
|
|
|
class MainScreenComponent extends React.Component {
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
componentWillMount() {
|
2017-11-10 21:18:19 +02:00
|
|
|
this.setState({
|
2017-11-10 22:34:36 +02:00
|
|
|
promptOptions: null,
|
2017-11-10 21:18:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:36:47 +02:00
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
if (newProps.windowCommand) {
|
|
|
|
this.doCommand(newProps.windowCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 21:18:19 +02:00
|
|
|
toggleVisiblePanes() {
|
2017-11-12 19:02:20 +02:00
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NOTE_VISIBLE_PANES_TOGGLE',
|
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:13:14 +02:00
|
|
|
async doCommand(command) {
|
2017-11-11 19:36:47 +02:00
|
|
|
if (!command) return;
|
|
|
|
|
|
|
|
const createNewNote = async (title, isTodo) => {
|
|
|
|
const folderId = Setting.value('activeFolderId');
|
|
|
|
if (!folderId) return;
|
|
|
|
|
|
|
|
const note = await Note.save({
|
|
|
|
title: title,
|
|
|
|
parent_id: folderId,
|
|
|
|
is_todo: isTodo ? 1 : 0,
|
|
|
|
});
|
|
|
|
Note.updateGeolocation(note.id);
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let commandProcessed = true;
|
|
|
|
|
|
|
|
if (command.name === 'newNote') {
|
2017-11-13 02:23:12 +02:00
|
|
|
if (!this.props.folders.length) {
|
|
|
|
bridge().showErrorMessageBox(_('Please create a notebook first.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:36:47 +02:00
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('Note title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) await createNewNote(answer, false);
|
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'newTodo') {
|
2017-11-13 02:23:12 +02:00
|
|
|
if (!this.props.folders.length) {
|
|
|
|
bridge().showErrorMessageBox(_('Please create a notebook first'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:36:47 +02:00
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('To-do title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) await createNewNote(answer, true);
|
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'newNotebook') {
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('Notebook title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) {
|
|
|
|
let folder = null;
|
|
|
|
try {
|
|
|
|
folder = await Folder.save({ title: answer }, { userSideValidation: true });
|
|
|
|
} catch (error) {
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
if (folder) {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'FOLDER_SELECT',
|
|
|
|
id: folder.id,
|
|
|
|
});
|
|
|
|
}
|
2017-11-11 19:36:47 +02:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:13:14 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'setTags') {
|
|
|
|
const tags = await Tag.tagsByNoteId(command.noteId);
|
|
|
|
const tagTitles = tags.map((a) => { return a.title });
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Add or remove tags:'),
|
|
|
|
description: _('Separate each tag by a comma.'),
|
|
|
|
value: tagTitles.join(', '),
|
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer !== null) {
|
|
|
|
const tagTitles = answer.split(',').map((a) => { return a.trim() });
|
|
|
|
await Tag.setNoteTagsByTitles(command.noteId, tagTitles);
|
|
|
|
}
|
2017-11-11 19:36:47 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-11-16 20:51:11 +02:00
|
|
|
} else if (command.name === 'renameNotebook') {
|
|
|
|
const folder = await Folder.load(command.id);
|
|
|
|
if (!folder) return;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Rename notebook:'),
|
|
|
|
value: folder.title,
|
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer !== null) {
|
|
|
|
try {
|
|
|
|
await Folder.save({ id: folder.id, title: answer }, { userSideValidation: true });
|
|
|
|
} catch (error) {
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
2017-11-17 20:57:27 +02:00
|
|
|
});
|
|
|
|
} else if (command.name === 'search') {
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-18 01:33:20 +02:00
|
|
|
label: _('Search:'),
|
2017-11-17 20:57:27 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer !== null) {
|
|
|
|
const searchId = uuid.create();
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_ADD',
|
|
|
|
search: {
|
|
|
|
id: searchId,
|
|
|
|
title: answer,
|
|
|
|
query_pattern: answer,
|
|
|
|
query_folder_id: null,
|
|
|
|
type_: BaseModel.TYPE_SEARCH,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_SELECT',
|
|
|
|
id: searchId,
|
|
|
|
});
|
|
|
|
}
|
2017-11-28 00:50:46 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'editAlarm') {
|
|
|
|
const note = await Note.load(command.noteId);
|
|
|
|
|
2017-12-01 23:55:02 +02:00
|
|
|
let defaultDate = new Date(Date.now() + 2 * 3600 * 1000);
|
|
|
|
defaultDate.setMinutes(0);
|
|
|
|
defaultDate.setSeconds(0);
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-12-01 20:56:35 +02:00
|
|
|
label: _('Set alarm:'),
|
2017-11-28 00:50:46 +02:00
|
|
|
inputType: 'datetime',
|
|
|
|
buttons: ['ok', 'cancel', 'clear'],
|
2017-12-01 23:55:02 +02:00
|
|
|
value: note.todo_due ? new Date(note.todo_due) : defaultDate,
|
2017-11-28 00:50:46 +02:00
|
|
|
onClose: async (answer, buttonType) => {
|
|
|
|
let newNote = null;
|
|
|
|
|
|
|
|
if (buttonType === 'clear') {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: 0,
|
|
|
|
};
|
|
|
|
} else if (answer !== null) {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: answer.getTime(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newNote) {
|
|
|
|
await Note.save(newNote);
|
2017-11-30 01:03:10 +02:00
|
|
|
eventManager.emit('alarmChange', { noteId: note.id });
|
2017-11-28 00:50:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 20:57:27 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-11-11 19:36:47 +02:00
|
|
|
} else {
|
|
|
|
commandProcessed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandProcessed) {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'WINDOW_COMMAND',
|
|
|
|
name: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:57:13 +02:00
|
|
|
styles(themeId, width, height, messageBoxVisible) {
|
|
|
|
const styleKey = themeId + '_' + width + '_' + height + '_' + messageBoxVisible;
|
2017-11-30 01:03:10 +02:00
|
|
|
if (styleKey === this.styleKey_) return this.styles_;
|
2017-11-06 22:54:58 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
const theme = themeStyle(themeId);
|
2017-11-06 22:54:58 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styleKey_ = styleKey;
|
2017-11-06 20:35:04 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_ = {};
|
|
|
|
|
|
|
|
this.styles_.header = {
|
|
|
|
width: width,
|
|
|
|
};
|
|
|
|
|
2017-12-05 01:57:13 +02:00
|
|
|
this.styles_.messageBox = {
|
|
|
|
width: width,
|
|
|
|
height: 30,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingLeft: 10,
|
|
|
|
backgroundColor: theme.warningBackgroundColor,
|
|
|
|
}
|
|
|
|
|
|
|
|
const rowHeight = height - theme.headerHeight - (messageBoxVisible ? this.styles_.messageBox.height : 0);
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.sideBar = {
|
|
|
|
width: Math.floor(layoutUtils.size(width * .2, 150, 300)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.noteList = {
|
|
|
|
width: Math.floor(layoutUtils.size(width * .2, 150, 300)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.noteText = {
|
|
|
|
width: Math.floor(layoutUtils.size(width - this.styles_.sideBar.width - this.styles_.noteList.width, 0)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.prompt = {
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2017-11-08 19:51:55 +02:00
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
return this.styles_;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const style = this.props.style;
|
|
|
|
const promptOptions = this.state.promptOptions;
|
|
|
|
const folders = this.props.folders;
|
|
|
|
const notes = this.props.notes;
|
2017-12-07 02:57:36 +02:00
|
|
|
const messageBoxVisible = this.props.hasDisabledSyncItems;
|
2017-11-30 01:03:10 +02:00
|
|
|
|
2017-12-07 02:57:36 +02:00
|
|
|
const styles = this.styles(this.props.theme, style.width, style.height, messageBoxVisible);
|
2017-12-05 01:57:13 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-11-30 01:03:10 +02:00
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
const headerButtons = [];
|
|
|
|
|
|
|
|
headerButtons.push({
|
|
|
|
title: _('New note'),
|
|
|
|
iconName: 'fa-file-o',
|
2017-11-13 02:23:12 +02:00
|
|
|
enabled: !!folders.length,
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newNote' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
headerButtons.push({
|
|
|
|
title: _('New to-do'),
|
|
|
|
iconName: 'fa-check-square-o',
|
2017-11-13 02:23:12 +02:00
|
|
|
enabled: !!folders.length,
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newTodo' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
headerButtons.push({
|
|
|
|
title: _('New notebook'),
|
|
|
|
iconName: 'fa-folder-o',
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newNotebook' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
2017-11-08 23:22:24 +02:00
|
|
|
|
2017-11-17 20:57:27 +02:00
|
|
|
headerButtons.push({
|
2017-11-18 01:33:20 +02:00
|
|
|
title: _('Search'),
|
2017-11-17 20:57:27 +02:00
|
|
|
iconName: 'fa-search',
|
|
|
|
onClick: () => { this.doCommand({ name: 'search' }) },
|
|
|
|
});
|
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
headerButtons.push({
|
|
|
|
title: _('Layout'),
|
|
|
|
iconName: 'fa-columns',
|
2017-11-13 02:23:12 +02:00
|
|
|
enabled: !!notes.length,
|
2017-11-10 22:34:36 +02:00
|
|
|
onClick: () => {
|
|
|
|
this.toggleVisiblePanes();
|
|
|
|
},
|
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
if (!this.promptOnClose_) {
|
|
|
|
this.promptOnClose_ = (answer, buttonType) => {
|
|
|
|
return this.state.promptOptions.onClose(answer, buttonType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:57:13 +02:00
|
|
|
const onViewDisabledItemsClick = () => {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
2017-12-05 20:56:39 +02:00
|
|
|
routeName: 'Status',
|
2017-12-05 01:57:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-07 02:57:36 +02:00
|
|
|
const messageComp = messageBoxVisible ? (
|
2017-12-05 01:57:13 +02:00
|
|
|
<div style={styles.messageBox}>
|
|
|
|
<span style={theme.textStyle}>
|
|
|
|
{_('Some items cannot be synchronised.')} <a href="#" onClick={() => { onViewDisabledItemsClick() }}>{_('View them now')}</a>
|
|
|
|
</span>
|
|
|
|
</div>
|
2017-12-05 20:56:39 +02:00
|
|
|
) : null;
|
2017-12-05 01:57:13 +02:00
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
return (
|
|
|
|
<div style={style}>
|
2017-11-12 01:13:14 +02:00
|
|
|
<PromptDialog
|
2017-11-23 20:47:51 +02:00
|
|
|
autocomplete={promptOptions && ('autocomplete' in promptOptions) ? promptOptions.autocomplete : null}
|
2017-11-30 01:03:10 +02:00
|
|
|
defaultValue={promptOptions && promptOptions.value ? promptOptions.value : ''}
|
2017-11-12 01:13:14 +02:00
|
|
|
theme={this.props.theme}
|
2017-11-30 01:03:10 +02:00
|
|
|
style={styles.prompt}
|
|
|
|
onClose={this.promptOnClose_}
|
2017-11-12 01:13:14 +02:00
|
|
|
label={promptOptions ? promptOptions.label : ''}
|
|
|
|
description={promptOptions ? promptOptions.description : null}
|
2017-11-28 00:50:46 +02:00
|
|
|
visible={!!this.state.promptOptions}
|
|
|
|
buttons={promptOptions && ('buttons' in promptOptions) ? promptOptions.buttons : null}
|
|
|
|
inputType={promptOptions && ('inputType' in promptOptions) ? promptOptions.inputType : null} />
|
2017-11-30 01:03:10 +02:00
|
|
|
<Header style={styles.header} showBackButton={false} buttons={headerButtons} />
|
2017-12-05 01:57:13 +02:00
|
|
|
{messageComp}
|
2017-11-30 01:03:10 +02:00
|
|
|
<SideBar style={styles.sideBar} />
|
|
|
|
<NoteList style={styles.noteList} />
|
|
|
|
<NoteText style={styles.noteText} visiblePanes={this.props.noteVisiblePanes} />
|
2017-11-06 20:35:04 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
2017-11-06 22:54:58 +02:00
|
|
|
return {
|
2017-11-10 22:34:36 +02:00
|
|
|
theme: state.settings.theme,
|
2017-11-11 19:36:47 +02:00
|
|
|
windowCommand: state.windowCommand,
|
2017-11-12 19:02:20 +02:00
|
|
|
noteVisiblePanes: state.noteVisiblePanes,
|
2017-11-13 02:23:12 +02:00
|
|
|
folders: state.folders,
|
|
|
|
notes: state.notes,
|
2017-12-05 20:56:39 +02:00
|
|
|
hasDisabledSyncItems: state.hasDisabledSyncItems,
|
2017-11-06 22:54:58 +02:00
|
|
|
};
|
2017-11-06 20:35:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const MainScreen = connect(mapStateToProps)(MainScreenComponent);
|
|
|
|
|
|
|
|
module.exports = { MainScreen };
|