Added support for debugging events.

d6
Frode Petterson 2014-12-17 16:57:46 +01:00
parent 1aef9e376d
commit 74fc840366
1 changed files with 23 additions and 5 deletions

View File

@ -105,6 +105,22 @@ H5P.EventDispatcher = (function () {
} }
}; };
/**
* 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.
* *
@ -113,18 +129,20 @@ H5P.EventDispatcher = (function () {
* @param {...*} args * @param {...*} args
*/ */
self.trigger = function (type) { self.trigger = function (type) {
if (self.debug !== undefined) {
// Class has debug enabled. Log events.
console.log(self.debug + ' - Firing event "' + type + '", ' + (events[type] === undefined ? 0 : events[type].length) + ' listeners.', getArgs(arguments, 1));
}
if (events[type] === undefined) { if (events[type] === undefined) {
return; return;
} }
// Copy all arguments except the first // Copy all arguments except the first
var i, args = []; var args = getArgs(arguments, 1);
for (i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
// Call all listeners // Call all listeners
for (i = 0; i < events[type].length; i++) { for (var i = 0; i < events[type].length; i++) {
events[type][i].apply(self, args); events[type][i].apply(self, args);
} }
}; };