1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-24 02:16:10 +01:00
onion/js/components/ascribe_slides_container/slides_container.js

107 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
2015-07-02 14:42:04 +02:00
import Router from 'react-router';
import ReactAddons from 'react/addons';
2015-07-02 14:42:04 +02:00
let State = Router.State;
let Navigation = Router.Navigation;
let SlidesContainer = React.createClass({
propTypes: {
children: React.PropTypes.arrayOf(React.PropTypes.element)
},
2015-07-02 14:42:04 +02:00
mixins: [State, Navigation],
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);
}
// if slide_num is not set, this will be done in componentDidMount
2015-07-02 14:42:04 +02:00
return {
containerWidth: 0,
2015-07-02 14:42:04 +02:00
slideNum: slideNum
};
},
componentDidMount() {
// 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});
}
// 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.');
}
},
// 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',
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)'
}}>
<div className="row">
{this.renderChildren()}
</div>
</div>
</div>
);
}
});
export default SlidesContainer;