From 67288c2a0b7d13e4e025986973019e6e2481057c Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Sun, 22 Mar 2015 20:44:35 +0100 Subject: [PATCH] Added auto save loop. Added save after xAPI events. Made it possible to disable saving. --- js/h5p.js | 57 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/js/h5p.js b/js/h5p.js index a416d26..ab30799 100644 --- a/js/h5p.js +++ b/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,20 +1543,22 @@ H5P.on = function(instance, eventType, handler) { H5P.init(document.body); } - // 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++) { - var instance = H5P.instances[i]; - if (instance.getCurrentState instanceof Function || - typeof instance.getCurrentState === 'function') { - var state = instance.getCurrentState(); - if (state !== undefined) { - // Async is not used to prevent the request from being cancelled. - contentUserDataAjax(instance.contentId, 'state', undefined, state, true, true, false); + 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++) { + var instance = H5P.instances[i]; + if (instance.getCurrentState instanceof Function || + typeof instance.getCurrentState === 'function') { + var state = instance.getCurrentState(); + if (state !== undefined) { + // Async is not used to prevent the request from being cancelled. + contentUserDataAjax(instance.contentId, 'state', undefined, state, true, true, false); + } } } - } - }); + }); + } }); })(H5P.jQuery);