diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index cd87159420..8ba7fef1a6 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -1,9 +1,11 @@ "use strict"; -var ajaxify = ajaxify || {}; +var ajaxify = ajaxify || { + isPopState: false +}; $(document).ready(function() { - require(['templates'], function (templatesModule) { + require(['templates', 'ajaxifyCache'], function (templatesModule, cache) { /*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/ var location = document.location || window.location, @@ -13,7 +15,9 @@ $(document).ready(function() { window.onpopstate = function (event) { if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) { + ajaxify.isPopState = true; ajaxify.go(event.state.url, function() { + ajaxify.isPopState = false; $(window).trigger('action:popstate', {url: event.state.url}); }, true); } @@ -55,6 +59,12 @@ $(document).ready(function() { // "quiet": If set to true, will not call pushState app.enterRoom(''); + // If the url is in the cache, load from cache instead + if (cache.get(url)) { return true; } + else { + cache.url = ajaxify.currentPage; + } + $(window).off('scroll'); if ($('#content').hasClass('ajaxifying') && apiXHR) { @@ -114,6 +124,7 @@ $(document).ready(function() { templates.parse(tpl_url, data, function(template) { translator.translate(template, function(translatedTemplate) { setTimeout(function() { + cache.set(); $('#content').html(translatedTemplate); ajaxify.variables.parse(); diff --git a/public/src/modules/ajaxifyCache.js b/public/src/modules/ajaxifyCache.js new file mode 100644 index 0000000000..eaa682b0da --- /dev/null +++ b/public/src/modules/ajaxifyCache.js @@ -0,0 +1,35 @@ +'use strict'; +/* globals define, app, ajaxify */ + +define('ajaxifyCache', function() { + var Cache = { + url: undefined, + DOM: undefined, + tempDOM: undefined + }; + + Cache.set = function() { + Cache.DOM = $('#content > *').detach(); + }; + + Cache.get = function(url) { + if (url === Cache.url && ajaxify.isPopState) { + // Swap DOM elements + setTimeout(function() { + Cache.tempDOM = $('#content > *').detach(); + $('#content').append(Cache.DOM); + Cache.DOM = Cache.tempDOM; + }, 100); // 100ms for realism! :sunglasses: + + // Set the values that normally get set on ajaxify + Cache.url = ajaxify.currentPage; + ajaxify.currentPage = url; + + return true; + } else { + return false; + } + }; + + return Cache; +}); \ No newline at end of file