Skip to content

Commit

Permalink
MDL-57608 media_videojs: load youtube module only when requested
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Jan 12, 2017
1 parent 8ed0851 commit 357e965
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion media/player/videojs/amd/build/loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions media/player/videojs/amd/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'media_videojs/video', 'core/event'], function($, videojs, Event) {
define(['jquery', 'core/event'], function($, Event) {

/**
* Stores the method we need to execute on the first load of videojs module.
*/
var onload;

/**
* 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
*/
var setUp = function() {
var setUp = function(executeonload) {
onload = executeonload;
// Notify Video.js about the nodes already present on the page.
notifyVideoJS(null, $('body'));
// We need to call popover automatically if nodes are added to the page later.
Event.getLegacyEvents().done(function(events) {
$(document).on(events.FILTER_CONTENT_UPDATED, notifyVideoJS);
Expand All @@ -53,9 +62,20 @@ define(['jquery', 'media_videojs/video', 'core/event'], function($, videojs, Eve
.addBack(selector)
.find('audio, video').each(function() {
var id = $(this).attr('id'),
config = $(this).data('setup');
config = $(this).data('setup'),
modules = ['media_videojs/video'];

videojs(id, config);
if (config.techOrder && config.techOrder.indexOf('youtube') !== -1) {
// Add YouTube to the list of modules we require.
modules.push('media_videojs/Youtube');
}
require(modules, function(videojs) {
if (onload) {
onload(videojs);
onload = null;
}
videojs(id, config);
});
});
};

Expand Down
16 changes: 7 additions & 9 deletions media/player/videojs/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,19 @@ protected function get_regex_youtube() {
*/
public function setup($page) {

// Load core video JS.
// 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(current_language());
$page->requires->js_amd_inline(<<<EOT
require(["media_videojs/video"], function(videojs) {
$contents
require(["media_videojs/loader"], function(loader) {
loader.setUp(function(videojs) {
$contents
});
});
EOT
);

// Load Youtube JS.
$page->requires->js_amd_inline('require(["media_videojs/Youtube"])');

// Load dynamic loader.
$page->requires->js_call_amd('media_videojs/loader', 'setUp');
}
}

0 comments on commit 357e965

Please sign in to comment.