improve pad naming UX, slight refactoring
* prevent naming conflicts * migrate localestorage to use named attributes * use ctime and atime * display default names in table * sort pads by most recent atime * move more functions into cryptpad common * change table styles
This commit is contained in:
@@ -2,9 +2,17 @@ define([
|
||||
'/bower_components/chainpad-crypto/crypto.js',
|
||||
'/bower_components/jquery/dist/jquery.min.js',
|
||||
], function (Crypto) {
|
||||
/* This file exposes functionality which is specific to Cryptpad, but not to
|
||||
any particular pad type. This includes functions for committing metadata
|
||||
about pads to your local storage for future use and improved usability.
|
||||
|
||||
Additionally, there is some basic functionality for import/export.
|
||||
*/
|
||||
var $ = window.jQuery;
|
||||
var common = {};
|
||||
|
||||
var isArray = function (o) { return Object.prototype.toString.call(o) === '[object Array]'; };
|
||||
|
||||
var getSecrets = common.getSecrets = function () {
|
||||
var secret = {};
|
||||
if (!/#/.test(window.location.href)) {
|
||||
@@ -20,7 +28,42 @@ define([
|
||||
var storageKey = common.storageKey = 'CryptPad_RECENTPADS';
|
||||
var timeframe = common.timeframe = 1000 * 60 * 60 * 24 * 30;
|
||||
|
||||
var getRecentPads = function () {
|
||||
/*
|
||||
the first time this gets called, your local storage will migrate to a
|
||||
new format. No more indices for values, everything is named now.
|
||||
|
||||
* href
|
||||
* atime (access time)
|
||||
* title
|
||||
* ??? // what else can we put in here?
|
||||
*/
|
||||
var migrateRecentPads = common.migrateRecentPads = function (pads) {
|
||||
return pads.map(function (pad) {
|
||||
if (isArray(pad)) {
|
||||
return {
|
||||
href: pad[0],
|
||||
atime: pad[1],
|
||||
title: pad[2],
|
||||
};
|
||||
} else if (typeof(pad) === 'object') {
|
||||
if (!pad.ctime) { pad.ctime = pad.atime; }
|
||||
/*
|
||||
if (pad.date) {
|
||||
pad.atime = pad.date;
|
||||
delete pad.date;
|
||||
pad.date = undefined;
|
||||
}*/
|
||||
return pad;
|
||||
} else {
|
||||
console.error("[Cryptpad.migrateRecentPads] pad had unexpected value");
|
||||
console.log(pad);
|
||||
return {};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/* fetch and migrate your pad history from localStorage */
|
||||
var getRecentPads = common.getRecentPads = function () {
|
||||
var recentPadsStr = localStorage[storageKey];
|
||||
|
||||
var recentPads = [];
|
||||
@@ -32,28 +75,60 @@ define([
|
||||
// just overwrite it.
|
||||
}
|
||||
}
|
||||
return recentPads;
|
||||
return migrateRecentPads(recentPads);
|
||||
};
|
||||
|
||||
var setRecentPads = function (pads) {
|
||||
localStorage[storageKey] = JSON.stringify(pads);
|
||||
/* commit a list of pads to localStorage */
|
||||
var setRecentPads = common.setRecentPads = function (pads) {
|
||||
localStorage.setItem(storageKey, JSON.stringify(pads));
|
||||
};
|
||||
|
||||
/* Sort pads according to how recently they were accessed */
|
||||
var mostRecent = common.mostRecent = function (a, b) {
|
||||
return new Date(b.atime).getTime() - new Date(a.atime).getTime();
|
||||
};
|
||||
|
||||
var forgetPad = common.forgetPad = function (href) {
|
||||
var recentPads = getRecentPads().filter(function (pad) {
|
||||
return pad.href !== href;
|
||||
});
|
||||
setRecentPads(recentPads);
|
||||
};
|
||||
|
||||
var rememberPad = common.rememberPad = window.rememberPad = function (title) {
|
||||
// bail out early
|
||||
if (!/#/.test(window.location.hash)) { return; }
|
||||
|
||||
var recentPads = getRecentPads();
|
||||
var pads = getRecentPads();
|
||||
|
||||
var now = new Date();
|
||||
var href = window.location.href;
|
||||
|
||||
var out = recentPads.filter(function (pad) {
|
||||
return (pad && pad[0] !== window.location.href &&
|
||||
(now.getTime() - new Date(pad[1]).getTime()) < timeframe);
|
||||
var isUpdate = false;
|
||||
|
||||
var out = pads.map(function (pad) {
|
||||
if (pad && pad.href === href) {
|
||||
isUpdate = true;
|
||||
// bump the atime
|
||||
pad.atime = now;
|
||||
|
||||
pad.title = title;
|
||||
}
|
||||
return pad;
|
||||
}).filter(function (pad) {
|
||||
// remove pads with an expired atime
|
||||
return (now.getTime() - new Date(pad.atime).getTime()) < timeframe;
|
||||
});
|
||||
|
||||
// href, atime, name
|
||||
out.push([window.location.href, now, title || '']);
|
||||
if (!isUpdate) {
|
||||
// href, atime, name
|
||||
out.push({
|
||||
href: href,
|
||||
atime: now,
|
||||
ctime: now,
|
||||
title: title || window.location.hash.slice(1,9),
|
||||
});
|
||||
}
|
||||
setRecentPads(out);
|
||||
};
|
||||
|
||||
@@ -62,14 +137,13 @@ define([
|
||||
var recent = getRecentPads();
|
||||
|
||||
var renamed = recent.map(function (pad) {
|
||||
if (pad[0] === href) {
|
||||
if (pad.href === href) {
|
||||
// update the atime
|
||||
pad[1] = new Date().toISOString();
|
||||
pad.atime = new Date().toISOString();
|
||||
|
||||
// set the name
|
||||
pad[2] = name;
|
||||
pad.title = name;
|
||||
}
|
||||
//console.log(pad);
|
||||
return pad;
|
||||
});
|
||||
|
||||
@@ -81,14 +155,22 @@ define([
|
||||
var hashSlice = window.location.hash.slice(1,9);
|
||||
var title = '';
|
||||
getRecentPads().some(function (pad) {
|
||||
if (pad[0] === href) {
|
||||
title = pad[2] || hashSlice;
|
||||
if (pad.href === href) {
|
||||
title = pad.title || hashSlice;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return title;
|
||||
};
|
||||
|
||||
var causesNamingConflict = common.causesNamingConflict = function (title) {
|
||||
var href = window.location.href;
|
||||
return getRecentPads().some(function (pad) {
|
||||
return pad.title === title &&
|
||||
pad.href !== href;
|
||||
});
|
||||
};
|
||||
|
||||
var importContent = common.importContent = function (type, f) {
|
||||
return function () {
|
||||
var $files = $('<input type="file">').click();
|
||||
|
||||
Reference in New Issue
Block a user