1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Use date and version for previous paste keys in local storage

This commit is contained in:
sam 2012-05-16 18:44:56 +02:00
parent 2a55070305
commit 461fa4992d

View File

@ -159,15 +159,22 @@ window.zerobin = {
return (a-b); return (a-b);
}, },
getKeys: function(){ /** Return a reverse sorted list of all the keys in local storage that
are prefixed with with the passed version (default being this lib
version) */
getLocalStorageKeys: function(version){
version = version || zerobin.version;
var keys = []; var keys = [];
for(var i = 0; i <= localStorage.length; i++){ for (var key in localStorage){
if(localStorage.key(i) !== null){ if (key.indexOf(version) !== -1){
keys[i] = parseInt(localStorage.key(i), 10); keys.push(key);
} }
} }
return keys.sort(zerobin.numOrdA); keys.sort();
keys.reverse();
return keys;
}, },
/** Get a tinyurl using JSONP */ /** Get a tinyurl using JSONP */
getTinyURL: function(longURL, success) { getTinyURL: function(longURL, success) {
var api = 'http://json-tinyurl.appspot.com/?url='; var api = 'http://json-tinyurl.appspot.com/?url=';
@ -199,39 +206,43 @@ window.zerobin = {
})() })()
}, },
storatePaste: function(url){ /** Store the paste of a URL in local storate, with a storage format
if (zerobin.support.localStorage){ version prefix and the paste date as the key */
var paste = (zerobin.getFormatedDate() + " " + storePaste: function(url, date){
zerobin.getFormatedTime() + ";" + url);
var keys = zerobin.getKeys();
if(keys.length < 1){keys[0] = 0;} date = date || new Date();
date = (date.getFullYear() + '-' + (date.getMonth() + 1) + '-' +
date.getDate() + ' ' + zerobin.getFormatedTime(date));
var keys = zerobin.getLocalStorageKeys();
if (localStorage.length > 19) { if (localStorage.length > 19) {
void localStorage.removeItem(keys[0]); void localStorage.removeItem(keys[19]);
} }
localStorage.setItem(keys.reverse()[0]+1, paste); localStorage.setItem(zerobin.version + "#" + date, url);
}
}, },
/** Return a list of the previous paste url with creation date */ /** Return a list of the previous paste url with the creation date
If the paste is from today, date format should be "at hh:ss",
else it should be "the mm-dd-yyy"
*/
getPreviousPastes: function(){ getPreviousPastes: function(){
var pastes = [], var pastes = [],
keys = zerobin.getKeys(), keys = zerobin.getLocalStorageKeys(),
date = zerobin.getFormatedDate(); today = zerobin.getFormatedDate();
keys.reverse();
for (var i = 0; i <= keys.length-1; i++) { $.each(keys, function(i, key){
var paste = localStorage.getItem(keys[i]).split(';'); var pasteDateTime = key.replace(/^[^#]+#/, '');
var displayDate = paste[0].split(' ')[0]; var displayDate = zerobin.getFormatedDate(new Date(pasteDateTime));
var prefix = 'the '; var prefix = 'the ';
if (displayDate === date){ if (displayDate === today){
displayDate = paste[0].split(' ')[1]; displayDate = pasteDateTime.split(' ')[1];
prefix = 'at '; prefix = 'at ';
} }
pastes.push({displayDate: displayDate, prefix: prefix, link: paste[1]}); pastes.push({displayDate: displayDate, prefix: prefix,
} link: localStorage.getItem(key)});
});
return pastes; return pastes;
}, },
@ -396,7 +407,9 @@ $('.btn-primary').live("click", function(e){
bar.container.hide(); bar.container.hide();
} else { } else {
var paste_url = '/paste/' + data.paste + '#' + key; var paste_url = '/paste/' + data.paste + '#' + key;
zerobin.storatePaste(paste_url); if (zerobin.support.localStorage){
zerobin.storePaste(paste_url);
}
window.location = (paste_url); window.location = (paste_url);
} }
}); });