Added auto save loop. Added save after xAPI events. Made it possible to disable saving.
parent
a712d6ed61
commit
67288c2a0b
33
js/h5p.js
33
js/h5p.js
|
@ -114,6 +114,37 @@ H5P.init = function (target) {
|
|||
H5P.on(instance, 'xAPI', H5P.xAPICompletedListener);
|
||||
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) {
|
||||
var resizeDelay;
|
||||
if (H5P.externalEmbed === false) {
|
||||
|
@ -1512,6 +1543,7 @@ H5P.on = function(instance, eventType, handler) {
|
|||
H5P.init(document.body);
|
||||
}
|
||||
|
||||
if (H5PIntegration.saveFreq !== false) {
|
||||
// Store the current state of the H5P when leaving the page.
|
||||
H5P.$window.on('unload', function () {
|
||||
for (var i = 0; i < H5P.instances.length; i++) {
|
||||
|
@ -1526,6 +1558,7 @@ H5P.on = function(instance, eventType, handler) {
|
|||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
})(H5P.jQuery);
|
||||
|
|
Loading…
Reference in New Issue