1 /** The minplayer namespace. */
  2 var minplayer = minplayer || {};
  3 
  4 /** All the media player implementations */
  5 minplayer.players = minplayer.players || {};
  6 
  7 /**
  8  * @constructor
  9  * @extends minplayer.players.base
 10  * @class The vimeo media player.
 11  *
 12  * @param {object} context The jQuery context.
 13  * @param {object} options This components options.
 14  * @param {object} queue The event queue to pass events around.
 15  */
 16 minplayer.players.vimeo = function(context, options, queue) {
 17 
 18   // Derive from players base.
 19   minplayer.players.base.call(this, context, options, queue);
 20 };
 21 
 22 /** Derive from minplayer.players.base. */
 23 minplayer.players.vimeo.prototype = new minplayer.players.base();
 24 
 25 /** Reset the constructor. */
 26 minplayer.players.vimeo.prototype.constructor = minplayer.players.vimeo;
 27 
 28 /**
 29  * @see minplayer.plugin.construct
 30  * @this minplayer.players.vimeo
 31  */
 32 minplayer.players.vimeo.prototype.construct = function() {
 33 
 34   // Call the players.flash constructor.
 35   minplayer.players.base.prototype.construct.call(this);
 36 
 37   // Set the plugin name within the options.
 38   this.options.pluginName = 'vimeo';
 39 };
 40 
 41 /**
 42  * @see minplayer.players.base#getPriority
 43  * @param {object} file A {@link minplayer.file} object.
 44  * @return {number} The priority of this media player.
 45  */
 46 minplayer.players.vimeo.getPriority = function(file) {
 47   return 10;
 48 };
 49 
 50 /**
 51  * @see minplayer.players.base#canPlay
 52  *
 53  * @param {object} file A {@link minplayer.file} object.
 54  * @return {boolean} If this player can play this media type.
 55  */
 56 minplayer.players.vimeo.canPlay = function(file) {
 57 
 58   // Check for the mimetype for vimeo.
 59   if (file.mimetype === 'video/vimeo') {
 60     return true;
 61   }
 62 
 63   // If the path is a vimeo path, then return true.
 64   return (file.path.search(/^http(s)?\:\/\/(www\.)?vimeo\.com/i) === 0);
 65 };
 66 
 67 /**
 68  * Determines if the player should show the playloader.
 69  *
 70  * @param {string} preview The preview image.
 71  * @return {bool} If this player implements its own playLoader.
 72  */
 73 minplayer.players.vimeo.prototype.hasPlayLoader = function(preview) {
 74   return minplayer.hasTouch;
 75 };
 76 
 77 /**
 78  * Determines if the player should show the playloader.
 79  *
 80  * @return {bool} If this player implements its own playLoader.
 81  */
 82 minplayer.players.vimeo.prototype.hasController = function() {
 83   return minplayer.hasTouch;
 84 };
 85 
 86 /**
 87  * Return the ID for a provided media file.
 88  *
 89  * @param {object} file A {@link minplayer.file} object.
 90  * @return {string} The ID for the provided media.
 91  */
 92 minplayer.players.vimeo.getMediaId = function(file) {
 93   var regex = /^http[s]?\:\/\/(www\.)?vimeo\.com\/(\?v\=)?([0-9]+)/i;
 94   if (file.path.search(regex) === 0) {
 95     return file.path.match(regex)[3];
 96   }
 97   else {
 98     return file.path;
 99   }
100 };
101 
102 /**
103  * Parse a single playlist node.
104  *
105  * @param {object} item The vimeo item.
106  * @return {object} The mediafront node.
107  */
108 minplayer.players.vimeo.parseNode = function(item) {
109   return {
110     title: item.title,
111     description: item.description,
112     mediafiles: {
113       image: {
114         'thumbnail': {
115           path: item.thumbnail_small
116         },
117         'image': {
118           path: item.thumbnail_large
119         }
120       },
121       media: {
122         'media': {
123           player: 'vimeo',
124           id: item.id
125         }
126       }
127     }
128   };
129 };
130 
131 /** Keep track of loaded nodes from vimeo. */
132 minplayer.players.vimeo.nodes = {};
133 
134 /**
135  * Returns information about this vimeo video.
136  *
137  * @param {object} file The file to get the node from.
138  * @param {function} callback Callback when the node is loaded.
139  */
140 minplayer.players.vimeo.getNode = function(file, callback) {
141   if (minplayer.players.vimeo.nodes.hasOwnProperty(file.id)) {
142     callback(minplayer.players.vimeo.nodes[file.id]);
143   }
144   else {
145     jQuery.ajax({
146       url: 'https://vimeo.com/api/v2/video/' + file.id + '.json',
147       dataType: 'jsonp',
148       success: function(data) {
149         var node = minplayer.players.vimeo.parseNode(data[0]);
150         minplayer.players.vimeo.nodes[file.id] = node;
151         callback(node);
152       }
153     });
154   }
155 };
156 
157 /**
158  * Returns a preview image for this media player.
159  *
160  * @param {object} file A {@link minplayer.file} object.
161  * @param {string} type The type of image.
162  * @param {function} callback Called when the image is retrieved.
163  */
164 minplayer.players.vimeo.getImage = function(file, type, callback) {
165   minplayer.players.vimeo.getNode(file, function(node) {
166     callback(node.mediafiles.image.image);
167   });
168 };
169 
170 /**
171  * @see minplayer.players.base#reset
172  */
173 minplayer.players.vimeo.prototype.reset = function() {
174 
175   // Reset the flash variables..
176   minplayer.players.base.prototype.reset.call(this);
177 };
178 
179 /**
180  * @see minplayer.players.base#create
181  * @return {object} The media player entity.
182  */
183 minplayer.players.vimeo.prototype.createPlayer = function() {
184   minplayer.players.base.prototype.createPlayer.call(this);
185 
186   // Insert the Vimeo Froogaloop player.
187   var vimeo_script = 'http://a.vimeocdn.com/js/froogaloop2.min.js';
188   if (jQuery('script[src="' + vimeo_script + '"]').length === 0) {
189     var tag = document.createElement('script');
190     tag.src = vimeo_script;
191     var firstScriptTag = document.getElementsByTagName('script')[0];
192     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
193   }
194 
195   // Create the iframe for this player.
196   var iframe = document.createElement('iframe');
197   iframe.setAttribute('id', this.options.id + '-player');
198   iframe.setAttribute('type', 'text/html');
199   iframe.setAttribute('width', '100%');
200   iframe.setAttribute('height', '100%');
201   iframe.setAttribute('frameborder', '0');
202   jQuery(iframe).addClass('vimeo-player');
203 
204   // Get the source.
205   var src = 'https://player.vimeo.com/video/';
206   src += this.mediaFile.id + '?';
207 
208   // Add the parameters to the src.
209   src += jQuery.param({
210     'wmode': 'opaque',
211     'api': 1,
212     'player_id': this.options.id + '-player',
213     'title': 0,
214     'byline': 0,
215     'portrait': 0,
216     'loop': this.options.loop
217   });
218 
219   // Set the source of the iframe.
220   iframe.setAttribute('src', src);
221 
222   // Now register this player when the froogaloop code is loaded.
223   this.poll(this.options.id + '_vimeo', (function(player) {
224     return function() {
225       if (window.Froogaloop) {
226         player.player = window.Froogaloop(iframe);
227         var playerTimeout = 0;
228         player.player.addEvent('ready', function() {
229           clearTimeout(playerTimeout);
230           player.onReady();
231           player.onError('');
232         });
233         playerTimeout = setTimeout(function() {
234           player.onReady();
235         }, 3000);
236       }
237       return !window.Froogaloop;
238     };
239   })(this), 200);
240 
241   // Trigger that the load has started.
242   this.trigger('loadstart');
243 
244   // Return the player.
245   return iframe;
246 };
247 
248 /**
249  * @see minplayer.players.base#onReady
250  */
251 minplayer.players.vimeo.prototype.onReady = function(player_id) {
252 
253   // Add the other listeners.
254   this.player.addEvent('loadProgress', (function(player) {
255     return function(progress) {
256       player.duration.set(parseFloat(progress.duration));
257       player.bytesLoaded.set(progress.bytesLoaded);
258       player.bytesTotal.set(progress.bytesTotal);
259     };
260   })(this));
261 
262   this.player.addEvent('playProgress', (function(player) {
263     return function(progress) {
264       player.duration.set(parseFloat(progress.duration));
265       player.currentTime.set(parseFloat(progress.seconds));
266     };
267   })(this));
268 
269   this.player.addEvent('play', (function(player) {
270     return function() {
271       player.onPlaying();
272     };
273   })(this));
274 
275   this.player.addEvent('pause', (function(player) {
276     return function() {
277       player.onPaused();
278     };
279   })(this));
280 
281   this.player.addEvent('finish', (function(player) {
282     return function() {
283       player.onComplete();
284     };
285   })(this));
286 
287   minplayer.players.base.prototype.onReady.call(this);
288   this.onLoaded();
289 
290   // Make sure we autoplay if it is set.
291   if (this.options.autoplay) {
292     this.play();
293   }
294 };
295 
296 /**
297  * Clears the media player.
298  */
299 minplayer.players.vimeo.prototype.clear = function() {
300   if (this.player) {
301     this.player.api('unload');
302   }
303 
304   minplayer.players.base.prototype.clear.call(this);
305 };
306 
307 /**
308  * @see minplayer.players.base#load
309  */
310 minplayer.players.vimeo.prototype.load = function(file, callback) {
311   minplayer.players.base.prototype.load.call(this, file, function() {
312     this.construct();
313     if (callback) {
314       callback.call(this);
315     }
316   });
317 };
318 
319 /**
320  * @see minplayer.players.base#play
321  */
322 minplayer.players.vimeo.prototype.play = function(callback) {
323   minplayer.players.base.prototype.play.call(this, function() {
324     this.player.api('play');
325     if (callback) {
326       callback.call(this);
327     }
328   });
329 };
330 
331 /**
332  * @see minplayer.players.base#pause
333  */
334 minplayer.players.vimeo.prototype.pause = function(callback) {
335   minplayer.players.base.prototype.pause.call(this, function() {
336     this.player.api('pause');
337     if (callback) {
338       callback.call(this);
339     }
340   });
341 };
342 
343 /**
344  * @see minplayer.players.base#stop
345  */
346 minplayer.players.vimeo.prototype.stop = function(callback) {
347   minplayer.players.base.prototype.stop.call(this, function() {
348     this.player.api('unload');
349     if (callback) {
350       callback.call(this);
351     }
352   });
353 };
354 
355 /**
356  * @see minplayer.players.base#_seek
357  */
358 minplayer.players.vimeo.prototype._seek = function(pos) {
359   this.player.api('seekTo', pos);
360 };
361 
362 /**
363  * @see minplayer.players.base#setVolume
364  */
365 minplayer.players.vimeo.prototype.setVolume = function(vol, callback) {
366   minplayer.players.base.prototype.setVolume.call(this, vol, function() {
367     this.volume.set(vol);
368     this.player.api('setVolume', vol);
369     if (callback) {
370       callback.call(this);
371     }
372   });
373 };
374 
375 /**
376  * @see minplayer.players.base#getVolume
377  */
378 minplayer.players.vimeo.prototype._getVolume = function(callback) {
379   this.player.api('getVolume', function(vol) {
380     callback(vol);
381   });
382 };
383 
384 /**
385  * @see minplayer.players.base#getDuration.
386  */
387 minplayer.players.vimeo.prototype._getDuration = function(callback) {
388   this.player.api('getDuration', function(duration) {
389     callback(duration);
390   });
391 };
392