Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
clean up code for recently saved fetching, sorting, rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kenliu committed Aug 29, 2011
1 parent 602d500 commit 4c9e9c8
Showing 1 changed file with 20 additions and 44 deletions.
64 changes: 20 additions & 44 deletions src/main/popup.html
Expand Up @@ -84,58 +84,24 @@
);
}

function loadRecentlySaved() {
var opts = {
'page': CURRENT_PAGE
}

function loadRecentlySaved() {
// hide prev navigation if on first page
if (CURRENT_PAGE == 1) {
$('#prev_page').css('visibility', 'hidden');
} else {
$('#prev_page').css('visibility', 'visible');
}
$('#prev_page').css('visibility', (CURRENT_PAGE == 1) ? 'hidden' : 'visible');

get(localStorage.username, localStorage.password, opts,
get(localStorage.username, localStorage.password, { 'page': CURRENT_PAGE },
function(xhr) {
var response = JSON.parse(xhr.responseText);
var urls = response.list;

//this is inefficient, but we now loop through
if (!$.isArray(urls)) { //RIL API returns an empty array if there are no more pages

//JSON.parse does not respect the order of the urls returned so we have to re-sort on our own
//we get the time stamps into an array, then do a sort so that we get the most recent time stamps at the top
var sort_array = new Array();
for (item in urls){
var link = urls[item];
sort_array.push(link.time_added);
}
sort_array = sort_array.sort().reverse();

//we have the sorted timestamps, now we loop through the list of urls to get the urls with matching timestamps
var html = '<ul class="recent_urls">';
for (var i=0; i<sort_array.length; i++) {
for (item in urls){
var link = urls[item]
if(link.time_added == sort_array[i]){
var linktext = (link.title != "") ? link.title : (link.url.substring(0, Math.min(50, link.url.length))) //show URL if no title, truncate if necessary
//TODO is this the best way to deal with long URLs?
html += '<li><a title="' + link.url + '" href="' + link.url + '" target="new">' + linktext + '</a></li>';
}
}
}
html += '</ul>';
console.log(html);
$('#recent_urls').html(html);
//$('#recent_urls li').append();
if (!$.isArray(urls)) { //RIL API returns an empty array if there are no more pages
//JSON.parse does not respect the order of the urls returned so we sort by time_added
var sorted = _(urls).sortBy(function(url){ return url.time_added; }).reverse();
$('#recent_urls').html(renderRecentlySaved(sorted));
} else {
CURRENT_PAGE--; //not very elegant way to prevent from paging forward
}

}
}
);

);
//TODO hide next button on last page
}

Expand All @@ -149,7 +115,17 @@
CURRENT_PAGE--;
}
loadRecentlySaved();
}
}

function renderRecentlySaved(items, number){
var lis = _(items).map(function(link){
var linktext = (link.title != "") ? link.title : (link.url.substring(0, Math.min(50, link.url.length))); //show URL if no title, truncate if necessary
return '<li><a title="' + link.url + '" href="' + link.url + '" target="new">' + linktext + '</a></li>';
});
var html = _(lis).reduce(function(memo, it){ return memo + it }, '<ul class="recent_urls">') + '</ul>';
//console.log(html);
return html;
}

function saveAll(){
chrome.tabs.getAllInWindow(null, function(tabs) {
Expand Down

0 comments on commit 4c9e9c8

Please sign in to comment.