|
1 | 1 | /* |
2 | 2 | * React Video - React component to load video from Vimeo or Youtube across any device |
3 | | - * @version v1.4.0 |
| 3 | + * @version v1.5.0 |
4 | 4 | * @link https://github.com/pedronauck/react-video |
5 | 5 | * @license MIT |
6 | 6 | * @author Pedro Nauck (https://github.com/pedronauck) |
@@ -72,7 +72,8 @@ return /******/ (function(modules) { // webpackBootstrap |
72 | 72 | displayName: 'Video', |
73 | 73 | propTypes: { |
74 | 74 | from: React.PropTypes.oneOf(['youtube', 'vimeo']), |
75 | | - videoId: React.PropTypes.string.isRequired |
| 75 | + videoId: React.PropTypes.string.isRequired, |
| 76 | + onError: React.PropTypes.func |
76 | 77 | }, |
77 | 78 | getDefaultProps:function() { |
78 | 79 | return { |
@@ -147,28 +148,34 @@ return /******/ (function(modules) { // webpackBootstrap |
147 | 148 | }, |
148 | 149 | fetchYoutubeData:function() { |
149 | 150 | var id = this.props.videoId; |
150 | | - var url = ("//gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json"); |
151 | 151 |
|
152 | | - ajax.get(url, function(err, res) { |
153 | | - var gallery = res.entry['media$group']['media$thumbnail']; |
154 | | - var thumb = gallery.sort(function(a, b) {return b.width - a.width;})[0].url; |
155 | | - |
156 | | - this.setState({ |
157 | | - thumb: thumb, |
158 | | - imageLoaded: true |
159 | | - }); |
160 | | - }.bind(this)); |
| 152 | + ajax.get({ |
| 153 | + url: ("//gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json"), |
| 154 | + onSuccess:function(err, res) { |
| 155 | + var gallery = res.entry['media$group']['media$thumbnail']; |
| 156 | + var thumb = gallery.sort(function(a, b) {return b.width - a.width;})[0].url; |
| 157 | + |
| 158 | + this.setState({ |
| 159 | + thumb: thumb, |
| 160 | + imageLoaded: true |
| 161 | + }) |
| 162 | + }, |
| 163 | + onError: this.props.onError |
| 164 | + }); |
161 | 165 | }, |
162 | 166 | fetchVimeoData:function() { |
163 | 167 | var id = this.props.videoId; |
164 | | - var url = ("//vimeo.com/api/v2/video/" + id + ".json"); |
165 | | - |
166 | | - ajax.get(url, function(err, res) { |
167 | | - this.setState({ |
168 | | - thumb: res[0].thumbnail_large, |
169 | | - imageLoaded: true |
170 | | - }); |
171 | | - }.bind(this)); |
| 168 | + |
| 169 | + ajax.get({ |
| 170 | + url: ("//vimeo.com/api/v2/video/" + id + ".json"), |
| 171 | + onSuccess:function(err, res) { |
| 172 | + this.setState({ |
| 173 | + thumb: res[0].thumbnail_large, |
| 174 | + imageLoaded: true |
| 175 | + }); |
| 176 | + }, |
| 177 | + onError: this.props.onError |
| 178 | + }); |
172 | 179 | } |
173 | 180 | }); |
174 | 181 |
|
@@ -202,43 +209,50 @@ return /******/ (function(modules) { // webpackBootstrap |
202 | 209 | /* 3 */ |
203 | 210 | /***/ function(module, exports, __webpack_require__) { |
204 | 211 |
|
205 | | - /** @jsx React.DOM */var get = function(url, cb) { |
| 212 | + /** @jsx React.DOM */exports.get = function(opts) { |
| 213 | + var url = opts.url; |
| 214 | + var successCb = opts.onSuccess; |
| 215 | + var errorCb = opts.onError; |
206 | 216 | var req = false; |
207 | 217 |
|
208 | 218 | // XDomainRequest onload |
209 | | - var oldIE = function () { |
210 | | - cb(null, JSON.parse(req.responseText)); |
| 219 | + var _oldIE = function () { |
| 220 | + successCb(null, JSON.parse(req.responseText)); |
211 | 221 | }; |
212 | 222 |
|
213 | 223 | // XMLHttpRequest onload |
214 | | - var onLoad = function () { |
| 224 | + var _onLoad = function () { |
215 | 225 | if (req.readyState !== 4) return; |
216 | | - if (req.status === 200) cb(null, JSON.parse(req.responseText)); |
| 226 | + if (req.status === 200) successCb(null, JSON.parse(req.responseText)); |
217 | 227 | else { |
218 | | - cb({ error: 'Sorry, an error ocurred on the server' }, null); |
| 228 | + var err = { error: 'Sorry, an error ocurred on the server' }; |
| 229 | + |
| 230 | + if (errorCb && typeof errorCb === 'function') return errorCb(err); |
| 231 | + successCb(err, null); |
219 | 232 | } |
220 | 233 | }; |
221 | 234 |
|
222 | | - var onError = function() { |
223 | | - cb({ error: 'Problem with your internet conection' }, null); |
| 235 | + var _onError = function() { |
| 236 | + var err = { error: 'Sorry, an error ocurred on the server' }; |
| 237 | + |
| 238 | + if (errorCb && typeof errorCb === 'function') return errorCb(err); |
| 239 | + successCb(err, null); |
224 | 240 | }; |
225 | 241 |
|
226 | 242 | try { |
227 | 243 | req = new XDomainRequest(); |
228 | | - req.onload = oldIE; |
| 244 | + req.onload = _oldIE; |
229 | 245 | } |
230 | 246 | catch (e) { |
231 | 247 | req = new XMLHttpRequest(); |
232 | | - req.onreadystatechange = onLoad; |
| 248 | + req.onreadystatechange = _onLoad; |
233 | 249 | } |
234 | 250 |
|
235 | | - req.onerror = onError; |
| 251 | + req.onerror = _onError; |
236 | 252 | req.open('GET', url, true); |
237 | 253 | req.send(); |
238 | 254 | }; |
239 | 255 |
|
240 | | - module.exports = { get: get }; |
241 | | - |
242 | 256 |
|
243 | 257 | /***/ }, |
244 | 258 | /* 4 */ |
|
0 commit comments