diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index c09a0ab9d..f0b5367a0 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -15,5 +15,35 @@ define([ return secret; }; + var rememberPad = common.rememberPad = window.rememberPad = function () { + // bail out early + if (!/#/.test(window.location.hash)) { return; } + + var storageKey = 'CryptPad_RECENTPADS'; + + var recentPadsStr = localStorage[storageKey]; + + var recentPads = []; + if (recentPadsStr) { + try { + recentPads = JSON.parse(recentPadsStr); + } catch (err) { + // couldn't parse the localStorage? + // just overwrite it. + } + } + + var now = new Date(); + var timeframe = 1000 * 60 * 60 * 24 * 30; + + var out = recentPads.filter(function (pad) { + return (pad && pad[0] !== window.location.href && + (now.getTime() - new Date(pad[1]).getTime()) < timeframe); + }); + + out.push([window.location.href, now]); + localStorage[storageKey] = JSON.stringify(out); + }; + return common; }); diff --git a/www/common/toolbar.js b/www/common/toolbar.js index 508d21d50..134bb7afb 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -204,32 +204,6 @@ define([ lagElement.textContent = lagMsg; }; - // this is a little hack, it should go in it's own file. - // FIXME ok, so let's put it in its own file then - // TODO there should also be a 'clear recent pads' button - var rememberPad = function () { - // FIXME, this is overly complicated, use array methods - var recentPadsStr = localStorage['CryptPad_RECENTPADS']; - var recentPads = []; - if (recentPadsStr) { recentPads = JSON.parse(recentPadsStr); } - // TODO use window.location.hash or something like that - if (window.location.href.indexOf('#') === -1) { return; } - var now = new Date(); - var out = []; - for (var i = recentPads.length; i >= 0; i--) { - if (recentPads[i] && - // TODO precompute this time value, maybe make it configurable? - // FIXME precompute the date too, why getTime every time? - now.getTime() - recentPads[i][1] < (1000*60*60*24*30) && - recentPads[i][0] !== window.location.href) - { - out.push(recentPads[i]); - } - } - out.push([window.location.href, now.getTime()]); - localStorage['CryptPad_RECENTPADS'] = JSON.stringify(out); - }; - var create = function ($container, myUserName, realtime, getLag, userList, config) { var toolbar = createRealtimeToolbar($container); createEscape(toolbar.find('.rtwysiwyg-toolbar-leftside')); @@ -251,8 +225,6 @@ define([ saveElement = createSaveElement(saveContentID, toolbar.find('.rtwysiwyg-toolbar-rightside')); } - rememberPad(); - var connected = false; userList.onChange = function(newUserData) {