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.

456 lines
13 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. $allModules = $(this),
  13. settings = ( $.isPlainObject(parameters) )
  14. ? $.extend(true, {}, $.fn.video.settings, parameters)
  15. : $.fn.video.settings,
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. query = arguments[0],
  20. methodInvoked = (typeof query == 'string'),
  21. queryArguments = [].slice.call(arguments, 1),
  22. selector = settings.selector,
  23. className = settings.className,
  24. error = settings.error,
  25. metadata = settings.metadata,
  26. namespace = settings.namespace,
  27. eventNamespace = '.' + namespace,
  28. moduleNamespace = namespace + '-module',
  29. invokedResponse
  30. ;
  31. $allModules
  32. .each(function() {
  33. var
  34. $module = $(this),
  35. $placeholder = $module.find(selector.placeholder),
  36. $playButton = $module.find(selector.playButton),
  37. $embed = $module.find(selector.embed),
  38. element = this,
  39. instance = $module.data(moduleNamespace),
  40. module
  41. ;
  42. module = {
  43. initialize: function() {
  44. module.debug('Initializing video');
  45. $placeholder
  46. .on('click' + eventNamespace, module.play)
  47. ;
  48. $playButton
  49. .on('click' + eventNamespace, module.play)
  50. ;
  51. module.instantiate();
  52. },
  53. instantiate: function() {
  54. module.verbose('Storing instance of module', module);
  55. instance = module;
  56. $module
  57. .data(moduleNamespace, module)
  58. ;
  59. },
  60. destroy: function() {
  61. module.verbose('Destroying previous instance of video');
  62. $module
  63. .removeData(moduleNamespace)
  64. .off(eventNamespace)
  65. ;
  66. },
  67. // sets new video
  68. change: function(source, id, url) {
  69. module.debug('Changing video to ', source, id, url);
  70. $module
  71. .data(metadata.source, source)
  72. .data(metadata.id, id)
  73. .data(metadata.url, url)
  74. ;
  75. settings.onChange();
  76. },
  77. // clears video embed
  78. reset: function() {
  79. module.debug('Clearing video embed and showing placeholder');
  80. $module
  81. .removeClass(className.active)
  82. ;
  83. $embed
  84. .html(' ')
  85. ;
  86. $placeholder
  87. .show()
  88. ;
  89. settings.onReset();
  90. },
  91. // plays current video
  92. play: function() {
  93. module.debug('Playing video');
  94. var
  95. source = $module.data(metadata.source) || false,
  96. url = $module.data(metadata.url) || false,
  97. id = $module.data(metadata.id) || false
  98. ;
  99. $embed
  100. .html( module.generate.html(source, id, url) )
  101. ;
  102. $module
  103. .addClass(className.active)
  104. ;
  105. settings.onPlay();
  106. },
  107. generate: {
  108. // generates iframe html
  109. html: function(source, id, url) {
  110. module.debug('Generating embed html');
  111. var
  112. width = (settings.width == 'auto')
  113. ? $module.width()
  114. : settings.width,
  115. height = (settings.height == 'auto')
  116. ? $module.height()
  117. : settings.height,
  118. html
  119. ;
  120. if(source && id) {
  121. if(source == 'vimeo') {
  122. html = ''
  123. + '<iframe src="http://player.vimeo.com/video/' + id + '?=' + module.generate.url(source) + '"'
  124. + ' width="' + width + '" height="' + height + '"'
  125. + ' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  126. ;
  127. }
  128. else if(source == 'youtube') {
  129. html = ''
  130. + '<iframe src="http://www.youtube.com/embed/' + id + '?=' + module.generate.url(source) + '"'
  131. + ' width="' + width + '" height="' + height + '"'
  132. + ' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  133. ;
  134. }
  135. }
  136. else if(url) {
  137. html = ''
  138. + '<iframe src="' + url + '"'
  139. + ' width="' + width + '" height="' + height + '"'
  140. + ' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  141. ;
  142. }
  143. else {
  144. module.error(error.noVideo);
  145. }
  146. return html;
  147. },
  148. // generate url parameters
  149. url: function(source) {
  150. var
  151. api = (settings.api)
  152. ? 1
  153. : 0,
  154. autoplay = (settings.autoplay)
  155. ? 1
  156. : 0,
  157. hd = (settings.hd)
  158. ? 1
  159. : 0,
  160. showUI = (settings.showUI)
  161. ? 1
  162. : 0,
  163. // opposite used for some params
  164. hideUI = !(settings.showUI)
  165. ? 1
  166. : 0,
  167. url = ''
  168. ;
  169. if(source == 'vimeo') {
  170. url = ''
  171. + 'api=' + api
  172. + '&amp;title=' + showUI
  173. + '&amp;byline=' + showUI
  174. + '&amp;portrait=' + showUI
  175. + '&amp;autoplay=' + autoplay
  176. ;
  177. if(settings.color) {
  178. url += '&amp;color=' + settings.color;
  179. }
  180. }
  181. if(source == 'ustream') {
  182. url = ''
  183. + 'autoplay=' + autoplay
  184. ;
  185. if(settings.color) {
  186. url += '&amp;color=' + settings.color;
  187. }
  188. }
  189. else if(source == 'youtube') {
  190. url = ''
  191. + 'enablejsapi=' + api
  192. + '&amp;autoplay=' + autoplay
  193. + '&amp;autohide=' + hideUI
  194. + '&amp;hq=' + hd
  195. + '&amp;modestbranding=1'
  196. ;
  197. if(settings.color) {
  198. url += '&amp;color=' + settings.color;
  199. }
  200. }
  201. return url;
  202. }
  203. },
  204. setting: function(name, value) {
  205. if(value !== undefined) {
  206. if( $.isPlainObject(name) ) {
  207. $.extend(true, settings, name);
  208. }
  209. else {
  210. settings[name] = value;
  211. }
  212. }
  213. else {
  214. return settings[name];
  215. }
  216. },
  217. internal: function(name, value) {
  218. if(value !== undefined) {
  219. if( $.isPlainObject(name) ) {
  220. $.extend(true, module, name);
  221. }
  222. else {
  223. module[name] = value;
  224. }
  225. }
  226. else {
  227. return module[name];
  228. }
  229. },
  230. debug: function() {
  231. if(settings.debug) {
  232. if(settings.performance) {
  233. module.performance.log(arguments);
  234. }
  235. else {
  236. module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  237. module.debug.apply(console, arguments);
  238. }
  239. }
  240. },
  241. verbose: function() {
  242. if(settings.verbose && settings.debug) {
  243. if(settings.performance) {
  244. module.performance.log(arguments);
  245. }
  246. else {
  247. module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  248. module.verbose.apply(console, arguments);
  249. }
  250. }
  251. },
  252. error: function() {
  253. module.error = Function.prototype.bind.call(console.error, console, settings.moduleName + ':');
  254. module.error.apply(console, arguments);
  255. },
  256. performance: {
  257. log: function(message) {
  258. var
  259. currentTime,
  260. executionTime,
  261. previousTime
  262. ;
  263. if(settings.performance) {
  264. currentTime = new Date().getTime();
  265. previousTime = time || currentTime;
  266. executionTime = currentTime - previousTime;
  267. time = currentTime;
  268. performance.push({
  269. 'Element' : element,
  270. 'Name' : message[0],
  271. 'Arguments' : [].slice.call(message, 1) || '',
  272. 'Execution Time' : executionTime
  273. });
  274. }
  275. clearTimeout(module.performance.timer);
  276. module.performance.timer = setTimeout(module.performance.display, 100);
  277. },
  278. display: function() {
  279. var
  280. title = settings.moduleName + ':',
  281. totalTime = 0
  282. ;
  283. time = false;
  284. clearTimeout(module.performance.timer);
  285. $.each(performance, function(index, data) {
  286. totalTime += data['Execution Time'];
  287. });
  288. title += ' ' + totalTime + 'ms';
  289. if(moduleSelector) {
  290. title += ' \'' + moduleSelector + '\'';
  291. }
  292. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  293. console.groupCollapsed(title);
  294. if(console.table) {
  295. console.table(performance);
  296. }
  297. else {
  298. $.each(performance, function(index, data) {
  299. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  300. });
  301. }
  302. console.groupEnd();
  303. }
  304. performance = [];
  305. }
  306. },
  307. invoke: function(query, passedArguments, context) {
  308. var
  309. maxDepth,
  310. found,
  311. response
  312. ;
  313. passedArguments = passedArguments || queryArguments;
  314. context = element || context;
  315. if(typeof query == 'string' && instance !== undefined) {
  316. query = query.split(/[\. ]/);
  317. maxDepth = query.length - 1;
  318. $.each(query, function(depth, value) {
  319. var camelCaseValue = (depth != maxDepth)
  320. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  321. : query
  322. ;
  323. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  324. instance = instance[value];
  325. }
  326. else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
  327. instance = instance[camelCaseValue];
  328. }
  329. else if( instance[value] !== undefined ) {
  330. found = instance[value];
  331. return false;
  332. }
  333. else if( instance[camelCaseValue] !== undefined ) {
  334. found = instance[camelCaseValue];
  335. return false;
  336. }
  337. else {
  338. module.error(error.method);
  339. return false;
  340. }
  341. });
  342. }
  343. if ( $.isFunction( found ) ) {
  344. response = found.apply(context, passedArguments);
  345. }
  346. else if(found !== undefined) {
  347. response = found;
  348. }
  349. if($.isArray(invokedResponse)) {
  350. invokedResponse.push(response);
  351. }
  352. else if(typeof invokedResponse == 'string') {
  353. invokedResponse = [invokedResponse, response];
  354. }
  355. else if(response !== undefined) {
  356. invokedResponse = response;
  357. }
  358. return found;
  359. }
  360. };
  361. if(methodInvoked) {
  362. if(instance === undefined) {
  363. module.initialize();
  364. }
  365. module.invoke(query);
  366. }
  367. else {
  368. if(instance !== undefined) {
  369. module.destroy();
  370. }
  371. module.initialize();
  372. }
  373. })
  374. ;
  375. return (invokedResponse !== undefined)
  376. ? invokedResponse
  377. : this
  378. ;
  379. };
  380. $.fn.video.settings = {
  381. moduleName : 'Video',
  382. namespace : 'video',
  383. debug : true,
  384. verbose : true,
  385. performance : true,
  386. metadata : {
  387. source : 'source',
  388. id : 'id',
  389. url : 'url'
  390. },
  391. onPlay : function(){},
  392. onReset : function(){},
  393. onChange : function(){},
  394. // callbacks not coded yet (needs to use jsapi)
  395. onPause : function() {},
  396. onStop : function() {},
  397. width : 'auto',
  398. height : 'auto',
  399. autoplay : false,
  400. color : '#442359',
  401. hd : true,
  402. showUI : false,
  403. api : true,
  404. error : {
  405. noVideo : 'No video specified',
  406. method : 'The method you called is not defined'
  407. },
  408. className : {
  409. active : 'active'
  410. },
  411. selector : {
  412. embed : '.embed',
  413. placeholder : '.placeholder',
  414. playButton : '.play'
  415. }
  416. };
  417. })( jQuery, window , document );