media/player/videojs/amd/src/loader.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Video JS loader.
  17. *
  18. * This takes care of applying the filter on content which was dynamically loaded.
  19. *
  20. * @module media_videojs/loader
  21. * @copyright 2016 Frédéric Massart - FMCorz.net
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. import Ajax from 'core/ajax';
  25. import Config from 'core/config';
  26. import {eventTypes} from 'core_filters/events';
  27. import LocalStorage from 'core/localstorage';
  28. import Notification from 'core/notification';
  29. import jQuery from 'jquery';
  30. /** @var {bool} Whether this is the first load of videojs module */
  31. let firstLoad;
  32. /** @var {string} The language that is used in the player */
  33. let language;
  34. /** @var {object} List of languages and translations for the current page */
  35. let langStringCache;
  36. /**
  37. * Initialisei teh videojs Loader.
  38. *
  39. * Adds the listener for the event to then notify video.js.
  40. *
  41. * @method
  42. * @param {string} lang Language to be used in the player
  43. * @listens event:filterContentUpdated
  44. */
  45. export const setUp = (lang) => {
  46. language = lang;
  47. firstLoad = true;
  48. // Notify Video.js about the nodes already present on the page.
  49. notifyVideoJS({
  50. detail: {
  51. nodes: document.body,
  52. }
  53. });
  54. // We need to call popover automatically if nodes are added to the page later.
  55. document.addEventListener(eventTypes.filterContentUpdated, notifyVideoJS);
  56. };
  57. /**
  58. * Notify video.js of new nodes.
  59. *
  60. * @param {Event} e The event.
  61. */
  62. const notifyVideoJS = e => {
  63. const nodes = jQuery(e.detail.nodes);
  64. const selector = '.mediaplugin_videojs';
  65. const langStrings = getLanguageJson();
  66. // Find the descendants matching the expected parent of the audio and video
  67. // tags. Then also addBack the nodes matching the same selector. Finally,
  68. // we find the audio and video tags contained in those parents. Kind thanks
  69. // to jQuery for the simplicity.
  70. nodes.find(selector)
  71. .addBack(selector)
  72. .find('audio, video').each((index, element) => {
  73. const id = jQuery(element).attr('id');
  74. const config = jQuery(element).data('setup-lazy');
  75. const modulePromises = [import('media_videojs/video-lazy')];
  76. if (config.techOrder && config.techOrder.indexOf('youtube') !== -1) {
  77. // Add YouTube to the list of modules we require.
  78. modulePromises.push(import('media_videojs/Youtube-lazy'));
  79. }
  80. if (config.techOrder && config.techOrder.indexOf('OgvJS') !== -1) {
  81. config.ogvjs = {
  82. worker: true,
  83. wasm: true,
  84. base: Config.wwwroot + '/media/player/videojs/ogvloader.php/' + Config.jsrev + '/'
  85. };
  86. // Add Ogv.JS to the list of modules we require.
  87. modulePromises.push(import('media_videojs/videojs-ogvjs-lazy'));
  88. }
  89. Promise.all([langStrings, ...modulePromises])
  90. .then(([langJson, videojs]) => {
  91. if (firstLoad) {
  92. videojs.addLanguage(language, langJson);
  93. firstLoad = false;
  94. }
  95. videojs(id, config);
  96. return;
  97. })
  98. .catch(Notification.exception);
  99. });
  100. };
  101. /**
  102. * Returns the json object of the language strings to be used in the player.
  103. *
  104. * @returns {Promise}
  105. */
  106. const getLanguageJson = () => {
  107. if (langStringCache) {
  108. return Promise.resolve(langStringCache);
  109. }
  110. const cacheKey = `media_videojs/${language}`;
  111. const rawCacheContent = LocalStorage.get(cacheKey);
  112. if (rawCacheContent) {
  113. const cacheContent = JSON.parse(rawCacheContent);
  114. langStringCache = cacheContent;
  115. return Promise.resolve(langStringCache);
  116. }
  117. const request = {
  118. methodname: 'media_videojs_get_language',
  119. args: {
  120. lang: language,
  121. },
  122. };
  123. return Ajax.call([request])[0]
  124. .then(langStringData => {
  125. LocalStorage.set(cacheKey, langStringData);
  126. return langStringData;
  127. })
  128. .then(result => JSON.parse(result))
  129. .then(langStrings => {
  130. langStringCache = langStrings;
  131. return langStrings;
  132. });
  133. };