From d14eed9965d0f9d11487812eb290148b15316b6c Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sun, 6 Mar 2016 11:08:42 -0700 Subject: [PATCH] Add state for navigation Currently selected navigation item is available under the navigation variable so that the whole application has access to it. --- app/actions/navigation.jsx | 5 + app/components/EntrySelector.jsx | 11 +- app/components/LibraryNav.jsx | 125 ++++++++++++++---- .../__tests__/EntrySelector-test.jsx | 2 +- app/constants/navigation.jsx | 1 + app/containers/App.jsx | 23 +++- app/reducers/contextMenu.jsx | 5 +- app/reducers/index.jsx | 4 +- app/reducers/navigation.jsx | 18 +++ app/utils.jsx | 5 + 10 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 app/actions/navigation.jsx create mode 100644 app/constants/navigation.jsx create mode 100644 app/reducers/navigation.jsx diff --git a/app/actions/navigation.jsx b/app/actions/navigation.jsx new file mode 100644 index 0000000..c77cf6a --- /dev/null +++ b/app/actions/navigation.jsx @@ -0,0 +1,5 @@ +import * as types from '../constants/navigation' + +export function updateSelection(selection) { + return { type: types.UPDATE_SELECTION, selection } +} diff --git a/app/components/EntrySelector.jsx b/app/components/EntrySelector.jsx index 6bb2544..e1a3fd4 100644 --- a/app/components/EntrySelector.jsx +++ b/app/components/EntrySelector.jsx @@ -39,7 +39,7 @@ export default class EntrySelector extends React.Component { constructor(props){ super(props) - this.state = {entries: []} + this.state = {notes: []} } static get childContextTypes(){ @@ -57,14 +57,7 @@ export default class EntrySelector extends React.Component { } addNoteTapped = () => { - this.createNewNote((note) => { - var notes = this.state.notes - notes.splice(0, 0, note) - this.setState({notes: notes}, () => { - //this.refs['textField0'].focus() - }) - }) - + console.log(this.props.navigation) }; render(){ diff --git a/app/components/LibraryNav.jsx b/app/components/LibraryNav.jsx index 018cf82..efb77a6 100644 --- a/app/components/LibraryNav.jsx +++ b/app/components/LibraryNav.jsx @@ -54,32 +54,33 @@ export default class LibraryNav extends React.Component { navItems: [ { 'name': 'Entries', + 'isNotebook': true, 'icon': , - 'notes': 10, 'clicked': this.props.entriesTapped || this.entriesTapped }, { 'name': 'Starred', + 'notes': 0, 'icon': , - 'notes': 1, 'clicked': this.props.starredTapped || this.starredTapped }, { 'name': 'Recents', + 'notes': 0, 'icon': , - 'notes': 10, 'clicked': this.props.recentsTapped || this.recentsTapped }, { 'name': 'Trash', + 'isNotebook': true, 'icon': , - 'notes': 0, 'clicked': this.props.trashTapped || this.trashTapped }, { 'name': 'All Notes', - 'icon': , 'notes': 0, + 'glob': '*.qvnotebook/*.qvnote', + 'icon': , 'clicked': this.props.allNotesTapped || this.allNotesTapped }, @@ -87,12 +88,44 @@ export default class LibraryNav extends React.Component { notebooks: [ ] } + this.loadDefaultNotebooks() this.getNotebooks() } + loadDefaultNotebooks = () => { + var notebooks = this.state.navItems + for(var i=0; i { var dataPath = utils.getAppDataPath() - var notebooks = glob.sync(path.join(dataPath, '*.qvnotebook')) + var notebooks = glob.sync(path.join(dataPath, '!(Entries|Trash).qvnotebook')) for(var i=0; i { + var nbPath = utils.getNotebookPath(notebook) + var dir = mkdirp.sync(nbPath) + + var notePath = utils.getNotebookPath(notebook) + + var meta = { + 'name': notebook.title, + 'uuid': notebook.uuid + } + + var metaPath = path.join(nbPath, 'meta.json') + var t = jsfile.writeFileSync(metaPath, meta) + + }; + + createNotebookPath = (notebook, callback) => { + mkdirp(notebook.path, (err) => { + if(err){ + console.log('There was an error creating the directory '+notebook.path) + console.log(err) + } + else{ + var nbs = this.state.notebooks + nbs.splice(0, 0, notebook) + + this.setState({notebooks: nbs}, () => { + this.createNotebookMeta(notebook, callback) + }) + } + }) + + }; createNewNotebook = (callback) => { var nbUuid = uuid.v4().toUpperCase() @@ -200,23 +266,7 @@ export default class LibraryNav extends React.Component { 'notes': 0 } - mkdirp(nbPath, (err) => { - if(err){ - console.log('There was an error creating the directory '+notePath) - console.log(err) - } - else{ - var nbs = this.state.notebooks - nbs.splice(0, 0, notebook) - - this.setState({notebooks: nbs}, () => { - if(this.refs['textField0']){ - this.refs['textField0'].focus() - } - this.createNotebookMeta(notebook, callback) - }) - } - }) + this.createNotebookPath(notebook, callback) }; createNotebookMeta = (notebook, callback) => { @@ -230,7 +280,7 @@ export default class LibraryNav extends React.Component { if(err){ console.log(err) } - if(callback){ + if(utils.isFunction(callback)){ callback(notebook, err) } }) @@ -258,6 +308,14 @@ export default class LibraryNav extends React.Component { }; addNotebookTapped = (callback) => { + if(!utils.isFunction(callback)){ + callback = () => { + if(this.refs['textField0']){ + this.refs['textField0'].focus() + } + } + } + this.createNewNotebook(callback) }; @@ -274,6 +332,15 @@ export default class LibraryNav extends React.Component { //Right click type = 'rightClick' } + + if (item.isNotebook){ + var notebook = utils.loadNotebookByName(item.name) + this.props.updateSelection(notebook) + } + else if(item.glob){ + this.props.updateSelection(item) + } + item.clicked(i, item, type, ev) }; @@ -295,7 +362,7 @@ export default class LibraryNav extends React.Component { console.log(err) } this.setState({notebooks: nbs}, ()=>{ - if(callback){ + if(utils.isFunction(callback)){ callback(nb, err) } }) @@ -331,7 +398,11 @@ export default class LibraryNav extends React.Component { this.props.updateContextMenu(this.contextMenuItems(i)) this.props.openContextMenu(x, y) } + else{ + this.props.updateSelection(this.state.notebooks[i]) + } this.refs.mainList.setIndex(-1) + }; preventEventProp = (ev) => { @@ -343,7 +414,6 @@ export default class LibraryNav extends React.Component { {this.state.notebooks.map((notebook, i) =>{ var l = null - if (notebook.state == 'editing'){ l = {} + closeContextMenu: () => {}, + updateSelection: () => {} }; diff --git a/app/components/__tests__/EntrySelector-test.jsx b/app/components/__tests__/EntrySelector-test.jsx index a9ba52f..f36d00f 100644 --- a/app/components/__tests__/EntrySelector-test.jsx +++ b/app/components/__tests__/EntrySelector-test.jsx @@ -12,6 +12,6 @@ describe('EntrySelector', () => { var entrySelector = TestUtils.renderIntoDocument( ) - expect(entrySelector.state.entries.length).toEqual(0) + expect(entrySelector.state.notes.length).toEqual(0) }) }) diff --git a/app/constants/navigation.jsx b/app/constants/navigation.jsx new file mode 100644 index 0000000..e585150 --- /dev/null +++ b/app/constants/navigation.jsx @@ -0,0 +1 @@ +export const UPDATE_SELECTION = 'UPDATE_SELECTION' diff --git a/app/containers/App.jsx b/app/containers/App.jsx index 624a608..629ee7d 100644 --- a/app/containers/App.jsx +++ b/app/containers/App.jsx @@ -3,6 +3,7 @@ import getMuiTheme from 'material-ui/lib/styles/getMuiTheme' import Styles from 'material-ui/lib/styles' import mui from 'material-ui' import * as ContextMenuActions from '../actions/contextMenu' +import * as NavigationActions from '../actions/navigation' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' @@ -51,7 +52,13 @@ class App extends React.Component { render() { - const { contextMenu, contextMenuActions } = this.props + const { + contextMenu, + contextMenuActions, + navigation, + navigationActions + } = this.props + return (
@@ -91,18 +102,22 @@ class App extends React.Component { App.propTypes = { contextMenu: React.PropTypes.object.isRequired, - contextMenuActions: React.PropTypes.object.isRequired + contextMenuActions: React.PropTypes.object.isRequired, + navigation: React.PropTypes.object.isRequired, + navigationActions: React.PropTypes.object.isRequired } function mapStateToProps(state) { return { - contextMenu: state.contextMenu + contextMenu: state.contextMenu, + navigation: state.navigation } } function mapDispatchToProps(dispatch) { return { - contextMenuActions: bindActionCreators(ContextMenuActions, dispatch) + contextMenuActions: bindActionCreators(ContextMenuActions, dispatch), + navigationActions: bindActionCreators(NavigationActions, dispatch) } } diff --git a/app/reducers/contextMenu.jsx b/app/reducers/contextMenu.jsx index 1ff5d8f..718020e 100644 --- a/app/reducers/contextMenu.jsx +++ b/app/reducers/contextMenu.jsx @@ -1,13 +1,12 @@ import { UPDATE_CONTEXT_MENU, OPEN_CONTEXT_MENU, CLOSE_CONTEXT_MENU } from '../constants/contextMenu' -const initialState = - { +const initialState = { opened: false, x: 0, y: 0, items: [] - } +} export default function contextMenu(state = initialState, action){ switch (action.type) { diff --git a/app/reducers/index.jsx b/app/reducers/index.jsx index 0ffef29..8601da9 100644 --- a/app/reducers/index.jsx +++ b/app/reducers/index.jsx @@ -1,8 +1,10 @@ import { combineReducers } from 'redux' import contextMenu from './contextMenu' +import navigation from './navigation' const rootReducer = combineReducers({ - contextMenu + contextMenu, + navigation }) export default rootReducer diff --git a/app/reducers/navigation.jsx b/app/reducers/navigation.jsx new file mode 100644 index 0000000..bd0c414 --- /dev/null +++ b/app/reducers/navigation.jsx @@ -0,0 +1,18 @@ +import { UPDATE_SELECTION } from '../constants/navigation' + +const initialState = +{ + selection: { + } +} + +export default function navigation(state = initialState, action){ + switch (action.type) { + case UPDATE_SELECTION: + return Object.assign({}, state, { + selection: action.selection + }) + default: + return state + } +} diff --git a/app/utils.jsx b/app/utils.jsx index dbc575b..fb5167a 100644 --- a/app/utils.jsx +++ b/app/utils.jsx @@ -51,3 +51,8 @@ export function loadNotebookByName(nameOrUUID){ export function getNotebookPathFromUUID(uuid){ return getNotebookPath({uuid: uuid}) } + +export function isFunction(functionToCheck) { + var getType = {} + return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]' +}