2017-11-04 18:40:34 +02:00
|
|
|
const { ItemList } = require('./ItemList.min.js');
|
2017-11-05 02:17:48 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { themeStyle } = require('../theme.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
|
|
|
const Menu = bridge().Menu;
|
|
|
|
const MenuItem = bridge().MenuItem;
|
2017-11-04 18:40:34 +02:00
|
|
|
|
|
|
|
class NoteListComponent extends React.Component {
|
|
|
|
|
2017-11-09 21:21:10 +02:00
|
|
|
style() {
|
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
|
|
|
|
const itemHeight = 26;
|
|
|
|
|
|
|
|
let style = {
|
|
|
|
root: {
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
},
|
|
|
|
listItem: {
|
|
|
|
height: itemHeight,
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
textDecoration: 'none',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
color: theme.color,
|
|
|
|
paddingLeft: 6,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
cursor: 'default',
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
borderBottom: '1px solid ' + theme.dividerColor,
|
|
|
|
},
|
|
|
|
listItemSelected: {
|
|
|
|
backgroundColor: theme.selectedColor,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
itemContextMenu(event) {
|
|
|
|
const noteId = event.target.getAttribute('data-id');
|
|
|
|
if (!noteId) throw new Error('No data-id on element');
|
|
|
|
|
|
|
|
const menu = new Menu()
|
2017-11-08 23:22:24 +02:00
|
|
|
menu.append(new MenuItem({label: _('Delete'), click: async () => {
|
2017-11-08 19:51:55 +02:00
|
|
|
const ok = bridge().showConfirmMessageBox(_('Delete note?'));
|
|
|
|
if (!ok) return;
|
|
|
|
await Note.delete(noteId);
|
|
|
|
}}))
|
|
|
|
menu.popup(bridge().window());
|
|
|
|
}
|
|
|
|
|
|
|
|
itemRenderer(index, item, theme) {
|
2017-11-05 20:36:27 +02:00
|
|
|
const onClick = (item) => {
|
2017-11-05 01:27:13 +02:00
|
|
|
this.props.dispatch({
|
2017-11-08 23:22:24 +02:00
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: item.id,
|
2017-11-05 01:27:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:21:10 +02:00
|
|
|
let style = Object.assign({}, this.style().listItem);
|
|
|
|
if (this.props.selectedNoteId === item.id) style = Object.assign(style, this.style().listItemSelected);
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-09 21:21:10 +02:00
|
|
|
return <a data-id={item.id} className="list-item" onContextMenu={(event) => this.itemContextMenu(event)} href="#" style={style} onClick={() => { onClick(item) }} key={index}>{item.title}</a>
|
2017-11-04 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-11-08 19:51:55 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-11-10 19:58:17 +02:00
|
|
|
const style = this.props.style;
|
|
|
|
|
|
|
|
if (!this.props.notes.length) {
|
|
|
|
const padding = 10;
|
|
|
|
const emptyDivStyle = Object.assign({
|
|
|
|
padding: padding + 'px',
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
}, style);
|
|
|
|
emptyDivStyle.width = emptyDivStyle.width - padding * 2;
|
|
|
|
emptyDivStyle.height = emptyDivStyle.height - padding * 2;
|
|
|
|
return <div style={emptyDivStyle}>{_('No notes in here. Create one by clicking on "New note".')}</div>
|
|
|
|
}
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-04 18:40:34 +02:00
|
|
|
return (
|
2017-11-04 21:46:37 +02:00
|
|
|
<ItemList
|
|
|
|
itemHeight={this.props.itemHeight}
|
2017-11-10 19:58:17 +02:00
|
|
|
style={style}
|
2017-11-04 21:46:37 +02:00
|
|
|
className={"note-list"}
|
|
|
|
items={this.props.notes}
|
2017-11-08 19:51:55 +02:00
|
|
|
itemRenderer={ (index, item) => { return this.itemRenderer(index, item, theme) } }
|
2017-11-05 01:27:13 +02:00
|
|
|
></ItemList>
|
2017-11-04 18:40:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
return {
|
2017-11-05 01:27:13 +02:00
|
|
|
notes: state.notes,
|
2017-11-06 01:55:01 +02:00
|
|
|
selectedNoteId: state.selectedNoteId,
|
2017-11-08 19:51:55 +02:00
|
|
|
theme: state.settings.theme,
|
2017-11-04 18:40:34 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const NoteList = connect(mapStateToProps)(NoteListComponent);
|
|
|
|
|
|
|
|
module.exports = { NoteList };
|