namespaces
Svein-Tore Griff With 2015-02-11 15:56:35 +01:00
parent 9a0da8614b
commit 079f4992b4
2 changed files with 35 additions and 55 deletions

View File

@ -1,8 +1,9 @@
/** @namespace H5P */ /** @namespace H5P */
var H5P = H5P || {}; var H5P = H5P || {};
H5P.Event = function() { H5P.Event = function(type, data) {
// We're going to add bubbling, propagation and other features here later this.type = type;
this.data = data;
}; };
H5P.EventDispatcher = (function () { H5P.EventDispatcher = (function () {
@ -109,49 +110,29 @@ 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, event) {
var left = [event];
for (var i = skip; i < args.length; i++) {
left.push(args[i]);
}
return left;
};
/** /**
* Dispatch event. * Dispatch event.
* *
* @public * @public
* @param {String} type Event type * @param {String|Function}
* @param {...*} args *
*/ */
self.trigger = function (type, event) { self.trigger = function (event, eventData) {
if (self.debug !== undefined) { if (event === undefined) {
// Class has debug enabled. Log events. return;
console.log(self.debug + ' - Firing event "' + type + '", ' + (triggers[type] === undefined ? 0 : triggers[type].length) + ' listeners.', getArgs(arguments, 1)); }
} if (typeof event === 'string') {
event = new H5P.Event(event, eventData);
if (event === null) { }
event = new H5P.Event(); else if (eventData !== undefined) {
} event.data = eventData;
if (triggers[type] === undefined) { }
if (triggers[event.type] === undefined) {
return; return;
} }
// Copy all arguments except the first two
var args = getArgs(arguments, 2, event);
// Call all listeners // Call all listeners
for (var i = 0; i < triggers[type].length; i++) { for (var i = 0; i < triggers[event.type].length; i++) {
triggers[type][i].apply(self, args); triggers[event.type][i].call(self, event);
} }
}; };
} }

View File

@ -1,20 +1,21 @@
var H5P = H5P || {}; var H5P = H5P || {};
H5P.xAPIListener = function(event) { H5P.xAPIListener = function(event) {
if ('verb' in event.statement) { var statement = event.data.statement;
if (event.statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') { if ('verb' in statement) {
var score = event.statement.result.score.raw; if (statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') {
var maxScore = event.statement.result.score.max; var score = statement.result.score.raw;
var contentId = event.statement.object.extensions['http://h5p.org/x-api/h5p-local-content-id']; var maxScore = statement.result.score.max;
var contentId = statement.object.extensions['http://h5p.org/x-api/h5p-local-content-id'];
H5P.setFinished(contentId, score, maxScore); H5P.setFinished(contentId, score, maxScore);
} }
} }
}; };
H5P.xAPIEmitter = function (event) { H5P.xAPIEmitter = function (event) {
if (event.statement !== undefined) { if (event.data.statement !== undefined) {
for (var i = 0; i < H5P.xAPIListeners.length; i++) { for (var i = 0; i < H5P.xAPIListeners.length; i++) {
H5P.xAPIListeners[i](event.statement); H5P.xAPIListeners[i](event.data.statement);
} }
} }
}; };
@ -30,15 +31,14 @@ H5P.onXAPI(function(statement) {
}); });
H5P.XAPIEvent = function() { H5P.XAPIEvent = function() {
H5P.Event.call(this); H5P.Event.call(this, 'xAPI', {'statement': {}});
this.statement = {};
}; };
H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype); H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype);
H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent; H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent;
H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) { H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) {
this.statement.result = { this.data.statement.result = {
'score': { 'score': {
'min': 0, 'min': 0,
'max': maxScore, 'max': maxScore,
@ -49,7 +49,7 @@ H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) {
H5P.XAPIEvent.prototype.setVerb = function(verb) { H5P.XAPIEvent.prototype.setVerb = function(verb) {
if (H5P.jQuery.inArray(verb, H5P.XAPIEvent.allowedXAPIVerbs) !== -1) { if (H5P.jQuery.inArray(verb, H5P.XAPIEvent.allowedXAPIVerbs) !== -1) {
this.statement.verb = { this.data.statement.verb = {
'id': 'http://adlnet.gov/expapi/verbs/' + verb, 'id': 'http://adlnet.gov/expapi/verbs/' + verb,
'display': { 'display': {
'en-US': verb 'en-US': verb
@ -63,7 +63,7 @@ H5P.XAPIEvent.prototype.setVerb = function(verb) {
}; };
H5P.XAPIEvent.prototype.setObject = function(instance) { H5P.XAPIEvent.prototype.setObject = function(instance) {
this.statement.object = { this.data.statement.object = {
// TODO: Correct this. contentId might be vid // TODO: Correct this. contentId might be vid
'id': window.location.origin + Drupal.settings.basePath + 'node/' + instance.contentId, 'id': window.location.origin + Drupal.settings.basePath + 'node/' + instance.contentId,
'objectType': 'Activity', 'objectType': 'Activity',
@ -74,7 +74,7 @@ H5P.XAPIEvent.prototype.setObject = function(instance) {
}; };
H5P.XAPIEvent.prototype.setActor = function() { H5P.XAPIEvent.prototype.setActor = function() {
this.statement.actor = H5P.getActor(); this.data.statement.actor = H5P.getActor();
}; };
H5P.XAPIEvent.prototype.getMaxScore = function() { H5P.XAPIEvent.prototype.getMaxScore = function() {
@ -86,7 +86,7 @@ H5P.XAPIEvent.prototype.getScore = function() {
}; };
H5P.XAPIEvent.prototype.getVerifiedStatementValue = function(keys) { H5P.XAPIEvent.prototype.getVerifiedStatementValue = function(keys) {
var val = this.statement; var val = this.data.statement;
for (var i in keys) { for (var i in keys) {
if (val[keys[i]] === undefined) { if (val[keys[i]] === undefined) {
return null; return null;
@ -125,8 +125,7 @@ H5P.XAPIEvent.allowedXAPIVerbs = [
]; ];
H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) { H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) {
var event = this.createXAPIEventTemplate(verb, extra); this.trigger(this.createXAPIEventTemplate(verb, extra));
this.trigger('xAPI', event);
}; };
H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) { H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) {
@ -136,7 +135,7 @@ H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) {
event.setVerb(verb); event.setVerb(verb);
if (extra !== undefined) { if (extra !== undefined) {
for (var i in extra) { for (var i in extra) {
event.statement[i] = extra[i]; event.data.statement[i] = extra[i];
} }
} }
if (!('object' in event)) { if (!('object' in event)) {
@ -148,7 +147,7 @@ H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) {
H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) { H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) {
var event = this.createXAPIEventTemplate('completed'); var event = this.createXAPIEventTemplate('completed');
event.setScoredResult(score, maxScore); event.setScoredResult(score, maxScore);
this.trigger('xAPI', event); this.trigger(event);
} }
H5P.getActor = function() { H5P.getActor = function() {