Skip to content

Commit 461fa49

Browse files
committed
Use date and version for previous paste keys in local storage
1 parent 2a55070 commit 461fa49

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

zerobin/static/js/behavior.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,22 @@ window.zerobin = {
159159
return (a-b);
160160
},
161161

162-
getKeys: function(){
162+
/** Return a reverse sorted list of all the keys in local storage that
163+
are prefixed with with the passed version (default being this lib
164+
version) */
165+
getLocalStorageKeys: function(version){
166+
version = version || zerobin.version;
163167
var keys = [];
164-
for(var i = 0; i <= localStorage.length; i++){
165-
if(localStorage.key(i) !== null){
166-
keys[i] = parseInt(localStorage.key(i), 10);
167-
}
168+
for (var key in localStorage){
169+
if (key.indexOf(version) !== -1){
170+
keys.push(key);
171+
}
168172
}
169-
return keys.sort(zerobin.numOrdA);
173+
keys.sort();
174+
keys.reverse();
175+
return keys;
170176
},
177+
171178
/** Get a tinyurl using JSONP */
172179
getTinyURL: function(longURL, success) {
173180
var api = 'http://json-tinyurl.appspot.com/?url=';
@@ -199,39 +206,43 @@ window.zerobin = {
199206
})()
200207
},
201208

202-
storatePaste: function(url){
203-
if (zerobin.support.localStorage){
204-
var paste = (zerobin.getFormatedDate() + " " +
205-
zerobin.getFormatedTime() + ";" + url);
206-
var keys = zerobin.getKeys();
209+
/** Store the paste of a URL in local storate, with a storage format
210+
version prefix and the paste date as the key */
211+
storePaste: function(url, date){
207212

208-
if(keys.length < 1){keys[0] = 0;}
213+
date = date || new Date();
214+
date = (date.getFullYear() + '-' + (date.getMonth() + 1) + '-' +
215+
date.getDate() + ' ' + zerobin.getFormatedTime(date));
216+
217+
var keys = zerobin.getLocalStorageKeys();
209218

210219
if (localStorage.length > 19) {
211-
void localStorage.removeItem(keys[0]);
220+
void localStorage.removeItem(keys[19]);
212221
}
213222

214-
localStorage.setItem(keys.reverse()[0]+1, paste);
215-
}
223+
localStorage.setItem(zerobin.version + "#" + date, url);
216224
},
217225

218-
/** Return a list of the previous paste url with creation date */
226+
/** Return a list of the previous paste url with the creation date
227+
If the paste is from today, date format should be "at hh:ss",
228+
else it should be "the mm-dd-yyy"
229+
*/
219230
getPreviousPastes: function(){
220231
var pastes = [],
221-
keys = zerobin.getKeys(),
222-
date = zerobin.getFormatedDate();
223-
keys.reverse();
232+
keys = zerobin.getLocalStorageKeys(),
233+
today = zerobin.getFormatedDate();
224234

225-
for (var i = 0; i <= keys.length-1; i++) {
226-
var paste = localStorage.getItem(keys[i]).split(';');
227-
var displayDate = paste[0].split(' ')[0];
235+
$.each(keys, function(i, key){
236+
var pasteDateTime = key.replace(/^[^#]+#/, '');
237+
var displayDate = zerobin.getFormatedDate(new Date(pasteDateTime));
228238
var prefix = 'the ';
229-
if (displayDate === date){
230-
displayDate = paste[0].split(' ')[1];
239+
if (displayDate === today){
240+
displayDate = pasteDateTime.split(' ')[1];
231241
prefix = 'at ';
232242
}
233-
pastes.push({displayDate: displayDate, prefix: prefix, link: paste[1]});
234-
}
243+
pastes.push({displayDate: displayDate, prefix: prefix,
244+
link: localStorage.getItem(key)});
245+
});
235246

236247
return pastes;
237248
},
@@ -396,7 +407,9 @@ $('.btn-primary').live("click", function(e){
396407
bar.container.hide();
397408
} else {
398409
var paste_url = '/paste/' + data.paste + '#' + key;
399-
zerobin.storatePaste(paste_url);
410+
if (zerobin.support.localStorage){
411+
zerobin.storePaste(paste_url);
412+
}
400413
window.location = (paste_url);
401414
}
402415
});

0 commit comments

Comments
 (0)