From 93dcf29c15a3f6b75bab93fa18a62805b98dd984 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 30 Jun 2016 10:43:56 +0200 Subject: [PATCH] more functions for working with localstorage --- www/common/cryptpad-common.js | 58 ++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 0f4814c33..29a3ae80b 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -17,12 +17,10 @@ define([ return secret; }; - var rememberPad = common.rememberPad = window.rememberPad = function () { - // bail out early - if (!/#/.test(window.location.hash)) { return; } - - var storageKey = 'CryptPad_RECENTPADS'; + var storageKey = common.storageKey = 'CryptPad_RECENTPADS'; + var timeframe = common.timeframe = 1000 * 60 * 60 * 24 * 30; + var getRecentPads = function () { var recentPadsStr = localStorage[storageKey]; var recentPads = []; @@ -34,17 +32,61 @@ define([ // just overwrite it. } } + return recentPads; + }; + + var setRecentPads = function (pads) { + localStorage[storageKey] = JSON.stringify(pads); + }; + + var rememberPad = common.rememberPad = window.rememberPad = function (title) { + // bail out early + if (!/#/.test(window.location.hash)) { return; } + + var recentPads = getRecentPads(); 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); + // href, atime, name + out.push([window.location.href, now, title || '']); + setRecentPads(out); + }; + + var setPadTitle = common.setPadTitle = function (name) { + var href = window.location.href; + var recent = getRecentPads(); + + var renamed = recent.map(function (pad) { + if (pad[0] === href) { + // update the atime + pad[1] = new Date().toISOString(); + + // set the name + pad[2] = name; + } + //console.log(pad); + return pad; + }); + + setRecentPads(renamed); + }; + + var getPadTitle = common.getPadTitle = function () { + var href = window.location.href; + var hashSlice = window.location.hash.slice(1,9); + var title = ''; + getRecentPads().some(function (pad) { + if (pad[0] === href) { + title = pad[2] || hashSlice; + return true; + } + }); + return title; }; var importContent = common.importContent = function (type, f) {