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';
|
2016-01-31 18:29:38 +05:30
|
|
|
import { TransitionMotion, spring } from 'react-motion';
|
|
|
|
|
|
|
|
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-03-02 02:06:14 +05:30
|
|
|
import authFlow from 'services/authFlow';
|
2016-03-13 14:06:31 +05:30
|
|
|
import { userShape } from 'components/user/User';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
import * as actions from './actions';
|
2016-03-27 13:22:00 +05:30
|
|
|
import MeasureHeight from './MeasureHeight';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const opacitySpringConfig = {stiffness: 300, damping: 20};
|
2016-03-28 09:53:45 +05:30
|
|
|
const transformSpringConfig = {stiffness: 500, damping: 50, precision: 0.5};
|
|
|
|
const changeContextSpringConfig = {stiffness: 500, damping: 20, precision: 0.5};
|
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 = {
|
2016-03-13 14:06:31 +05:30
|
|
|
// context props
|
2016-02-13 20:58:47 +05:30
|
|
|
auth: PropTypes.shape({
|
|
|
|
error: PropTypes.string,
|
|
|
|
login: PropTypes.shape({
|
|
|
|
login: PropTypes.string,
|
|
|
|
password: PropTypes.string
|
|
|
|
})
|
|
|
|
}).isRequired,
|
2016-03-13 14:06:31 +05:30
|
|
|
user: userShape.isRequired,
|
2016-02-13 20:58:47 +05:30
|
|
|
setError: React.PropTypes.func.isRequired,
|
|
|
|
clearErrors: React.PropTypes.func.isRequired,
|
2016-03-13 14:06:31 +05:30
|
|
|
resolve: React.PropTypes.func.isRequired,
|
|
|
|
reject: React.PropTypes.func.isRequired,
|
|
|
|
|
|
|
|
// local props
|
2016-03-15 11:10:18 +05:30
|
|
|
Title: PropTypes.element,
|
|
|
|
Body: PropTypes.element,
|
|
|
|
Footer: PropTypes.element,
|
|
|
|
Links: PropTypes.element,
|
|
|
|
children: PropTypes.element
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
2016-03-13 14:06:31 +05:30
|
|
|
static childContextTypes = {
|
|
|
|
auth: PropTypes.shape({
|
|
|
|
error: PropTypes.string,
|
|
|
|
login: PropTypes.shape({
|
|
|
|
login: PropTypes.string,
|
|
|
|
password: PropTypes.string
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
user: userShape,
|
|
|
|
clearErrors: React.PropTypes.func,
|
|
|
|
resolve: PropTypes.func,
|
|
|
|
reject: PropTypes.func
|
|
|
|
};
|
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
state = {
|
|
|
|
contextHeight: 0
|
|
|
|
};
|
|
|
|
|
2016-03-13 14:06:31 +05:30
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
auth: this.props.auth,
|
|
|
|
user: this.props.user,
|
|
|
|
clearErrors: this.props.clearErrors,
|
|
|
|
resolve: this.props.resolve,
|
|
|
|
reject: this.props.reject
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-03 11:06:00 +05:30
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-03-28 09:16:51 +05:30
|
|
|
const nextPanel = nextProps.Body && nextProps.Body.type.panelId;
|
|
|
|
const previousPanel = this.props.Body && this.props.Body.type.panelId;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
if (nextPanel !== previousPanel) {
|
|
|
|
const direction = this.getDirection(nextPanel, previousPanel);
|
|
|
|
const forceHeight = direction === 'Y' && nextPanel !== previousPanel ? 1 : 0;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
this.props.clearErrors();
|
|
|
|
this.setState({
|
|
|
|
direction,
|
2016-03-28 09:16:51 +05:30
|
|
|
forceHeight
|
2016-02-13 20:58:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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-03-27 13:22:00 +05:30
|
|
|
const {canAnimateHeight, contextHeight, forceHeight} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
const {Title, Body, Footer, Links} = this.props;
|
2016-03-27 13:22:00 +05:30
|
|
|
|
2016-03-15 11:10:18 +05:30
|
|
|
if (this.props.children) {
|
|
|
|
return this.props.children;
|
|
|
|
} else if (!Title || !Body || !Footer || !Links) {
|
|
|
|
throw new Error('Title, Body, Footer and Links are required');
|
|
|
|
}
|
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
const panelId = Body.type.panelId;
|
|
|
|
const hasGoBack = Body.type.hasGoBack;
|
|
|
|
|
|
|
|
const formHeight = this.state[`formHeight${panelId}`] || 0;
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
return (
|
|
|
|
<TransitionMotion
|
2016-03-03 10:53:17 +05:30
|
|
|
styles={[
|
2016-03-28 09:16:51 +05:30
|
|
|
{key: panelId, data: {Title, Body, Footer, Links, hasBackButton: hasGoBack}, style: {
|
2016-02-03 11:06:00 +05:30
|
|
|
transformSpring: spring(0, transformSpringConfig),
|
|
|
|
opacitySpring: spring(1, opacitySpringConfig)
|
2016-03-03 10:53:17 +05:30
|
|
|
}},
|
|
|
|
{key: 'common', style: {
|
2016-03-27 13:22:00 +05:30
|
|
|
heightSpring: spring(forceHeight || formHeight, transformSpringConfig),
|
2016-02-06 15:32:23 +05:30
|
|
|
switchContextHeightSpring: spring(forceHeight || contextHeight, changeContextSpringConfig)
|
2016-03-03 10:53:17 +05:30
|
|
|
}}
|
|
|
|
]}
|
2016-01-31 18:29:38 +05:30
|
|
|
willEnter={this.willEnter}
|
|
|
|
willLeave={this.willLeave}
|
|
|
|
>
|
|
|
|
{(items) => {
|
2016-03-03 10:53:17 +05:30
|
|
|
const panels = items.filter(({key}) => key !== 'common');
|
|
|
|
const common = items.filter(({key}) => key === 'common')[0];
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
const contentHeight = {
|
|
|
|
overflow: 'hidden',
|
2016-03-12 19:53:55 +05:30
|
|
|
height: forceHeight ? common.style.switchContextHeightSpring : 'auto'
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const bodyHeight = {
|
|
|
|
position: 'relative',
|
2016-03-27 13:22:00 +05:30
|
|
|
height: `${canAnimateHeight ? common.style.heightSpring : formHeight}px`
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
return (
|
2016-03-28 09:16:51 +05:30
|
|
|
<Form id={panelId} onSubmit={this.onFormSubmit} onInvalid={this.onFormInvalid}>
|
2016-01-31 18:29:38 +05:30
|
|
|
<Panel>
|
|
|
|
<PanelHeader>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getHeader(config))}
|
2016-01-31 18:29:38 +05:30
|
|
|
</PanelHeader>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={contentHeight}>
|
2016-03-27 13:22:00 +05:30
|
|
|
<MeasureHeight
|
|
|
|
state={this.props.auth.error}
|
|
|
|
onMeasure={this.onUpdateContextHeight}
|
|
|
|
>
|
2016-02-06 15:32:23 +05:30
|
|
|
<PanelBody>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={bodyHeight}>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getBody(config))}
|
2016-02-06 14:33:51 +05:30
|
|
|
</div>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelFooter>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getFooter(config))}
|
2016-02-06 14:33:51 +05:30
|
|
|
</PanelFooter>
|
2016-03-27 13:22:00 +05:30
|
|
|
</MeasureHeight>
|
2016-02-06 14:33:51 +05:30
|
|
|
</div>
|
2016-01-31 18:29:38 +05:30
|
|
|
</Panel>
|
2016-02-06 15:32:23 +05:30
|
|
|
<div className={helpLinksStyles}>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getLinks(config))}
|
2016-01-31 18:29:38 +05:30
|
|
|
</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-03-03 10:53:17 +05:30
|
|
|
willEnter = (config) => this.getTransitionStyles(config);
|
|
|
|
willLeave = (config) => this.getTransitionStyles(config, {isLeave: true});
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
/**
|
2016-03-03 10:53:17 +05:30
|
|
|
* @param {Object} config
|
2016-03-12 19:53:55 +05:30
|
|
|
* @param {string} config.key
|
2016-02-06 15:32:23 +05:30
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Object} [options.isLeave=false] - true, if this is a leave transition
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2016-03-03 10:53:17 +05:30
|
|
|
getTransitionStyles({key}, options = {}) {
|
|
|
|
const {isLeave = false} = options;
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const map = {
|
2016-03-28 09:16:51 +05:30
|
|
|
login: -1,
|
|
|
|
register: -1,
|
|
|
|
password: 1,
|
|
|
|
activation: 1,
|
|
|
|
permissions: -1,
|
|
|
|
changePassword: 1,
|
|
|
|
forgotPassword: 1
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
2016-03-03 10:53:17 +05:30
|
|
|
const sign = map[key];
|
|
|
|
|
|
|
|
const transform = sign * 100;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return {
|
2016-03-03 10:53:17 +05:30
|
|
|
transformSpring: isLeave ? spring(transform, transformSpringConfig) : transform,
|
|
|
|
opacitySpring: isLeave ? spring(0, opacitySpringConfig) : 1
|
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-03-01 00:06:23 +05:30
|
|
|
getDirection(next, prev) {
|
2016-03-28 09:16:51 +05:30
|
|
|
const not = (panelId) => prev !== panelId && next !== panelId;
|
|
|
|
|
|
|
|
const map = {
|
|
|
|
login: not('password') && not('forgotPassword') ? 'Y' : 'X',
|
|
|
|
password: not('login') && not('forgotPassword') ? 'Y' : 'X',
|
|
|
|
register: not('activation') ? 'Y' : 'X',
|
|
|
|
activation: not('register') ? 'Y' : 'X',
|
|
|
|
permissions: 'Y',
|
|
|
|
changePassword: 'Y',
|
|
|
|
forgotPassword: not('password') && not('login') ? 'Y' : 'X'
|
2016-03-01 00:06:23 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
return map[next];
|
|
|
|
}
|
|
|
|
|
2016-03-27 13:22:00 +05:30
|
|
|
onUpdateHeight = (height, key) => {
|
|
|
|
const heightKey = `formHeight${key}`;
|
|
|
|
|
|
|
|
// we need to skip first render, because there is no panel to make transition from
|
|
|
|
// const canAnimateHeight = Object.keys(this.state.height).length > 1 || this.state[heightKey];
|
2016-03-28 09:16:51 +05:30
|
|
|
const canAnimateHeight = true; // NOTE: lets try to always animate
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
this.setState({
|
2016-02-13 20:58:47 +05:30
|
|
|
canAnimateHeight,
|
2016-03-27 13:22:00 +05:30
|
|
|
[heightKey]: height
|
2016-01-31 18:29:38 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-03-02 02:06:14 +05:30
|
|
|
authFlow.goBack();
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
getHeader({key, style, data}) {
|
|
|
|
const {Title, hasBackButton} = data;
|
|
|
|
const {transformSpring} = style;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
style = {
|
|
|
|
...this.getDefaultTransitionStyles(key, style),
|
2016-02-06 15:32:23 +05:30
|
|
|
opacity: 1 // reset default
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const scrollStyle = this.translate(transformSpring, 'Y');
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const sideScrollStyle = {
|
2016-01-31 18:29:38 +05:30
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const 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 (
|
2016-03-28 09:16:51 +05:30
|
|
|
<div key={`header/${key}`} style={style}>
|
2016-01-31 18:29:38 +05:30
|
|
|
{hasBackButton ? backButton : null}
|
|
|
|
<div style={scrollStyle}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Title}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
getBody({key, style, data}) {
|
|
|
|
const {Body} = data;
|
|
|
|
const {transformSpring} = style;
|
|
|
|
const {direction} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
let transform = this.translate(transformSpring, direction);
|
|
|
|
let 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
|
|
|
}
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
style = {
|
|
|
|
...this.getDefaultTransitionStyles(key, style),
|
2016-02-06 15:32:23 +05:30
|
|
|
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-03-27 13:22:00 +05:30
|
|
|
<MeasureHeight
|
2016-03-28 09:16:51 +05:30
|
|
|
key={`body/${key}`}
|
2016-03-27 13:22:00 +05:30
|
|
|
style={style}
|
|
|
|
state={this.props.auth.error}
|
|
|
|
onMeasure={(height) => this.onUpdateHeight(height, key)}
|
|
|
|
>
|
2016-02-13 20:58:47 +05:30
|
|
|
{React.cloneElement(Body, {
|
|
|
|
ref: (body) => {
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
})}
|
2016-03-27 13:22:00 +05:30
|
|
|
</MeasureHeight>
|
2016-01-31 18:29:38 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
getFooter({key, style, data}) {
|
|
|
|
const {Footer} = data;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
style = this.getDefaultTransitionStyles(key, style);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
2016-03-28 09:16:51 +05:30
|
|
|
<div key={`footer/${key}`} style={style}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Footer}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
getLinks({key, style, data}) {
|
|
|
|
const {Links} = data;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
style = this.getDefaultTransitionStyles(key, style);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
2016-03-28 09:16:51 +05:30
|
|
|
<div key={`links/${key}`} style={style}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Links}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-02-06 15:32:23 +05:30
|
|
|
|
|
|
|
/**
|
2016-03-03 10:53:17 +05:30
|
|
|
* @param {string} key
|
2016-03-12 19:53:55 +05:30
|
|
|
* @param {Object} style
|
|
|
|
* @param {number} style.opacitySpring
|
2016-02-06 15:32:23 +05:30
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2016-03-03 10:53:17 +05:30
|
|
|
getDefaultTransitionStyles(key, {opacitySpring}) {
|
2016-02-06 15:32:23 +05:30
|
|
|
return {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
opacity: opacitySpring,
|
2016-03-28 09:16:51 +05:30
|
|
|
pointerEvents: key === this.props.Body.type.panelId ? 'auto' : 'none'
|
2016-02-06 15:32:23 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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,
|
2016-03-02 02:06:14 +05:30
|
|
|
resolve: authFlow.resolve.bind(authFlow),
|
|
|
|
reject: authFlow.reject.bind(authFlow)
|
2016-02-13 20:58:47 +05:30
|
|
|
}), {
|
|
|
|
clearErrors: actions.clearErrors,
|
|
|
|
setError: actions.setError
|
|
|
|
})(PanelTransition);
|