Skip to content

Commit

Permalink
v0.0.0:
Browse files Browse the repository at this point in the history
* Initial commit.
  • Loading branch information
jussi-kalliokoski committed Aug 5, 2014
0 parents commit 66364ee
Show file tree
Hide file tree
Showing 31 changed files with 1,992 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/node_modules
/bower_components
/.tmp
/dist
85 changes: 85 additions & 0 deletions .jscs.json
@@ -0,0 +1,85 @@
{
"excludeFiles": [
"node_modules/**",
"bower_components"
],
"additionalRules": [
"node_modules/jscs-trailing-comma/rules/*.js"
],
"requireTrailingCommaInExpandedLiterals": true,
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"disallowMultipleVarDecl": true,
"requireBlocksOnNewline": 1,
"requireSpacesInsideObjectBrackets": "all",
"disallowDanglingUnderscores": true,
"disallowSpaceAfterObjectKeys": true,
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": [
","
],
"requireSpaceBeforeBinaryOperators": [
"=",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!==",
">",
"<",
">=",
"<="
],
"requireSpaceAfterBinaryOperators": true,
"disallowImplicitTypeConversion": [
"numeric",
"boolean",
"binary",
"string"
],
"requireCamelCaseOrUpperCaseIdentifiers": true,
"disallowKeywords": [
"with"
],
"disallowMultipleLineStrings": true,
"validateLineBreaks": "LF",
"validateQuoteMarks": {
"mark": "\"",
"escape": true
},
"validateIndentation": 4,
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"maximumLineLength": 140,
"requireSpaceAfterLineComment": true
}
16 changes: 16 additions & 0 deletions .jshintrc
@@ -0,0 +1,16 @@
{
"node": true,
"browser": true,
"onecase": true,
"newcap": false,
"maxlen": 140,
"boss": true,
"latedef": true,
"noarg": true,
"eqeqeq": true,
"eqnull": true,
"undef": true,
"strict": true,
"trailing": true,
"expr": true
}
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.10"
env:
global:
- BROWSERSTACK_USER: jussikalliokoski1
- secure: "MP5agaxoREVKUCkFBxxiqZCmyJ35M833SwNWhhqZe6VEA7tRUKrg1xAxRCYFVsqnIjJC4iTvs1mNxpZLAPADe3Eq1v2RRrLMo9FmATSrxSjShU0afPxPLytpRPldyg1hD7QW3j3t2d0NMGboXubSmkmeueAX0YADgGsCbUa8Wqs="
after_success:
- npm run-script coveralls
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,9 @@
# Changelog

## Unreleased

This is a list of things that are done and will be in the next release. Use the same style as for the rest of the items and always keep this at the top of the document (latest release first).

## v0.0.0

* Initial release.
167 changes: 167 additions & 0 deletions Carousel/index.js
@@ -0,0 +1,167 @@
"use strict";

var React = require("react");
var Swipable = require("../Swipable");
var range = require("../utils/range");
var getClassName = require("../utils/getClassName");
var setPosition = require("../utils/setPosition").setPosition;
var noop = require("../utils/noop");

module.exports = React.createClass({
displayName: "Carousel",

mixins: [Swipable],

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,
};
},

isMoving: function () {
return this.props.pageIndex !== this.props.previousPageIndex;
},

calculatePageStyle: function (index) {
return {
width: this.props.pageWidth + "px",
height: this.props.pageHeight + "px",
left: (this.props.pageWidth * index) + "px",
};
},

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 React.DOM.div({
className: this.props.baseClass + "__page",
key: buffer.index,
style: this.calculatePageStyle(buffer.index),
}, pageView);
}.bind(this));
},

calculateSliderStyle: function () {
var style = {
height: (this.props.pageHeight) + "px",
left: Math.floor((this.props.width - this.props.pageWidth) / 2) + "px",
top: Math.floor((this.props.height - this.props.pageHeight) / 2) + "px",
};

var x = this.props.pageIndex * -this.props.pageWidth;
var y = 0;

setPosition(style, x, y);

return style;
},

getSliderClassName: function () {
return getClassName(this.props.baseClass + "__slider", {
moving: this.isMoving(),
});
},

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(),
},
React.DOM.div({
className: this.getSliderClassName(),
style: this.calculateSliderStyle(),
}, this.renderPages())
);
},
});

0 comments on commit 66364ee

Please sign in to comment.