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

Fix some eslint errors #585

Merged
merged 1 commit into from
Apr 25, 2016
Merged
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
28 changes: 19 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import Router from './src/Router';
import Scene from './src/Scene';
import Actions from './src/Actions';
import getInitialState from './src/State';
import Reducer from './src/Reducer';
import TabBar from './src/TabBar';
import Modal from './src/Modal';
import DefaultRenderer from './src/DefaultRenderer';
import Switch from './src/Switch';
import Modal from './src/Modal';
import NavBar from './src/NavBar';
import Reducer from './src/Reducer';
import Router from './src/Router';
import Scene from './src/Scene';
import Switch from './src/Switch';
import TabBar from './src/TabBar';
import getInitialState from './src/State';

module.exports = { DefaultRenderer, NavBar, Switch, Modal, Router, TabBar,
Scene, Actions, Reducer, getInitialState };
module.exports = {
Actions,
DefaultRenderer,
Modal,
NavBar,
Reducer,
Router,
Scene,
Switch,
TabBar,
getInitialState,
};
64 changes: 36 additions & 28 deletions src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ function isNumeric(n) {
}

function filterParam(data) {
if (data.toString() != '[object Object]')
return { data: data };
if (data.toString() !== '[object Object]') {
return { data };
}
if (!data) {
return {};
}
var proto = (data || {}).constructor.name;
const proto = (data || {}).constructor.name;
// avoid passing React Native parameters
if (proto != 'Object') {
if (proto !== 'Object') {
data = {};
}
return data;
Expand All @@ -51,69 +52,76 @@ class Actions {
assert(root.props, 'props should be defined for stack');
const key = root.key || 'root';
assert(key, 'unique key should be defined ', root);
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, FOCUS_ACTION, RESET_ACTION, 'create',
'init', 'callback', 'iterate', 'current'].indexOf(key) == -1, key + ' is not allowed as key name');
const { children, ...staticProps } = root.props;
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION,
FOCUS_ACTION, RESET_ACTION, 'create', 'init', 'callback', 'iterate', 'current',
].indexOf(key) === -1, `${key} is not allowed as key name`);
const { ...staticProps } = root.props;
let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION);
if (type === 'switch') {
type = JUMP_ACTION;
}
let res = { name:key, ...staticProps, key, sceneKey:key, type, parent:parentProps.key };
const res = { name: key, ...staticProps, key, sceneKey: key, type, parent: parentProps.key };
if (root.props.children) {
const list = root.props.children instanceof Array ? root.props.children : [root.props.children];
const list = root.props.children instanceof Array ?
root.props.children : [root.props.children];
res.children = list.map(c => this.iterate(c, res, refs).key);
} else {
assert(staticProps.component, 'component property is not set for key=' + key);
assert(staticProps.component, `component property is not set for key=${key}`);
// wrap scene if parent is "tabs"
if (parentProps.tabs) {
const innerKey = res.key + '_';
const inner = { ...res, name:key, key: innerKey, type: PUSH_ACTION, parent:res.key };
const innerKey = `${res.key}_`;
const inner = { ...res, name: key, key: innerKey, type: PUSH_ACTION, parent: res.key };
refs[innerKey] = inner;
res.children = [innerKey];
delete res.component;
}
res.index = 0;
}
if (this[key]) {
console.log('Key ' + root.key + ' is already defined!');
console.log(`Key ${root.key} is already defined!`);
}
this[key] =
(props = {}) => { assert(this.callback, 'Actions.callback is not defined!');
this.callback({ key: root.key, type, ...filterParam(props) }); };
this[key] = (props = {}) => {
assert(this.callback, 'Actions.callback is not defined!');
this.callback({ key: root.key, type, ...filterParam(props) });
};
refs[res.key] = res;

return res;
}

pop(props = {}) {
props = filterParam(props);
const data = isNumeric(props) ? { num: props } : props;
this.callback && this.callback({ ...props, type: POP_ACTION });
if (this.callback) {
this.callback({ ...filterParam(props), type: POP_ACTION });
}
}

jump(props = {}) {
props = filterParam(props);
this.callback && this.callback({ ...props, type: JUMP_ACTION });
if (this.callback) {
this.callback({ ...filterParam(props), type: JUMP_ACTION });
}
}

init(props = {}) {
props = filterParam(props);
this.callback && this.callback({ ...props, type: INIT_ACTION });
if (this.callback) {
this.callback({ ...filterParam(props), type: INIT_ACTION });
}
}

refresh(props = {}) {
props = filterParam(props);
this.callback && this.callback({ ...props, type: REFRESH_ACTION });
if (this.callback) {
this.callback({ ...filterParam(props), type: REFRESH_ACTION });
}
}

focus(props = {}) {
props = filterParam(props);
this.callback && this.callback({ ...props, type: FOCUS_ACTION });
if (this.callback) {
this.callback({ ...filterParam(props), type: FOCUS_ACTION });
}
}

create(scene:Scene) {
assert(scene, 'root scene should be defined');
let refs = {};
const refs = {};
this.iterate(scene, {}, refs);
return refs;
}
Expand Down
167 changes: 92 additions & 75 deletions src/DefaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,52 @@
* LICENSE file in the root directory of this source tree.
*
*/
import React, { Component, Animated, PropTypes, StyleSheet, View, NavigationExperimental } from 'react-native';
import React, {
Animated,
NavigationExperimental,
PropTypes,
StyleSheet,
View,
} from 'react-native';
const {
AnimatedView: NavigationAnimatedView,
Card: NavigationCard,
RootContainer: NavigationRootContainer,
Header: NavigationHeader,
} = NavigationExperimental;
import TabBar from './TabBar';
import NavBar from './NavBar';
AnimatedView: NavigationAnimatedView,
Card: NavigationCard,
RootContainer: NavigationRootContainer,
Header: NavigationHeader,
} = NavigationExperimental;
import Actions from './Actions';
import NavBar from './NavBar';
import TabBar from './TabBar';

export default class DefaultRenderer extends Component {
constructor(props) {
super(props);
this._renderCard = this._renderCard.bind(this);
this._renderScene = this._renderScene.bind(this);
this._renderHeader = this._renderHeader.bind(this);
}
const propTypes = {
navigationState: PropTypes.object,
};

const styles = StyleSheet.create({
animatedView: {
flex: 1,
backgroundColor: 'transparent',
},
});

class DefaultRenderer extends React.Component {
static childContextTypes = {
navigationState: PropTypes.any,
};

constructor(props) {
super(props);
this.renderCard = this.renderCard.bind(this);
this.renderScene = this.renderScene.bind(this);
this.renderHeader = this.renderHeader.bind(this);
}

getChildContext() {
return {
navigationState: this.props.navigationState,
};
}

componentDidMount() {
this.dispatchFocusAction(this.props);
}
Expand All @@ -47,10 +70,41 @@ export default class DefaultRenderer extends Component {
Actions.focus({ scene });
}

getChildContext() {
return {
navigationState: this.props.navigationState,
};
renderHeader(/* NavigationSceneRendererProps*/ props) {
const state = props.navigationState;
let selected = state.children[state.index];
while (selected.hasOwnProperty('children')) {
selected = selected.children[selected.index];
}
const Component = state.navBar || selected.navBar || NavBar;
return <Component {...props} getTitle={titleState => titleState.title} />;
}

renderCard(/* NavigationSceneRendererProps*/ props) {
const { key, direction, panHandlers, getSceneStyle } = props.scene.navigationState;

const optionals = {};
if (getSceneStyle) optionals.style = getSceneStyle(props);

return (
<NavigationCard
{...props}
key={`card_${key}`}
direction={direction || 'horizontal'}
panHandlers={panHandlers}
renderScene={this.renderScene}
{...optionals}
/>
);
}

renderScene(/* NavigationSceneRendererProps*/ props) {
return (
<DefaultRenderer
key={props.scene.navigationState.key}
navigationState={props.scene.navigationState}
/>
);
}

render() {
Expand All @@ -64,20 +118,20 @@ export default class DefaultRenderer extends Component {
}
if (Component) {
return (
<View style={[{ flex: 1 }, navigationState.sceneStyle]}>
<Component {...navigationState} navigationState={navigationState} />
</View>
);
<View style={[{ flex: 1 }, navigationState.sceneStyle]}>
<Component {...navigationState} navigationState={navigationState} />
</View>
);
}

const selected = navigationState.children[navigationState.index];
// return <DefaultRenderer key={selected.key} navigationState={selected}/>

let applyAnimation = selected.applyAnimation || navigationState.applyAnimation;
let style = selected.style || navigationState.style;
const applyAnimation = selected.applyAnimation || navigationState.applyAnimation;
const style = selected.style || navigationState.style;
let direction = selected.direction || navigationState.direction || 'horizontal';

let optionals = {};
const optionals = {};
if (applyAnimation) {
optionals.applyAnimation = applyAnimation;
} else {
Expand All @@ -91,55 +145,18 @@ export default class DefaultRenderer extends Component {
}

return (
<NavigationAnimatedView
navigationState={navigationState}
style={[styles.animatedView, style]}
renderOverlay={this._renderHeader}
direction={direction}
renderScene={this._renderCard}
{...optionals}
/>
);
}

_renderHeader(/* NavigationSceneRendererProps*/ props) {
const state = props.navigationState;
const child = state.children[state.index];
let selected = state.children[state.index];
while (selected.hasOwnProperty('children')) {
selected = selected.children[selected.index];
}
const Component = state.navBar || selected.navBar || NavBar;
return <Component {...props} getTitle={state => state.title} />;
<NavigationAnimatedView
navigationState={navigationState}
style={[styles.animatedView, style]}
renderOverlay={this.renderHeader}
direction={direction}
renderScene={this.renderCard}
{...optionals}
/>
);
}

_renderCard(/* NavigationSceneRendererProps*/ props) {
const { key, direction, panHandlers, getSceneStyle } = props.scene.navigationState;

const optionals = {};
if (getSceneStyle) optionals.style = getSceneStyle(props);

return (
<NavigationCard
{...props}
key={'card_' + key}
direction={direction || 'horizontal'}
panHandlers={panHandlers}
renderScene={this._renderScene}
{...optionals}
/>
);
}

_renderScene(/* NavigationSceneRendererProps*/ props) {
return <DefaultRenderer key={props.scene.navigationState.key} navigationState={props.scene.navigationState} />;
}

}

const styles = StyleSheet.create({
animatedView: {
flex: 1,
backgroundColor:'transparent'
},
});
DefaultRenderer.propTypes = propTypes;

export default DefaultRenderer;
Loading