Add stateful redux functions for navigation

This commit is contained in:
Joey Payne 2016-03-11 15:04:50 -07:00
commit 0cc8e837bf
3 changed files with 267 additions and 5 deletions

View file

@ -1,5 +1,74 @@
import * as types from '../constants/navigation'
export function updateSelection(selection) {
return { type: types.UPDATE_SELECTION, selection }
export function updateSelection(selection, callback) {
return {
type: types.UPDATE_SELECTION,
selection,
callback
}
}
export function addMenuItem(item, callback){
return {
type: types.ADD_MENU_ITEM,
item,
callback
}
}
export function updateMenuItem(item, index, callback){
return {
type: types.UPDATE_MENU_ITEM,
item,
index,
callback
}
}
export function removeMenuItem(index, callback){
return {
type: types.REMOVE_MENU_ITEM,
index,
callback
}
}
export function addNotebook(notebook, callback){
return {
type: types.ADD_NOTEBOOK,
notebook,
callback
}
}
export function removeNotebook(index, callback){
return {
type: types.REMOVE_NOTEBOOK,
index,
callback
}
}
export function updateNotebook(notebook, index, callback){
return {
type: types.UPDATE_NOTEBOOK,
notebook,
index,
callback
}
}
export function sortNotebooks(sortFunc, callback){
return {
type: types.SORT_NOTEBOOKS,
sortFunc,
callback
}
}
export function refreshNavigation(callback){
return {
type: types.REFRESH,
callback
}
}

View file

@ -1 +1,16 @@
export const UPDATE_SELECTION = 'UPDATE_SELECTION'
export const REFRESH = 'REFRESH'
// Menu item manipulation
export const MENU_TYPE = 'MENU_TYPE'
export const ADD_MENU_ITEM = 'ADD_MENU_ITEM'
export const UPDATE_MENU_ITEM = 'UPDATE_MENU_ITEM'
export const REMOVE_MENU_ITEM = 'REMOVE_MENU_ITEM'
// Notebook item manipulation
export const NOTEBOOK_TYPE = 'NOTEBOOK_TYPE'
export const ADD_NOTEBOOK = 'ADD_NOTEBOOK'
export const REMOVE_NOTEBOOK = 'REMOVE_NOTEBOOK'
export const SORT_NOTEBOOKS = 'SORT_NOTEBOOKS'
export const UPDATE_NOTEBOOK = 'UPDATE_NOTEBOOK'

View file

@ -1,17 +1,195 @@
import { UPDATE_SELECTION } from '../constants/navigation'
import {
UPDATE_SELECTION,
ADD_MENU_ITEM,
UPDATE_MENU_ITEM,
REMOVE_MENU_ITEM,
ADD_NOTEBOOK,
REMOVE_NOTEBOOK,
UPDATE_NOTEBOOK,
SORT_NOTEBOOKS,
NOTEBOOK_TYPE,
MENU_TYPE,
REFRESH
} from '../constants/navigation'
import * as utils from '../utils'
import glob from 'glob'
import jsfile from 'jsonfile'
import path from 'path-extra'
// Load default selection
utils.createNotebookDir('Entries')
const initialState =
{
selection: {
selection: utils.loadNotebookByName('Entries'),
selectionIndex: 0,
selectionType: MENU_TYPE,
menuItems: [],
notebooks: [],
callback: () => {}
}
var emptyFunc = () => {}
function findIndexGeneric(array, notebook, type){
for(var i=0; i<array.length; i++){
var item = array[i]
if (item.uuid == notebook.uuid){
return {
'type': type,
'index': i
}
}
}
return null
}
function findIndex(state, notebook){
var result = null
result = findIndexGeneric(state.menuItems, notebook, MENU_TYPE)
if (result != null){
return result
}
result = findIndexGeneric(state.notebooks, notebook, NOTEBOOK_TYPE)
if (result != null){
return result
}
return {'type': MENU_TYPE, 'index': 0}
}
function refreshMenuItems(state){
var menuItems = state.menuItems
for(var i=0; i<menuItems.length; i++){
var nb = menuItems[i]
if(nb.isNotebook){
var temp = {
title: nb.name,
uuid: nb.name,
notes: 0
}
var loaded = utils.loadNotebookByName(nb.name)
nb.title = loaded.title
nb.uuid = loaded.uuid
nb.path = loaded.path
nb.notes = loaded.notes
}
else if(nb.glob){
var dataPath = utils.getAppDataPath()
var notes = glob.sync(path.join(dataPath, nb.glob))
nb.title = nb.name
nb.uuid = nb.name
nb.notes = notes.length
}
}
return menuItems
}
function refreshNotebooks(){
var dataPath = utils.getAppDataPath()
var notebooks = glob.sync(path.join(dataPath, '!(Entries|Trash).qvnotebook'))
var newNotebooks = []
for(var i=0; i<notebooks.length; i++){
var nbPath = notebooks[i]
var obj = jsfile.readFileSync(path.join(nbPath, 'meta.json'))
var notes = glob.sync(path.join(nbPath, '*.qvnote'))
var nb = {}
nb.title = obj.name
nb.uuid = obj.uuid
nb.notes = notes.length
nb.path = nbPath
if(nb.title == ''){
nb.state = 'editing'
}
else{
nb.state = 'displaying'
}
newNotebooks.push(nb)
}
newNotebooks.sort(utils.compareNotebooks)
return newNotebooks
}
export default function navigation(state = initialState, action){
switch (action.type) {
case UPDATE_SELECTION:
var selectionDetails = findIndex(state, action.selection)
return Object.assign({}, state, {
selection: action.selection
selection: action.selection,
selectionType: selectionDetails.type,
selectionIndex: selectionDetails.index,
callback: action.callback || emptyFunc
})
case ADD_MENU_ITEM:
state.menuItems.push(action.item)
return Object.assign({}, state, {
menuItems: state.menuItems,
callback: action.callback || emptyFunc
})
case REMOVE_MENU_ITEM:
state.menuItems.splice(action.index, 1)
return Object.assign({}, state, {
menuItems: state.menuItems,
callback: action.callback || emptyFunc
})
case ADD_NOTEBOOK:
state.notebooks.splice(0, 0, action.notebook)
return Object.assign({}, state, {
notebooks: state.notebooks,
callback: action.callback || emptyFunc
})
case REMOVE_NOTEBOOK:
state.notebooks.splice(action.index, 1)
return Object.assign({}, state, {
notebooks: state.notebooks,
callback: action.callback || emptyFunc
})
case UPDATE_MENU_ITEM:
state.menuItems[action.index] = action.item
return Object.assign({}, state, {
menuItems: state.menuItems,
callback: action.callback || emptyFunc
})
case UPDATE_NOTEBOOK:
state.notebooks[action.index] = action.notebook
return Object.assign({}, state, {
notebooks: state.notebooks,
callback: action.callback || emptyFunc
})
case SORT_NOTEBOOKS:
state.notebooks.sort(action.sortFunc)
return Object.assign({}, state, {
notebooks: state.notebooks,
callback: action.callback || emptyFunc
})
case REFRESH:
var menuItems = refreshMenuItems(state)
var newNotebooks = refreshNotebooks()
return Object.assign({}, state, {
notebooks: newNotebooks,
menuItems: menuItems,
callback: action.callback || emptyFunc
})
default:
return state
}