Remember which sites are removed and don't show them on new tabs
authorEdward Lee <edilee@mozilla.com>
Fri, 13 Mar 2009 16:31:30 -0500
changeset 124 e578c454274c
parent 123 48eccc5283ee
child 125 bcbeae74dd68
push id86
push useredward.lee@engineering.uiuc.edu
push dateFri, 13 Mar 2009 21:31:55 +0000
Remember which sites are removed and don't show them on new tabs
chrome/content/text.html
modules/sites.js
--- a/chrome/content/text.html
+++ b/chrome/content/text.html
@@ -55,16 +55,19 @@
   #freq>ol>li{ padding-left: 45px;}
   
   #freq>ol span{ position: absolute; margin-left: -32px; margin-top:-2px;}
   #freq>ol img.favicon{ width: 16px; height: 16px; display:inline-block;}
   #freq>ol img.handle{ position: relative; top: 3px; cursor: move; padding-left: 8px;}
 
   #freq>ol edit{ font-size: 90%; opacity: .4; font-style: italic;}
   #freq>ol edit:hover{ text-decoration: underline;}
+  #freq>ol edit{ display: none; }
+  #freq>ol remove{ font-size: 90%; opacity: .4; font-style: italic;}
+  #freq>ol remove:hover{ text-decoration: underline;}
 
   #freq>ol .options{ padding-left: 23px; color: #cc2200; padding-top: 10px; display: none;}      
   #freq>ol .hover-actions{ display: inline-block; float:right; display: none;}
   
   #freq li ul{ list-style: none; padding-left: 0; color:#666; padding-top: 5px;}
   #freq li li{ color: #666; line-height: 14pt; cursor: pointer;}
   #freq li li:hover { text-decoration: underline;}  
 
@@ -205,16 +208,17 @@
 </div>
 
 <div id="templates">
   <li class="site">
     <span><img class="favicon" src="#"/></span>
     <name></name>
     
     <div class="hover-actions">
+      <remove>remove</remove>
       <edit>edit</edit>
       <img class="handle" src="chrome://abouttab/content/gfx/handle.png"/>
     </div>
 
     <ul class="rss"></ul>
     <div class="options">
       Configuration options go here: remove, rename, etc. <br/>
       <input type="text" value="feed url"/> 
@@ -249,16 +253,21 @@ function addSite( item ) {
     });
   el.find(".favicon").attr({ src: item.favicon });
   
   el.hover(
     function(){$(this).find(".hover-actions").show();},
     function(){$(this).find(".hover-actions").hide();}
   );
   
+  el.find("remove").click(function() {
+    Sites.remove(getPos(el));
+    window.location.reload();
+  });
+
   el.find("edit").click(function(){
     var parent = $(this).parents("li.site");
     parent.find(".rss").slideUp("fast");
     parent.find(".options").slideDown("fast");
     parent.find("edit").hide();
   });
   
   el.find(".options button").click(function(){
--- a/modules/sites.js
+++ b/modules/sites.js
@@ -37,43 +37,50 @@
 const EXPORTED_SYMBOLS = ['Sites'];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 let cache = [];
+let removed = [];
 
 let Sites = {
   get: function Sites_get(maxSites) {
+    // Helper to consistently return the right size of results
+    let ret = function Sites_getRet() cache.slice(0, maxSites);
+
     // Sanity check for injecting into SQL
     maxSites = parseInt(maxSites);
 
     // Use the cached results if we have enough
-    if (maxSites <= cache.length) {
-      return cache.slice(0, maxSites);
-    }
+    if (maxSites <= cache.length)
+      return ret();
+
+    // Fetch more than necessary sites to prepare for needing more sites
+    let numSites = maxSites * 3 + removed.length;
 
     // Standard filtering to get non-hidden, typed history
     let baseWhere = "frecency != 0 AND visit_count > 0 AND typed = 1 ";
     // Get a column for the top page of a site
     let getCol = function(col) "(SELECT " + col + " FROM moz_places " + 
       "WHERE favicon_id = s.favicon_id AND " + baseWhere +
-      "ORDER BY frecency DESC LIMIT 1) ";
+      "ORDER BY frecency DESC LIMIT 1) AS _" + col + " ";
   
     let sql = Cc["@mozilla.org/browser/nav-history-service;1"].
       getService(Ci.nsPIPlacesDatabase).DBConnection.createStatement(
         "SELECT f.url, " + ["title", "url", "id"].map(getCol) +
         "FROM moz_places s " +
         "JOIN moz_favicons f ON f.id = s.favicon_id " +
-        "WHERE favicon_id NOT NULL AND " + baseWhere + 
+        "WHERE favicon_id NOT NULL AND " + baseWhere + "AND " +
+          "_id NOT IN (" + removed + ") " +
         "GROUP BY favicon_id " +
         "ORDER BY MAX(frecency) DESC " +
-        "LIMIT " + maxSites);
+        "LIMIT " + numSites);
     let attrs = ["favicon", "title", "uri", "id"];
 
     try {
       // Reset the cache
       cache = [];
 
       // Fetch each result and store them in the cache
       while (sql.executeStep()) {
@@ -92,23 +99,43 @@ let Sites = {
       Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
         logStringMessage(ex);
     }
     finally {
       sql.reset();
       sql.finalize();
     }
 
-    return cache;
+    // Just give the number of sites requested
+    return ret();
   },
 
+  /**
+   * Move an item at the original position to the new position causing other
+   * items to get shifted if needed
+   *
+   * @param origPos
+   *        Item's original position
+   * @param newPos
+   *        New position for the item
+   */
   move: function Sites_move(origPos, newPos) {
     // Remove the item from the orig position and put it at the new position
     cache.splice(newPos, 0, cache.splice(origPos, 1)[0]);
   },
+
+  /**
+   * Remove the item in the given cache position
+   *
+   * @param pos
+   *        Position to remove
+   */
+  remove: function Sites_remove(pos) {
+    removed.push(cache.splice(pos, 1)[0].id);
+  },
 };
 
 function toURI(uriString) {
   if (!uriString)
     return null;
 
   return Cc["@mozilla.org/network/io-service;1"].
     getService(Ci.nsIIOService).