Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added action type: 'pushToCurrent' #389

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import assert from 'assert';
import Scene from './Scene';
export const JUMP_ACTION = 'jump';
export const PUSH_ACTION = 'push';
export const PUSH_TO_CURRENT_ACTION = 'pushToCurrent';
export const REPLACE_ACTION = 'replace';
export const POP_ACTION2 = 'back';
export const POP_ACTION = 'BackAction';
Expand Down Expand Up @@ -51,7 +52,7 @@ class Actions {
assert(root.props, "props should be defined for stack");
const key = root.key;
assert(key, "unique key should be defined ",root);
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, RESET_ACTION, 'create',
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, PUSH_TO_CURRENT_ACTION, RESET_ACTION, 'create',
'init','callback','iterate','current'].indexOf(key)==-1, key+" is not allowed as key name");
const {children, ...staticProps} = root.props;
let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION);
Expand Down
65 changes: 47 additions & 18 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import {PUSH_ACTION, POP_ACTION2, FOCUS_ACTION, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from './Actions';
import {PUSH_ACTION, PUSH_TO_CURRENT_ACTION, POP_ACTION2, FOCUS_ACTION, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from './Actions';
import assert from 'assert';
import Immutable from 'immutable';
import {getInitialState} from './State';
Expand All @@ -16,13 +16,13 @@ function findElement(state, key) {
if (state.sceneKey != key){
if (state.children){
let result = undefined;
state.children.forEach(el=>{
for (let el of state.children) {
let res = findElement(el, key);
if (res){
result = res;
return res;
break;
}
});
}
return result;
} else {
return false;
Expand All @@ -34,27 +34,26 @@ function findElement(state, key) {

function getCurrent(state){
if (!state.children){
return state.key;
return state;
}
return getCurrent(state.children[state.index]);
}


var _uniqPushed = 0;

function update(state,action){
// clone state, TODO: clone effectively?
if (!state.scenes[action.key] && action.key.indexOf('_')!=-1){
action.key = action.key.substring(action.key.indexOf('_')+1);
//console.log("Transform to key="+action.key);
if (!state.scenes[action.key]) {
console.log("No scene for key="+action.key);
return state;
}
const newProps = {...state.scenes[action.key], ...action};
let newProps = {...state.scenes[action.key], ...action};
let newState = Immutable.fromJS(state).toJS();

// change route property
//newState.scenes[action.key] = newProps;

// get parent
const parent = newProps.parent;
let parent = newProps.parent;
assert(parent, "No parent is defined for route="+action.key);

// find parent in the state
Expand All @@ -70,9 +69,12 @@ function update(state,action){
assert(el, "Cannot find element for parent="+el.parent+" within current state");
}
if (el.children.length > 1) {
el.children.pop();
let popped = el.children.pop();
el.index = el.children.length - 1;
newState.scenes.current = getCurrent(newState);
if (popped.ephemeral) {
delete newState.scenes[popped.key];
}
newState.scenes.current = getCurrent(newState).key;
return newState;
} else {
console.log("Cannot do pop");
Expand All @@ -81,15 +83,41 @@ function update(state,action){

case REFRESH_ACTION:
let ind = -1;
el.children.forEach((c,i)=>{if (c.sceneKey==action.key){ind=i}});
for (let i=0; i < el.children.length; i++) {
let c = el.children[i];
if (c.ephemeral) {
if (c.key === action.key) {
ind = i;
break;
}
} else {
if (c.sceneKey === action.key) {
ind = i;
break;
}
}
}
assert(ind!=-1, "Cannot find route with key="+action.key+" for parent="+el.key);
el.children[ind] = getInitialState(newProps, newState.scenes, ind, action);
return newState;

case PUSH_TO_CURRENT_ACTION:
parent = getCurrent(newState).parent;
newProps.parent = parent;
el = findElement(newState, parent);
assert(el, "Cannot find element for parent="+parent+" within current state:"+JSON.stringify(newState));
// fall through to PUSH_ACTION

case PUSH_ACTION:
newProps.ephemeral = true;
newProps.key = `${_uniqPushed++}$${newProps.key}`;
el.children.push(getInitialState(newProps, newState.scenes, el.children.length, action));
el.index = el.children.length - 1;
newState.scenes.current = getCurrent(newState);
newState.scenes.current = getCurrent(newState).key;
if (newProps.ephemeral) {
assert(!newState.scenes.hasOwnProperty(newState.scenes.current), "scenes should not contain ephemeral key="+newState.scenes.current);
newState.scenes[newState.scenes.current] = newProps;
}
return newState;

case JUMP_ACTION:
Expand All @@ -110,7 +138,7 @@ function update(state,action){
} else {
el.children = [getInitialState(newProps, newState.scenes, 0, action)];
}
newState.scenes.current = getCurrent(newState);
newState.scenes.current = getCurrent(newState).key;
return newState;

default:
Expand Down Expand Up @@ -146,6 +174,7 @@ function reducer({initialState, scenes}){
case POP_ACTION2:
case POP_ACTION:
case REFRESH_ACTION:
case PUSH_TO_CURRENT_ACTION:
case PUSH_ACTION:
case JUMP_ACTION:
case REPLACE_ACTION:
Expand All @@ -161,4 +190,4 @@ function reducer({initialState, scenes}){

}

export default reducer;
export default reducer;