2015-07-02 13:31:35 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
2015-07-02 14:42:04 +02:00
|
|
|
import Router from 'react-router';
|
2015-07-02 13:31:35 +02:00
|
|
|
import ReactAddons from 'react/addons';
|
|
|
|
|
2015-07-02 14:42:04 +02:00
|
|
|
let State = Router.State;
|
|
|
|
let Navigation = Router.Navigation;
|
|
|
|
|
2015-07-02 13:31:35 +02:00
|
|
|
let SlidesContainer = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
children: React.PropTypes.arrayOf(React.PropTypes.element)
|
|
|
|
},
|
|
|
|
|
2015-07-02 14:42:04 +02:00
|
|
|
mixins: [State, Navigation],
|
|
|
|
|
2015-07-02 13:31:35 +02:00
|
|
|
getInitialState() {
|
2015-07-02 14:42:04 +02:00
|
|
|
// handle queryParameters
|
|
|
|
let queryParams = this.getQuery();
|
|
|
|
let slideNum = 0;
|
|
|
|
|
|
|
|
if(queryParams && 'slide_num' in queryParams) {
|
|
|
|
slideNum = parseInt(queryParams.slide_num, 10);
|
|
|
|
}
|
2015-07-02 14:53:49 +02:00
|
|
|
// if slide_num is not set, this will be done in componentDidMount
|
2015-07-02 14:42:04 +02:00
|
|
|
|
2015-07-02 13:31:35 +02:00
|
|
|
return {
|
|
|
|
containerWidth: 0,
|
2015-07-02 14:42:04 +02:00
|
|
|
slideNum: slideNum
|
2015-07-02 13:31:35 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-07-02 14:53:49 +02:00
|
|
|
// check if slide_num was defined, and if not then default to 0
|
|
|
|
let queryParams = this.getQuery();
|
|
|
|
if(!('slide_num' in queryParams)) {
|
|
|
|
this.transitionTo(this.getPathname(), null, {slide_num: 0});
|
|
|
|
}
|
|
|
|
|
2015-07-02 13:31:35 +02:00
|
|
|
// init container width
|
|
|
|
this.handleContainerResize();
|
|
|
|
|
|
|
|
// we're using an event listener on window here,
|
|
|
|
// as it was not possible to listen to the resize events of a dom node
|
|
|
|
window.addEventListener('resize', this.handleContainerResize);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleContainerResize);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleContainerResize() {
|
|
|
|
this.setState({
|
|
|
|
containerWidth: this.refs.containerWrapper.getDOMNode().offsetWidth
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-07-02 13:56:24 +02:00
|
|
|
// We let every one from the outsite set the page number of the slider,
|
2015-07-02 14:42:04 +02:00
|
|
|
// though only if the slideNum is actually in the range of our children-list.
|
|
|
|
setSlideNum(slideNum) {
|
2015-07-02 15:40:54 +02:00
|
|
|
if(slideNum < 0 || slideNum < React.Children.count(this.props.children)) {
|
2015-07-02 14:42:04 +02:00
|
|
|
|
|
|
|
this.transitionTo(this.getPathname(), null, {slide_num: slideNum});
|
2015-07-02 13:56:24 +02:00
|
|
|
this.setState({
|
2015-07-02 14:42:04 +02:00
|
|
|
slideNum: slideNum
|
2015-07-02 13:56:24 +02:00
|
|
|
});
|
2015-07-02 14:42:04 +02:00
|
|
|
|
2015-07-02 13:56:24 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('You\'re calling a page number that is out of range.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-02 13:31:35 +02:00
|
|
|
// Since we need to give the slides a width, we need to call ReactAddons.addons.cloneWithProps
|
|
|
|
// Also, a key is nice to have!
|
|
|
|
renderChildren() {
|
|
|
|
return ReactAddons.Children.map(this.props.children, (child, i) => {
|
|
|
|
return ReactAddons.addons.cloneWithProps(child, {
|
2015-07-02 13:56:24 +02:00
|
|
|
className: 'ascribe-slide',
|
2015-07-02 13:31:35 +02:00
|
|
|
style: {
|
|
|
|
width: this.state.containerWidth
|
|
|
|
},
|
|
|
|
key: i
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="container ascribe-sliding-container-wrapper"
|
|
|
|
ref="containerWrapper">
|
|
|
|
<div
|
|
|
|
className="container ascribe-sliding-container"
|
|
|
|
style={{
|
|
|
|
width: this.state.containerWidth * React.Children.count(this.props.children),
|
2015-07-02 14:42:04 +02:00
|
|
|
transform: 'translateX(' + (-1) * this.state.containerWidth * this.state.slideNum + 'px)'
|
2015-07-02 13:31:35 +02:00
|
|
|
}}>
|
|
|
|
<div className="row">
|
|
|
|
{this.renderChildren()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default SlidesContainer;
|