Use H5P.EventDispatcher to trigger and handle events

pull/48/head
Thomas Horn Sivertsen 2018-05-22 11:11:05 +02:00
parent 4246857bf7
commit 019ee64e17
1 changed files with 13 additions and 11 deletions

View File

@ -1,6 +1,6 @@
/*jshint -W083 */ /*jshint -W083 */
(function ($, Version) { (function ($, Version, EventDispatcher) {
var info, $container, librariesCache = {}, scriptsCache = {}; var info, $container, librariesCache = {}, scriptsCache = {};
// Initialize // Initialize
@ -70,6 +70,15 @@
* @returns {_L1.ContentUpgrade} * @returns {_L1.ContentUpgrade}
*/ */
function ContentUpgrade(libraryId, events) { function ContentUpgrade(libraryId, events) {
if( typeof EventDispatcher !== 'undefined'){
EventDispatcher.call(this);
if( typeof events !== 'undefined'){
for (var i in events){
this.on(i, events[i]);
}
}
}
var self = this; var self = this;
// Get selected version // Get selected version
@ -85,8 +94,6 @@
// Track number of working // Track number of working
self.working = 0; self.working = 0;
self.events = events || {};
var start = function () { var start = function () {
self.trigger('start'); self.trigger('start');
@ -448,15 +455,10 @@
/** /**
* Trigger * Trigger
* @param action * Fallback function in case EventDispatcher is undefined
* @param data
*/ */
ContentUpgrade.prototype.trigger = function (action, data) { ContentUpgrade.prototype.trigger = function () {};
if( typeof this.events[action] !== "undefined"){
this.events[action].call(this, data);
}
};
H5P.ContentUpgrade = ContentUpgrade; H5P.ContentUpgrade = ContentUpgrade;
})(H5P.jQuery, H5P.Version); })(H5P.jQuery, H5P.Version, H5P.EventDispatcher);