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.

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