Skip to content

Commit

Permalink
MDL-57101 media_videojs: Set-up player on dynamically loaded content
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Nov 29, 2016
1 parent 3eabedb commit a174f24
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 38 deletions.
18 changes: 18 additions & 0 deletions filter/mediaplugin/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class filter_mediaplugin extends moodle_text_filter {
/** @var bool True if currently filtering trusted text */
private $trusted;

/**
* Setup page with filter requirements and other prepare stuff.
*
* @param moodle_page $page The page we are going to add requirements to.
* @param context $context The context which contents are going to be filtered.
*/
public function setup($page, $context) {
// This only requires execution once per request.
static $jsinitialised = false;
if ($jsinitialised) {
return;
}
$jsinitialised = true;

$mediamanager = core_media_manager::instance();
$mediamanager->setup($page);
}

public function filter($text, array $options = array()) {
global $CFG, $PAGE;

Expand Down
14 changes: 14 additions & 0 deletions media/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ public static function instance() {
return self::$instance;
}

/**
* Setup page requirements.
*
* This should must only be called once per page request.
*
* @param moodle_page $page The page we are going to add requirements to.
*/
public function setup($page) {
$players = $this->get_players();
foreach ($players as $player) {
$player->setup($page);
}
}

/**
* Resets cached singleton instance. To be used after $CFG->media_plugins_sortorder is modified
*/
Expand Down
11 changes: 11 additions & 0 deletions media/classes/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,15 @@ protected static function pick_video_size(&$width, &$height) {
$height = $CFG->media_default_height;
}
}

/**
* Setup page requirements.
*
* @param moodle_page $page The page we are going to add requirements to.
* @since Moodle 3.2
*/
public function setup($page) {
// Override is need be.
}

}
56 changes: 56 additions & 0 deletions media/player/videojs/amd/src/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Video JS loader.
*
* This takes care of applying the filter on content which was dynamically loaded.
*
* @package media_videojs
* @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'], function($, videojs) {

/**
* Set-up.
*
* Adds the listener for the event to then notify video.js.
*/
var setUp = function() {
$(document).on(M.core.event.FILTER_CONTENT_UPDATED, notifyVideoJS);
};

/**
* Notify video.js of new nodes.
*
* @param {Event} e The event.
* @param {NodeList} nodes List of new nodes.
*/
var notifyVideoJS = function(e, nodes) {
nodes.find('.mediaplugin_videojs audio, .mediaplugin_videojs video')
.each(function() {
var id = $(this).attr('id'),
config = $(this).data('setup');

videojs(id, config);
});
};

return {
setUp: setUp
};

});
64 changes: 26 additions & 38 deletions media/player/videojs/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ public function embed($urls, $name, $width, $height, $options) {
$title = $this->get_name($name, $urls);
$title = preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title);

// Ensure JS is loaded. This will also load language strings and populate $this->language with the current language.
$this->load_amd_module();
if ($this->youtube) {
$this->load_amd_module('Youtube');
$datasetup[] = '"techOrder": ["youtube"]';
$datasetup[] = '"sources": [{"type": "video/youtube", "src":"' . $urls[0] . '"}]';
$sources = ''; // Do not specify <source> tags - it may confuse browser.
Expand Down Expand Up @@ -132,10 +129,9 @@ public function embed($urls, $name, $width, $height, $options) {
}

// Attributes for the video/audio tag.
static $playercounter = 1;
$attributes = [
'data-setup' => '{' . join(', ', $datasetup) . '}',
'id' => 'id_videojs_' . ($playercounter++),
'id' => 'id_videojs_' . uniqid(),
'class' => get_config('media_videojs', $isaudio ? 'audiocssclass' : 'videocssclass')
];

Expand Down Expand Up @@ -246,39 +242,6 @@ public function get_rank() {
return 2000;
}

/**
* Makes sure the player is loaded on the page and the language strings are set.
* We only need to do it once on a page.
*
* @param string $module module to load
*/
protected function load_amd_module($module = 'video') {
global $PAGE;
if (array_key_exists($module, $this->loadedonpage) && $PAGE === $this->loadedonpage[$module]) {
// This is exactly the same page object we used last time.
// Prevent from calling multiple times on the same page.
return;
}

$contents = '';
$alias = '';
if ($module === 'video') {
$alias = 'videojs';
$path = new moodle_url('/media/player/videojs/videojs/video-js.swf');
$contents .= $alias . '.options.flash.swf = "' . $path . '";' . "\n";
$contents .= $this->find_language(current_language());
}

$PAGE->requires->js_amd_inline(<<<EOT
require(["media_videojs/$module"], function($alias) {
$contents
});
EOT
);

$this->loadedonpage[$module] = $PAGE;
}

/**
* Tries to match the current language to existing language files
*
Expand Down Expand Up @@ -354,4 +317,29 @@ protected function get_regex_youtube() {
$middle = '([a-z0-9\-_]+)';
return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
}

/**
* Setup page requirements.
*
* @param moodle_page $page The page we are going to add requirements to.
*/
public function setup($page) {

// Load core video JS.
$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
});
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 a174f24

Please sign in to comment.