Added auto save loop. Added save after xAPI events. Made it possible to disable saving.

semantics-font
Frode Petterson 2015-03-22 20:44:35 +01:00
parent a712d6ed61
commit 67288c2a0b
1 changed files with 45 additions and 12 deletions

View File

@ -114,6 +114,37 @@ H5P.init = function (target) {
H5P.on(instance, 'xAPI', H5P.xAPICompletedListener); H5P.on(instance, 'xAPI', H5P.xAPICompletedListener);
H5P.on(instance, 'xAPI', H5P.externalDispatcher.trigger); H5P.on(instance, 'xAPI', H5P.externalDispatcher.trigger);
// Auto save current state if supported
if (H5PIntegration.saveFreq !== false && (
instance.getCurrentState instanceof Function ||
typeof instance.getCurrentState === 'function')) {
var saveTimer, save = function () {
var state = instance.getCurrentState();
if (state !== undefined) {
H5P.setUserData(contentId, 'state', state, true, true);
}
saveTimer = null;
};
if (H5PIntegration.saveFreq) {
// Only run the loop when there's stuff happening (reduces load)
H5P.$body.on('mousedown keydown touchstart', function () {
if (!saveTimer) {
saveTimer = setTimeout(save, H5PIntegration.saveFreq * 1000);
}
});
}
// xAPI events will schedule a save in three seconds.
H5P.on(instance, 'xAPI', function () {
if (saveTimer) {
clearTimeout(saveTimer);
}
saveTimer = setTimeout(save, 3000);
});
}
if (H5P.isFramed) { if (H5P.isFramed) {
var resizeDelay; var resizeDelay;
if (H5P.externalEmbed === false) { if (H5P.externalEmbed === false) {
@ -1512,20 +1543,22 @@ H5P.on = function(instance, eventType, handler) {
H5P.init(document.body); H5P.init(document.body);
} }
// Store the current state of the H5P when leaving the page. if (H5PIntegration.saveFreq !== false) {
H5P.$window.on('unload', function () { // Store the current state of the H5P when leaving the page.
for (var i = 0; i < H5P.instances.length; i++) { H5P.$window.on('unload', function () {
var instance = H5P.instances[i]; for (var i = 0; i < H5P.instances.length; i++) {
if (instance.getCurrentState instanceof Function || var instance = H5P.instances[i];
typeof instance.getCurrentState === 'function') { if (instance.getCurrentState instanceof Function ||
var state = instance.getCurrentState(); typeof instance.getCurrentState === 'function') {
if (state !== undefined) { var state = instance.getCurrentState();
// Async is not used to prevent the request from being cancelled. if (state !== undefined) {
contentUserDataAjax(instance.contentId, 'state', undefined, state, true, true, false); // Async is not used to prevent the request from being cancelled.
contentUserDataAjax(instance.contentId, 'state', undefined, state, true, true, false);
}
} }
} }
} });
}); }
}); });
})(H5P.jQuery); })(H5P.jQuery);