YoutubeVideoLoader.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['./EmbedMedia', 'text!./YoutubeVideo.html'], function (EmbedMedia, Template) {
  9. // YouTube Url patterns
  10. // these formats are also used in the IT Tests. If modified, make sure to update them as well in the file:
  11. // dashboard-it -> tests/utilities/Media.js
  12. var siteFormat = /.*\.youtube\.com\/watch\?v=([A-Za-z0-9-_]+).*/;
  13. var shortFormat = /.*\/youtu\.be\/([A-Za-z0-9-_]+).*/;
  14. var embedFormat = /.*\.youtube\.com\/embed\/([A-Za-z0-9-_]+).*/;
  15. var FORMATS = [siteFormat, shortFormat, embedFormat];
  16. var getYoutubeId = function getYoutubeId(url) {
  17. var match = void 0;
  18. for (var i = 0; i < FORMATS.length; i++) {
  19. match = FORMATS[i].exec(url);
  20. if (match) {
  21. return match[1];
  22. }
  23. }
  24. return null;
  25. };
  26. var YoutubeVideoLoader = function () {
  27. function YoutubeVideoLoader(type) {
  28. _classCallCheck(this, YoutubeVideoLoader);
  29. this.type = type;
  30. }
  31. YoutubeVideoLoader.prototype.tryToLoad = function tryToLoad(url) {
  32. var youtubeId = getYoutubeId(url);
  33. return youtubeId && new EmbedMedia(url, this.type, Template, {
  34. youtubeId: youtubeId
  35. });
  36. };
  37. return YoutubeVideoLoader;
  38. }();
  39. return YoutubeVideoLoader;
  40. });
  41. //# sourceMappingURL=YoutubeVideoLoader.js.map