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.

678 lines
19 KiB

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