From e36582c71423b0304809baddf783d5dacbc908cd Mon Sep 17 00:00:00 2001 From: Paal Joergensen Date: Tue, 14 Jul 2015 14:37:39 +0200 Subject: [PATCH] Added H5P base class that all H5P libraries automatically extends --- js/h5p.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/js/h5p.js b/js/h5p.js index a57fe64..63c29a8 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -72,6 +72,38 @@ H5P.DISABLE_ABOUT = 16; */ H5P.opened = {}; +/** + * H5P Base class. Adds utility functions to a library's prototype object. + * + * Functions here may be overridable by the libraries. In special cases, + * it is also possible to override H5P.Base on a global level. + * */ +H5P.Base = function (constructor, standalone, library) { + /** + * Is library standalone or not? Not beeing standalone, means it is + * included in another library + * + * @method isStandalone + * @return {Boolean} + */ + this.isStandalone = function () { + return standalone; + }; + + /** + * Returns the file path of a file in the current library + * @method getLibraryFilePath + * @param {string} filePath The path to the file relative to the library folder + * @return {string} The full path to the file + */ + this.getLibraryFilePath = function (filePath) { + return H5P.getLibraryPath(library.library) + '/' + filePath; + }; + + // This order makes it possible for an H5P library to override H5P.Base functions! + return H5P.jQuery.extend({}, this, constructor.prototype); +}; + /** * Initialize H5P content. * Scans for ".h5p-content" in the document and initializes H5P instances where found. @@ -135,7 +167,7 @@ H5P.init = function (target) { }); // Create new instance. - var instance = H5P.newRunnable(library, contentId, $container, true); + var instance = H5P.newRunnable(library, contentId, $container, true, {standalone: true}); // Check if we should add and display a fullscreen button for this H5P. if (contentData.fullScreen == 1 && H5P.canHasFullScreen) { @@ -724,6 +756,10 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize, extras) { extras.previousState = library.userDatas.state; } + // Makes all H5P libraries extend H5P.Base: + var standalone = extras.standalone || false; + constructor.prototype = H5P.Base(constructor, standalone, library); + var instance; // Some old library versions have their own custom third parameter. // Make sure we don't send them the extras. @@ -759,6 +795,7 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize, extras) { } if ($attachTo !== undefined) { + $attachTo.toggleClass('h5p-standalone', standalone); instance.attach($attachTo); H5P.trigger(instance, 'domChanged', { '$target': $attachTo,