Merge branch 'new-embed' of github.com:h5p/h5p-php-library into new-embed
commit
1fef537c8c
273
h5p.classes.php
273
h5p.classes.php
|
@ -14,6 +14,7 @@ interface H5PFrameworkInterface {
|
||||||
* - h5pVersion: The version of the H5P plugin/module
|
* - h5pVersion: The version of the H5P plugin/module
|
||||||
*/
|
*/
|
||||||
public function getPlatformInfo();
|
public function getPlatformInfo();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches a file from a remote server using HTTP GET
|
* Fetches a file from a remote server using HTTP GET
|
||||||
|
@ -770,15 +771,14 @@ class H5PValidator {
|
||||||
// When upgrading, we opnly add allready installed libraries,
|
// When upgrading, we opnly add allready installed libraries,
|
||||||
// and new dependent libraries
|
// and new dependent libraries
|
||||||
$upgrades = array();
|
$upgrades = array();
|
||||||
foreach ($libraries as &$library) {
|
foreach ($libraries as $libString => &$library) {
|
||||||
// Is this library already installed?
|
// Is this library already installed?
|
||||||
if ($this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']) !== FALSE) {
|
if ($this->h5pC->getLibraryId($library, $libString) !== FALSE) {
|
||||||
$upgrades[H5PCore::libraryToString($library)] = $library;
|
$upgrades[$libString] = $library;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while ($missingLibraries = $this->getMissingLibraries($upgrades)) {
|
while ($missingLibraries = $this->getMissingLibraries($upgrades)) {
|
||||||
foreach ($missingLibraries as $missing) {
|
foreach ($missingLibraries as $libString => $missing) {
|
||||||
$libString = H5PCore::libraryToString($missing);
|
|
||||||
$library = $libraries[$libString];
|
$library = $libraries[$libString];
|
||||||
if ($library) {
|
if ($library) {
|
||||||
$upgrades[$libString] = $library;
|
$upgrades[$libString] = $library;
|
||||||
|
@ -798,15 +798,15 @@ class H5PValidator {
|
||||||
}
|
}
|
||||||
|
|
||||||
$missingLibraries = $this->getMissingLibraries($libraries);
|
$missingLibraries = $this->getMissingLibraries($libraries);
|
||||||
foreach ($missingLibraries as $missing) {
|
foreach ($missingLibraries as $libString => $missing) {
|
||||||
if ($this->h5pF->getLibraryId($missing['machineName'], $missing['majorVersion'], $missing['minorVersion'])) {
|
if ($this->h5pC->getLibraryId($missing, $libString)) {
|
||||||
unset($missingLibraries[H5PCore::libraryToString($missing)]);
|
unset($missingLibraries[$libString]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($missingLibraries)) {
|
if (!empty($missingLibraries)) {
|
||||||
foreach ($missingLibraries as $library) {
|
foreach ($missingLibraries as $libString => $library) {
|
||||||
$this->h5pF->setErrorMessage($this->h5pF->t('Missing required library @library', array('@library' => H5PCore::libraryToString($library))));
|
$this->h5pF->setErrorMessage($this->h5pF->t('Missing required library @library', array('@library' => $libString)));
|
||||||
}
|
}
|
||||||
if (!$this->h5pF->mayUpdateLibraries()) {
|
if (!$this->h5pF->mayUpdateLibraries()) {
|
||||||
$this->h5pF->setInfoMessage($this->h5pF->t("Note that the libraries may exist in the file you uploaded, but you're not allowed to upload new libraries. Contact the site administrator about this."));
|
$this->h5pF->setInfoMessage($this->h5pF->t("Note that the libraries may exist in the file you uploaded, but you're not allowed to upload new libraries. Contact the site administrator about this."));
|
||||||
|
@ -937,8 +937,9 @@ class H5PValidator {
|
||||||
private function getMissingDependencies($dependencies, $libraries) {
|
private function getMissingDependencies($dependencies, $libraries) {
|
||||||
$missing = array();
|
$missing = array();
|
||||||
foreach ($dependencies as $dependency) {
|
foreach ($dependencies as $dependency) {
|
||||||
if (!isset($libraries[H5PCore::libraryToString($dependency)])) {
|
$libString = H5PCore::libraryToString($dependency);
|
||||||
$missing[H5PCore::libraryToString($dependency)] = $dependency;
|
if (!isset($libraries[$libString])) {
|
||||||
|
$missing[$libString] = $dependency;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $missing;
|
return $missing;
|
||||||
|
@ -1238,75 +1239,16 @@ class H5PStorage {
|
||||||
* TRUE if one or more libraries were updated
|
* TRUE if one or more libraries were updated
|
||||||
* FALSE otherwise
|
* FALSE otherwise
|
||||||
*/
|
*/
|
||||||
public function savePackage($content = NULL, $contentMainId = NULL, $skipContent = FALSE, $upgradeOnly = FALSE) {
|
public function savePackage($content = NULL, $contentMainId = NULL, $skipContent = FALSE) {
|
||||||
// Save the libraries we processed during validation
|
if ($this->h5pF->mayUpdateLibraries()) {
|
||||||
$library_saved = FALSE;
|
// Save the libraries we processed during validation
|
||||||
$upgradedLibsCount = 0;
|
$this->saveLibraries();
|
||||||
$mayUpdateLibraries = $this->h5pF->mayUpdateLibraries();
|
|
||||||
|
|
||||||
foreach ($this->h5pC->librariesJsonData as &$library) {
|
|
||||||
$libraryId = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']);
|
|
||||||
$library['saveDependencies'] = TRUE;
|
|
||||||
|
|
||||||
if (!$libraryId) {
|
|
||||||
$new = TRUE;
|
|
||||||
}
|
|
||||||
elseif ($this->h5pF->isPatchedLibrary($library)) {
|
|
||||||
$new = FALSE;
|
|
||||||
$library['libraryId'] = $libraryId;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$library['libraryId'] = $libraryId;
|
|
||||||
// We already have the same or a newer version of this library
|
|
||||||
$library['saveDependencies'] = FALSE;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$mayUpdateLibraries) {
|
|
||||||
// This shouldn't happen, but just to be safe...
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->h5pF->saveLibraryData($library, $new);
|
|
||||||
|
|
||||||
$libraries_path = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR . 'libraries';
|
|
||||||
if (!is_dir($libraries_path)) {
|
|
||||||
mkdir($libraries_path, 0777, true);
|
|
||||||
}
|
|
||||||
$destination_path = $libraries_path . DIRECTORY_SEPARATOR . H5PCore::libraryToString($library, TRUE);
|
|
||||||
H5PCore::deleteFileTree($destination_path);
|
|
||||||
rename($library['uploadDirectory'], $destination_path);
|
|
||||||
|
|
||||||
$library_saved = TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->h5pC->librariesJsonData as &$library) {
|
|
||||||
if ($library['saveDependencies']) {
|
|
||||||
$this->h5pF->deleteLibraryDependencies($library['libraryId']);
|
|
||||||
if (isset($library['preloadedDependencies'])) {
|
|
||||||
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['preloadedDependencies'], 'preloaded');
|
|
||||||
}
|
|
||||||
if (isset($library['dynamicDependencies'])) {
|
|
||||||
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['dynamicDependencies'], 'dynamic');
|
|
||||||
}
|
|
||||||
if (isset($library['editorDependencies'])) {
|
|
||||||
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['editorDependencies'], 'editor');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure libraries dependencies, parameter filtering and export files gets regenerated for all content who uses this library.
|
|
||||||
$this->h5pF->clearFilteredParameters($library['libraryId']);
|
|
||||||
|
|
||||||
$upgradedLibsCount++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$skipContent) {
|
if (!$skipContent) {
|
||||||
$current_path = $this->h5pF->getUploadedH5pFolderPath() . DIRECTORY_SEPARATOR . 'content';
|
$basePath = $this->h5pF->getUploadedH5pFolderPath();
|
||||||
|
$current_path = $basePath . DIRECTORY_SEPARATOR . 'content';
|
||||||
|
|
||||||
// Find out which libraries are used by this package/content
|
|
||||||
$librariesInUse = array();
|
|
||||||
$nextWeight = $this->h5pC->findLibraryDependencies($librariesInUse, $this->h5pC->mainJsonData);
|
|
||||||
|
|
||||||
// Save content
|
// Save content
|
||||||
if ($content === NULL) {
|
if ($content === NULL) {
|
||||||
$content = array();
|
$content = array();
|
||||||
|
@ -1314,7 +1256,16 @@ class H5PStorage {
|
||||||
if (!is_array($content)) {
|
if (!is_array($content)) {
|
||||||
$content = array('id' => $content);
|
$content = array('id' => $content);
|
||||||
}
|
}
|
||||||
$content['library'] = $librariesInUse['preloaded-' . $this->h5pC->mainJsonData['mainLibrary']]['library'];
|
|
||||||
|
// Find main library version
|
||||||
|
foreach ($this->h5pC->mainJsonData['preloadedDependencies'] as $dep) {
|
||||||
|
if ($dep['machineName'] === $this->h5pC->mainJsonData['mainLibrary']) {
|
||||||
|
$dep['libraryId'] = $this->h5pC->getLibraryId($dep);
|
||||||
|
$content['library'] = $dep;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$content['params'] = file_get_contents($current_path . DIRECTORY_SEPARATOR . 'content.json');
|
$content['params'] = file_get_contents($current_path . DIRECTORY_SEPARATOR . 'content.json');
|
||||||
$contentId = $this->h5pC->saveContent($content, $contentMainId);
|
$contentId = $this->h5pC->saveContent($content, $contentMainId);
|
||||||
$this->contentId = $contentId;
|
$this->contentId = $contentId;
|
||||||
|
@ -1326,22 +1277,116 @@ class H5PStorage {
|
||||||
|
|
||||||
// Move the content folder
|
// Move the content folder
|
||||||
$destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId;
|
$destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId;
|
||||||
@rename($current_path, $destination_path);
|
$this->h5pC->copyFileTree($current_path, $destination_path);
|
||||||
|
|
||||||
// Save the content library dependencies
|
// Remove temp content folder
|
||||||
$this->h5pF->saveLibraryUsage($contentId, $librariesInUse);
|
H5PCore::deleteFileTree($basePath);
|
||||||
H5PCore::deleteFileTree($this->h5pF->getUploadedH5pFolderPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update supported library list if neccessary:
|
// Update supported library list if neccessary:
|
||||||
$this->h5pC->validateLibrarySupport(TRUE);
|
$this->h5pC->validateLibrarySupport(TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
if ($upgradeOnly) {
|
/**
|
||||||
// TODO - support translation
|
* Helps savePackage.
|
||||||
$this->h5pF->setInfoMessage($this->h5pF->t('@num libraries were upgraded!', array('@num' => $upgradedLibsCount)));
|
*
|
||||||
|
* @return int Number of libraries saved
|
||||||
|
*/
|
||||||
|
private function saveLibraries() {
|
||||||
|
// Keep track of the number of libraries that have been saved
|
||||||
|
$newOnes = 0;
|
||||||
|
$oldOnes = 0;
|
||||||
|
|
||||||
|
// Find libraries directory and make sure it exists
|
||||||
|
$libraries_path = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR . 'libraries';
|
||||||
|
if (!is_dir($libraries_path)) {
|
||||||
|
mkdir($libraries_path, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $library_saved;
|
// Go through libraries that came with this package
|
||||||
|
foreach ($this->h5pC->librariesJsonData as $libString => &$library) {
|
||||||
|
// Find local library identifier
|
||||||
|
$libraryId = $this->h5pC->getLibraryId($library, $libString);
|
||||||
|
|
||||||
|
// Assume new library
|
||||||
|
$new = TRUE;
|
||||||
|
if ($libraryId) {
|
||||||
|
// Found old library
|
||||||
|
$library['libraryId'] = $libraryId;
|
||||||
|
|
||||||
|
if ($this->h5pF->isPatchedLibrary($library)) {
|
||||||
|
// This is a newer version than ours. Upgrade!
|
||||||
|
$new = FALSE;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$library['saveDependencies'] = FALSE;
|
||||||
|
// This is an older version, no need to save.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indicate that the dependencies of this library should be saved.
|
||||||
|
$library['saveDependencies'] = TRUE;
|
||||||
|
|
||||||
|
// Save library meta data
|
||||||
|
$this->h5pF->saveLibraryData($library, $new);
|
||||||
|
|
||||||
|
// Make sure destination dir is free
|
||||||
|
$destination_path = $libraries_path . DIRECTORY_SEPARATOR . H5PCore::libraryToString($library, TRUE);
|
||||||
|
H5PCore::deleteFileTree($destination_path);
|
||||||
|
|
||||||
|
// Move library folder
|
||||||
|
$this->h5pC->copyFileTree($library['uploadDirectory'], $destination_path);
|
||||||
|
H5PCore::deleteFileTree($library['uploadDirectory']);
|
||||||
|
|
||||||
|
if ($new) {
|
||||||
|
$newOnes++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$oldOnes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go through the libraries again to save dependencies.
|
||||||
|
foreach ($this->h5pC->librariesJsonData as &$library) {
|
||||||
|
if (!$library['saveDependencies']) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Should the table be locked for this operation?
|
||||||
|
|
||||||
|
// Remove any old dependencies
|
||||||
|
$this->h5pF->deleteLibraryDependencies($library['libraryId']);
|
||||||
|
|
||||||
|
// Insert the different new ones
|
||||||
|
if (isset($library['preloadedDependencies'])) {
|
||||||
|
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['preloadedDependencies'], 'preloaded');
|
||||||
|
}
|
||||||
|
if (isset($library['dynamicDependencies'])) {
|
||||||
|
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['dynamicDependencies'], 'dynamic');
|
||||||
|
}
|
||||||
|
if (isset($library['editorDependencies'])) {
|
||||||
|
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library['editorDependencies'], 'editor');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure libraries dependencies, parameter filtering and export files gets regenerated for all content who uses this library.
|
||||||
|
$this->h5pF->clearFilteredParameters($library['libraryId']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tell the user what we've done.
|
||||||
|
if ($newOnes && $oldOnes) {
|
||||||
|
$message = $this->h5pF->t('Added %new new H5P libraries and updated %old old.', array('%new' => $newOnes, '%old' => $oldOnes));
|
||||||
|
}
|
||||||
|
elseif ($newOnes) {
|
||||||
|
$message = $this->h5pF->t('Added %new new H5P libraries.', array('%new' => $newOnes));
|
||||||
|
}
|
||||||
|
elseif ($oldOnes) {
|
||||||
|
$message = $this->h5pF->t('Updated %old H5P libraries.', array('%old' => $oldOnes));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($message)) {
|
||||||
|
$this->h5pF->setInfoMessage($message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1546,6 +1591,8 @@ class H5PCore {
|
||||||
'js/jquery.js',
|
'js/jquery.js',
|
||||||
'js/h5p.js',
|
'js/h5p.js',
|
||||||
'js/h5p-event-dispatcher.js',
|
'js/h5p-event-dispatcher.js',
|
||||||
|
'js/h5p-x-api-event.js',
|
||||||
|
'js/h5p-x-api.js',
|
||||||
);
|
);
|
||||||
public static $adminScripts = array(
|
public static $adminScripts = array(
|
||||||
'js/jquery.js',
|
'js/jquery.js',
|
||||||
|
@ -1839,7 +1886,7 @@ class H5PCore {
|
||||||
* @param array $library To find all dependencies for.
|
* @param array $library To find all dependencies for.
|
||||||
* @param int $nextWeight An integer determining the order of the libraries
|
* @param int $nextWeight An integer determining the order of the libraries
|
||||||
* when they are loaded
|
* when they are loaded
|
||||||
* @param bool $editor Used interally to force all preloaded sub dependencies
|
* @param bool $editor Used interally to force all preloaded sub dependencies
|
||||||
* of an editor dependecy to be editor dependencies.
|
* of an editor dependecy to be editor dependencies.
|
||||||
*/
|
*/
|
||||||
public function findLibraryDependencies(&$dependencies, $library, $nextWeight = 1, $editor = FALSE) {
|
public function findLibraryDependencies(&$dependencies, $library, $nextWeight = 1, $editor = FALSE) {
|
||||||
|
@ -1941,7 +1988,7 @@ class H5PCore {
|
||||||
|
|
||||||
@mkdir($destination);
|
@mkdir($destination);
|
||||||
while (false !== ($file = readdir($dir))) {
|
while (false !== ($file = readdir($dir))) {
|
||||||
if (($file != '.') && ($file != '..')) {
|
if (($file != '.') && ($file != '..') && $file != '.git' && $file != '.gitignore') {
|
||||||
if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
|
if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
|
||||||
$this->copyFileTree($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
|
$this->copyFileTree($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
|
||||||
}
|
}
|
||||||
|
@ -2218,6 +2265,28 @@ class H5PCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache for getting library ids
|
||||||
|
private $libraryIdMap = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small helper for getting the library's ID.
|
||||||
|
*
|
||||||
|
* @param array $library
|
||||||
|
* @param string [$libString]
|
||||||
|
* @return int Identifier, or FALSE if non-existent
|
||||||
|
*/
|
||||||
|
public function getLibraryId($library, $libString = NULL) {
|
||||||
|
if (!$libString) {
|
||||||
|
$libString = self::libraryToString($library);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($libraryIdMap[$libString])) {
|
||||||
|
$libraryIdMap[$libString] = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $libraryIdMap[$libString];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2601,7 +2670,7 @@ class H5PContentValidator {
|
||||||
else {
|
else {
|
||||||
// If validator is not found, something exists in content that does
|
// If validator is not found, something exists in content that does
|
||||||
// not have a corresponding semantics field. Remove it.
|
// not have a corresponding semantics field. Remove it.
|
||||||
$this->h5pF->setErrorMessage($this->h5pF->t('H5P internal error: no validator exists for @key', array('@key' => $key)));
|
// $this->h5pF->setErrorMessage($this->h5pF->t('H5P internal error: no validator exists for @key', array('@key' => $key)));
|
||||||
unset($group->$key);
|
unset($group->$key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2611,7 +2680,7 @@ class H5PContentValidator {
|
||||||
if (!(isset($field->optional) && $field->optional)) {
|
if (!(isset($field->optional) && $field->optional)) {
|
||||||
// Check if field is in group.
|
// Check if field is in group.
|
||||||
if (! property_exists($group, $field->name)) {
|
if (! property_exists($group, $field->name)) {
|
||||||
$this->h5pF->setErrorMessage($this->h5pF->t('No value given for mandatory field ' . $field->name));
|
//$this->h5pF->setErrorMessage($this->h5pF->t('No value given for mandatory field ' . $field->name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2636,18 +2705,6 @@ class H5PContentValidator {
|
||||||
$library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']);
|
$library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']);
|
||||||
$library['semantics'] = $this->h5pC->loadLibrarySemantics($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']);
|
$library['semantics'] = $this->h5pC->loadLibrarySemantics($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']);
|
||||||
$this->libraries[$value->library] = $library;
|
$this->libraries[$value->library] = $library;
|
||||||
|
|
||||||
// Find all dependencies for this library
|
|
||||||
$depkey = 'preloaded-' . $libspec['machineName'];
|
|
||||||
if (!isset($this->dependencies[$depkey])) {
|
|
||||||
$this->dependencies[$depkey] = array(
|
|
||||||
'library' => $library,
|
|
||||||
'type' => 'preloaded'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->nextWeight = $this->h5pC->findLibraryDependencies($this->dependencies, $library, $this->nextWeight);
|
|
||||||
$this->dependencies[$depkey]['weight'] = $this->nextWeight++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$library = $this->libraries[$value->library];
|
$library = $this->libraries[$value->library];
|
||||||
|
@ -2662,6 +2719,18 @@ class H5PContentValidator {
|
||||||
$validkeys = array_merge($validkeys, $semantics->extraAttributes);
|
$validkeys = array_merge($validkeys, $semantics->extraAttributes);
|
||||||
}
|
}
|
||||||
$this->filterParams($value, $validkeys);
|
$this->filterParams($value, $validkeys);
|
||||||
|
|
||||||
|
// Find all dependencies for this library
|
||||||
|
$depkey = 'preloaded-' . $library['machineName'];
|
||||||
|
if (!isset($this->dependencies[$depkey])) {
|
||||||
|
$this->dependencies[$depkey] = array(
|
||||||
|
'library' => $library,
|
||||||
|
'type' => 'preloaded'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->nextWeight = $this->h5pC->findLibraryDependencies($this->dependencies, $library, $this->nextWeight);
|
||||||
|
$this->dependencies[$depkey]['weight'] = $this->nextWeight++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -90,7 +90,7 @@ var H5P = H5P || (function () {
|
||||||
loadContent(contentId, h5ps[i]);
|
loadContent(contentId, h5ps[i]);
|
||||||
contentId++;
|
contentId++;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return integration object
|
* Return integration object
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
/** @namespace H5P */
|
/** @namespace H5P */
|
||||||
var H5P = H5P || {};
|
var H5P = H5P || {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Event class for the EventDispatcher
|
||||||
|
* @class
|
||||||
|
*/
|
||||||
|
H5P.Event = function(type, data) {
|
||||||
|
this.type = type;
|
||||||
|
this.data = data;
|
||||||
|
};
|
||||||
|
|
||||||
H5P.EventDispatcher = (function () {
|
H5P.EventDispatcher = (function () {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,35 +21,39 @@ H5P.EventDispatcher = (function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep track of events and listeners for each event.
|
* Keep track of listeners for each event.
|
||||||
* @private
|
* @private
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
var events = {};
|
var triggers = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add new event listener.
|
* Add new event listener.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @throws {TypeError} listener must be a function
|
* @throws {TypeError} listener - Must be a function
|
||||||
* @param {String} type Event type
|
* @param {String} type - Event type
|
||||||
* @param {Function} listener Event listener
|
* @param {Function} listener - Event listener
|
||||||
|
* @param {Function} thisArg - Optionally specify the this value when calling listener.
|
||||||
*/
|
*/
|
||||||
self.on = function (type, listener) {
|
self.on = function (type, listener, thisArg) {
|
||||||
if (!(listener instanceof Function)) {
|
if (thisArg === undefined) {
|
||||||
|
thisArg = self;
|
||||||
|
}
|
||||||
|
if (typeof listener !== 'function') {
|
||||||
throw TypeError('listener must be a function');
|
throw TypeError('listener must be a function');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger event before adding to avoid recursion
|
// Trigger event before adding to avoid recursion
|
||||||
self.trigger('newListener', type, listener);
|
self.trigger('newListener', {'type': type, 'listener': listener});
|
||||||
|
|
||||||
if (!events[type]) {
|
if (!triggers[type]) {
|
||||||
// First
|
// First
|
||||||
events[type] = [listener];
|
triggers[type] = [{'listener': listener, 'thisArg': thisArg}];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Append
|
// Append
|
||||||
events[type].push(listener);
|
triggers[type].push({'listener': listener, 'thisArg': thisArg});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,21 +61,25 @@ H5P.EventDispatcher = (function () {
|
||||||
* Add new event listener that will be fired only once.
|
* Add new event listener that will be fired only once.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @throws {TypeError} listener must be a function
|
* @throws {TypeError} listener - must be a function
|
||||||
* @param {String} type Event type
|
* @param {String} type - Event type
|
||||||
* @param {Function} listener Event listener
|
* @param {Function} listener - Event listener
|
||||||
|
* @param {Function} thisArg - Optionally specify the this value when calling listener.
|
||||||
*/
|
*/
|
||||||
self.once = function (type, listener) {
|
self.once = function (type, listener, thisArg) {
|
||||||
|
if (thisArg === undefined) {
|
||||||
|
thisArg = self;
|
||||||
|
}
|
||||||
if (!(listener instanceof Function)) {
|
if (!(listener instanceof Function)) {
|
||||||
throw TypeError('listener must be a function');
|
throw TypeError('listener must be a function');
|
||||||
}
|
}
|
||||||
|
|
||||||
var once = function () {
|
var once = function (event) {
|
||||||
self.off(type, once);
|
self.off(event, once);
|
||||||
listener.apply(self, arguments);
|
listener.apply(thisArg, event);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.on(type, once);
|
self.on(type, once, thisArg);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,80 +87,66 @@ H5P.EventDispatcher = (function () {
|
||||||
* If no listener is specified, all listeners will be removed.
|
* If no listener is specified, all listeners will be removed.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @throws {TypeError} listener must be a function
|
* @throws {TypeError} listener - must be a function
|
||||||
* @param {String} type Event type
|
* @param {String} type - Event type
|
||||||
* @param {Function} [listener] Event listener
|
* @param {Function} listener - Event listener
|
||||||
*/
|
*/
|
||||||
self.off = function (type, listener) {
|
self.off = function (type, listener) {
|
||||||
if (listener !== undefined && !(listener instanceof Function)) {
|
if (listener !== undefined && !(listener instanceof Function)) {
|
||||||
throw TypeError('listener must be a function');
|
throw TypeError('listener must be a function');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (events[type] === undefined) {
|
if (triggers[type] === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listener === undefined) {
|
if (listener === undefined) {
|
||||||
// Remove all listeners
|
// Remove all listeners
|
||||||
delete events[type];
|
delete triggers[type];
|
||||||
self.trigger('removeListener', type);
|
self.trigger('removeListener', type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find specific listener
|
// Find specific listener
|
||||||
for (var i = 0; i < events[type].length; i++) {
|
for (var i = 0; i < triggers[type].length; i++) {
|
||||||
if (events[type][i] === listener) {
|
if (triggers[type][i].listener === listener) {
|
||||||
events[type].unshift(i, 1);
|
triggers[type].unshift(i, 1);
|
||||||
self.trigger('removeListener', type, listener);
|
self.trigger('removeListener', type, {'listener': listener});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up empty arrays
|
// Clean up empty arrays
|
||||||
if (!events[type].length) {
|
if (!triggers[type].length) {
|
||||||
delete events[type];
|
delete triggers[type];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a copy of the arguments list. Skips the given number of arguments.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Array} args List of arguments
|
|
||||||
* @param {Number} skip Number of arguments to skip
|
|
||||||
* @param {Array} Copy og arguments list
|
|
||||||
*/
|
|
||||||
var getArgs = function (args, skip) {
|
|
||||||
var left = [];
|
|
||||||
for (var i = skip; i < args.length; i++) {
|
|
||||||
left.push(args[i]);
|
|
||||||
}
|
|
||||||
return left;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch event.
|
* Dispatch event.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @param {String} type Event type
|
* @param {String|Function} event - Event object or event type as string
|
||||||
* @param {...*} args
|
* @param {mixed} eventData
|
||||||
|
* Custom event data(used when event type as string is used as first
|
||||||
|
* argument
|
||||||
*/
|
*/
|
||||||
self.trigger = function (type) {
|
self.trigger = function (event, eventData) {
|
||||||
if (self.debug !== undefined) {
|
if (event === undefined) {
|
||||||
// Class has debug enabled. Log events.
|
return;
|
||||||
console.log(self.debug + ' - Firing event "' + type + '", ' + (events[type] === undefined ? 0 : events[type].length) + ' listeners.', getArgs(arguments, 1));
|
}
|
||||||
}
|
if (typeof event === 'string') {
|
||||||
|
event = new H5P.Event(event, eventData);
|
||||||
if (events[type] === undefined) {
|
}
|
||||||
|
else if (eventData !== undefined) {
|
||||||
|
event.data = eventData;
|
||||||
|
}
|
||||||
|
if (triggers[event.type] === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy all arguments except the first
|
|
||||||
var args = getArgs(arguments, 1);
|
|
||||||
|
|
||||||
// Call all listeners
|
// Call all listeners
|
||||||
for (var i = 0; i < events[type].length; i++) {
|
for (var i = 0; i < triggers[event.type].length; i++) {
|
||||||
events[type][i].apply(self, args);
|
triggers[event.type][i].listener.call(triggers[event.type][i].thisArg, event);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
var H5P = H5P || {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for xAPI events
|
||||||
|
*
|
||||||
|
* @class
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent = function() {
|
||||||
|
H5P.Event.call(this, 'xAPI', {'statement': {}});
|
||||||
|
};
|
||||||
|
|
||||||
|
H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype);
|
||||||
|
H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helperfunction to set scored result statements
|
||||||
|
*
|
||||||
|
* @param {int} score
|
||||||
|
* @param {int} maxScore
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) {
|
||||||
|
this.data.statement.result = {
|
||||||
|
'score': {
|
||||||
|
'min': 0,
|
||||||
|
'max': maxScore,
|
||||||
|
'raw': score
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helperfunction to set a verb.
|
||||||
|
*
|
||||||
|
* @param {string} verb
|
||||||
|
* Verb in short form, one of the verbs defined at
|
||||||
|
* http://adlnet.gov/expapi/verbs/
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.setVerb = function(verb) {
|
||||||
|
if (H5P.jQuery.inArray(verb, H5P.XAPIEvent.allowedXAPIVerbs) !== -1) {
|
||||||
|
this.data.statement.verb = {
|
||||||
|
'id': 'http://adlnet.gov/expapi/verbs/' + verb,
|
||||||
|
'display': {
|
||||||
|
'en-US': verb
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
H5P.error('illegal verb');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helperfunction to get the statements verb id
|
||||||
|
*
|
||||||
|
* @param {boolean} full
|
||||||
|
* if true the full verb id prefixed by http://adlnet.gov/expapi/verbs/ will be returned
|
||||||
|
* @returns {string} - Verb or null if no verb with an id has been defined
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.getVerb = function(full) {
|
||||||
|
var statement = this.data.statement;
|
||||||
|
if ('verb' in statement) {
|
||||||
|
if (full === true) {
|
||||||
|
return statement.verb;
|
||||||
|
}
|
||||||
|
return statement.verb.id.slice(31);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helperfunction to set the object part of the statement.
|
||||||
|
*
|
||||||
|
* The id is found automatically (the url to the content)
|
||||||
|
*
|
||||||
|
* @param {object} instance - the H5P instance
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.setObject = function(instance) {
|
||||||
|
if (instance.contentId) {
|
||||||
|
this.data.statement.object = {
|
||||||
|
'id': H5P.contentDatas['cid-' + instance.contentId].url,
|
||||||
|
'objectType': 'Activity',
|
||||||
|
'extensions': {
|
||||||
|
'http://h5p.org/x-api/h5p-local-content-id': instance.contentId
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Not triggered by an H5P content type...
|
||||||
|
this.data.statement.object = {
|
||||||
|
'objectType': 'Activity'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to set the actor, email and name will be added automatically
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.setActor = function() {
|
||||||
|
this.data.statement.actor = {
|
||||||
|
'name': H5P.user.name,
|
||||||
|
'mbox': 'mailto:' + H5P.user.mail,
|
||||||
|
'objectType': 'Agent'
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the max value of the result - score part of the statement
|
||||||
|
*
|
||||||
|
* @returns {int} the max score, or null if not defined
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.getMaxScore = function() {
|
||||||
|
return this.getVerifiedStatementValue(['result', 'score', 'max']);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the raw value of the result - score part of the statement
|
||||||
|
*
|
||||||
|
* @returns {int} the max score, or null if not defined
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.getScore = function() {
|
||||||
|
return this.getVerifiedStatementValue(['result', 'score', 'raw']);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Figure out if a property exists in the statement and return it
|
||||||
|
*
|
||||||
|
* @param {array} keys
|
||||||
|
* List describing the property we're looking for. For instance
|
||||||
|
* ['result', 'score', 'raw'] for result.score.raw
|
||||||
|
* @returns the value of the property if it is set, null otherwise
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.prototype.getVerifiedStatementValue = function(keys) {
|
||||||
|
var val = this.data.statement;
|
||||||
|
for (var i = 0; i < keys.length; i++) {
|
||||||
|
if (val[keys[i]] === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
val = val[keys[i]];
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of verbs defined at http://adlnet.gov/expapi/verbs/
|
||||||
|
*
|
||||||
|
* @type Array
|
||||||
|
*/
|
||||||
|
H5P.XAPIEvent.allowedXAPIVerbs = [
|
||||||
|
'answered',
|
||||||
|
'asked',
|
||||||
|
'attempted',
|
||||||
|
'attended',
|
||||||
|
'commented',
|
||||||
|
'completed',
|
||||||
|
'exited',
|
||||||
|
'experienced',
|
||||||
|
'failed',
|
||||||
|
'imported',
|
||||||
|
'initialized',
|
||||||
|
'interacted',
|
||||||
|
'launched',
|
||||||
|
'mastered',
|
||||||
|
'passed',
|
||||||
|
'preferred',
|
||||||
|
'progressed',
|
||||||
|
'registered',
|
||||||
|
'responded',
|
||||||
|
'resumed',
|
||||||
|
'scored',
|
||||||
|
'shared',
|
||||||
|
'suspended',
|
||||||
|
'terminated',
|
||||||
|
'voided'
|
||||||
|
];
|
|
@ -0,0 +1,75 @@
|
||||||
|
var H5P = H5P || {};
|
||||||
|
|
||||||
|
// Create object where external code may register and listen for H5P Events
|
||||||
|
H5P.externalDispatcher = new H5P.EventDispatcher();
|
||||||
|
|
||||||
|
if (H5P.isFramed && H5P.externalEmbed === false) {
|
||||||
|
H5P.externalDispatcher.on('xAPI', window.top.H5P.externalDispatcher.trigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventDispatcher extensions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function for triggering xAPI added to the EventDispatcher
|
||||||
|
*
|
||||||
|
* @param {string} verb - the short id of the verb we want to trigger
|
||||||
|
* @param {oject} extra - extra properties for the xAPI statement
|
||||||
|
*/
|
||||||
|
H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) {
|
||||||
|
this.trigger(this.createXAPIEventTemplate(verb, extra));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to create event templates added to the EventDispatcher
|
||||||
|
*
|
||||||
|
* Will in the future be used to add representations of the questions to the
|
||||||
|
* statements.
|
||||||
|
*
|
||||||
|
* @param {string} verb - verb id in short form
|
||||||
|
* @param {object} extra - Extra values to be added to the statement
|
||||||
|
* @returns {Function} - XAPIEvent object
|
||||||
|
*/
|
||||||
|
H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) {
|
||||||
|
var event = new H5P.XAPIEvent();
|
||||||
|
|
||||||
|
event.setActor();
|
||||||
|
event.setVerb(verb);
|
||||||
|
if (extra !== undefined) {
|
||||||
|
for (var i in extra) {
|
||||||
|
event.data.statement[i] = extra[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!('object' in event)) {
|
||||||
|
event.setObject(this);
|
||||||
|
}
|
||||||
|
return event;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to create xAPI completed events
|
||||||
|
*
|
||||||
|
* @param {int} score - will be set as the 'raw' value of the score object
|
||||||
|
* @param {int} maxScore - will be set as the "max" value of the score object
|
||||||
|
*/
|
||||||
|
H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) {
|
||||||
|
var event = this.createXAPIEventTemplate('completed');
|
||||||
|
event.setScoredResult(score, maxScore);
|
||||||
|
this.trigger(event);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal H5P function listening for xAPI completed events and stores scores
|
||||||
|
*
|
||||||
|
* @param {function} event - xAPI event
|
||||||
|
*/
|
||||||
|
H5P.xAPICompletedListener = function(event) {
|
||||||
|
var statement = event.data.statement;
|
||||||
|
if ('verb' in statement) {
|
||||||
|
if (statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') {
|
||||||
|
var score = statement.result.score.raw;
|
||||||
|
var maxScore = statement.result.score.max;
|
||||||
|
var contentId = statement.object.extensions['http://h5p.org/x-api/h5p-local-content-id'];
|
||||||
|
H5P.setFinished(contentId, score, maxScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
87
js/h5p.js
87
js/h5p.js
|
@ -8,6 +8,8 @@ H5P.isFramed = (window.self !== window.top);
|
||||||
// Useful jQuery object.
|
// Useful jQuery object.
|
||||||
H5P.$window = H5P.jQuery(window);
|
H5P.$window = H5P.jQuery(window);
|
||||||
|
|
||||||
|
H5P.instances = [];
|
||||||
|
|
||||||
// Detect if we support fullscreen, and what prefix to use.
|
// Detect if we support fullscreen, and what prefix to use.
|
||||||
if (document.documentElement.requestFullScreen) {
|
if (document.documentElement.requestFullScreen) {
|
||||||
H5P.fullScreenBrowserPrefix = '';
|
H5P.fullScreenBrowserPrefix = '';
|
||||||
|
@ -93,12 +95,16 @@ H5P.init = function () {
|
||||||
H5P.opened[contentId] = new Date();
|
H5P.opened[contentId] = new Date();
|
||||||
|
|
||||||
// Handle events when the user finishes the content. Useful for logging exercise results.
|
// Handle events when the user finishes the content. Useful for logging exercise results.
|
||||||
instance.$.on('finish', function (event) {
|
H5P.on(instance, 'finish', function (event) {
|
||||||
if (event.data !== undefined) {
|
if (event.data !== undefined) {
|
||||||
H5P.setFinished(contentId, event.data.score, event.data.maxScore, event.data.time);
|
H5P.setFinished(contentId, event.data.score, event.data.maxScore, event.data.time);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Listen for xAPI events.
|
||||||
|
H5P.on(instance, 'xAPI', H5P.xAPICompletedListener);
|
||||||
|
H5P.on(instance, 'xAPI', H5P.externalDispatcher.trigger);
|
||||||
|
|
||||||
if (H5P.isFramed)
|
if (H5P.isFramed)
|
||||||
var resizeDelay;{
|
var resizeDelay;{
|
||||||
if (H5P.externalEmbed === false) {
|
if (H5P.externalEmbed === false) {
|
||||||
|
@ -124,7 +130,7 @@ H5P.init = function () {
|
||||||
iframe.parentElement.style.height = parentHeight;
|
iframe.parentElement.style.height = parentHeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
instance.$.on('resize', function () {
|
H5P.on(instance, 'resize', function () {
|
||||||
// Use a delay to make sure iframe is resized to the correct size.
|
// Use a delay to make sure iframe is resized to the correct size.
|
||||||
clearTimeout(resizeDelay);
|
clearTimeout(resizeDelay);
|
||||||
resizeDelay = setTimeout(function () {
|
resizeDelay = setTimeout(function () {
|
||||||
|
@ -156,10 +162,10 @@ H5P.init = function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
H5P.communicator.on('resize', function () {
|
H5P.communicator.on('resize', function () {
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.$.on('resize', function () {
|
H5P.on(instance, 'resize', function () {
|
||||||
if (H5P.isFullscreen) {
|
if (H5P.isFullscreen) {
|
||||||
return; // Skip iframe resize
|
return; // Skip iframe resize
|
||||||
}
|
}
|
||||||
|
@ -184,16 +190,17 @@ H5P.init = function () {
|
||||||
H5P.jQuery(window.top).resize(function () {
|
H5P.jQuery(window.top).resize(function () {
|
||||||
if (window.parent.H5P.isFullscreen) {
|
if (window.parent.H5P.isFullscreen) {
|
||||||
// Use timeout to avoid bug in certain browsers when exiting fullscreen. Some browser will trigger resize before the fullscreenchange event.
|
// Use timeout to avoid bug in certain browsers when exiting fullscreen. Some browser will trigger resize before the fullscreenchange event.
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
H5P.instances.push(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize content.
|
// Resize content.
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Insert H5Ps that should be in iframes.
|
// Insert H5Ps that should be in iframes.
|
||||||
|
@ -325,9 +332,9 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) {
|
||||||
*/
|
*/
|
||||||
var entered = function () {
|
var entered = function () {
|
||||||
// Do not rely on window resize events.
|
// Do not rely on window resize events.
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
instance.$.trigger('focus');
|
H5P.trigger(instance, 'focus');
|
||||||
instance.$.trigger('enterFullScreen');
|
H5P.trigger(instance, 'enterFullScreen');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -341,15 +348,15 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) {
|
||||||
$classes.removeClass(classes);
|
$classes.removeClass(classes);
|
||||||
|
|
||||||
// Do not rely on window resize events.
|
// Do not rely on window resize events.
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
instance.$.trigger('focus');
|
H5P.trigger(instance, 'focus');
|
||||||
|
|
||||||
H5P.exitFullScreen = undefined;
|
H5P.exitFullScreen = undefined;
|
||||||
if (exitCallback !== undefined) {
|
if (exitCallback !== undefined) {
|
||||||
exitCallback();
|
exitCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.$.trigger('exitFullScreen');
|
H5P.trigger(instance, 'exitFullScreen');
|
||||||
};
|
};
|
||||||
|
|
||||||
H5P.isFullscreen = true;
|
H5P.isFullscreen = true;
|
||||||
|
@ -479,6 +486,7 @@ H5P.classFromName = function (name) {
|
||||||
* @param {Number} contentId
|
* @param {Number} contentId
|
||||||
* @param {jQuery} $attachTo An optional element to attach the instance to.
|
* @param {jQuery} $attachTo An optional element to attach the instance to.
|
||||||
* @param {Boolean} skipResize Optionally skip triggering of the resize event after attaching.
|
* @param {Boolean} skipResize Optionally skip triggering of the resize event after attaching.
|
||||||
|
* @param {Object} The parent of this H5P
|
||||||
* @return {Object} Instance.
|
* @return {Object} Instance.
|
||||||
*/
|
*/
|
||||||
H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
|
H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
|
||||||
|
@ -518,12 +526,16 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
|
||||||
instance.$ = H5P.jQuery(instance);
|
instance.$ = H5P.jQuery(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (instance.contentId === undefined) {
|
||||||
|
instance.contentId = contentId;
|
||||||
|
}
|
||||||
|
|
||||||
if ($attachTo !== undefined) {
|
if ($attachTo !== undefined) {
|
||||||
instance.attach($attachTo);
|
instance.attach($attachTo);
|
||||||
|
|
||||||
if (skipResize === undefined || !skipResize) {
|
if (skipResize === undefined || !skipResize) {
|
||||||
// Resize content.
|
// Resize content.
|
||||||
instance.$.trigger('resize');
|
H5P.trigger(instance, 'resize');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
|
@ -536,7 +548,7 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
|
||||||
* @returns {undefined}
|
* @returns {undefined}
|
||||||
*/
|
*/
|
||||||
H5P.error = function (err) {
|
H5P.error = function (err) {
|
||||||
if (window['console'] !== undefined && console.error !== undefined) {
|
if (window.console !== undefined && console.error !== undefined) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1214,3 +1226,48 @@ if (String.prototype.trim === undefined) {
|
||||||
return H5P.trim(this);
|
return H5P.trim(this);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger an event on an instance
|
||||||
|
*
|
||||||
|
* Helper function that triggers an event if the instance supports event handling
|
||||||
|
*
|
||||||
|
* @param {function} instance
|
||||||
|
* An H5P instance
|
||||||
|
* @param {string} eventType
|
||||||
|
* The event type
|
||||||
|
*/
|
||||||
|
H5P.trigger = function(instance, eventType) {
|
||||||
|
// Try new event system first
|
||||||
|
if (instance.trigger !== undefined) {
|
||||||
|
instance.trigger(eventType);
|
||||||
|
}
|
||||||
|
// Try deprecated event system
|
||||||
|
else if (instance.$ !== undefined && instance.$.trigger !== undefined) {
|
||||||
|
instance.$.trigger(eventType);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an event handler
|
||||||
|
*
|
||||||
|
* Helper function that registers an event handler for an event type if
|
||||||
|
* the instance supports event handling
|
||||||
|
*
|
||||||
|
* @param {function} instance
|
||||||
|
* An h5p instance
|
||||||
|
* @param {string} eventType
|
||||||
|
* The event type
|
||||||
|
* @param {function} handler
|
||||||
|
* Callback that gets triggered for events of the specified type
|
||||||
|
*/
|
||||||
|
H5P.on = function(instance, eventType, handler) {
|
||||||
|
// Try new event system first
|
||||||
|
if (instance.on !== undefined) {
|
||||||
|
instance.on(eventType, handler);
|
||||||
|
}
|
||||||
|
// Try deprecated event system
|
||||||
|
else if (instance.$ !== undefined && instance.$.on !== undefined) {
|
||||||
|
instance.$.on(eventType, handler);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue