You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
11 KiB

  1. /* ******************************
  2. Module - Video Component
  3. Author: Jack Lukic
  4. Notes: First Commit June 30, 2012
  5. This is a video playlist and video embed plugin which helps
  6. provide helpers for adding embed code for vimeo and youtube and
  7. abstracting event handlers for each library
  8. ****************************** */
  9. ;(function ($, window, document, undefined) {
  10. $.fn.video = function(parameters) {
  11. var
  12. settings = $.extend(true, {}, $.fn.video.settings, parameters),
  13. // make arguments available
  14. moduleArguments = arguments || false,
  15. invokedResponse
  16. ;
  17. $(this)
  18. .each(function() {
  19. var
  20. $module = $(this),
  21. $placeholder = $module.find(settings.selector.placeholder),
  22. $playButton = $module.find(settings.selector.playButton),
  23. $embed = $module.find(settings.selector.embed),
  24. element = this,
  25. instance = $module.data('module-' + settings.namespace),
  26. methodInvoked = (typeof parameters == 'string'),
  27. namespace = settings.namespace,
  28. metadata = settings.metadata,
  29. className = settings.className,
  30. module
  31. ;
  32. module = {
  33. initialize: function() {
  34. module.debug('Initializing video');
  35. $placeholder
  36. .off('.video')
  37. .on('click.' + namespace, module.play)
  38. ;
  39. $playButton
  40. .off('.video')
  41. .on('click.' + namespace, module.play)
  42. ;
  43. $module
  44. .data('module-' + namespace, module)
  45. ;
  46. },
  47. // sets new video
  48. change: function(source, flv) {
  49. module.debug('Changing video to ', flv);
  50. $module
  51. .data(metadata.source, source)
  52. .data(metadata.flv, flv)
  53. ;
  54. settings.onChange();
  55. },
  56. // clears video embed
  57. reset: function() {
  58. module.debug('Clearing video embed and showing placeholder');
  59. $module
  60. .removeClass(className.active)
  61. ;
  62. $embed
  63. .html(' ')
  64. ;
  65. $placeholder
  66. .show()
  67. ;
  68. settings.onReset();
  69. },
  70. // plays current video
  71. play: function() {
  72. module.debug('Playing video');
  73. var
  74. source = $module.data(metadata.source),
  75. flv = $module.data(metadata.flv)
  76. ;
  77. $embed
  78. .html( module.generate.html(source, flv) )
  79. ;
  80. $module
  81. .addClass(className.active)
  82. ;
  83. settings.onPlay();
  84. },
  85. generate: {
  86. // generates iframe html
  87. html: function(source, flv) {
  88. module.debug('Generating embed html');
  89. var
  90. width = (settings.width == 'auto')
  91. ? $module.width()
  92. : settings.width,
  93. height = (settings.height == 'auto')
  94. ? $module.height()
  95. : settings.height,
  96. html
  97. ;
  98. if(source == 'vimeo') {
  99. html = ''
  100. + '<iframe src="http://player.vimeo.com/video/' + flv + '?=' + module.generate.url(source) + '"'
  101. + ' width="' + width + '" height="' + height + '"'
  102. + ' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  103. ;
  104. }
  105. else if(source == 'youtube') {
  106. html = ''
  107. + '<iframe src="http://www.youtube.com/embed/' + flv + '?=' + module.generate.url(source) + '"'
  108. + ' width="' + width + '" height="' + height + '"'
  109. + ' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  110. ;
  111. }
  112. return html;
  113. },
  114. // generate url parameters
  115. url: function(source) {
  116. var
  117. api = (settings.api)
  118. ? 1
  119. : 0,
  120. autoplay = (settings.autoplay)
  121. ? 1
  122. : 0,
  123. hd = (settings.hd)
  124. ? 1
  125. : 0,
  126. showUI = (settings.showUI)
  127. ? 1
  128. : 0,
  129. // opposite used for some params
  130. hideUI = !(settings.showUI)
  131. ? 1
  132. : 0,
  133. url = ''
  134. ;
  135. if(source == 'vimeo') {
  136. url = ''
  137. + 'api=' + api
  138. + '&amp;title=' + showUI
  139. + '&amp;byline=' + showUI
  140. + '&amp;portrait=' + showUI
  141. + '&amp;autoplay=' + autoplay
  142. ;
  143. if(settings.color) {
  144. url += '&amp;color=' + settings.color;
  145. }
  146. }
  147. else if(source == 'youtube') {
  148. url = ''
  149. + 'enablejsapi=' + api
  150. + '&amp;autoplay=' + autoplay
  151. + '&amp;autohide=' + hideUI
  152. + '&amp;hq=' + hd
  153. + '&amp;modestbranding=1'
  154. ;
  155. if(settings.color) {
  156. url += '&amp;color=' + settings.color;
  157. }
  158. }
  159. return url;
  160. }
  161. },
  162. /* standard module */
  163. debug: function(message, variableName) {
  164. if(settings.debug) {
  165. if(variableName !== undefined) {
  166. console.info(settings.moduleName + ': ' + message, variableName);
  167. }
  168. else {
  169. console.info(settings.moduleName + ': ' + message);
  170. }
  171. }
  172. },
  173. error: function(errorMessage) {
  174. console.warn(settings.moduleName + ': ' + errorMessage);
  175. },
  176. invoke: function(methodName, context, methodArguments) {
  177. var
  178. method
  179. ;
  180. methodArguments = methodArguments || Array.prototype.slice.call( arguments, 2 );
  181. if(typeof methodName == 'string' && instance !== undefined) {
  182. methodName = methodName.split('.');
  183. $.each(methodName, function(index, name) {
  184. if( $.isPlainObject( instance[name] ) ) {
  185. instance = instance[name];
  186. return true;
  187. }
  188. else if( $.isFunction( instance[name] ) ) {
  189. method = instance[name];
  190. return true;
  191. }
  192. module.error(settings.errors.method);
  193. return false;
  194. });
  195. }
  196. if ( $.isFunction( method ) ) {
  197. return method.apply(context, methodArguments);
  198. }
  199. // return retrieved variable or chain
  200. return method;
  201. }
  202. };
  203. // check for invoking internal method
  204. if(methodInvoked) {
  205. invokedResponse = module.invoke(moduleArguments[0], this, Array.prototype.slice.call( moduleArguments, 1 ) );
  206. }
  207. // otherwise initialize
  208. else {
  209. if(instance) {
  210. module.destroy();
  211. }
  212. module.initialize();
  213. }
  214. })
  215. ;
  216. // chain or return queried method
  217. return (invokedResponse !== undefined)
  218. ? invokedResponse
  219. : this
  220. ;
  221. };
  222. $.fn.videoPlaylist = function(video, parameters) {
  223. var
  224. $allModules = $(this),
  225. $video = $(video),
  226. $iframe = $video.find('.embed iframe'),
  227. settings = $.extend({}, $.fn.videoPlaylist.settings, parameters, true)
  228. ;
  229. $allModules
  230. .each(function() {
  231. var
  232. $element = $(this),
  233. metadata = settings.metadata,
  234. namespace = settings.namespace,
  235. className = settings.className,
  236. module = {
  237. initialize: function() {
  238. $element
  239. .on('click.' + namespace , module.changeVideo)
  240. ;
  241. },
  242. changeVideo: function() {
  243. var
  244. flv = $element.data(metadata.flv) || false,
  245. source = $element.data(metadata.source) || false,
  246. placeholder = $element.data(metadata.placeholder) || false
  247. ;
  248. if(flv && source) {
  249. $video
  250. .data(metadata.source, source)
  251. .data(metadata.flv, flv)
  252. ;
  253. if(settings.showPlaceholder) {
  254. $video
  255. .removeClass(className.active)
  256. .find($.fn.video.selector.placeholder)
  257. .attr('src', placeholder)
  258. ;
  259. }
  260. else {
  261. try {
  262. $video
  263. .video('play')
  264. ;
  265. }
  266. catch(error) {
  267. console.warn('Video Playlist Module: ' + settings.error.init);
  268. }
  269. }
  270. $allModules
  271. .removeClass(className.active)
  272. ;
  273. $element
  274. .addClass(className.active)
  275. ;
  276. }
  277. }
  278. }
  279. ;
  280. module.initialize();
  281. })
  282. ;
  283. if(settings.playFirst) {
  284. $allModules
  285. .eq(0)
  286. .trigger('click')
  287. ;
  288. // we all like a good hack
  289. if($iframe.size() > 0) {
  290. $iframe
  291. .attr('src', $iframe.attr('src').replace('autoplay=1', 'autoplay=0') )
  292. ;
  293. }
  294. }
  295. };
  296. $.fn.video.settings = {
  297. moduleName : 'Video',
  298. namespace : 'video',
  299. debug : false,
  300. metadata : {
  301. source : 'source',
  302. flv : 'flv'
  303. },
  304. onPlay : function(){},
  305. onReset : function(){},
  306. onChange : function(){},
  307. // callbacks not coded yet (needs to use jsapi)
  308. play : function() {},
  309. pause : function() {},
  310. stop : function() {},
  311. width : 'auto',
  312. height : 'auto',
  313. autoplay : false,
  314. color : '#442359',
  315. hd : true,
  316. showUI : false,
  317. api : true,
  318. errors : {
  319. method : 'The method you called is not defined'
  320. },
  321. className : {
  322. active : 'active'
  323. },
  324. selector : {
  325. embed : '.embed',
  326. placeholder : '.placeholder',
  327. playButton : '.play'
  328. }
  329. };
  330. $.fn.videoPlaylist.settings = {
  331. moduleName : 'Video Playlist',
  332. namespace : 'videoPlaylist',
  333. source : 'vimeo',
  334. showPlaceholder : false,
  335. playFirst : true,
  336. metadata: {
  337. flv : 'flv',
  338. source : 'source',
  339. placeholder : 'placeholder'
  340. },
  341. errors: {
  342. init : 'The video player you specified was not yet initialized'
  343. },
  344. className : {
  345. active : 'active'
  346. }
  347. };
  348. })( jQuery, window , document );