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,6 +1543,7 @@ H5P.on = function(instance, eventType, handler) {
H5P.init(document.body); H5P.init(document.body);
} }
if (H5PIntegration.saveFreq !== false) {
// Store the current state of the H5P when leaving the page. // Store the current state of the H5P when leaving the page.
H5P.$window.on('unload', function () { H5P.$window.on('unload', function () {
for (var i = 0; i < H5P.instances.length; i++) { for (var i = 0; i < H5P.instances.length; i++) {
@ -1526,6 +1558,7 @@ H5P.on = function(instance, eventType, handler) {
} }
} }
}); });
}
}); });
})(H5P.jQuery); })(H5P.jQuery);