2015-02-03 20:11:01 +01:00
|
|
|
/** @namespace H5P */
|
|
|
|
var H5P = H5P || {};
|
|
|
|
|
2015-02-16 14:22:54 +01:00
|
|
|
/**
|
|
|
|
* The Event class for the EventDispatcher
|
|
|
|
* @class
|
|
|
|
*/
|
2015-02-11 15:56:35 +01:00
|
|
|
H5P.Event = function(type, data) {
|
|
|
|
this.type = type;
|
|
|
|
this.data = data;
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
H5P.EventDispatcher = (function () {
|
2015-02-20 11:41:09 +01:00
|
|
|
|
2015-02-03 20:11:01 +01:00
|
|
|
/**
|
|
|
|
* The base of the event system.
|
|
|
|
* Inherit this class if you want your H5P to dispatch events.
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
function EventDispatcher() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of listeners for each event.
|
|
|
|
* @private
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
var triggers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new event listener.
|
|
|
|
*
|
|
|
|
* @public
|
2015-02-16 14:22:54 +01:00
|
|
|
* @throws {TypeError} listener - Must be a function
|
|
|
|
* @param {String} type - Event type
|
|
|
|
* @param {Function} listener - Event listener
|
2015-02-16 14:26:09 +01:00
|
|
|
* @param {Function} thisArg - Optionally specify the this value when calling listener.
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-16 14:22:54 +01:00
|
|
|
self.on = function (type, listener, thisArg) {
|
|
|
|
if (thisArg === undefined) {
|
|
|
|
thisArg = self;
|
2015-02-11 17:31:01 +01:00
|
|
|
}
|
2015-02-18 10:59:47 +01:00
|
|
|
if (typeof listener !== 'function') {
|
2015-02-03 20:11:01 +01:00
|
|
|
throw TypeError('listener must be a function');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger event before adding to avoid recursion
|
2015-02-11 17:31:01 +01:00
|
|
|
self.trigger('newListener', {'type': type, 'listener': listener});
|
2015-02-03 20:11:01 +01:00
|
|
|
|
|
|
|
if (!triggers[type]) {
|
|
|
|
// First
|
2015-02-16 14:22:54 +01:00
|
|
|
triggers[type] = [{'listener': listener, 'thisArg': thisArg}];
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Append
|
2015-02-16 14:22:54 +01:00
|
|
|
triggers[type].push({'listener': listener, 'thisArg': thisArg});
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new event listener that will be fired only once.
|
|
|
|
*
|
|
|
|
* @public
|
2015-02-16 14:26:09 +01:00
|
|
|
* @throws {TypeError} listener - must be a function
|
|
|
|
* @param {String} type - Event type
|
|
|
|
* @param {Function} listener - Event listener
|
|
|
|
* @param {Function} thisArg - Optionally specify the this value when calling listener.
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-16 14:22:54 +01:00
|
|
|
self.once = function (type, listener, thisArg) {
|
|
|
|
if (thisArg === undefined) {
|
|
|
|
thisArg = self;
|
2015-02-11 17:31:01 +01:00
|
|
|
}
|
2015-02-03 20:11:01 +01:00
|
|
|
if (!(listener instanceof Function)) {
|
|
|
|
throw TypeError('listener must be a function');
|
|
|
|
}
|
|
|
|
|
2015-02-11 17:31:01 +01:00
|
|
|
var once = function (event) {
|
|
|
|
self.off(event, once);
|
2015-02-16 14:22:54 +01:00
|
|
|
listener.apply(thisArg, event);
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-02-16 14:22:54 +01:00
|
|
|
self.on(type, once, thisArg);
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove event listener.
|
|
|
|
* If no listener is specified, all listeners will be removed.
|
|
|
|
*
|
|
|
|
* @public
|
2015-02-16 14:26:09 +01:00
|
|
|
* @throws {TypeError} listener - must be a function
|
|
|
|
* @param {String} type - Event type
|
|
|
|
* @param {Function} listener - Event listener
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
|
|
|
self.off = function (type, listener) {
|
|
|
|
if (listener !== undefined && !(listener instanceof Function)) {
|
|
|
|
throw TypeError('listener must be a function');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (triggers[type] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener === undefined) {
|
|
|
|
// Remove all listeners
|
|
|
|
delete triggers[type];
|
|
|
|
self.trigger('removeListener', type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find specific listener
|
|
|
|
for (var i = 0; i < triggers[type].length; i++) {
|
2015-02-11 17:31:01 +01:00
|
|
|
if (triggers[type][i].listener === listener) {
|
2015-02-03 20:11:01 +01:00
|
|
|
triggers[type].unshift(i, 1);
|
2015-02-11 17:31:01 +01:00
|
|
|
self.trigger('removeListener', type, {'listener': listener});
|
2015-02-03 20:11:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up empty arrays
|
|
|
|
if (!triggers[type].length) {
|
|
|
|
delete triggers[type];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch event.
|
|
|
|
*
|
|
|
|
* @public
|
2015-02-16 14:26:09 +01:00
|
|
|
* @param {String|Function} event - Event object or event type as string
|
|
|
|
* @param {mixed} eventData
|
|
|
|
* Custom event data(used when event type as string is used as first
|
|
|
|
* argument
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-11 15:56:35 +01:00
|
|
|
self.trigger = function (event, eventData) {
|
|
|
|
if (event === undefined) {
|
|
|
|
return;
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
2015-02-11 15:56:35 +01:00
|
|
|
if (typeof event === 'string') {
|
|
|
|
event = new H5P.Event(event, eventData);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
2015-02-11 15:56:35 +01:00
|
|
|
else if (eventData !== undefined) {
|
|
|
|
event.data = eventData;
|
|
|
|
}
|
|
|
|
if (triggers[event.type] === undefined) {
|
2015-02-03 20:11:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Call all listeners
|
2015-02-11 15:56:35 +01:00
|
|
|
for (var i = 0; i < triggers[event.type].length; i++) {
|
2015-02-16 14:22:54 +01:00
|
|
|
triggers[event.type][i].listener.call(triggers[event.type][i].thisArg, event);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return EventDispatcher;
|
2015-02-20 11:41:09 +01:00
|
|
|
})();
|