├── .gitignore
├── .jshintrc
├── .npmignore
├── .travis.yml
├── LICENSE
├── README.md
├── example
├── components
│ ├── App
│ │ ├── App.jsx
│ │ ├── App.scss
│ │ └── reset.scss
│ ├── ImageSlideshow
│ │ ├── ImageSlideshow.jsx
│ │ └── ImageSlideshow.scss
│ ├── SimpleSlideshow
│ │ ├── SimpleSlideshow.jsx
│ │ └── SimpleSlideshow.scss
│ └── Slide
│ │ ├── Slide.jsx
│ │ └── Slide.scss
└── server.js
├── gulp
├── clean.js
├── jsx.js
├── nodemon.js
└── sass.js
├── gulpfile.js
├── package.json
├── src
├── PictureShow.jsx
└── PictureShow.scss
└── test
├── PictureShow.test.jsx
└── util
└── createDOM.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | _temp
3 | dist
4 | coverage
5 | lib
6 | logs
7 | *.log
8 |
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "node": true,
3 | "browser": true,
4 | "esnext": true,
5 | "bitwise": true,
6 | "eqeqeq": true,
7 | "immed": true,
8 | "indent": 2,
9 | "newcap": false,
10 | "noarg": true,
11 | "undef": true,
12 | "unused": "vars",
13 | "strict": false,
14 | "expr": true,
15 | "globals": {
16 | "after": true,
17 | "afterEach": true,
18 | "before": true,
19 | "beforeEach": true,
20 | "describe": true,
21 | "expect": true,
22 | "it": true,
23 | "runs": true,
24 | "spyOn": true,
25 | "unescape": true,
26 | "waits": true,
27 | "waitsFor": true
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | test/
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - '0.12'
4 | - '0.11'
5 | - '0.10.38'
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Dow Jones & Company
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **This project is not actively maintained, and as a result it uses deprecated react apis**
2 |
3 | # React Picture Show [](http://travis-ci.org/dowjones/react-picture-show) [](http://badge.fury.io/js/react-picture-show)
4 |
5 | A Bare bones slideshow component that handles transitions between slides and exposes control so that it is easy to decorate with other controls. Out of the box, it supports **swiping to paginate, clicking left and right, and lasyloading slides**
6 |
7 | **[PictureShow Demo](http://dowjones.github.io/react-picture-show/)**
8 |
9 | ## installation
10 |
11 | #### node
12 |
13 | ``` jsx
14 | npm install react-picture-show
15 | ```
16 |
17 | ## Usage
18 |
19 | #### Basic
20 |
21 | ```jsx
22 |
23 |
24 |
25 |
26 | another thing
27 |
28 |
29 |
30 |
31 | ```
32 |
33 | ### Component Properties Overview
34 |
35 | Properties | Type | Default | Description | Supported
36 | :--------- | :--- | :------ | :---------- | :--------
37 | **[ratio](#ratio)** | ```Array``` | Null | Shape of the slideshow, for example: `[16,9]` | yes
38 | **[animationSpeed](#animationspeed)** | ```Number``` | 1500 | Speed of slide transitions in px/s | yes
39 | **[startingSlide](#startingslide)** | ```Number``` | 0 | Initial slide view | yes
40 | **[onClickSlide](#onClickSlide)** | ```Function``` | null | override click handler for slide | yes
41 | **[onBeforeTransition](#onbeforetransition)** | ```Function``` | noop | Function called before transition starts | yes
42 | **[onAfterTransition](#onaftertransition)** | ```Function``` | noop | Function called after transition ends | no
43 | **[slideBuffer](#slidebuffer)** | ```Number``` | 1 | The number of slides loaded to the left and right of the slide in view | yes
44 | **[clickDivide](#clickdivide)** | ```Number``` | 0.45 | The division between previous and next when clicking the slideshow | yes
45 | **[infinite](#infinite)** | ```Boolean``` | true | Is the Slideshow continuous | no
46 | **[suppressPending](#suppresspending)** | ```Boolean``` | true | Should slides outside the slideBuffer be suppressed | no
47 |
48 | ### Public methods on mounted component
49 |
50 | Method | Description | Supported
51 | :----- | :---------- | :--------
52 | next | Go forward one slide | yes
53 | previous | Go backward one slide | yes
54 | goToSlide | Go to a specific slide index | yes
55 | disable | Deactivate slideshow | no
56 | enable | Acticate slideshow | no
57 |
58 | ### How slides work
59 |
60 | Slides are the direct child components of a `` ``. They are cloned with the following additional properties:
61 |
62 | ```jsx
63 | {
64 | slideRatio: Number, // the ratio of the outer slide (w/h)
65 | slidePending: Boolean // whether the slide can be lazyloaded
66 | }
67 | ```
68 | By cloning the children with these props, you are free to create 'slide' components that react to them however you want. If the child already has one of these props it will be replaced (even for ``` ``` components)
69 |
70 | _Note: If ```suppressPending``` property is true on ``` ```, then the slide will not render, so you will not need to handle the ```pending``` prop at the slide level. ```suppressPending``` exists for edge cases where the user wants to define how 'not loading' works_
71 |
72 | ## Properties in Depth
73 |
74 | #### ratio
75 |
76 | Defines the shape of the slideshow as a fixed ratio so that it can flex inside its parent container.
77 |
78 | #### animationSpeed
79 |
80 | speed...
81 |
82 | #### startingSlide
83 |
84 | staring slide...
85 |
86 | #### onClickSlide
87 |
88 | function...
89 |
90 | #### onBeforeTransition
91 |
92 | function...
93 |
94 | #### onAfterTransition
95 |
96 | function...
97 |
98 | #### slideBuffer
99 |
100 | lazy loading...
101 |
102 | #### clickDivide
103 |
104 | next and prev...
105 |
106 | #### infinite
107 |
108 | more stuff...
109 |
110 | #### suppressPending
111 |
112 | control pending....
113 |
114 | ## License
115 |
116 | [MIT](/LICENSE)
117 |
--------------------------------------------------------------------------------
/example/components/App/App.jsx:
--------------------------------------------------------------------------------
1 | /* global window, document */
2 |
3 | var React = require('react'),
4 | SimpleSlideshow = require('../SimpleSlideshow/SimpleSlideshow.jsx'),
5 | ImageSlideshow = require('../ImageSlideshow/ImageSlideshow.jsx'),
6 | App;
7 |
8 | module.exports = App = React.createClass({
9 | render: function () {
10 | return (
11 |
12 |
13 | {this.props.title}
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
34 |
35 |
36 | );
37 | }
38 | });
39 |
40 | if ('object' === typeof window) {
41 | window.renderApp = function (props) {
42 | var appFactory = React.createFactory(App);
43 | React.render(appFactory(props), document);
44 | };
45 | }
46 |
--------------------------------------------------------------------------------
/example/components/App/App.scss:
--------------------------------------------------------------------------------
1 |
2 | @import './reset';
3 | @import './PictureShow';
4 | @import './SimpleSlideshow/SimpleSlideshow';
5 | @import './ImageSlideshow/ImageSlideshow';
6 | @import './Slide/Slide';
7 |
8 | .main-well {
9 | width: 600px;
10 | margin: 40px auto;
11 | }
12 |
13 | .example {
14 | margin-bottom: 30px;
15 | }
16 |
--------------------------------------------------------------------------------
/example/components/App/reset.scss:
--------------------------------------------------------------------------------
1 | /*! normalize.css v3.0.2 | MIT License | git.io/normalize */
2 |
3 | /**
4 | * 1. Set default font family to sans-serif.
5 | * 2. Prevent iOS text size adjust after orientation change, without disabling
6 | * user zoom.
7 | */
8 |
9 | html {
10 | font-family: sans-serif; /* 1 */
11 | -ms-text-size-adjust: 100%; /* 2 */
12 | -webkit-text-size-adjust: 100%; /* 2 */
13 | }
14 |
15 | /**
16 | * Remove default margin.
17 | */
18 |
19 | body {
20 | margin: 0;
21 | }
22 |
23 | /* HTML5 display definitions
24 | ========================================================================== */
25 |
26 | /**
27 | * Correct `block` display not defined for any HTML5 element in IE 8/9.
28 | * Correct `block` display not defined for `details` or `summary` in IE 10/11
29 | * and Firefox.
30 | * Correct `block` display not defined for `main` in IE 11.
31 | */
32 |
33 | article,
34 | aside,
35 | details,
36 | figcaption,
37 | figure,
38 | footer,
39 | header,
40 | hgroup,
41 | main,
42 | menu,
43 | nav,
44 | section,
45 | summary {
46 | display: block;
47 | }
48 |
49 | /**
50 | * 1. Correct `inline-block` display not defined in IE 8/9.
51 | * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera.
52 | */
53 |
54 | audio,
55 | canvas,
56 | progress,
57 | video {
58 | display: inline-block; /* 1 */
59 | vertical-align: baseline; /* 2 */
60 | }
61 |
62 | /**
63 | * Prevent modern browsers from displaying `audio` without controls.
64 | * Remove excess height in iOS 5 devices.
65 | */
66 |
67 | audio:not([controls]) {
68 | display: none;
69 | height: 0;
70 | }
71 |
72 | /**
73 | * Address `[hidden]` styling not present in IE 8/9/10.
74 | * Hide the `template` element in IE 8/9/11, Safari, and Firefox < 22.
75 | */
76 |
77 | [hidden],
78 | template {
79 | display: none;
80 | }
81 |
82 | /* Links
83 | ========================================================================== */
84 |
85 | /**
86 | * Remove the gray background color from active links in IE 10.
87 | */
88 |
89 | a {
90 | background-color: transparent;
91 | }
92 |
93 | /**
94 | * Improve readability when focused and also mouse hovered in all browsers.
95 | */
96 |
97 | a:active,
98 | a:hover {
99 | outline: 0;
100 | }
101 |
102 | /* Text-level semantics
103 | ========================================================================== */
104 |
105 | /**
106 | * Address styling not present in IE 8/9/10/11, Safari, and Chrome.
107 | */
108 |
109 | abbr[title] {
110 | border-bottom: 1px dotted;
111 | }
112 |
113 | /**
114 | * Address style set to `bolder` in Firefox 4+, Safari, and Chrome.
115 | */
116 |
117 | b,
118 | strong {
119 | font-weight: bold;
120 | }
121 |
122 | /**
123 | * Address styling not present in Safari and Chrome.
124 | */
125 |
126 | dfn {
127 | font-style: italic;
128 | }
129 |
130 | /**
131 | * Address variable `h1` font-size and margin within `section` and `article`
132 | * contexts in Firefox 4+, Safari, and Chrome.
133 | */
134 |
135 | h1 {
136 | font-size: 2em;
137 | margin: 0.67em 0;
138 | }
139 |
140 | /**
141 | * Address styling not present in IE 8/9.
142 | */
143 |
144 | mark {
145 | background: #ff0;
146 | color: #000;
147 | }
148 |
149 | /**
150 | * Address inconsistent and variable font size in all browsers.
151 | */
152 |
153 | small {
154 | font-size: 80%;
155 | }
156 |
157 | /**
158 | * Prevent `sub` and `sup` affecting `line-height` in all browsers.
159 | */
160 |
161 | sub,
162 | sup {
163 | font-size: 75%;
164 | line-height: 0;
165 | position: relative;
166 | vertical-align: baseline;
167 | }
168 |
169 | sup {
170 | top: -0.5em;
171 | }
172 |
173 | sub {
174 | bottom: -0.25em;
175 | }
176 |
177 | /* Embedded content
178 | ========================================================================== */
179 |
180 | /**
181 | * Remove border when inside `a` element in IE 8/9/10.
182 | */
183 |
184 | img {
185 | border: 0;
186 | }
187 |
188 | /**
189 | * Correct overflow not hidden in IE 9/10/11.
190 | */
191 |
192 | svg:not(:root) {
193 | overflow: hidden;
194 | }
195 |
196 | /* Grouping content
197 | ========================================================================== */
198 |
199 | /**
200 | * Address margin not present in IE 8/9 and Safari.
201 | */
202 |
203 | figure {
204 | margin: 1em 40px;
205 | }
206 |
207 | /**
208 | * Address differences between Firefox and other browsers.
209 | */
210 |
211 | hr {
212 | -moz-box-sizing: content-box;
213 | box-sizing: content-box;
214 | height: 0;
215 | }
216 |
217 | /**
218 | * Contain overflow in all browsers.
219 | */
220 |
221 | pre {
222 | overflow: auto;
223 | }
224 |
225 | /**
226 | * Address odd `em`-unit font size rendering in all browsers.
227 | */
228 |
229 | code,
230 | kbd,
231 | pre,
232 | samp {
233 | font-family: monospace, monospace;
234 | font-size: 1em;
235 | }
236 |
237 | /* Forms
238 | ========================================================================== */
239 |
240 | /**
241 | * Known limitation: by default, Chrome and Safari on OS X allow very limited
242 | * styling of `select`, unless a `border` property is set.
243 | */
244 |
245 | /**
246 | * 1. Correct color not being inherited.
247 | * Known issue: affects color of disabled elements.
248 | * 2. Correct font properties not being inherited.
249 | * 3. Address margins set differently in Firefox 4+, Safari, and Chrome.
250 | */
251 |
252 | button,
253 | input,
254 | optgroup,
255 | select,
256 | textarea {
257 | color: inherit; /* 1 */
258 | font: inherit; /* 2 */
259 | margin: 0; /* 3 */
260 | }
261 |
262 | /**
263 | * Address `overflow` set to `hidden` in IE 8/9/10/11.
264 | */
265 |
266 | button {
267 | overflow: visible;
268 | }
269 |
270 | /**
271 | * Address inconsistent `text-transform` inheritance for `button` and `select`.
272 | * All other form control elements do not inherit `text-transform` values.
273 | * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera.
274 | * Correct `select` style inheritance in Firefox.
275 | */
276 |
277 | button,
278 | select {
279 | text-transform: none;
280 | }
281 |
282 | /**
283 | * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio`
284 | * and `video` controls.
285 | * 2. Correct inability to style clickable `input` types in iOS.
286 | * 3. Improve usability and consistency of cursor style between image-type
287 | * `input` and others.
288 | */
289 |
290 | button,
291 | html input[type="button"], /* 1 */
292 | input[type="reset"],
293 | input[type="submit"] {
294 | -webkit-appearance: button; /* 2 */
295 | cursor: pointer; /* 3 */
296 | }
297 |
298 | /**
299 | * Re-set default cursor for disabled elements.
300 | */
301 |
302 | button[disabled],
303 | html input[disabled] {
304 | cursor: default;
305 | }
306 |
307 | /**
308 | * Remove inner padding and border in Firefox 4+.
309 | */
310 |
311 | button::-moz-focus-inner,
312 | input::-moz-focus-inner {
313 | border: 0;
314 | padding: 0;
315 | }
316 |
317 | /**
318 | * Address Firefox 4+ setting `line-height` on `input` using `!important` in
319 | * the UA stylesheet.
320 | */
321 |
322 | input {
323 | line-height: normal;
324 | }
325 |
326 | /**
327 | * It's recommended that you don't attempt to style these elements.
328 | * Firefox's implementation doesn't respect box-sizing, padding, or width.
329 | *
330 | * 1. Address box sizing set to `content-box` in IE 8/9/10.
331 | * 2. Remove excess padding in IE 8/9/10.
332 | */
333 |
334 | input[type="checkbox"],
335 | input[type="radio"] {
336 | box-sizing: border-box; /* 1 */
337 | padding: 0; /* 2 */
338 | }
339 |
340 | /**
341 | * Fix the cursor style for Chrome's increment/decrement buttons. For certain
342 | * `font-size` values of the `input`, it causes the cursor style of the
343 | * decrement button to change from `default` to `text`.
344 | */
345 |
346 | input[type="number"]::-webkit-inner-spin-button,
347 | input[type="number"]::-webkit-outer-spin-button {
348 | height: auto;
349 | }
350 |
351 | /**
352 | * 1. Address `appearance` set to `searchfield` in Safari and Chrome.
353 | * 2. Address `box-sizing` set to `border-box` in Safari and Chrome
354 | * (include `-moz` to future-proof).
355 | */
356 |
357 | input[type="search"] {
358 | -webkit-appearance: textfield; /* 1 */
359 | -moz-box-sizing: content-box;
360 | -webkit-box-sizing: content-box; /* 2 */
361 | box-sizing: content-box;
362 | }
363 |
364 | /**
365 | * Remove inner padding and search cancel button in Safari and Chrome on OS X.
366 | * Safari (but not Chrome) clips the cancel button when the search input has
367 | * padding (and `textfield` appearance).
368 | */
369 |
370 | input[type="search"]::-webkit-search-cancel-button,
371 | input[type="search"]::-webkit-search-decoration {
372 | -webkit-appearance: none;
373 | }
374 |
375 | /**
376 | * Define consistent border, margin, and padding.
377 | */
378 |
379 | fieldset {
380 | border: 1px solid #c0c0c0;
381 | margin: 0 2px;
382 | padding: 0.35em 0.625em 0.75em;
383 | }
384 |
385 | /**
386 | * 1. Correct `color` not being inherited in IE 8/9/10/11.
387 | * 2. Remove padding so people aren't caught out if they zero out fieldsets.
388 | */
389 |
390 | legend {
391 | border: 0; /* 1 */
392 | padding: 0; /* 2 */
393 | }
394 |
395 | /**
396 | * Remove default vertical scrollbar in IE 8/9/10/11.
397 | */
398 |
399 | textarea {
400 | overflow: auto;
401 | }
402 |
403 | /**
404 | * Don't inherit the `font-weight` (applied by a rule above).
405 | * NOTE: the default cannot safely be changed in Chrome and Safari on OS X.
406 | */
407 |
408 | optgroup {
409 | font-weight: bold;
410 | }
411 |
412 | /* Tables
413 | ========================================================================== */
414 |
415 | /**
416 | * Remove most spacing between table cells.
417 | */
418 |
419 | table {
420 | border-collapse: collapse;
421 | border-spacing: 0;
422 | }
423 |
424 | td,
425 | th {
426 | padding: 0;
427 | }
--------------------------------------------------------------------------------
/example/components/ImageSlideshow/ImageSlideshow.jsx:
--------------------------------------------------------------------------------
1 | var React = require('react'),
2 | PictureShow = require('../../../src/PictureShow.jsx'),
3 | Slide = require('../Slide/Slide.jsx'),
4 | ImageSlideshow;
5 |
6 | module.exports = ImageSlideshow = React.createClass({
7 | render: function () {
8 | return (
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | );
19 | }
20 | });
21 |
--------------------------------------------------------------------------------
/example/components/ImageSlideshow/ImageSlideshow.scss:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dowjones/react-picture-show/034131bb2e5ec249e2f0ba631917088639db66dd/example/components/ImageSlideshow/ImageSlideshow.scss
--------------------------------------------------------------------------------
/example/components/SimpleSlideshow/SimpleSlideshow.jsx:
--------------------------------------------------------------------------------
1 |
2 | var React = require('react'),
3 | PictureShow = require('../../../src/PictureShow.jsx'),
4 | SimpleSlideshow;
5 |
6 | module.exports = SimpleSlideshow = React.createClass({
7 | render: function () {
8 | return (
9 |
10 |
11 | Slide One
12 | Slide Two
13 | Slide Three
14 | Slide Four
15 |
16 |
17 | );
18 | }
19 | });
20 |
--------------------------------------------------------------------------------
/example/components/SimpleSlideshow/SimpleSlideshow.scss:
--------------------------------------------------------------------------------
1 |
2 | .simple-slide {
3 | color: #fff;
4 | width: 100%;
5 | height: 100%;
6 | padding: 20px;
7 | font-size: 30px;
8 | -webkit-box-sizing: border-box;
9 | -moz-box-sizing: border-box;
10 | box-sizing: border-box;
11 | }
--------------------------------------------------------------------------------
/example/components/Slide/Slide.jsx:
--------------------------------------------------------------------------------
1 |
2 | var React = require('react/addons'),
3 | Slide;
4 |
5 | function extractShape (w, h, slideRatio) {
6 | return w/h < slideRatio[0]/slideRatio[1] ? 'tall' : 'wide';
7 | }
8 |
9 | function hasShape (w, h) {
10 | return typeof w === 'number' && typeof h === 'number';
11 | }
12 |
13 | module.exports = Slide = React.createClass({
14 |
15 | propTypes: {
16 | src: React.PropTypes.string,
17 | width: React.PropTypes.number,
18 | height: React.PropTypes.number,
19 | slideRatio: React.PropTypes.number,
20 | slidePending: React.PropTypes.bool
21 | },
22 |
23 | getDefaultProps: function () {
24 | return {
25 | slideRatio: 1
26 | };
27 | },
28 |
29 | render: function () {
30 |
31 | var width = this.props.width,
32 | height = this.props.height,
33 | check = hasShape(width, height),
34 | shapeClass = check ? extractShape(width, height, this.props.slideRatio) : 'stretch';
35 |
36 | var imgStyle;
37 |
38 | if (shapeClass === 'tall') {
39 | imgStyle = {
40 | maxHeight: height
41 | };
42 | } else if (shapeClass === 'wide') {
43 | imgStyle = {
44 | maxWidth: width
45 | };
46 | }
47 |
48 | return (
49 |
50 |
51 |
52 | );
53 | }
54 | });
55 |
--------------------------------------------------------------------------------
/example/components/Slide/Slide.scss:
--------------------------------------------------------------------------------
1 | // Slide
2 |
3 | /*
4 | * Centering method from (ghost element)
5 | * http://css-tricks.com/centering-in-the-unknown/
6 | */
7 |
8 | .ps-slide {
9 | width: 100%;
10 | height: 100%;
11 | text-align: center;
12 | white-space: nowrap;
13 | }
14 |
15 | .ps-slide:before {
16 | content: '';
17 | display: inline-block;
18 | height: 100%;
19 | vertical-align: middle;
20 | }
21 |
22 | .ps-slide > img {
23 | display: inline-block;
24 | vertical-align: middle;
25 | }
26 |
27 | .stretch.ps-img {
28 | width: 100%;
29 | height: 100%;
30 | }
31 |
32 | .tall.ps-img {
33 | height: 100%;
34 | width: auto;
35 | }
36 |
37 | .wide.ps-img {
38 | width: 100%;
39 | height: auto;
40 | }
--------------------------------------------------------------------------------
/example/server.js:
--------------------------------------------------------------------------------
1 | require("babel/register");
2 |
3 | var React = require('react'),
4 | express = require('express'),
5 | compression = require('compression'),
6 | disableHttpCache = require('connect-cache-control'),
7 | morgan = require('morgan'),
8 | serveStatic = require('serve-static'),
9 | errors = require('common-errors'),
10 | PORT = process.env.PORT || 8000,
11 | IS_PROD = 'production' === process.env.NODE_ENV,
12 | app = module.exports = express();
13 |
14 | // utility middleware
15 | app.use(disableHttpCache);
16 | app.use(morgan(IS_PROD ? 'combined' : 'dev'));
17 | app.use(compression());
18 |
19 | app.use('/favicon.ico', function (req, res, next) {
20 | res.end('');
21 | });
22 |
23 | // app resources
24 | app.use('/assets', serveStatic(__dirname + '/_temp'));
25 |
26 | var App = React.createFactory(require('./components/App/App.jsx'));
27 | app.use('/', function (req, res, next) {
28 | var props = {};
29 | res.send('' + React.renderToString(App(props)));
30 | });
31 |
32 | app.use(errors.middleware.errorHandler);
33 |
34 | if (!module.parent) {
35 | app.listen(PORT, function () {
36 | console.log('on :%s', PORT);
37 | });
38 | }
39 |
--------------------------------------------------------------------------------
/gulp/clean.js:
--------------------------------------------------------------------------------
1 | var rimraf = require('rimraf');
2 |
3 | exports.clean = clean;
4 |
5 | function clean(cb) {
6 | return rimraf('./example/_temp', cb);
7 | }
8 |
--------------------------------------------------------------------------------
/gulp/jsx.js:
--------------------------------------------------------------------------------
1 | var gulp = require('gulp'),
2 | gutil = require('gulp-util'),
3 | buffer = require('gulp-buffer'),
4 | source = require('vinyl-source-stream'),
5 | watchify = require('watchify'),
6 | browserify = require('browserify'),
7 | babelify = require('babelify'),
8 | uglify = require('gulp-uglify');
9 |
10 | exports.toJs = function () {
11 | return createStream(createBundler(false))
12 | .pipe(buffer())
13 | .pipe(uglify())
14 | .pipe(gulp.dest('./example/_temp'));
15 | };
16 |
17 | exports.toJsWatch = function () {
18 | var bundler = createBundler(true);
19 |
20 | function createBundlerStream() {
21 | return createStream(bundler)
22 | .pipe(gulp.dest('./example/_temp'));
23 | }
24 |
25 | bundler = watchify(bundler);
26 | bundler.on('update', createBundlerStream);
27 | bundler.on('time', function (time) {
28 | console.log('App.js built in: %dms', time);
29 | });
30 |
31 | return createBundlerStream();
32 | };
33 |
34 | function createBundler(isDebug) {
35 | return browserify('./example/components/App/App.jsx', {
36 | cache: {},
37 | packageCache: {},
38 | fullPaths: true,
39 | transform: ['babelify'],
40 | debug: isDebug
41 | });
42 | }
43 |
44 | function createStream(bundler) {
45 | return bundler.bundle()
46 | .on('error', gutil.log.bind(gutil, 'Browserify Error'))
47 | .pipe(source('App.js'));
48 | }
49 |
--------------------------------------------------------------------------------
/gulp/nodemon.js:
--------------------------------------------------------------------------------
1 | var gulp = require('gulp'),
2 | nodemon = require('gulp-nodemon');
3 |
4 | exports.start = start;
5 |
6 | function start() {
7 | var config = {
8 | script: 'example/server.js',
9 | watch: ['example/**/*.js', 'src/**/*.js']
10 | };
11 | return nodemon(config);
12 | }
13 |
--------------------------------------------------------------------------------
/gulp/sass.js:
--------------------------------------------------------------------------------
1 | var gulp = require('gulp'),
2 | gutil = require('gulp-util'),
3 | sass = require('gulp-sass'),
4 | livereload = require('gulp-livereload');
5 |
6 | exports.toCss = sassToCss;
7 | exports.toCssWatch = sassToCssWatch;
8 |
9 | function sassToCss() {
10 | return gulp.src('./example/components/App/App.scss')
11 | .pipe(sass({
12 | 'includePaths': [
13 | './src/',
14 | './example/components'
15 | ]
16 | }))
17 | .on('error', gutil.log.bind(gutil, 'SASS Error'))
18 | .pipe(gulp.dest('./example/_temp'))
19 | .pipe(livereload());
20 | }
21 |
22 | function sassToCssWatch() {
23 | livereload.listen();
24 | sassToCss();
25 | return gulp.watch('./example/components/**/*.scss', sassToCss);
26 | }
27 |
--------------------------------------------------------------------------------
/gulpfile.js:
--------------------------------------------------------------------------------
1 | var gulp = require('gulp'),
2 | nodemon = require('./gulp/nodemon'),
3 | jsx = require('./gulp/jsx'),
4 | sass = require('./gulp/sass'),
5 | clean = require('./gulp/clean');
6 |
7 | /**
8 | * NOTE: The purpose of this file is to
9 | * specify all tasks. Do not put task-specific
10 | * logic here. Use the /gulp directory for that.
11 | */
12 |
13 | gulp.task('default', ['jsx', 'sass']);
14 | gulp.task('dev', ['jsx-watch', 'sass-watch', 'nodemon']);
15 |
16 | gulp.task('jsx', jsx.toJs);
17 | gulp.task('jsx-watch', jsx.toJsWatch);
18 |
19 | gulp.task('sass', sass.toCss);
20 | gulp.task('sass-watch', sass.toCssWatch);
21 |
22 | gulp.task('nodemon', nodemon.start);
23 |
24 | gulp.task('clean', clean.clean);
25 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-picture-show",
3 | "version": "1.4.0",
4 | "description": "Slideshow Component for React.js",
5 | "main": "lib/PictureShow.js",
6 | "scripts": {
7 | "test": "mocha --compilers js:babel/register test/PictureShow.test.jsx",
8 | "test-watch": "mocha -G -w -R min -r should --compilers js:babel/register test/PictureShow.test.jsx",
9 | "build:css": "node-sass src/PictureShow.scss lib/PictureShow.css",
10 | "build:js": "babel ./src --out-dir ./lib",
11 | "build": "npm run build:js && npm run build:css",
12 | "prepublish": "npm run build",
13 | "babel-node": "babel-node --stage 0 --ignore='foo|bar|baz'"
14 | },
15 | "repository": {
16 | "type": "git",
17 | "url": "https://github.com/areusjs/react-picture-show.git"
18 | },
19 | "keywords": [
20 | "react-component",
21 | "slideshow",
22 | "carousel"
23 | ],
24 | "author": "gregoryskiano@gmail.com",
25 | "license": "MIT",
26 | "bugs": {
27 | "url": "https://github.com/areusjs/react-picture-show/issues"
28 | },
29 | "homepage": "https://github.com/areusjs/react-picture-show",
30 | "devDependencies": {
31 | "b": "^2.0.1",
32 | "babel": "^5.8.21",
33 | "babel-eslint": "^4.0.5",
34 | "babel-jest": "^5.3.0",
35 | "babelify": "^6.1.3",
36 | "balanced-match": "^0.2.0",
37 | "browserify": "^11.0.1",
38 | "common-errors": "^0.4.18",
39 | "compression": "^1.4.1",
40 | "connect-cache-control": "^1.0.0",
41 | "express": "^4.11.2",
42 | "gulp": "^3.8.11",
43 | "gulp-buffer": "0.0.2",
44 | "gulp-livereload": "^3.7.0",
45 | "gulp-nodemon": "^1.0.5",
46 | "gulp-rev": "^3.0.1",
47 | "gulp-sass": "^1.3.3",
48 | "gulp-uglify": "^1.1.0",
49 | "gulp-util": "^3.0.3",
50 | "jsdom": "^3.1.2",
51 | "mocha": "^2.2.5",
52 | "morgan": "^1.5.1",
53 | "node-jsx": "^0.13.3",
54 | "node-sass": "^3.2.0",
55 | "proxyquire": "^1.3.2",
56 | "reactify": "^1.1.1",
57 | "rimraf": "^2.2.8",
58 | "serve-static": "^1.8.1",
59 | "should": "^5.0.1",
60 | "sinon": "^1.15.4",
61 | "vinyl-source-stream": "^1.0.0",
62 | "watchify": "^2.3.0"
63 | },
64 | "dependencies": {
65 | "lodash": "^3.3.1",
66 | "react": ">=0.11.2 <1.0.0",
67 | "react-swipeable": "^2.0.0"
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/PictureShow.jsx:
--------------------------------------------------------------------------------
1 | /* globals window, navigator */
2 |
3 | import throttle from 'lodash/function/throttle';
4 | const React = require('react');
5 | const Swipeable = require('react-swipeable');
6 | const noop = function () {};
7 |
8 | // speed expressed in px/second
9 | // returns milliseconds
10 | function getTransitionTime (distance, speed) {
11 | return distance / speed * 1000;
12 | }
13 |
14 | // adapted from: http://blogs.msdn.com/b/giorgio/archive/2009/04/14/how-to-detect-ie8-using-javascript-client-side.aspx
15 | function getInternetExplorerVersion(minimum) {
16 | var rv = -1; // Return value assumes failure.
17 | if (navigator.appName === 'Microsoft Internet Explorer') {
18 | var ua = navigator.userAgent,
19 | re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
20 | if (re.exec(ua) !== null) {
21 | rv = parseFloat(RegExp.$1);
22 | }
23 | }
24 | return rv;
25 | }
26 |
27 | function support3d () {
28 | var v = getInternetExplorerVersion();
29 | return v > -1 ? v > 9 : true;
30 | }
31 |
32 | const PictureShow = React.createClass({
33 |
34 | propTypes: {
35 | ratio: React.PropTypes.array,
36 | animationSpeed: React.PropTypes.number,
37 | startingSlide: React.PropTypes.number,
38 | onClickSlide: React.PropTypes.func,
39 | onBeforeTransition: React.PropTypes.func,
40 | onAfterTransition: React.PropTypes.func,
41 | slideBuffer: React.PropTypes.number,
42 | clickDivide: React.PropTypes.number,
43 | infinite: React.PropTypes.bool,
44 | suppressPending: React.PropTypes.bool
45 | },
46 |
47 | getDefaultProps: function (argument) {
48 | return {
49 | ratio: null,
50 | animationSpeed: 1500,
51 | startingSlide: 0,
52 | onClickSlide: null,
53 | onBeforeTransition: noop,
54 | onAfterTransition: noop,
55 | slideBuffer: 1,
56 | clickDivide: 0.45,
57 | infinite: true,
58 | suppressPending: true
59 | };
60 | },
61 |
62 | getInitialState: function () {
63 | // store an object on this instance
64 | this.preloaded = [];
65 |
66 | return {
67 | slideIdx: this.props.startingSlide,
68 | panels: ['A','B','C'],
69 | ratio: 3/2,
70 | use3dFallback: false
71 | };
72 | },
73 |
74 | // TODO: make sure the poly fill is in our set of polyfills
75 | // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
76 | componentDidMount: function () {
77 |
78 | // decide if we use 3d transforms
79 | // not in IE9 or iE8
80 | if (!support3d()) {
81 | this.setState({
82 | use3dFallback: true
83 | });
84 | }
85 |
86 | if (!this.props.ratio) {
87 | this._handleResize();
88 |
89 | if (window.addEventListener) {
90 | window.addEventListener('resize', this._handleResize, false);
91 | }
92 | }
93 | },
94 |
95 | componentWillUnmount: function () {
96 | if (!this.props.ratio && window.removeEventListener) {
97 | window.removeEventListener('resize', this._handleResize, false);
98 | }
99 | },
100 |
101 | goToSlide: function (slideIdx, direction, event) {
102 |
103 | var before = this.state.slideIdx;
104 |
105 | if (event && event.stopPropagation) {
106 | event.stopPropagation();
107 | }
108 |
109 | direction = direction || (slideIdx > before ? 'right' : 'left');
110 |
111 | var elm = this.getDOMNode(),
112 | width = elm.offsetWidth,
113 | animationTime = getTransitionTime(width, this.props.animationSpeed);
114 |
115 | var panels = this.state.panels,
116 | trickPanel;
117 |
118 | if (slideIdx === this.state.slideIdx) {
119 | return;
120 | } else if (direction === 'right' && slideIdx < before) {
121 | trickPanel = panels.shift();
122 | panels.push(trickPanel);
123 | } else if (direction === 'left' && slideIdx > before) {
124 | trickPanel = panels.pop();
125 | panels.unshift(trickPanel);
126 | } else {
127 | trickPanel = null;
128 | }
129 |
130 | this.props.onBeforeTransition(before, slideIdx);
131 |
132 | this.setState({
133 | slideIdx: slideIdx,
134 | direction: direction,
135 | panels: panels,
136 | trickPanel: trickPanel,
137 | animationTime: animationTime
138 | });
139 |
140 | },
141 |
142 | next: function (event) {
143 | this.goToSlide(this.state.slideIdx < React.Children.count(this.props.children) - 1 ? this.state.slideIdx + 1 : 0, 'right', event);
144 | },
145 |
146 | previous: function (event) {
147 | this.goToSlide(this.state.slideIdx > 0 ? this.state.slideIdx - 1 : React.Children.count(this.props.children) - 1, 'left', event);
148 | },
149 |
150 | _handleResize: throttle(function () {
151 | var box = this.refs.wrap.getDOMNode().getBoundingClientRect();
152 | this.setState({
153 | ratio: [box.width, box.height]
154 | });
155 | }, 30),
156 |
157 | _handleSlideClick: function (event) {
158 | if (this.state.swiping) {
159 | return;
160 | }
161 |
162 | var elm = this.getDOMNode(),
163 | box = elm.getBoundingClientRect(),
164 | left = box.left,
165 | right = left + box.width,
166 | divide = left + ((right - left) * this.props.clickDivide),
167 | direction = event.clientX < divide ? 'previous' : 'next';
168 |
169 | if (this.props.onClickSlide) {
170 | this.props.onClickSlide(direction, event);
171 | } else if (direction === 'previous') {
172 | this.previous();
173 | } else {
174 | this.next();
175 | }
176 | },
177 |
178 | _handleSwipe: function (ev, x, y, isFlick) {
179 | this.setState({
180 | swiping: false
181 | });
182 |
183 | if (x > 0) {
184 | this.next();
185 | } else {
186 | this.previous();
187 | }
188 | },
189 |
190 | _handleSwiping: function () {
191 | // TODO: track finger
192 | this.setState({
193 | swiping: true
194 | });
195 | },
196 |
197 | _getLeftDistance: function (startIdx, endIdx) {
198 | var d = startIdx - endIdx;
199 | return d < 0 ? React.Children.count(this.props.children) + d : d;
200 | },
201 |
202 | _getRightDistance: function (startIdx, endIdx) {
203 | return this._getLeftDistance(endIdx, startIdx);
204 | },
205 |
206 | _shouldLoad: function (idx) {
207 | if (this.preloaded.indexOf(idx) > -1) {
208 | return true;
209 | } else if (this._getLeftDistance(this.state.slideIdx, idx) <= this.props.slideBuffer ||
210 | this._getRightDistance(this.state.slideIdx, idx) <= this.props.slideBuffer) {
211 | this.preloaded.push(idx);
212 | return true;
213 | } else {
214 | return false;
215 | }
216 | },
217 |
218 | _getPanelStyle: function (idx, key) {
219 |
220 | var slots = React.Children.count(this.props.children),
221 | panelWidth = slots * 100,
222 | panelPosition = this.state.slideIdx * -100;
223 |
224 | var display = key === this.state.trickPanel ? 'none' : null,
225 | shift = (idx - 1) * panelWidth,
226 | left = (panelPosition + shift) + '%', // for IE
227 | transform = 'translate3d(' + ((panelPosition + shift)/slots) + '%,0,0)';
228 |
229 | if (this.state.use3dFallback) {
230 | return {
231 | WebkitTransitionDuration: this.state.animationTime + 'ms',
232 | transitionDuration: this.state.animationTime + 'ms',
233 | width: panelWidth + '%',
234 | left: left,
235 | display: display
236 | };
237 | } else {
238 | return {
239 | WebkitTransitionDuration: this.state.animationTime + 'ms',
240 | transitionDuration: this.state.animationTime + 'ms',
241 | width: panelWidth + '%',
242 | WebkitTransform: transform,
243 | MozTransform: transform,
244 | MsTransform: transform,
245 | transform: transform,
246 | display: display
247 | };
248 | }
249 | },
250 |
251 | render: function () {
252 |
253 | var ratio = this.props.ratio || this.state.ratio;
254 | var slots = React.Children.count(this.props.children);
255 |
256 | var mainClass = [
257 | 'picture-show',
258 | (!this.props.ratio ? 'stretch' : undefined),
259 | this.props.className
260 | ].join(' ');
261 |
262 | var wrapStyle = this.props.ratio ? {
263 | paddingBottom: (ratio[1] / ratio[0] * 100 ).toFixed(4) + "%"
264 | } : null;
265 |
266 | var slideStyle = {
267 | width: (100 / slots) + '%'
268 | };
269 |
270 | var slides = [];
271 |
272 | React.Children.forEach(this.props.children, function (slide, idx) {
273 |
274 | var isPending = !this._shouldLoad(idx),
275 | slideContent;
276 |
277 | if (this.props.suppressPending && isPending) {
278 | slideContent = null;
279 | } else {
280 | slideContent = React.cloneElement(slide, {
281 | slideRatio: ratio,
282 | slidePending: isPending
283 | });
284 | }
285 |
286 | slides.push(
287 |
288 | {slideContent}
289 |
290 | );
291 |
292 | }.bind(this));
293 |
294 | return (
295 |
300 |
301 | {['A', 'B', 'C'].map(function (key) {
302 | var panelStyle = this._getPanelStyle(this.state.panels.indexOf(key), key);
303 | return (
304 |
305 | {slides}
306 |
307 | );
308 | }.bind(this))}
309 |
310 |
311 | );
312 | }
313 |
314 | });
315 |
316 | export default PictureShow;
317 |
--------------------------------------------------------------------------------
/src/PictureShow.scss:
--------------------------------------------------------------------------------
1 |
2 | // Container
3 |
4 | .picture-show {
5 | overflow: hidden;
6 | -webkit-box-sizing: border-box;
7 | -moz-box-sizing: border-box;
8 | box-sizing: border-box;
9 |
10 | &.stretch {
11 | position: relative;
12 | width: 100%;
13 | height: auto;
14 |
15 | & > .ps-wrap {
16 | background: none;
17 | overflow: hidden;
18 | position: absolute;
19 | top: 0px;
20 | bottom: 0px;
21 | left: 0px;
22 | right: 0px;
23 | height: auto;
24 | width: auto;
25 | }
26 | }
27 | }
28 |
29 | .ps-wrap {
30 | position: relative;
31 | width: 100%;
32 | height: 0;
33 | }
34 |
35 | .ps-slides {
36 | position: absolute;
37 | display: block;
38 | height: 100%;
39 | left: 0;
40 | top: 0;
41 | -webkit-transition: all 400ms cubic-bezier(0.365, 0.45, 0.23, 0.95);
42 | -moz-transition: all 400ms cubic-bezier(0.365, 0.45, 0.23, 0.95);
43 | transition: all 400ms cubic-bezier(0.365, 0.45, 0.23, 0.95);
44 | -webkit-transition-timing-function: cubic-bezier(0.365, 0.45, 0.23, 0.95);
45 | -moz-transition-timing-function: cubic-bezier(0.365, 0.45, 0.23, 0.95);
46 | transition-timing-function: cubic-bezier(0.365, 0.45, 0.23, 0.95);
47 | }
48 |
49 | .ps-slides:hover {
50 | cursor: pointer;
51 | }
52 |
53 | .ps-slide-wrap {
54 | overflow: hidden;
55 | height: 100%;
56 | float: left;
57 | background-color: #FAFAFA;
58 | -webkit-touch-callout: none;
59 | -webkit-user-select: none;
60 | -moz-user-select: none;
61 | -ms-user-select: none;
62 | user-select: none;
63 | }
64 |
--------------------------------------------------------------------------------
/test/PictureShow.test.jsx:
--------------------------------------------------------------------------------
1 | import sinon from 'sinon';
2 | import assert from 'should';
3 | import jsdom from 'jsdom';
4 |
5 | for (var i in require.cache) delete require.cache[i];
6 |
7 | global.window = {}; global.window.document = {createElement: function(){}};
8 | global.document = jsdom.jsdom('');
9 | global.window = document.parentWindow;
10 | global.navigator = {
11 | userAgent: 'node.js'
12 | };
13 |
14 | const React = require('react/addons');
15 | const TestUtils = React.addons.TestUtils;
16 | const PictureShow = require( '../src/PictureShow.jsx');
17 |
18 |
19 | let slideshowElm;
20 |
21 | function setUp() {
22 | slideshowElm = (
23 |
24 |
25 |
26 |
27 |
28 |
29 | );
30 | }
31 |
32 | describe('PictureShow Structure', function () {
33 |
34 | beforeEach(setUp);
35 |
36 | it('should apply correct classes', function () {
37 |
38 | var slideshow = TestUtils.renderIntoDocument(slideshowElm),
39 | wrap,
40 | panels;
41 |
42 | slideshow.getDOMNode().className.should.match(/\bpicture-show\b/);
43 | slideshow.getDOMNode().className.should.match(/\badded-class\b/);
44 |
45 | assert.doesNotThrow(function () {
46 | wrap = TestUtils.findRenderedDOMComponentWithClass(slideshow, 'ps-wrap');
47 | });
48 |
49 | panels = TestUtils.scryRenderedDOMComponentsWithClass(wrap, 'ps-slides');
50 |
51 | panels.length.should.equal(3);
52 |
53 | });
54 |
55 | it('should apply inline styles correctly', function () {
56 |
57 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
58 | var wrap = TestUtils.findRenderedDOMComponentWithClass(slideshow, 'ps-wrap');
59 | var panel = TestUtils.scryRenderedDOMComponentsWithClass(wrap, 'ps-slides')[0];
60 | var slide = TestUtils.scryRenderedDOMComponentsWithClass(panel, 'ps-slide-wrap')[0];
61 | var slideCount = React.Children.count(slideshow.props.children);
62 |
63 |
64 | wrap.props.style.paddingBottom.should.equal('66.6667%');
65 | panel.props.style.width.should.equal((slideCount * 100) + '%');
66 | slide.props.style.width.should.equal((100/slideCount) + '%');
67 |
68 | });
69 |
70 | it('should use ratio of element when no ratio is supplied', function () {
71 |
72 | var elm = (
73 |
74 |
75 |
76 |
77 |
78 | );
79 |
80 | var slideshow = TestUtils.renderIntoDocument(elm);
81 |
82 | slideshow.refs.wrap.props.should.have.property('style', null);
83 |
84 | });
85 |
86 | it('should add a listener to window resize if no ratio is supplied', function () {
87 |
88 | window.addEventListener = sinon.spy();
89 |
90 | var elm = (
91 |
92 |
93 |
94 |
95 |
96 | );
97 |
98 | var slideshow = TestUtils.renderIntoDocument(elm);
99 |
100 | window.addEventListener.calledWith('resize',slideshow._handleResize).should.be.true;
101 |
102 | });
103 |
104 | // TODO: add test for slide structure
105 |
106 | });
107 |
108 | describe('PictureShow Navigation', function () {
109 |
110 | beforeEach(setUp);
111 |
112 | it('should position panels correctly on next', function () {
113 |
114 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
115 | var wrap = TestUtils.findRenderedDOMComponentWithClass(slideshow, 'ps-wrap');
116 | var panels = TestUtils.scryRenderedDOMComponentsWithClass(wrap, 'ps-slides');
117 | var slideCount = React.Children.count(slideshow.props.children);
118 |
119 | function getShift (idx, panelPosition) {
120 | panelPosition = (panelPosition - 1) * slideCount;
121 | return 'translate3d(' + ((100 / slideCount) * (-idx + panelPosition)) + '%,0,0)';
122 | }
123 |
124 | // slide 0
125 |
126 | panels[0].props.style.transform.should.equal(getShift(0, 0));
127 | panels[1].props.style.transform.should.equal(getShift(0, 1));
128 | panels[2].props.style.transform.should.equal(getShift(0, 2));
129 |
130 | slideshow.next(); // slide 1
131 |
132 | panels[0].props.style.transform.should.equal(getShift(1, 0));
133 | panels[1].props.style.transform.should.equal(getShift(1, 1));
134 | panels[2].props.style.transform.should.equal(getShift(1, 2));
135 |
136 | slideshow.next(); // slide 2
137 |
138 | panels[0].props.style.transform.should.equal(getShift(2, 0));
139 | panels[1].props.style.transform.should.equal(getShift(2, 1));
140 | panels[2].props.style.transform.should.equal(getShift(2, 2));
141 |
142 | slideshow.next(); // slide 3
143 |
144 | panels[0].props.style.transform.should.equal(getShift(3, 0));
145 | panels[1].props.style.transform.should.equal(getShift(3, 1));
146 | panels[2].props.style.transform.should.equal(getShift(3, 2));
147 |
148 | slideshow.next(); // slide 0 <---- around the seam
149 |
150 | panels[0].props.style.transform.should.equal(getShift(0, 2));
151 | panels[1].props.style.transform.should.equal(getShift(0, 0));
152 | panels[2].props.style.transform.should.equal(getShift(0, 1));
153 |
154 | });
155 |
156 | it('should position panels correctly on previous', function () {
157 |
158 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
159 | var wrap = TestUtils.findRenderedDOMComponentWithClass(slideshow, 'ps-wrap');
160 | var panels = TestUtils.scryRenderedDOMComponentsWithClass(wrap, 'ps-slides');
161 | var slideCount = React.Children.count(slideshow.props.children);
162 |
163 | function getShift (idx, panelPosition) {
164 | panelPosition = (panelPosition - 1) * slideCount;
165 | return 'translate3d(' + ((100 / slideCount) * (-idx + panelPosition)) + '%,0,0)';
166 | }
167 |
168 | // slide 0
169 |
170 | panels[0].props.style.transform.should.equal(getShift(0, 0));
171 | panels[1].props.style.transform.should.equal(getShift(0, 1));
172 | panels[2].props.style.transform.should.equal(getShift(0, 2));
173 |
174 | slideshow.previous(); // slide 3 <---- around the seam
175 |
176 | panels[0].props.style.transform.should.equal(getShift(3, 1));
177 | panels[1].props.style.transform.should.equal(getShift(3, 2));
178 | panels[2].props.style.transform.should.equal(getShift(3, 0));
179 |
180 | slideshow.previous(); // slide 2
181 |
182 | panels[0].props.style.transform.should.equal(getShift(2, 1));
183 | panels[1].props.style.transform.should.equal(getShift(2, 2));
184 | panels[2].props.style.transform.should.equal(getShift(2, 0));
185 |
186 | slideshow.previous(); // slide 1
187 |
188 | panels[0].props.style.transform.should.equal(getShift(1, 1));
189 | panels[1].props.style.transform.should.equal(getShift(1, 2));
190 | panels[2].props.style.transform.should.equal(getShift(1, 0));
191 |
192 | slideshow.previous(); // slide 0
193 |
194 | panels[0].props.style.transform.should.equal(getShift(0, 1));
195 | panels[1].props.style.transform.should.equal(getShift(0, 2));
196 | panels[2].props.style.transform.should.equal(getShift(0, 0));
197 |
198 | slideshow.next(); // just double checking a turn
199 |
200 | panels[0].props.style.transform.should.equal(getShift(1, 1));
201 | panels[1].props.style.transform.should.equal(getShift(1, 2));
202 | panels[2].props.style.transform.should.equal(getShift(1, 0));
203 |
204 | });
205 |
206 | it('should jump to slides correctly', function () {
207 |
208 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
209 | var wrap = TestUtils.findRenderedDOMComponentWithClass(slideshow, 'ps-wrap');
210 | var panels = TestUtils.scryRenderedDOMComponentsWithClass(wrap, 'ps-slides');
211 | var slideCount = React.Children.count(slideshow.props.children);
212 |
213 | function getShift (idx, panelPosition) {
214 | panelPosition = (panelPosition - 1) * slideCount;
215 | return 'translate3d(' + ((100 / slideCount) * (-idx + panelPosition)) + '%,0,0)';
216 | }
217 |
218 | slideshow.goToSlide(3);
219 |
220 | panels[0].props.style.transform.should.equal(getShift(3, 0));
221 | panels[1].props.style.transform.should.equal(getShift(3, 1));
222 | panels[2].props.style.transform.should.equal(getShift(3, 2));
223 |
224 | slideshow.goToSlide(0);
225 |
226 | panels[0].props.style.transform.should.equal(getShift(0, 0));
227 | panels[1].props.style.transform.should.equal(getShift(0, 1));
228 | panels[2].props.style.transform.should.equal(getShift(0, 2));
229 |
230 | });
231 |
232 | });
233 |
234 | describe('PictureShow Preloading', function () {
235 |
236 | it('should not load panels outside slide buffer', function () {
237 |
238 | var elm = (
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 | );
248 |
249 | var slideshow = TestUtils.renderIntoDocument(elm);
250 | var panel = TestUtils.scryRenderedDOMComponentsWithClass(slideshow, 'ps-slides')[0];
251 | var slides = TestUtils.scryRenderedDOMComponentsWithClass(panel, 'ps-slide-wrap');
252 |
253 | // should load slides
254 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[0], 'A');});
255 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[1], 'B');});
256 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[5], 'F');});
257 |
258 | // should not be loaded
259 | assert.throws(function(){TestUtils.findRenderedDOMComponentWithClass(slides[2], 'C');});
260 | assert.throws(function(){TestUtils.findRenderedDOMComponentWithClass(slides[3], 'D');});
261 | assert.throws(function(){TestUtils.findRenderedDOMComponentWithClass(slides[4], 'E');});
262 |
263 | slideshow.next(); // now 2 should load
264 |
265 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[2], 'C');});
266 | assert.throws(function(){TestUtils.findRenderedDOMComponentWithClass(slides[3], 'D');});
267 | assert.throws(function(){TestUtils.findRenderedDOMComponentWithClass(slides[4], 'E');});
268 |
269 | slideshow.next(); // now 3 should load
270 |
271 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[3], 'D');});
272 |
273 | slideshow.previous();
274 | slideshow.previous();
275 | slideshow.previous();
276 |
277 | assert.doesNotThrow(function(){TestUtils.findRenderedDOMComponentWithClass(slides[4], 'E');});
278 |
279 | });
280 |
281 | });
282 |
283 | describe('PictureShow Interaction', function () {
284 |
285 | beforeEach(setUp);
286 |
287 | it('should paginate on mouse down', function () {
288 |
289 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
290 | var panel = TestUtils.scryRenderedDOMComponentsWithClass(slideshow, 'ps-slides')[1];
291 | var node = slideshow.getDOMNode();
292 | var originalFn = node.getBoundingClientRect;
293 |
294 | node.getBoundingClientRect = function () {
295 | return {
296 | left: 100,
297 | width: 200
298 | };
299 | };
300 |
301 | TestUtils.Simulate.mouseDown(panel, {
302 | clientX: 180 // previous
303 | });
304 |
305 | slideshow.state.slideIdx.should.equal(3);
306 |
307 | TestUtils.Simulate.mouseDown(panel, {
308 | clientX: 210 // next
309 | });
310 |
311 | TestUtils.Simulate.mouseDown(panel, {
312 | clientX: 220 // next
313 | });
314 |
315 | slideshow.state.slideIdx.should.equal(1);
316 |
317 | node.getBoundingClientRect = originalFn; // replace original function
318 |
319 | });
320 |
321 | it('should paginate on swipe', function () {
322 |
323 | var slideshow = TestUtils.renderIntoDocument(slideshowElm);
324 | var node = slideshow.getDOMNode();
325 |
326 | // finger moving right
327 |
328 | TestUtils.Simulate.touchStart(node, {
329 | touches: [{clientX: 180, clientY: 100}]
330 | });
331 | TestUtils.Simulate.touchMove(node, {
332 | touches: [{clientX: 200, clientY: 100}],
333 | changedTouches: [{clientX: 200, clientY: 100}]
334 | });
335 | TestUtils.Simulate.touchEnd(node, {
336 | touches: [{clientX: 240, clientY: 100}],
337 | changedTouches: [{clientX: 240, clientY: 100}]
338 | });
339 |
340 | slideshow.state.slideIdx.should.equal(3);
341 |
342 | // finger moving left
343 |
344 | TestUtils.Simulate.touchStart(node, {
345 | touches: [{clientX: 180, clientY: 100}]
346 | });
347 | TestUtils.Simulate.touchMove(node, {
348 | touches: [{clientX: 170, clientY: 100}],
349 | changedTouches: [{clientX: 170, clientY: 100}]
350 | });
351 | TestUtils.Simulate.touchEnd(node, {
352 | touches: [{clientX: 140, clientY: 100}],
353 | changedTouches: [{clientX: 140, clientY: 100}]
354 | });
355 |
356 | slideshow.state.slideIdx.should.equal(0);
357 |
358 | });
359 |
360 | });
361 |
362 | describe('PictureShow Events', function () {
363 |
364 | beforeEach(setUp);
365 |
366 | it('should run `onTransition`', function () {
367 |
368 | var cb = sinon.spy();
369 |
370 | var elm = React.addons.cloneWithProps(slideshowElm, {
371 | onBeforeTransition: cb
372 | });
373 |
374 | var slideshow = TestUtils.renderIntoDocument(elm);
375 |
376 | slideshow.next();
377 |
378 | cb.lastCall.args.should.eql([0,1]);
379 |
380 | slideshow.next();
381 |
382 | cb.lastCall.args.should.eql([1,2]);
383 |
384 | slideshow.previous();
385 |
386 | cb.lastCall.args.should.eql([2,1]);
387 |
388 | });
389 |
390 | it('should run `onClickSlide`', function () {
391 | var cb = sinon.spy();
392 |
393 | var elm = React.addons.cloneWithProps(slideshowElm, {
394 | onClickSlide: cb
395 | });
396 |
397 | var slideshow = TestUtils.renderIntoDocument(elm);
398 | var panel = TestUtils.scryRenderedDOMComponentsWithClass(slideshow, 'ps-slides')[1];
399 | var node = slideshow.getDOMNode();
400 | var originalFn = node.getBoundingClientRect;
401 |
402 | node.getBoundingClientRect = function () {
403 | return {
404 | left: 100,
405 | width: 200
406 | };
407 | };
408 |
409 | TestUtils.Simulate.mouseDown(panel, {
410 | clientX: 210 // next
411 | });
412 |
413 | cb.lastCall.args[0].should.equal('next');
414 | slideshow.state.slideIdx.should.equal(0); // should not fall through to next
415 |
416 | node.getBoundingClientRect = originalFn; // replace original function
417 | });
418 |
419 | });
420 |
421 | describe('PictureShow Children', function () {
422 |
423 | beforeEach(setUp);
424 |
425 | it('clone children with correct props', function () {
426 |
427 | var elm = (
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 | );
437 |
438 | var slideshow = TestUtils.renderIntoDocument(elm);
439 | var slideChildren = TestUtils.scryRenderedDOMComponentsWithTag(slideshow, 'img');
440 |
441 | slideChildren[0].props.should.eql({
442 | slideRatio: [1,2],
443 | slidePending: false,
444 | className: 'A'
445 | });
446 |
447 | slideChildren[2].props.should.eql({
448 | slideRatio: [1,2],
449 | slidePending: true,
450 | className: 'C'
451 | });
452 |
453 | });
454 |
455 | });
456 |
--------------------------------------------------------------------------------
/test/util/createDOM.js:
--------------------------------------------------------------------------------
1 |
2 | var jsdom = require('jsdom');
3 |
4 | module.exports = function createDOM () {
5 | // console.log(document);
6 | // document.innerHTML = '';
7 | // document = jsdom.jsdom('');
8 | // window = document.parentWindow;
9 | // global.navigator = window.navigator;
10 |
11 | };
--------------------------------------------------------------------------------