Skip to content

Commit 3e55334

Browse files
author
Amir Tocker
committed
Implement Video tag.
1 parent f1d679f commit 3e55334

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

src/components/Video/Video.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import React, {Component, PropTypes} from 'react';
2+
import cloudinary from 'cloudinary-core';
3+
import CloudinaryComponent from '../CloudinaryComponent';
4+
const Util = cloudinary.Util;
5+
const Cloudinary = cloudinary.Cloudinary;
6+
const DEFAULT_POSTER_OPTIONS = {
7+
format: 'jpg',
8+
resource_type: 'video'
9+
};
210

3-
export default class Video extends React.Component {
11+
export default class Video extends CloudinaryComponent {
412
constructor(props) {
513
super(props);
614
this.state = {};
@@ -29,11 +37,42 @@ export default class Video extends React.Component {
2937
}
3038

3139
render() {
40+
let {publicId, poster, sourceTypes, fallback, sourceTransformation, ...options} = Object.assign({}, this.context, this.props);
41+
sourceTransformation = sourceTransformation || {};
42+
sourceTypes = sourceTypes || Cloudinary.DEFAULT_VIDEO_PARAMS.source_types;
43+
options = this.getOptions(options, {});
44+
let cld = Cloudinary.new(options);
45+
let sources = [];
46+
let tagAttributes = cloudinary.Transformation.new(options).toHtmlAttributes();
47+
if (Util.isPlainObject(poster)) {
48+
let defaults = poster.publicId != null ? Cloudinary.DEFAULT_IMAGE_PARAMS : DEFAULT_POSTER_OPTIONS;
49+
poster = cld.url(poster.publicId || publicId, Util.defaults({}, poster, defaults));
50+
}
51+
if(!Util.isEmpty(poster)){
52+
tagAttributes["poster"] = poster;
53+
}
54+
55+
if(Util.isArray(sourceTypes)){
56+
sources = sourceTypes.map( srcType => {
57+
let transformation = sourceTransformation[srcType] || {};
58+
let src = cld.url(publicId, Util.defaults({}, transformation, {resource_type: 'video', format: srcType}));
59+
let mimeType = 'video/' + (srcType == 'ogv' ? 'ogg' : srcType);
60+
return <source key={mimeType} src={ src} type={ mimeType}/>;
61+
}
62+
);
63+
} else {
64+
tagAttributes["src"] = cld.url(publicId, {resource_type: 'video', format: sourceTypes});
65+
}
66+
console.log("Ready to return tag", tagAttributes);
3267
return (
33-
<div>Video Not Implemented yet.<video></video></div>
68+
<video {...tagAttributes}>
69+
{sources}
70+
{fallback}
71+
{this.props.children}
72+
</video>
3473
);
3574
}
3675
}
37-
Video.propTypes = {initialCount: React.PropTypes.number};
38-
Video.defaultProps = {initialCount: 0};
39-
Video.contextTypes = {};
76+
Video.propTypes = {publicId: PropTypes.string};
77+
// Video.defaultProps = {initialCount: 0};
78+
Video.contextTypes = CloudinaryComponent.contextTypes;

stories/index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { storiesOf, action, linkTo } from '@kadira/storybook';
33
import Image from '../src/components/Image';
44
import Video from '../src/components/Video';
55
import CloudinaryContext from '../src/components/CloudinaryContext';
6-
7-
let CLImage = Image;
6+
import cloudinary from 'cloudinary-core';
87

98
storiesOf('Image', module)
109
.add('image', ()=> {
@@ -30,10 +29,40 @@ storiesOf('Image', module)
3029
)
3130
});
3231
storiesOf('Video', module)
33-
.add('Video', ()=> {
32+
.add('Simple tag', ()=> {
33+
return (
34+
<Video cloudName="demo" controls publicId="dog"/>
35+
)
36+
}).add('With fallback', ()=> {
37+
return (
38+
<Video cloudName="demo" controls="controls" publicId="dog" fallback="Cannot display video"/>
39+
)
40+
}).add('With inline fallback', ()=> {
41+
return (
42+
<Video cloudName="demo" controls="controls" publicId="dog" >
43+
Cannot display <b>video</b>.
44+
</Video>
45+
)
46+
}).add('With source types', ()=> {
47+
return (
48+
<Video cloudName="demo" controls="controls" publicId="dog" sourceTypes={['webm', 'ogv', 'mp4']}>
49+
Cannot display video.
50+
</Video>
51+
)
52+
}).add('Simple tag with width', ()=> {
53+
return (
54+
<Video cloudName="demo" controls publicId="dog" width="300" crop="scale"/>
55+
)
56+
}).add('Simple tag with poster url', ()=> {
57+
let url = cloudinary.Cloudinary.new({cloud_name: "demo"}).url("sample", {width:300, crop: "scale"});
58+
return (
59+
<Video cloudName="demo" controls publicId="dog" width="300" crop="scale" poster={url}/>
60+
)
61+
}).add('Simple tag with poster object', ()=> {
3462
return (
35-
<Video />
36-
)});
63+
<Video cloudName="demo" controls publicId="dog" width="300" crop="scale" poster={{publicId: "sample"}}/>
64+
)
65+
});
3766
storiesOf('CloudinaryContext', module)
3867
.add('CloudinaryContext', ()=> {
3968
let t = { width: 0.5, crop: "scale"};
@@ -47,7 +76,6 @@ storiesOf('CloudinaryContext', module)
4776
</CloudinaryContext>
4877
)})
4978
.add('Nested Context', ()=> {
50-
let t = { width: 0.5, crop: "scale"};
5179
return (
5280
<CloudinaryContext cloudName="demo" width="50" crop="scale">
5381
<Image publicId="sample"/>

0 commit comments

Comments
 (0)