commit
2b1c894307
283
h5p.classes.php
283
h5p.classes.php
|
@ -770,15 +770,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 +797,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 +936,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 +1238,15 @@ 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);
|
|
||||||
$this->h5pC->copyFileTree($library['uploadDirectory'], $destination_path);
|
|
||||||
H5PCore::deleteFileTree($library['uploadDirectory']);
|
|
||||||
|
|
||||||
$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) {
|
||||||
|
@ -1315,7 +1255,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;
|
||||||
|
@ -1328,22 +1277,115 @@ class H5PStorage {
|
||||||
// Move the content folder
|
// Move the content folder
|
||||||
$destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId;
|
$destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId;
|
||||||
$this->h5pC->copyFileTree($current_path, $destination_path);
|
$this->h5pC->copyFileTree($current_path, $destination_path);
|
||||||
H5PCore::deleteFileTree($current_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1548,6 +1590,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',
|
||||||
|
@ -1959,7 +2003,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);
|
||||||
}
|
}
|
||||||
|
@ -2284,6 +2328,28 @@ class H5PCore {
|
||||||
}
|
}
|
||||||
return $disable;
|
return $disable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2645,6 +2711,9 @@ class H5PContentValidator {
|
||||||
$found = FALSE;
|
$found = FALSE;
|
||||||
foreach ($semantics->fields as $field) {
|
foreach ($semantics->fields as $field) {
|
||||||
if ($field->name == $key) {
|
if ($field->name == $key) {
|
||||||
|
if (isset($semantics->optional) && $semantics->optional) {
|
||||||
|
$field->optional = TRUE;
|
||||||
|
}
|
||||||
$function = $this->typeMap[$field->type];
|
$function = $this->typeMap[$field->type];
|
||||||
$found = TRUE;
|
$found = TRUE;
|
||||||
break;
|
break;
|
||||||
|
@ -2664,16 +2733,18 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach ($semantics->fields as $field) {
|
if (!(isset($semantics->optional) && $semantics->optional)) {
|
||||||
if (!(isset($field->optional) && $field->optional)) {
|
foreach ($semantics->fields as $field) {
|
||||||
// Check if field is in group.
|
if (!(isset($field->optional) && $field->optional)) {
|
||||||
if (! property_exists($group, $field->name)) {
|
// Check if field is in group.
|
||||||
$this->h5pF->setErrorMessage($this->h5pF->t('No value given for mandatory field ' . $field->name));
|
if (! property_exists($group, $field->name)) {
|
||||||
|
//$this->h5pF->setErrorMessage($this->h5pF->t('No value given for mandatory field ' . $field->name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2697,18 +2768,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];
|
||||||
|
@ -2723,6 +2782,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,8 +1,17 @@
|
||||||
/** @namespace H5P */
|
/** @namespace H5P */
|
||||||
var H5P = H5P || {};
|
var H5P = H5P || {};
|
||||||
|
|
||||||
H5P.EventDispatcher = (function () {
|
/**
|
||||||
|
* The Event class for the EventDispatcher
|
||||||
|
* @class
|
||||||
|
*/
|
||||||
|
H5P.Event = function(type, data) {
|
||||||
|
this.type = type;
|
||||||
|
this.data = data;
|
||||||
|
};
|
||||||
|
|
||||||
|
H5P.EventDispatcher = (function () {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base of the event system.
|
* The base of the event system.
|
||||||
* Inherit this class if you want your H5P to dispatch events.
|
* Inherit this class if you want your H5P to dispatch events.
|
||||||
|
@ -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,83 +87,69 @@ 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return EventDispatcher;
|
return EventDispatcher;
|
||||||
})();
|
})();
|
|
@ -0,0 +1,178 @@
|
||||||
|
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 {
|
||||||
|
console.log('illegal verb');
|
||||||
|
}
|
||||||
|
// Else: Fail silently...
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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': H5PIntegration.getContentUrl(instance.contentId),
|
||||||
|
'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() {
|
||||||
|
var user = H5PIntegration.getUser();
|
||||||
|
this.data.statement.actor = {
|
||||||
|
'name': user.name,
|
||||||
|
'mbox': 'mailto:' + 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 (window.top !== window.self && window.top.H5P !== undefined && window.top.H5P.externalDispatcher !== undefined) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
85
js/h5p.js
85
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 = '';
|
||||||
|
@ -125,13 +127,6 @@ H5P.init = function () {
|
||||||
// Keep track of when we started
|
// Keep track of when we started
|
||||||
H5P.opened[contentId] = new Date();
|
H5P.opened[contentId] = new Date();
|
||||||
|
|
||||||
// Handle events when the user finishes the content. Useful for logging exercise results.
|
|
||||||
instance.$.on('finish', function (event) {
|
|
||||||
if (event.data !== undefined) {
|
|
||||||
H5P.setFinished(contentId, event.data.score, event.data.maxScore, event.data.time);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (H5P.isFramed) {
|
if (H5P.isFramed) {
|
||||||
// Make it possible to resize the iframe when the content changes size. This way we get no scrollbars.
|
// Make it possible to resize the iframe when the content changes size. This way we get no scrollbars.
|
||||||
var iframe = window.parent.document.getElementById('h5p-iframe-' + contentId);
|
var iframe = window.parent.document.getElementById('h5p-iframe-' + contentId);
|
||||||
|
@ -155,28 +150,32 @@ H5P.init = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
var resizeDelay;
|
var resizeDelay;
|
||||||
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 () {
|
||||||
resizeIframe();
|
resizeIframe();
|
||||||
}, 1);
|
}, 1);
|
||||||
});
|
});
|
||||||
|
H5P.instances.push(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
H5P.on(instance, 'xAPI', H5P.xAPICompletedListener);
|
||||||
|
H5P.on(instance, 'xAPI', H5P.externalDispatcher.trigger);
|
||||||
|
|
||||||
// Resize everything when window is resized.
|
// Resize everything when window is resized.
|
||||||
$window.resize(function () {
|
$window.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');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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.
|
||||||
|
@ -240,8 +239,8 @@ 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');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -255,8 +254,8 @@ 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');
|
||||||
|
|
||||||
if (exitCallback !== undefined) {
|
if (exitCallback !== undefined) {
|
||||||
exitCallback();
|
exitCallback();
|
||||||
|
@ -385,6 +384,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) {
|
||||||
|
@ -419,17 +419,21 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance = new constructor(library.params, contentId);
|
var instance = new constructor(library.params, contentId);
|
||||||
|
|
||||||
if (instance.$ === undefined) {
|
if (instance.$ === undefined) {
|
||||||
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;
|
||||||
|
@ -1087,7 +1091,7 @@ H5P.setFinished = function (contentId, score, maxScore, time) {
|
||||||
var toUnix = function (date) {
|
var toUnix = function (date) {
|
||||||
return Math.round(date.getTime() / 1000);
|
return Math.round(date.getTime() / 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Post the results
|
// Post the results
|
||||||
// TODO: Should we use a variable with the complete path?
|
// TODO: Should we use a variable with the complete path?
|
||||||
H5P.jQuery.post(H5P.ajaxPath + 'setFinished', {
|
H5P.jQuery.post(H5P.ajaxPath + 'setFinished', {
|
||||||
|
@ -1131,3 +1135,48 @@ if (H5P.jQuery) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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