2016-02-13 20:58:47 +05:30
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { routeActions } from 'react-router-redux';
|
2016-01-31 18:29:38 +05:30
|
|
|
import { TransitionMotion, spring } from 'react-motion';
|
|
|
|
import ReactHeight from 'react-height';
|
|
|
|
|
|
|
|
import { Panel, PanelBody, PanelFooter, PanelHeader } from 'components/ui/Panel';
|
2016-02-13 20:58:47 +05:30
|
|
|
import { Form } from 'components/ui/Form';
|
2016-01-31 18:29:38 +05:30
|
|
|
import {helpLinks as helpLinksStyles} from 'components/auth/helpLinks.scss';
|
|
|
|
import panelStyles from 'components/ui/panel.scss';
|
|
|
|
import icons from 'components/ui/icons.scss';
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
import * as actions from './actions';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
const opacitySpringConfig = [300, 20];
|
2016-02-06 14:33:51 +05:30
|
|
|
const transformSpringConfig = [500, 50];
|
2016-02-06 15:32:23 +05:30
|
|
|
const changeContextSpringConfig = [500, 20];
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
class PanelTransition extends Component {
|
|
|
|
static displayName = 'PanelTransition';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
auth: PropTypes.shape({
|
|
|
|
error: PropTypes.string,
|
|
|
|
login: PropTypes.shape({
|
|
|
|
login: PropTypes.string,
|
|
|
|
password: PropTypes.string
|
|
|
|
})
|
|
|
|
}).isRequired,
|
|
|
|
goBack: React.PropTypes.func.isRequired,
|
|
|
|
setError: React.PropTypes.func.isRequired,
|
|
|
|
clearErrors: React.PropTypes.func.isRequired,
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
Title: PropTypes.element.isRequired,
|
|
|
|
Body: PropTypes.element.isRequired,
|
|
|
|
Footer: PropTypes.element.isRequired,
|
|
|
|
Links: PropTypes.element.isRequired
|
|
|
|
};
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
state = {
|
2016-02-06 14:33:51 +05:30
|
|
|
height: {},
|
|
|
|
contextHeight: 0
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-02-03 11:06:00 +05:30
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-02-13 20:58:47 +05:30
|
|
|
var nextPath = nextProps.path;
|
|
|
|
var previousPath = this.props.path;
|
|
|
|
|
|
|
|
if (nextPath !== previousPath) {
|
|
|
|
var direction = this.getDirection(nextPath, previousPath);
|
|
|
|
var forceHeight = direction === 'Y' && nextPath !== previousPath ? 1 : 0;
|
|
|
|
|
|
|
|
this.props.clearErrors();
|
|
|
|
this.setState({
|
|
|
|
direction,
|
|
|
|
forceHeight,
|
|
|
|
previousPath
|
|
|
|
});
|
|
|
|
|
|
|
|
if (forceHeight) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({forceHeight: 0});
|
|
|
|
}, 100);
|
|
|
|
}
|
2016-02-03 11:06:00 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-02-13 20:58:47 +05:30
|
|
|
const {height, canAnimateHeight, contextHeight, forceHeight} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-06 14:33:51 +05:30
|
|
|
const {path, Title, Body, Footer, Links} = this.props;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<TransitionMotion
|
|
|
|
styles={{
|
|
|
|
[path]: {
|
|
|
|
Title,
|
|
|
|
Body,
|
|
|
|
Footer,
|
|
|
|
Links,
|
2016-02-13 20:58:47 +05:30
|
|
|
hasBackButton: Title.type.goBack,
|
2016-02-03 11:06:00 +05:30
|
|
|
transformSpring: spring(0, transformSpringConfig),
|
|
|
|
opacitySpring: spring(1, opacitySpringConfig)
|
2016-01-31 18:29:38 +05:30
|
|
|
},
|
|
|
|
common: {
|
2016-02-06 14:33:51 +05:30
|
|
|
heightSpring: spring(forceHeight || height[path] || 0, transformSpringConfig),
|
2016-02-06 15:32:23 +05:30
|
|
|
switchContextHeightSpring: spring(forceHeight || contextHeight, changeContextSpringConfig)
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
}}
|
|
|
|
willEnter={this.willEnter}
|
|
|
|
willLeave={this.willLeave}
|
|
|
|
>
|
|
|
|
{(items) => {
|
2016-02-13 20:58:47 +05:30
|
|
|
const keys = Object.keys(items).filter((key) => key !== 'common');
|
|
|
|
|
|
|
|
const contentHeight = {
|
|
|
|
overflow: 'hidden',
|
|
|
|
height: forceHeight ? items.common.switchContextHeightSpring : 'auto'
|
|
|
|
};
|
|
|
|
|
|
|
|
const bodyHeight = {
|
|
|
|
position: 'relative',
|
|
|
|
height: `${canAnimateHeight ? items.common.heightSpring : height[path]}px`
|
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
return (
|
2016-02-13 20:58:47 +05:30
|
|
|
<Form id={path} onSubmit={this.onFormSubmit} onInvalid={this.onFormInvalid}>
|
2016-01-31 18:29:38 +05:30
|
|
|
<Panel>
|
|
|
|
<PanelHeader>
|
2016-02-06 15:32:23 +05:30
|
|
|
{keys.map((key) => this.getHeader(key, items[key]))}
|
2016-01-31 18:29:38 +05:30
|
|
|
</PanelHeader>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={contentHeight}>
|
|
|
|
<ReactHeight onHeightReady={this.onUpdateContextHeight}>
|
2016-02-06 15:32:23 +05:30
|
|
|
<PanelBody>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={bodyHeight}>
|
2016-02-06 14:33:51 +05:30
|
|
|
{keys.map((key) => this.getBody(key, items[key]))}
|
|
|
|
</div>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelFooter>
|
2016-02-06 15:32:23 +05:30
|
|
|
{keys.map((key) => this.getFooter(key, items[key]))}
|
2016-02-06 14:33:51 +05:30
|
|
|
</PanelFooter>
|
|
|
|
</ReactHeight>
|
|
|
|
</div>
|
2016-01-31 18:29:38 +05:30
|
|
|
</Panel>
|
2016-02-06 15:32:23 +05:30
|
|
|
<div className={helpLinksStyles}>
|
2016-01-31 18:29:38 +05:30
|
|
|
{keys.map((key) => this.getLinks(key, items[key]))}
|
|
|
|
</div>
|
2016-02-13 20:58:47 +05:30
|
|
|
</Form>
|
2016-01-31 18:29:38 +05:30
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TransitionMotion>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
onFormSubmit = () => {
|
|
|
|
this.body.onFormSubmit();
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
onFormInvalid = (errorMessage) => {
|
|
|
|
this.props.setError(errorMessage);
|
2016-02-06 15:32:23 +05:30
|
|
|
};
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
willEnter = (key, styles) => this.getTransitionStyles(key, styles);
|
|
|
|
willLeave = (key, styles) => this.getTransitionStyles(key, styles, {isLeave: true});
|
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {Object} styles
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Object} [options.isLeave=false] - true, if this is a leave transition
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getTransitionStyles(key, styles, options = {}) {
|
|
|
|
var {isLeave = false} = options;
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
var map = {
|
|
|
|
'/login': -1,
|
|
|
|
'/register': -1,
|
|
|
|
'/password': 1,
|
|
|
|
'/activation': 1,
|
2016-02-02 23:32:00 +05:30
|
|
|
'/oauth/permissions': -1
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
var sign = map[key];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...styles,
|
2016-02-06 15:32:23 +05:30
|
|
|
pointerEvents: isLeave ? 'none' : 'auto',
|
2016-01-31 18:29:38 +05:30
|
|
|
transformSpring: spring(sign * 100, transformSpringConfig),
|
2016-02-06 15:32:23 +05:30
|
|
|
opacitySpring: spring(isLeave ? 0 : 1, opacitySpringConfig)
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
onUpdateHeight = (height) => {
|
|
|
|
const canAnimateHeight = Object.keys(this.state.height).length > 1 || this.state.height[[this.props.path]];
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
this.setState({
|
2016-02-13 20:58:47 +05:30
|
|
|
canAnimateHeight,
|
2016-01-31 18:29:38 +05:30
|
|
|
height: {
|
|
|
|
...this.state.height,
|
|
|
|
[this.props.path]: height
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
onUpdateContextHeight = (height) => {
|
2016-02-06 14:33:51 +05:30
|
|
|
this.setState({
|
|
|
|
contextHeight: height
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
onGoBack = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
this.body.onGoBack && this.body.onGoBack();
|
|
|
|
this.props.goBack();
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
getDirection(next, prev) {
|
2016-02-06 14:33:51 +05:30
|
|
|
var not = (path) => prev !== path && next !== path;
|
|
|
|
|
|
|
|
var map = {
|
|
|
|
'/login': not('/password') ? 'Y' : 'X',
|
|
|
|
'/password': not('/login') ? 'Y' : 'X',
|
|
|
|
'/register': not('/activation') ? 'Y' : 'X',
|
|
|
|
'/activation': not('/register') ? 'Y' : 'X',
|
|
|
|
'/oauth/permissions': 'Y'
|
|
|
|
};
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
return map[next];
|
2016-02-06 14:33:51 +05:30
|
|
|
}
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
getHeader(key, props) {
|
|
|
|
var {hasBackButton, transformSpring, Title} = props;
|
|
|
|
|
|
|
|
var style = {
|
2016-02-06 15:32:23 +05:30
|
|
|
...this.getDefaultTransitionStyles(props),
|
|
|
|
opacity: 1 // reset default
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
var scrollStyle = this.translate(transformSpring, 'Y');
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
var sideScrollStyle = {
|
|
|
|
position: 'relative',
|
|
|
|
zIndex: 2,
|
2016-02-06 15:32:23 +05:30
|
|
|
...this.translate(-Math.abs(transformSpring))
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
var backButton = (
|
2016-02-13 20:58:47 +05:30
|
|
|
<button style={sideScrollStyle} type="button" onClick={this.onGoBack} className={panelStyles.headerControl}>
|
2016-01-31 18:29:38 +05:30
|
|
|
<span className={icons.arrowLeft} />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`header${key}`} style={style}>
|
|
|
|
{hasBackButton ? backButton : null}
|
|
|
|
<div style={scrollStyle}>
|
2016-02-13 20:58:47 +05:30
|
|
|
{React.cloneElement(Title, this.props)}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getBody(key, props) {
|
2016-02-06 15:32:23 +05:30
|
|
|
var {transformSpring, Body} = props;
|
2016-02-06 14:33:51 +05:30
|
|
|
var {direction} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
var transform = this.translate(transformSpring, direction);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
|
2016-02-02 23:32:00 +05:30
|
|
|
var verticalOrigin = 'top';
|
2016-02-06 14:33:51 +05:30
|
|
|
if (direction === 'Y') {
|
2016-02-02 23:32:00 +05:30
|
|
|
verticalOrigin = 'bottom';
|
2016-02-06 14:33:51 +05:30
|
|
|
transform = {};
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var style = {
|
2016-02-06 15:32:23 +05:30
|
|
|
...this.getDefaultTransitionStyles(props),
|
|
|
|
top: 'auto', // reset default
|
2016-02-02 23:32:00 +05:30
|
|
|
[verticalOrigin]: 0,
|
2016-02-06 14:33:51 +05:30
|
|
|
...transform
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2016-02-13 20:58:47 +05:30
|
|
|
<ReactHeight key={`body${key}`} style={style} onHeightReady={this.onUpdateHeight}>
|
|
|
|
{React.cloneElement(Body, {
|
|
|
|
...this.props,
|
|
|
|
ref: (body) => {
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
})}
|
2016-01-31 18:29:38 +05:30
|
|
|
</ReactHeight>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFooter(key, props) {
|
2016-02-06 15:32:23 +05:30
|
|
|
var {Footer} = props;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
var style = this.getDefaultTransitionStyles(props);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`footer${key}`} style={style}>
|
2016-02-13 20:58:47 +05:30
|
|
|
{React.cloneElement(Footer, this.props)}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getLinks(key, props) {
|
2016-02-06 15:32:23 +05:30
|
|
|
var {Links} = props;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
var style = this.getDefaultTransitionStyles(props);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`links${key}`} style={style}>
|
2016-02-13 20:58:47 +05:30
|
|
|
{React.cloneElement(Links, this.props)}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-02-06 15:32:23 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} props
|
|
|
|
* @param {string} props.pointerEvents
|
|
|
|
* @param {number} props.opacitySpring
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getDefaultTransitionStyles(props) {
|
|
|
|
var {pointerEvents, opacitySpring} = props;
|
|
|
|
|
|
|
|
return {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
opacity: opacitySpring,
|
|
|
|
pointerEvents
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} value
|
|
|
|
* @param {string} direction='X' - X|Y
|
|
|
|
* @param {string} unit='%' - %|px etc
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
translate(value, direction = 'X', unit = '%') {
|
|
|
|
return {
|
|
|
|
WebkitTransform: `translate${direction}(${value}${unit})`,
|
|
|
|
transform: `translate${direction}(${value}${unit})`
|
|
|
|
};
|
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export default connect((state) => ({
|
|
|
|
user: state.user,
|
|
|
|
auth: state.auth,
|
|
|
|
path: state.routing.location.pathname
|
|
|
|
}), {
|
|
|
|
goBack: routeActions.goBack,
|
|
|
|
login: actions.login,
|
|
|
|
logout: actions.logout,
|
|
|
|
register: actions.register,
|
|
|
|
activate: actions.activate,
|
|
|
|
clearErrors: actions.clearErrors,
|
2016-02-27 16:23:58 +05:30
|
|
|
oAuthComplete: actions.oAuthComplete,
|
2016-02-13 20:58:47 +05:30
|
|
|
setError: actions.setError
|
|
|
|
})(PanelTransition);
|