diff --git a/media/player/videojs/amd/build/loader.min.js b/media/player/videojs/amd/build/loader.min.js index f49e4368b38..df21832ca78 100644 --- a/media/player/videojs/amd/build/loader.min.js +++ b/media/player/videojs/amd/build/loader.min.js @@ -1,2 +1,2 @@ -define ("media_videojs/loader",["exports","core/event","jquery"],function(a,b,c){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.setUp=void 0;b=d(b);c=d(c);var l="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function d(a){return a&&a.__esModule?a:{default:a}}function e(a,b){return k(a)||j(a,b)||g(a,b)||f()}function f(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function g(a,b){if(!a)return;if("string"==typeof a)return h(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return h(a,b)}function h(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);ca.length)b=a.length;for(var c=0,d=Array(b);c.\n\n/**\n * Video JS loader.\n *\n * This takes care of applying the filter on content which was dynamically loaded.\n *\n * @module media_videojs/loader\n * @package media_videojs\n * @copyright 2016 Frédéric Massart - FMCorz.net\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Event from 'core/event';\nimport jQuery from 'jquery';\n\n/**\n * Stores the method we need to execute on the first load of videojs module.\n */\nlet onload;\n\n/**\n * Set-up.\n *\n * Adds the listener for the event to then notify video.js.\n * @param {Function} executeonload function to execute when media_videojs/video is loaded\n */\nexport const setUp = (executeonload) => {\n onload = executeonload;\n // Notify Video.js about the nodes already present on the page.\n notifyVideoJS(null, jQuery('body'));\n // We need to call popover automatically if nodes are added to the page later.\n Event.getLegacyEvents().done((events) => {\n jQuery(document).on(events.FILTER_CONTENT_UPDATED, notifyVideoJS);\n });\n};\n\n/**\n * Notify video.js of new nodes.\n *\n * @param {Event} e The event.\n * @param {NodeList} nodes List of new nodes.\n */\nconst notifyVideoJS = (e, nodes) => {\n const selector = '.mediaplugin_videojs';\n\n // Find the descendants matching the expected parent of the audio and video\n // tags. Then also addBack the nodes matching the same selector. Finally,\n // we find the audio and video tags contained in those parents. Kind thanks\n // to jQuery for the simplicity.\n nodes.find(selector)\n .addBack(selector)\n .find('audio, video').each((index, element) => {\n const id = jQuery(element).attr('id');\n const config = jQuery(element).data('setup-lazy');\n const modulePromises = [import('media_videojs/video-lazy')];\n\n if (config.techOrder && config.techOrder.indexOf('youtube') !== -1) {\n // Add YouTube to the list of modules we require.\n modulePromises.push(import('media_videojs/Youtube-lazy'));\n }\n if (config.techOrder && config.techOrder.indexOf('flash') !== -1) {\n // Add Flash to the list of modules we require.\n modulePromises.push(import('media_videojs/videojs-flash-lazy'));\n }\n Promise.all(modulePromises).then(([videojs]) => {\n if (onload) {\n onload(videojs);\n onload = null;\n }\n videojs(id, config);\n return;\n }).catch(Notification.exception);\n });\n};\n"],"file":"loader.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/loader.js"],"names":["firstLoad","language","setUp","lang","notifyVideoJS","Event","getLegacyEvents","done","events","document","on","FILTER_CONTENT_UPDATED","e","nodes","langStrings","getLanguageJson","find","addBack","each","index","element","id","attr","config","data","modulePromises","techOrder","indexOf","push","Promise","all","then","langJson","videojs","options","flash","swf","Config","wwwroot","addLanguage","catch","Notification","exception","cached","JSON","parse","LocalStorage","get","resolve","request","methodname","args","Ajax","call","result","set","stringify"],"mappings":"wOA0BA,OACA,OACA,OACA,OACA,OACA,O,6nCAKIA,CAAAA,C,CAKAC,C,CAQSC,CAAK,CAAG,SAACC,CAAD,CAAU,CAC3BF,CAAQ,CAAGE,CAAX,CACAH,CAAS,GAAT,CAEAI,CAAa,CAAC,IAAD,CAAO,cAAO,MAAP,CAAP,CAAb,CAEAC,UAAMC,eAAN,GAAwBC,IA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AXD,CAWOC,UAAaC,SAXpB,CAYH,CA3BL,CA4BH,C,CAOK3B,CAAe,CAAG,UAAM,CAC1B,GAAM4B,CAAAA,CAAM,CAAGC,IAAI,CAACC,KAAL,CAAWC,UAAaC,GAAb,CAAiB,eAAjB,GAAqC,IAAhD,CAAf,CACA,GAAI9C,CAAQ,GAAI0C,CAAAA,CAAhB,CAAwB,CACpB,MAAOd,CAAAA,OAAO,CAACmB,OAAR,CAAgBL,CAAM,CAAC1C,CAAD,CAAtB,CACV,CAED,GAAMgD,CAAAA,CAAO,CAAG,CACZC,UAAU,CAAE,4BADA,CAEZC,IAAI,CAAE,CACFhD,IAAI,CAAEF,CADJ,CAFM,CAAhB,CAMA,MAAOmD,WAAKC,IAAL,CAAU,CAACJ,CAAD,CAAV,EAAqB,CAArB,EAAwBlB,IAAxB,CAA6B,SAAAuB,CAAM,CAAI,CAC1CX,CAAM,CAAC1C,CAAD,CAAN,CAAmB2C,IAAI,CAACC,KAAL,CAAWS,CAAX,CAAnB,CACAR,UAAaS,GAAb,CAAiB,eAAjB,CAAkCX,IAAI,CAACY,SAAL,CAAeb,CAAf,CAAlC,EAEA,MAAOA,CAAAA,CAAM,CAAC1C,CAAD,CAChB,CALM,CAMV,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Video JS loader.\n *\n * This takes care of applying the filter on content which was dynamically loaded.\n *\n * @module media_videojs/loader\n * @package media_videojs\n * @copyright 2016 Frédéric Massart - FMCorz.net\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Config from 'core/config';\nimport Event from 'core/event';\nimport jQuery from 'jquery';\nimport Ajax from 'core/ajax';\nimport LocalStorage from 'core/localstorage';\nimport Notification from 'core/notification';\n\n/**\n * Whether this is the first load of videojs module.\n */\nlet firstLoad;\n\n/**\n * The language that is used in the player\n */\nlet language;\n\n/**\n * Set-up.\n *\n * Adds the listener for the event to then notify video.js.\n * @param {string} lang Language to be used in the player\n */\nexport const setUp = (lang) => {\n language = lang;\n firstLoad = true;\n // Notify Video.js about the nodes already present on the page.\n notifyVideoJS(null, jQuery('body'));\n // We need to call popover automatically if nodes are added to the page later.\n Event.getLegacyEvents().done((events) => {\n jQuery(document).on(events.FILTER_CONTENT_UPDATED, notifyVideoJS);\n });\n};\n\n/**\n * Notify video.js of new nodes.\n *\n * @param {Event} e The event.\n * @param {NodeList} nodes List of new nodes.\n */\nconst notifyVideoJS = (e, nodes) => {\n const selector = '.mediaplugin_videojs';\n const langStrings = getLanguageJson();\n\n // Find the descendants matching the expected parent of the audio and video\n // tags. Then also addBack the nodes matching the same selector. Finally,\n // we find the audio and video tags contained in those parents. Kind thanks\n // to jQuery for the simplicity.\n nodes.find(selector)\n .addBack(selector)\n .find('audio, video').each((index, element) => {\n const id = jQuery(element).attr('id');\n const config = jQuery(element).data('setup-lazy');\n const modulePromises = [import('media_videojs/video-lazy')];\n\n if (config.techOrder && config.techOrder.indexOf('youtube') !== -1) {\n // Add YouTube to the list of modules we require.\n modulePromises.push(import('media_videojs/Youtube-lazy'));\n }\n if (config.techOrder && config.techOrder.indexOf('flash') !== -1) {\n // Add Flash to the list of modules we require.\n modulePromises.push(import('media_videojs/videojs-flash-lazy'));\n }\n Promise.all([langStrings, ...modulePromises])\n .then(([langJson, videojs]) => {\n if (firstLoad) {\n videojs.options.flash.swf = `${Config.wwwroot}/media/player/videojs/videojs/video-js.swf`;\n videojs.addLanguage(language, langJson);\n\n firstLoad = false;\n }\n videojs(id, config);\n return;\n })\n .catch(Notification.exception);\n });\n};\n\n/**\n * Returns the json object of the language strings to be used in the player.\n *\n * @returns {Promise}\n */\nconst getLanguageJson = () => {\n const cached = JSON.parse(LocalStorage.get('media_videojs') || '{}');\n if (language in cached) {\n return Promise.resolve(cached[language]);\n }\n\n const request = {\n methodname: 'media_videojs_get_language',\n args: {\n lang: language\n },\n };\n return Ajax.call([request])[0].then(result => {\n cached[language] = JSON.parse(result);\n LocalStorage.set('media_videojs', JSON.stringify(cached));\n\n return cached[language];\n });\n};\n"],"file":"loader.min.js"} \ No newline at end of file diff --git a/media/player/videojs/amd/src/loader.js b/media/player/videojs/amd/src/loader.js index 933b0628739..a8c6f55e442 100644 --- a/media/player/videojs/amd/src/loader.js +++ b/media/player/videojs/amd/src/loader.js @@ -24,22 +24,32 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +import Config from 'core/config'; import Event from 'core/event'; import jQuery from 'jquery'; +import Ajax from 'core/ajax'; +import LocalStorage from 'core/localstorage'; +import Notification from 'core/notification'; /** - * Stores the method we need to execute on the first load of videojs module. + * Whether this is the first load of videojs module. */ -let onload; +let firstLoad; + +/** + * The language that is used in the player + */ +let language; /** * Set-up. * * Adds the listener for the event to then notify video.js. - * @param {Function} executeonload function to execute when media_videojs/video is loaded + * @param {string} lang Language to be used in the player */ -export const setUp = (executeonload) => { - onload = executeonload; +export const setUp = (lang) => { + language = lang; + firstLoad = true; // Notify Video.js about the nodes already present on the page. notifyVideoJS(null, jQuery('body')); // We need to call popover automatically if nodes are added to the page later. @@ -56,6 +66,7 @@ export const setUp = (executeonload) => { */ const notifyVideoJS = (e, nodes) => { const selector = '.mediaplugin_videojs'; + const langStrings = getLanguageJson(); // Find the descendants matching the expected parent of the audio and video // tags. Then also addBack the nodes matching the same selector. Finally, @@ -76,13 +87,42 @@ const notifyVideoJS = (e, nodes) => { // Add Flash to the list of modules we require. modulePromises.push(import('media_videojs/videojs-flash-lazy')); } - Promise.all(modulePromises).then(([videojs]) => { - if (onload) { - onload(videojs); - onload = null; + Promise.all([langStrings, ...modulePromises]) + .then(([langJson, videojs]) => { + if (firstLoad) { + videojs.options.flash.swf = `${Config.wwwroot}/media/player/videojs/videojs/video-js.swf`; + videojs.addLanguage(language, langJson); + + firstLoad = false; } videojs(id, config); return; - }).catch(Notification.exception); + }) + .catch(Notification.exception); }); }; + +/** + * Returns the json object of the language strings to be used in the player. + * + * @returns {Promise} + */ +const getLanguageJson = () => { + const cached = JSON.parse(LocalStorage.get('media_videojs') || '{}'); + if (language in cached) { + return Promise.resolve(cached[language]); + } + + const request = { + methodname: 'media_videojs_get_language', + args: { + lang: language + }, + }; + return Ajax.call([request])[0].then(result => { + cached[language] = JSON.parse(result); + LocalStorage.set('media_videojs', JSON.stringify(cached)); + + return cached[language]; + }); +}; diff --git a/media/player/videojs/classes/plugin.php b/media/player/videojs/classes/plugin.php index bc11f2a4e26..7ca33e3fc03 100644 --- a/media/player/videojs/classes/plugin.php +++ b/media/player/videojs/classes/plugin.php @@ -340,33 +340,27 @@ class media_videojs_plugin extends core_media_player_native { $langfiles = get_directory_list($basedir); $candidates = []; foreach ($langfiles as $langfile) { - if (strtolower(pathinfo($langfile, PATHINFO_EXTENSION)) !== 'js') { + if (strtolower(pathinfo($langfile, PATHINFO_EXTENSION)) !== 'json') { continue; } - $lang = basename($langfile, '.js'); - if (strtolower($langfile) === $this->language . '.js') { - // Found an exact match for the language. - $js = file_get_contents($basedir . $langfile); - break; + $lang = basename($langfile, '.json'); + if (strtolower($langfile) === $this->language . '.json') { + // Found an exact match for the language. It is stored in $this->language. + return; } if (substr($this->language, 0, 2) === strtolower(substr($langfile, 0, 2))) { // Not an exact match but similar, for example "pt_br" is similar to "pt". $candidates[$lang] = $langfile; } } - if (empty($js) && $candidates) { + + if ($candidates) { // Exact match was not found, take the first candidate. $this->language = key($candidates); - $js = file_get_contents($basedir . $candidates[$this->language]); + } else { + // Could not match, use default language of video player (English). + $this->language = null; } - // Add it as a language for Video.JS. - if (!empty($js)) { - return "$js\n"; - } - - // Could not match, use default language of video player (English). - $this->language = null; - return ""; } /** @@ -434,14 +428,10 @@ class media_videojs_plugin extends core_media_player_native { // Load dynamic loader. It will scan page for videojs media and load necessary modules. // Loader will be loaded on absolutely every page, however the videojs will only be loaded // when video is present on the page or added later to it in AJAX. - $path = new moodle_url('/media/player/videojs/videojs/video-js.swf'); - $contents = 'videojs.options.flash.swf = "' . $path . '";' . "\n"; - $contents .= $this->find_language(); + $this->find_language(); $page->requires->js_amd_inline(<<language'); }); EOT );