2018-05-03 10:27:06 +02:00
|
|
|
var H5P = window.H5P = window.H5P || {};
|
2015-02-03 20:11:01 +01:00
|
|
|
|
2015-02-16 14:22:54 +01:00
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* The Event class for the EventDispatcher.
|
|
|
|
*
|
2015-02-16 14:22:54 +01:00
|
|
|
* @class
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {string} type
|
|
|
|
* @param {*} data
|
|
|
|
* @param {Object} [extras]
|
2015-08-27 11:36:07 +02:00
|
|
|
* @param {boolean} [extras.bubbles]
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {boolean} [extras.external]
|
2015-02-16 14:22:54 +01:00
|
|
|
*/
|
2015-03-22 11:17:58 +01:00
|
|
|
H5P.Event = function(type, data, extras) {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.type = type;
|
|
|
|
this.data = data;
|
2015-03-22 20:37:10 +01:00
|
|
|
var bubbles = false;
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
// Is this an external event?
|
|
|
|
var external = false;
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
// Is this event scheduled to be sent externally?
|
|
|
|
var scheduledForExternal = false;
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-03-22 11:17:58 +01:00
|
|
|
if (extras === undefined) {
|
|
|
|
extras = {};
|
|
|
|
}
|
2015-03-22 20:37:10 +01:00
|
|
|
if (extras.bubbles === true) {
|
|
|
|
bubbles = true;
|
2015-03-22 11:17:58 +01:00
|
|
|
}
|
2015-04-07 19:33:21 +02:00
|
|
|
if (extras.external === true) {
|
|
|
|
external = true;
|
|
|
|
}
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
/**
|
|
|
|
* Prevent this event from bubbling up to parent
|
|
|
|
*/
|
2015-03-21 16:45:38 +01:00
|
|
|
this.preventBubbling = function() {
|
|
|
|
bubbles = false;
|
|
|
|
};
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
/**
|
|
|
|
* Get bubbling status
|
2015-05-05 13:13:57 +02:00
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
* true if bubbling false otherwise
|
2015-04-07 19:33:21 +02:00
|
|
|
*/
|
2015-03-21 16:45:38 +01:00
|
|
|
this.getBubbles = function() {
|
|
|
|
return bubbles;
|
|
|
|
};
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
/**
|
|
|
|
* Try to schedule an event for externalDispatcher
|
2015-05-05 13:13:57 +02:00
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
* true if external and not already scheduled, otherwise false
|
2015-04-07 19:33:21 +02:00
|
|
|
*/
|
|
|
|
this.scheduleForExternal = function() {
|
|
|
|
if (external && !scheduledForExternal) {
|
|
|
|
scheduledForExternal = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-05-05 13:13:57 +02:00
|
|
|
/**
|
|
|
|
* Callback type for event listeners.
|
|
|
|
*
|
|
|
|
* @callback H5P.EventCallback
|
|
|
|
* @param {H5P.Event} event
|
|
|
|
*/
|
|
|
|
|
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.
|
2015-05-05 13:13:57 +02:00
|
|
|
*
|
2015-02-03 20:11:01 +01:00
|
|
|
* @class
|
2015-05-05 13:13:57 +02:00
|
|
|
* @memberof H5P
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
|
|
|
function EventDispatcher() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of listeners for each event.
|
2015-05-05 13:13:57 +02:00
|
|
|
*
|
2015-02-03 20:11:01 +01:00
|
|
|
* @private
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
var triggers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new event listener.
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @throws {TypeError}
|
|
|
|
* listener must be a function
|
|
|
|
* @param {string} type
|
|
|
|
* Event type
|
|
|
|
* @param {H5P.EventCallback} listener
|
|
|
|
* Event listener
|
2016-04-05 18:35:02 +02:00
|
|
|
* @param {Object} [thisArg]
|
2015-05-05 13:13:57 +02:00
|
|
|
* Optionally specify the this value when calling listener.
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-20 15:34:38 +01:00
|
|
|
this.on = function (type, listener, thisArg) {
|
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
|
|
|
|
2015-06-04 14:39:02 +02:00
|
|
|
var trigger = {'listener': listener, 'thisArg': thisArg};
|
2015-02-03 20:11:01 +01:00
|
|
|
if (!triggers[type]) {
|
|
|
|
// First
|
2015-06-04 14:39:02 +02:00
|
|
|
triggers[type] = [trigger];
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Append
|
2015-06-04 14:39:02 +02:00
|
|
|
triggers[type].push(trigger);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new event listener that will be fired only once.
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @throws {TypeError}
|
|
|
|
* listener must be a function
|
|
|
|
* @param {string} type
|
|
|
|
* Event type
|
|
|
|
* @param {H5P.EventCallback} listener
|
|
|
|
* Event listener
|
2015-06-04 14:39:02 +02:00
|
|
|
* @param {Object} thisArg
|
2015-05-05 13:13:57 +02:00
|
|
|
* Optionally specify the this value when calling listener.
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-20 15:34:38 +01:00
|
|
|
this.once = function (type, listener, thisArg) {
|
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) {
|
2016-02-09 13:53:17 +01:00
|
|
|
self.off(event.type, once);
|
|
|
|
listener.call(this, 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.
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @throws {TypeError}
|
|
|
|
* listener must be a function
|
|
|
|
* @param {string} type
|
|
|
|
* Event type
|
|
|
|
* @param {H5P.EventCallback} listener
|
|
|
|
* Event listener
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-02-20 15:34:38 +01:00
|
|
|
this.off = function (type, listener) {
|
2015-02-03 20:11:01 +01:00
|
|
|
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-08-27 11:36:07 +02:00
|
|
|
triggers[type].splice(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];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-04 14:39:02 +02:00
|
|
|
/**
|
|
|
|
* Try to call all event listeners for the given event type.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} Event type
|
|
|
|
*/
|
2015-08-26 15:58:49 +02:00
|
|
|
var call = function (type, event) {
|
2015-06-04 14:39:02 +02:00
|
|
|
if (triggers[type] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 11:36:07 +02:00
|
|
|
// Clone array (prevents triggers from being modified during the event)
|
|
|
|
var handlers = triggers[type].slice();
|
|
|
|
|
2015-06-04 14:39:02 +02:00
|
|
|
// Call all listeners
|
2015-08-27 11:36:07 +02:00
|
|
|
for (var i = 0; i < handlers.length; i++) {
|
|
|
|
var trigger = handlers[i];
|
2015-06-04 14:39:02 +02:00
|
|
|
var thisArg = (trigger.thisArg ? trigger.thisArg : this);
|
|
|
|
trigger.listener.call(thisArg, event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-03 20:11:01 +01:00
|
|
|
/**
|
|
|
|
* Dispatch event.
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {string|H5P.Event} event
|
|
|
|
* Event object or event type as string
|
|
|
|
* @param {*} [eventData]
|
|
|
|
* Custom event data(used when event type as string is used as first
|
|
|
|
* argument).
|
|
|
|
* @param {Object} [extras]
|
2015-08-27 11:36:07 +02:00
|
|
|
* @param {boolean} [extras.bubbles]
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {boolean} [extras.external]
|
2015-02-03 20:11:01 +01:00
|
|
|
*/
|
2015-04-07 19:33:21 +02:00
|
|
|
this.trigger = function (event, eventData, extras) {
|
2015-02-11 15:56:35 +01:00
|
|
|
if (event === undefined) {
|
|
|
|
return;
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
2015-08-27 11:36:07 +02:00
|
|
|
if (event instanceof String || typeof event === 'string') {
|
2015-04-07 19:33:21 +02:00
|
|
|
event = new H5P.Event(event, eventData, extras);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
2015-02-11 15:56:35 +01:00
|
|
|
else if (eventData !== undefined) {
|
|
|
|
event.data = eventData;
|
|
|
|
}
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
// Check to see if this event should go externally after all triggering and bubbling is done
|
|
|
|
var scheduledForExternal = event.scheduleForExternal();
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-06-04 14:39:02 +02:00
|
|
|
// Call all listeners
|
|
|
|
call.call(this, event.type, event);
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-06-04 14:39:02 +02:00
|
|
|
// Call all * listeners
|
|
|
|
call.call(this, '*', event);
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-03-22 20:37:10 +01:00
|
|
|
// Bubble
|
2015-06-04 14:39:02 +02:00
|
|
|
if (event.getBubbles() && self.parent instanceof H5P.EventDispatcher &&
|
|
|
|
(self.parent.trigger instanceof Function || typeof self.parent.trigger === 'function')) {
|
2015-03-21 16:45:38 +01:00
|
|
|
self.parent.trigger(event);
|
2015-04-07 19:33:21 +02:00
|
|
|
}
|
2015-05-05 13:13:57 +02:00
|
|
|
|
2015-04-07 19:33:21 +02:00
|
|
|
if (scheduledForExternal) {
|
2015-06-04 14:39:02 +02:00
|
|
|
H5P.externalDispatcher.trigger.call(this, event);
|
2015-04-07 19:33:21 +02:00
|
|
|
}
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return EventDispatcher;
|
2015-02-20 11:41:09 +01:00
|
|
|
})();
|