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.

650 lines
18 KiB

9 years ago
  1. /*!
  2. * # Semantic UI 2.0.0 - Video
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ($, window, document, undefined) {
  12. "use strict";
  13. $.fn.embed = function(parameters) {
  14. var
  15. $allModules = $(this),
  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. returnedValue
  23. ;
  24. $allModules
  25. .each(function() {
  26. var
  27. settings = ( $.isPlainObject(parameters) )
  28. ? $.extend(true, {}, $.fn.embed.settings, parameters)
  29. : $.extend({}, $.fn.embed.settings),
  30. selector = settings.selector,
  31. className = settings.className,
  32. sources = settings.sources,
  33. error = settings.error,
  34. metadata = settings.metadata,
  35. namespace = settings.namespace,
  36. templates = settings.templates,
  37. eventNamespace = '.' + namespace,
  38. moduleNamespace = 'module-' + namespace,
  39. $window = $(window),
  40. $module = $(this),
  41. $placeholder = $module.find(selector.placeholder),
  42. $play = $module.find(selector.play),
  43. $embed = $module.find(selector.embed),
  44. element = this,
  45. instance = $module.data(moduleNamespace),
  46. module
  47. ;
  48. module = {
  49. initialize: function() {
  50. module.debug('Initializing embed');
  51. module.create();
  52. module.bind.events();
  53. module.instantiate();
  54. },
  55. instantiate: function() {
  56. module.verbose('Storing instance of module', module);
  57. instance = module;
  58. $module
  59. .data(moduleNamespace, module)
  60. ;
  61. },
  62. destroy: function() {
  63. module.verbose('Destroying previous instance of embed');
  64. module.reset();
  65. $module
  66. .removeData(moduleNamespace)
  67. .off(eventNamespace)
  68. ;
  69. },
  70. refresh: function() {
  71. module.verbose('Refreshing selector cache');
  72. $placeholder = $module.find(selector.placeholder);
  73. $play = $module.find(selector.play);
  74. $embed = $module.find(selector.embed);
  75. },
  76. bind: {
  77. events: function() {
  78. if( module.is.video() ) {
  79. module.debug('Adding placeholder events');
  80. $module
  81. .on('click' + eventNamespace, selector.placeholder, module.createAndShow)
  82. .on('click' + eventNamespace, selector.play, module.createAndShow)
  83. ;
  84. }
  85. }
  86. },
  87. create: function() {
  88. var
  89. placeholder = module.get.placeholder()
  90. ;
  91. if(placeholder) {
  92. module.createPlaceholder();
  93. }
  94. else {
  95. module.createAndShow();
  96. }
  97. },
  98. createPlaceholder: function(placeholder) {
  99. var
  100. icon = module.get.icon(),
  101. url = module.get.url(),
  102. embed = module.generate.embed(url)
  103. ;
  104. placeholder = placeholder || module.get.placeholder();
  105. $module.html( templates.placeholder(placeholder, icon) );
  106. module.debug('Creating placeholder for embed', placeholder, icon);
  107. },
  108. createEmbed: function(url) {
  109. module.refresh();
  110. url = url || module.get.url();
  111. $embed = $('<div/>')
  112. .addClass(className.embed)
  113. .html( module.generate.embed(url) )
  114. .appendTo($module)
  115. ;
  116. module.debug('Creating embed object', $embed);
  117. },
  118. createAndShow: function() {
  119. module.createEmbed();
  120. module.show();
  121. },
  122. // sets new embed
  123. change: function(source, id, url) {
  124. module.debug('Changing video to ', source, id, url);
  125. $module
  126. .data(metadata.source, source)
  127. .data(metadata.id, id)
  128. .data(metadata.url, url)
  129. ;
  130. module.create();
  131. settings.onChange.call(element);
  132. },
  133. // clears embed
  134. reset: function() {
  135. module.debug('Clearing embed and showing placeholder');
  136. module.remove.active();
  137. module.remove.embed();
  138. module.showPlaceholder();
  139. settings.onReset.call(element);
  140. },
  141. // shows current embed
  142. show: function() {
  143. module.debug('Showing embed');
  144. module.set.active();
  145. settings.onDisplay.call(element);
  146. },
  147. hide: function() {
  148. module.debug('Hiding embed');
  149. module.showPlaceholder();
  150. },
  151. showPlaceholder: function() {
  152. module.debug('Showing placeholder image');
  153. module.remove.active();
  154. settings.onPlaceholderDisplay.call(element);
  155. },
  156. get: {
  157. id: function() {
  158. return settings.id || $module.data(metadata.id);
  159. },
  160. placeholder: function() {
  161. return settings.placeholder || $module.data(metadata.placeholder);
  162. },
  163. icon: function() {
  164. return (settings.icon)
  165. ? settings.icon
  166. : ($module.data(metadata.icon) !== undefined)
  167. ? $module.data(metadata.icon)
  168. : module.determine.icon()
  169. ;
  170. },
  171. source: function(url) {
  172. return (settings.source)
  173. ? settings.source
  174. : ($module.data(metadata.source) !== undefined)
  175. ? $module.data(metadata.source)
  176. : module.determine.source()
  177. ;
  178. },
  179. type: function() {
  180. var source = module.get.source();
  181. return (sources[source] !== undefined)
  182. ? sources[source].type
  183. : false
  184. ;
  185. },
  186. url: function() {
  187. return (settings.url)
  188. ? settings.url
  189. : ($module.data(metadata.url) !== undefined)
  190. ? $module.data(metadata.url)
  191. : module.determine.url()
  192. ;
  193. }
  194. },
  195. determine: {
  196. source: function(url) {
  197. var
  198. matchedSource = false
  199. ;
  200. url = url || module.get.url();
  201. if(url) {
  202. $.each(sources, function(name, source) {
  203. if(url.search(source.domain) !== -1) {
  204. matchedSource = name;
  205. return false;
  206. }
  207. });
  208. }
  209. return matchedSource;
  210. },
  211. icon: function() {
  212. var
  213. source = module.get.source()
  214. ;
  215. return (sources[source] !== undefined)
  216. ? sources[source].icon
  217. : false
  218. ;
  219. },
  220. url: function() {
  221. var
  222. id = settings.id || $module.data(metadata.id),
  223. source = settings.source || $module.data(metadata.source),
  224. url
  225. ;
  226. url = (sources[source] !== undefined)
  227. ? sources[source].url.replace('{id}', id)
  228. : false
  229. ;
  230. if(url) {
  231. $module.data(metadata.url, url);
  232. }
  233. return url;
  234. }
  235. },
  236. set: {
  237. active: function() {
  238. $module.addClass(className.active);
  239. }
  240. },
  241. remove: {
  242. active: function() {
  243. $module.removeClass(className.active);
  244. },
  245. embed: function() {
  246. $embed.empty();
  247. }
  248. },
  249. encode: {
  250. parameters: function(parameters) {
  251. var
  252. urlString = [],
  253. index
  254. ;
  255. for (index in parameters) {
  256. urlString.push( encodeURIComponent(index) + '=' + encodeURIComponent( parameters[index] ) );
  257. }
  258. return urlString.join('&amp;');
  259. }
  260. },
  261. generate: {
  262. embed: function(url) {
  263. module.debug('Generating embed html');
  264. var
  265. source = module.get.source(),
  266. html,
  267. parameters
  268. ;
  269. url = module.get.url(url);
  270. if(url) {
  271. parameters = module.generate.parameters(source);
  272. html = templates.iframe(url, parameters);
  273. }
  274. else {
  275. module.error(error.noURL, $module);
  276. }
  277. return html;
  278. },
  279. parameters: function(source, extraParameters) {
  280. var
  281. sourceParameters = (sources[source].parameters !== undefined)
  282. ? sources[source].parameters(settings)
  283. : {},
  284. parameters
  285. ;
  286. if(extraParameters) {
  287. parameters = $.extend({}, sourceParameters, extraParameters);
  288. }
  289. parameters = settings.onEmbed(parameters);
  290. return module.encode.parameters(parameters);
  291. }
  292. },
  293. has: {
  294. placeholder: function() {
  295. return settings.placeholder || $module.data(metadata.placeholder);
  296. }
  297. },
  298. is: {
  299. autoplay: function() {
  300. return (settings.autoplay === 'auto')
  301. ? ($module.data('image') !== undefined)
  302. : settings.autoplay
  303. ;
  304. },
  305. video: function() {
  306. return module.get.type() == 'video';
  307. }
  308. },
  309. setting: function(name, value) {
  310. module.debug('Changing setting', name, value);
  311. if( $.isPlainObject(name) ) {
  312. $.extend(true, settings, name);
  313. }
  314. else if(value !== undefined) {
  315. settings[name] = value;
  316. }
  317. else {
  318. return settings[name];
  319. }
  320. },
  321. internal: function(name, value) {
  322. if( $.isPlainObject(name) ) {
  323. $.extend(true, module, name);
  324. }
  325. else if(value !== undefined) {
  326. module[name] = value;
  327. }
  328. else {
  329. return module[name];
  330. }
  331. },
  332. debug: function() {
  333. if(settings.debug) {
  334. if(settings.performance) {
  335. module.performance.log(arguments);
  336. }
  337. else {
  338. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  339. module.debug.apply(console, arguments);
  340. }
  341. }
  342. },
  343. verbose: function() {
  344. if(settings.verbose && settings.debug) {
  345. if(settings.performance) {
  346. module.performance.log(arguments);
  347. }
  348. else {
  349. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  350. module.verbose.apply(console, arguments);
  351. }
  352. }
  353. },
  354. error: function() {
  355. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  356. module.error.apply(console, arguments);
  357. },
  358. performance: {
  359. log: function(message) {
  360. var
  361. currentTime,
  362. executionTime,
  363. previousTime
  364. ;
  365. if(settings.performance) {
  366. currentTime = new Date().getTime();
  367. previousTime = time || currentTime;
  368. executionTime = currentTime - previousTime;
  369. time = currentTime;
  370. performance.push({
  371. 'Name' : message[0],
  372. 'Arguments' : [].slice.call(message, 1) || '',
  373. 'Element' : element,
  374. 'Execution Time' : executionTime
  375. });
  376. }
  377. clearTimeout(module.performance.timer);
  378. module.performance.timer = setTimeout(module.performance.display, 500);
  379. },
  380. display: function() {
  381. var
  382. title = settings.name + ':',
  383. totalTime = 0
  384. ;
  385. time = false;
  386. clearTimeout(module.performance.timer);
  387. $.each(performance, function(index, data) {
  388. totalTime += data['Execution Time'];
  389. });
  390. title += ' ' + totalTime + 'ms';
  391. if(moduleSelector) {
  392. title += ' \'' + moduleSelector + '\'';
  393. }
  394. if($allModules.length > 1) {
  395. title += ' ' + '(' + $allModules.length + ')';
  396. }
  397. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  398. console.groupCollapsed(title);
  399. if(console.table) {
  400. console.table(performance);
  401. }
  402. else {
  403. $.each(performance, function(index, data) {
  404. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  405. });
  406. }
  407. console.groupEnd();
  408. }
  409. performance = [];
  410. }
  411. },
  412. invoke: function(query, passedArguments, context) {
  413. var
  414. object = instance,
  415. maxDepth,
  416. found,
  417. response
  418. ;
  419. passedArguments = passedArguments || queryArguments;
  420. context = element || context;
  421. if(typeof query == 'string' && object !== undefined) {
  422. query = query.split(/[\. ]/);
  423. maxDepth = query.length - 1;
  424. $.each(query, function(depth, value) {
  425. var camelCaseValue = (depth != maxDepth)
  426. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  427. : query
  428. ;
  429. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  430. object = object[camelCaseValue];
  431. }
  432. else if( object[camelCaseValue] !== undefined ) {
  433. found = object[camelCaseValue];
  434. return false;
  435. }
  436. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  437. object = object[value];
  438. }
  439. else if( object[value] !== undefined ) {
  440. found = object[value];
  441. return false;
  442. }
  443. else {
  444. module.error(error.method, query);
  445. return false;
  446. }
  447. });
  448. }
  449. if ( $.isFunction( found ) ) {
  450. response = found.apply(context, passedArguments);
  451. }
  452. else if(found !== undefined) {
  453. response = found;
  454. }
  455. if($.isArray(returnedValue)) {
  456. returnedValue.push(response);
  457. }
  458. else if(returnedValue !== undefined) {
  459. returnedValue = [returnedValue, response];
  460. }
  461. else if(response !== undefined) {
  462. returnedValue = response;
  463. }
  464. return found;
  465. }
  466. };
  467. if(methodInvoked) {
  468. if(instance === undefined) {
  469. module.initialize();
  470. }
  471. module.invoke(query);
  472. }
  473. else {
  474. if(instance !== undefined) {
  475. instance.invoke('destroy');
  476. }
  477. module.initialize();
  478. }
  479. })
  480. ;
  481. return (returnedValue !== undefined)
  482. ? returnedValue
  483. : this
  484. ;
  485. };
  486. $.fn.embed.settings = {
  487. name : 'Embed',
  488. namespace : 'embed',
  489. debug : false,
  490. verbose : false,
  491. performance : true,
  492. icon : false,
  493. source : false,
  494. url : false,
  495. id : false,
  496. // standard video settings
  497. autoplay : 'auto',
  498. color : '#444444',
  499. hd : true,
  500. showUI : false,
  501. onDisplay : function() {},
  502. onPlaceholderDisplay : function() {},
  503. onReset : function() {},
  504. onChange : function() {},
  505. onEmbed : function(parameters) {
  506. return parameters;
  507. },
  508. width : 'auto',
  509. height : 'auto',
  510. // additional parameters to include with the embed
  511. parameters: false,
  512. // callbacks not coded yet (needs to use jsapi)
  513. api : true,
  514. onPause : function() {},
  515. onStop : function() {},
  516. metadata : {
  517. id : 'id',
  518. icon : 'icon',
  519. placeholder : 'placeholder',
  520. source : 'source',
  521. url : 'url'
  522. },
  523. error : {
  524. noURL : 'No URL specified',
  525. method : 'The method you called is not defined'
  526. },
  527. className : {
  528. active : 'active',
  529. embed : 'embed'
  530. },
  531. selector : {
  532. embed : '.embed',
  533. placeholder : '.placeholder',
  534. play : '.play'
  535. }
  536. };
  537. $.fn.embed.settings.sources = {
  538. youtube: {
  539. name : 'youtube',
  540. type : 'video',
  541. icon : 'video play',
  542. domain : 'youtube.com',
  543. url : '//www.youtube.com/embed/{id}',
  544. parameters: function(settings) {
  545. return {
  546. autohide : !settings.showUI,
  547. autoplay : settings.autoplay,
  548. color : settings.colors || undefined,
  549. hq : settings.hd,
  550. jsapi : settings.api,
  551. modestbranding : 1
  552. };
  553. }
  554. },
  555. vimeo: {
  556. name : 'vimeo',
  557. type : 'video',
  558. icon : 'video play',
  559. domain : 'vimeo.com',
  560. url : '//www.youtube.com/embed/{id}',
  561. parameters: function(settings) {
  562. return {
  563. api : settings.api,
  564. autoplay : settings.autoplay,
  565. byline : settings.showUI,
  566. color : settings.colors || undefined,
  567. portrait : settings.showUI,
  568. title : settings.showUI
  569. };
  570. }
  571. }
  572. };
  573. $.fn.embed.settings.templates = {
  574. iframe : function(url, parameters) {
  575. return ''
  576. + '<iframe src="' + url + '?=' + parameters + '"'
  577. + ' width="100%" height="100%"'
  578. + ' frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
  579. ;
  580. },
  581. placeholder : function(image, icon) {
  582. var
  583. html = ''
  584. ;
  585. if(icon) {
  586. html += '<i class="' + icon + ' icon"></i>';
  587. }
  588. if(image) {
  589. html += '<img class="placeholder" src="' + image + '">';
  590. }
  591. return html;
  592. }
  593. };
  594. })( jQuery, window , document );