Fixed this arg for external dispatcher.

redesign-copyrights Drupal-7.x-1.8
Frode Petterson 2015-06-04 14:39:02 +02:00
parent 3460e400a0
commit c40d273704
2 changed files with 35 additions and 26 deletions

View File

@ -99,13 +99,10 @@ H5P.EventDispatcher = (function () {
* Event type * Event type
* @param {H5P.EventCallback} listener * @param {H5P.EventCallback} listener
* Event listener * Event listener
* @param {function} thisArg * @param {Object} thisArg
* Optionally specify the this value when calling listener. * Optionally specify the this value when calling listener.
*/ */
this.on = function (type, listener, thisArg) { this.on = function (type, listener, thisArg) {
if (thisArg === undefined) {
thisArg = self;
}
if (typeof listener !== 'function') { if (typeof listener !== 'function') {
throw TypeError('listener must be a function'); throw TypeError('listener must be a function');
} }
@ -113,13 +110,14 @@ H5P.EventDispatcher = (function () {
// Trigger event before adding to avoid recursion // Trigger event before adding to avoid recursion
self.trigger('newListener', {'type': type, 'listener': listener}); self.trigger('newListener', {'type': type, 'listener': listener});
var trigger = {'listener': listener, 'thisArg': thisArg};
if (!triggers[type]) { if (!triggers[type]) {
// First // First
triggers[type] = [{'listener': listener, 'thisArg': thisArg}]; triggers[type] = [trigger];
} }
else { else {
// Append // Append
triggers[type].push({'listener': listener, 'thisArg': thisArg}); triggers[type].push(trigger);
} }
}; };
@ -132,20 +130,17 @@ H5P.EventDispatcher = (function () {
* Event type * Event type
* @param {H5P.EventCallback} listener * @param {H5P.EventCallback} listener
* Event listener * Event listener
* @param {function} thisArg * @param {Object} thisArg
* Optionally specify the this value when calling listener. * Optionally specify the this value when calling listener.
*/ */
this.once = function (type, listener, thisArg) { this.once = function (type, listener, thisArg) {
if (thisArg === undefined) {
thisArg = self;
}
if (!(listener instanceof Function)) { if (!(listener instanceof Function)) {
throw TypeError('listener must be a function'); throw TypeError('listener must be a function');
} }
var once = function (event) { var once = function (event) {
self.off(event, once); self.off(event, once);
listener.apply(thisArg, event); listener.apply(this, event);
}; };
self.on(type, once, thisArg); self.on(type, once, thisArg);
@ -193,6 +188,25 @@ H5P.EventDispatcher = (function () {
} }
}; };
/**
* Try to call all event listeners for the given event type.
*
* @private
* @param {string} Event type
*/
var call = function (type, event) {
if (triggers[type] === undefined) {
return;
}
// Call all listeners
for (var i = 0; i < triggers[type].length; i++) {
var trigger = triggers[type][i];
var thisArg = (trigger.thisArg ? trigger.thisArg : this);
trigger.listener.call(thisArg, event);
}
};
/** /**
* Dispatch event. * Dispatch event.
* *
@ -219,27 +233,20 @@ H5P.EventDispatcher = (function () {
// Check to see if this event should go externally after all triggering and bubbling is done // Check to see if this event should go externally after all triggering and bubbling is done
var scheduledForExternal = event.scheduleForExternal(); var scheduledForExternal = event.scheduleForExternal();
if (triggers[event.type] !== undefined) {
// Call all listeners // Call all listeners
for (var i = 0; i < triggers[event.type].length; i++) { call.call(this, event.type, event);
triggers[event.type][i].listener.call(triggers[event.type][i].thisArg, event);
}
}
if (triggers['*'] !== undefined) {
// Call all * listeners // Call all * listeners
for (var j = 0; j < triggers['*'].length; j++) { call.call(this, '*', event);
triggers['*'][j].listener.call(triggers['*'][j].thisArg, event);
}
}
// Bubble // Bubble
if (event.getBubbles() && self.parent instanceof H5P.EventDispatcher && typeof self.parent.trigger === 'function') { // TODO: Check instanceof Function as well? if (event.getBubbles() && self.parent instanceof H5P.EventDispatcher &&
(self.parent.trigger instanceof Function || typeof self.parent.trigger === 'function')) {
self.parent.trigger(event); self.parent.trigger(event);
} }
if (scheduledForExternal) { if (scheduledForExternal) {
H5P.externalDispatcher.trigger(event); H5P.externalDispatcher.trigger.call(this, event);
} }
}; };
} }

View File

@ -1914,7 +1914,9 @@ H5P.createTitle = function (rawTitle, maxLength) {
// Relay events to top window. // Relay events to top window.
if (H5P.isFramed && H5P.externalEmbed === false) { if (H5P.isFramed && H5P.externalEmbed === false) {
H5P.externalDispatcher.on('*', window.top.H5P.externalDispatcher.trigger); H5P.externalDispatcher.on('*', function (event) {
window.top.H5P.externalDispatcher.trigger.call(this, event);
});
} }
}); });