-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
572 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
var range = require("../utils/range"); | ||
var noop = require("../utils/noop"); | ||
|
||
var CarouselMixin = { | ||
propTypes: { | ||
baseClass: React.PropTypes.string, | ||
cacheSize: React.PropTypes.number, | ||
embedWidth: React.PropTypes.number, | ||
embedHeight: React.PropTypes.number, | ||
pageIndex: React.PropTypes.number, | ||
previousPageIndex: React.PropTypes.number, | ||
loop: React.PropTypes.bool, | ||
renderEmptyPages: React.PropTypes.bool, | ||
pages: React.PropTypes.arrayOf(React.PropTypes.any).isRequired, | ||
pageView: React.PropTypes.func.isRequired, | ||
onSwiped: React.PropTypes.func, | ||
swipeThreshold: React.PropTypes.number, | ||
swipeCancelThreshold: React.PropTypes.number, | ||
}, | ||
|
||
getDefaultProps: function () { | ||
return { | ||
baseClass: "merry-go-round", | ||
cacheSize: 1, | ||
embedWidth: 0, | ||
embedHeight: 0, | ||
pageIndex: 0, | ||
previousPageIndex: 0, | ||
loop: false, | ||
renderEmptyPages: false, | ||
onSwiped: noop, | ||
swipeThreshold: 10, | ||
swipeCancelThreshold: 10, | ||
}; | ||
}, | ||
|
||
isIndexWithinBounds: function (index) { | ||
return index >= 0 && index < this.props.pages.length; | ||
}, | ||
|
||
normalizeIndex: function (index) { | ||
if ( !this.props.loop ) { return index; } | ||
|
||
while ( index < 0 ) { | ||
index += this.props.pages.length; | ||
} | ||
|
||
return index % this.props.pages.length; | ||
}, | ||
|
||
isIndexInView: function (index, viewIndex) { | ||
return Math.abs(index - viewIndex) <= this.props.cacheSize; | ||
}, | ||
|
||
calculateBuffers: function () { | ||
var first = Math.min(this.props.previousPageIndex, this.props.pageIndex) - this.props.cacheSize; | ||
var last = Math.max(this.props.previousPageIndex, this.props.pageIndex) + this.props.cacheSize; | ||
var indices = range(first, last + 1); | ||
|
||
return indices.map(function calculateBuffer (index) { | ||
return { | ||
index: index, | ||
pageIndex: this.normalizeIndex(index), | ||
willBeDiscarded: !this.isIndexInView(this.props.pageIndex, index), | ||
isNew: !this.isIndexInView(this.props.previousPageIndex, index), | ||
}; | ||
}.bind(this)); | ||
}, | ||
|
||
renderPages: function () { | ||
var PageView = this.props.pageView; | ||
var buffers = this.calculateBuffers(); | ||
|
||
return buffers.map(function renderBuffer (buffer) { | ||
var pageView; | ||
|
||
if ( this.props.renderEmptyPages || this.isIndexWithinBounds(buffer.pageIndex) ) { | ||
pageView = PageView({ | ||
page: this.props.pages[buffer.pageIndex], | ||
index: buffer.pageIndex, | ||
willBeDiscarded: buffer.willBeDiscarded, | ||
isNew: buffer.isNew, | ||
}); | ||
} | ||
|
||
return this.renderPage(buffer, pageView); | ||
}.bind(this)); | ||
}, | ||
}; | ||
|
||
module.exports = CarouselMixin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
var Swipable = require("../Swipable"); | ||
var CarouselMixin = require("../CarouselMixin"); | ||
var getClassName = require("../utils/getClassName"); | ||
|
||
module.exports = React.createClass({ | ||
displayName: "Carousel", | ||
|
||
mixins: [Swipable, CarouselMixin], | ||
|
||
calculatePageStyle: function (index) { | ||
return { | ||
width: this.props.pageWidth + "px", | ||
height: this.props.pageHeight + "px", | ||
opacity: index === this.props.pageIndex ? "1.0" : "0.0", | ||
}; | ||
}, | ||
|
||
renderPage: function (buffer, pageView) { | ||
return React.DOM.div({ | ||
className: this.props.baseClass + "__page", | ||
key: buffer.index, | ||
style: this.calculatePageStyle(buffer.index), | ||
}, pageView); | ||
}, | ||
|
||
calculateStyle: function () { | ||
return { | ||
width: (this.props.width) + "px", | ||
height: (this.props.height) + "px", | ||
left: (-this.props.embedWidth) + "px", | ||
top: (-this.props.embedHeight) + "px", | ||
}; | ||
}, | ||
|
||
render: function () { | ||
if ( this.props.pages.length === 0 ) { | ||
// (jussi-kalliokoski): Nothing to render. Avoids infinite loop in calculateBuffers(). | ||
return React.DOM.div(); | ||
} | ||
|
||
return React.DOM.div({ | ||
className: this.props.baseClass, | ||
onTouchStart: this.handleTouchStart, | ||
onTouchMove: this.handleTouchMove, | ||
onTouchEnd: this.handleTouchEnd, | ||
onTouchCancel: this.handleTouchCancel, | ||
style: this.calculateStyle(), | ||
}, | ||
this.renderPages() | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"use strict"; | ||
|
||
module.exports.Carousel = require("./Carousel"); | ||
module.exports.Fader = require("./Fader"); | ||
module.exports.Container = require("./Container"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.