Prevent unnecessary DOM finds in JS resorting functions.

Also removed now unused functions earlierPage() and laterPage().
pull/127/merge
luccioman 8 years ago
parent b1b9ffbbc8
commit fb6743e8f8

@ -30,39 +30,37 @@ var logEnabled = false;
var feedRunning = true; var feedRunning = true;
var displayPage = function() { var displayPage = function() {
// For every search item that has already been displayed... var offset = 1;
$("#resultscontainer").find(".searchresults").each( function(i) { var totalcount = 0;
// Apply the "earlierpage" class IFF the item is from an earlier page. var itemscount = 0;
$(this).toggleClass("earlierpage", parseFloat($(this).data("ranking")) > highestRanking); // For every search item that has already been displayed and sorted ...
}); $("#resultscontainer").find(".searchresults").each(function(i) {
totalcount++;
// For every search item from an earlier page... var earlierPage = parseFloat($(this).data("ranking")) > highestRanking;
$("#resultscontainer").find(".searchresults.earlierpage").each( function(i) { // Apply the "earlierpage" class IFF the item is from an earlier page.
// Hide the item $(this).toggleClass("earlierpage", earlierPage);
$(this).removeClass("currentpage"); if (earlierPage) {
}); $(this).removeClass("currentpage");
offset++;
// For every search item from a current or later page... itemscount++;
$("#resultscontainer").find(".searchresults:not(.earlierpage)").each( function(i) { } else {
var laterPage = (i >= requestedResults); var laterPage = ((i - offset + 1) >= requestedResults);
$(this).toggleClass("laterpage", laterPage); $(this).toggleClass("laterpage", laterPage);
// If we now have too many results, hide the lowest-ranking ones. // If we now have too many results, hide the lowest-ranking ones.
if (laterPage) { if (laterPage) {
$(this).removeClass("currentpage"); $(this).removeClass("currentpage");
} } else {
else { $(this).removeClass("hidden");
$(this).removeClass("hidden"); $(this).addClass("currentpage");
$(this).addClass("currentpage"); itemscount++;
} }
}); }
});
// TODO: The following statistical displays could maybe be moved to the latestinfo() call.
// TODO: collected totalcount here seems to often be smaller than the "totalcount" that statistics() ends up with. Why is that?
var offset = $("#resultscontainer").find(".searchresults.earlierpage").length + 1;
var itemscount = $("#resultscontainer").find(".searchresults.earlierpage").length + $("#resultscontainer").find(".searchresults.currentpage").length; // TODO: The following statistical displays could maybe be moved to the
// latestinfo() call.
// TODO: This seems to often be smaller than the "totalcount" that statistics() ends up with. Why is that?
var totalcount = $("#resultscontainer").find(".searchresults").length;
$("#offset").html(offset); $("#offset").html(offset);
$("#itemscount").html(itemscount); $("#itemscount").html(itemscount);
@ -78,48 +76,6 @@ var displayPage = function() {
} }
}; };
var earlierPage = function() {
// Find all items that are on an earlier page.
var allEarlierItems = $("#resultscontainer").find(".searchresults.earlierpage");
// If going back one page would put us at the beginning...
if (allEarlierItems.length <= requestedResults) {
highestRanking = Infinity;
}
// If going back one page would still be in the middle of the results...
else {
var earlierItem = allEarlierItems.get().reverse()[ requestedResults - 1 ];
highestRanking = parseFloat($(earlierItem).data("ranking"));
if(logEnabled) {
console.log("highestRanking is now " + highestRanking);
}
}
// Update the display to show the new page.
displayPage();
};
var laterPage = function() {
// Find all items that are on a later page.
var allCurrentAndLaterItems = $("#resultscontainer").find(".searchresults:not(.earlierpage)");
// If going forward one page would put us past the end...
if (allCurrentAndLaterItems.length <= requestedResults) {
return;
}
// If going forward one page would still be in the middle of the results...
else {
var laterItem = allCurrentAndLaterItems.get(requestedResults);
highestRanking = parseFloat($(laterItem).data("ranking"));
if(logEnabled) {
console.log("highestRanking is now " + highestRanking);
}
}
// Update the display to show the new page.
displayPage();
};
// pageNumber starts at 0. // pageNumber starts at 0.
var numberedPage = function(pageNumber) { var numberedPage = function(pageNumber) {
// Find all items. // Find all items.
@ -353,38 +309,41 @@ var processItem = function(data) {
} }
// For every search item that has already been displayed... // For every search item that has already been displayed...
$("#resultscontainer").find(".searchresults").each( function(i) { var allResults = $("#resultscontainer").find(".searchresults");
// If the existing search item is lower-ranked than the new item... var allResultsLength = allResults.length;
if (parseFloat($(this).data("ranking")) <= parseFloat(newItem.data("ranking")) ) {
// Insert new item before the existing item // Special case if this is the first search item.
newItem.insertBefore(this); if(allResultsLength === 0) {
// Display the new item
return false; newItem.appendTo("#resultscontainer");
} } else {
// If the new item is lower-ranked than all existing items... allResults.each( function(i) {
else if (i == $("#resultscontainer").find(".searchresults").length - 1) { // If the existing search item is lower-ranked than the new item...
// And if the new item (position i + 1) would be ranked 0 to requestedResults - 1... if (parseFloat($(this).data("ranking")) <= parseFloat(newItem.data("ranking")) ) {
if (i + 1 < requestedResults) { // Insert new item before the existing item
// Insert new item at the end newItem.insertBefore(this);
newItem.appendTo("#resultscontainer");
return false; return false;
} }
// If the new item is too irrelevant to be displayed... // If the new item is lower-ranked than all existing items...
else { else if (i == allResultsLength - 1) {
// Insert new item at the end // And if the new item (position i + 1) would be ranked 0 to requestedResults - 1...
newItem.appendTo("#resultscontainer"); if (i + 1 < requestedResults) {
if(logEnabled) { // Insert new item at the end
console.log("Hiding search result because ranking " + newItem.data("ranking") + " too low."); newItem.appendTo("#resultscontainer");
} return false;
return false; }
} // If the new item is too irrelevant to be displayed...
} else {
}); // Insert new item at the end
newItem.appendTo("#resultscontainer");
// Special case if this is the first search item... if(logEnabled) {
if ($("#resultscontainer").find(".searchresults").length === 0) { console.log("Hiding search result because ranking " + newItem.data("ranking") + " too low.");
// Display the new item }
newItem.appendTo("#resultscontainer"); return false;
}
}
});
} }
displayPage(); displayPage();

Loading…
Cancel
Save