Bug 1144380 - active state on reading list sidebar persists after switching tabs, r=markh. a=readinglist
authorFlorian Quèze <florian@queze.net>
Fri, 20 Mar 2015 12:05:35 -0700
changeset 259723 4c492ecaf5e7
parent 259722 b0e7f69c3590
child 259724 2dbde197a04e
push id721
push userjlund@mozilla.com
push date2015-04-21 23:03 +0000
treeherdermozilla-release@d27c9211ebb3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh, readinglist
bugs1144380
milestone38.0a2
Bug 1144380 - active state on reading list sidebar persists after switching tabs, r=markh. a=readinglist
browser/base/content/browser-readinglist.js
browser/components/readinglist/sidebar.js
--- a/browser/base/content/browser-readinglist.js
+++ b/browser/base/content/browser-readinglist.js
@@ -237,22 +237,30 @@ let ReadingListUI = {
     if (this.enabled && state == "valid") {
       uri = gBrowser.currentURI;
       if (uri.schemeIs("about"))
         uri = ReaderParent.parseReaderUrl(uri.spec);
       else if (!uri.schemeIs("http") && !uri.schemeIs("https"))
         uri = null;
     }
 
+    let msg = {topic: "UpdateActiveItem", url: null};
     if (!uri) {
       this.toolbarButton.setAttribute("hidden", true);
+      if (this.isSidebarOpen)
+        document.getElementById("sidebar").contentWindow.postMessage(msg, "*");
       return;
     }
 
     let isInList = yield ReadingList.containsURL(uri);
+    if (this.isSidebarOpen) {
+      if (isInList)
+        msg.url = typeof uri == "string" ? uri : uri.spec;
+      document.getElementById("sidebar").contentWindow.postMessage(msg, "*");
+    }
     this.setToolbarButtonState(isInList);
   }),
 
   /**
    * Set the state of the ReadingList toolbar button in the urlbar.
    * If the current tab's page is in the ReadingList (active), sets the button
    * to allow removing the page. Otherwise, sets the button to allow adding the
    * page (not active).
@@ -312,16 +320,20 @@ let ReadingListUI = {
   /**
    * ReadingList event handler for when an item is added.
    *
    * @param {ReadingListItem} item - Item added.
    */
   onItemAdded(item) {
     if (this.isItemForCurrentBrowser(item)) {
       this.setToolbarButtonState(true);
+      if (this.isSidebarOpen) {
+        let msg = {topic: "UpdateActiveItem", url: item.url};
+        document.getElementById("sidebar").contentWindow.postMessage(msg, "*");
+      }
     }
   },
 
   /**
    * ReadingList event handler for when an item is deleted.
    *
    * @param {ReadingListItem} item - Item deleted.
    */
--- a/browser/components/readinglist/sidebar.js
+++ b/browser/components/readinglist/sidebar.js
@@ -56,16 +56,18 @@ let RLSidebar = {
     this.list = document.getElementById("list");
     this.emptyListInfo = document.getElementById("emptyListInfo");
     this.itemTemplate = document.getElementById("item-template");
 
     this.list.addEventListener("click", event => this.onListClick(event));
     this.list.addEventListener("mousemove", event => this.onListMouseMove(event));
     this.list.addEventListener("keydown", event => this.onListKeyDown(event), true);
 
+    window.addEventListener("message", event => this.onMessage(event));
+
     this.listPromise = this.ensureListItems();
     ReadingList.addListener(this);
 
     let initEvent = new CustomEvent("Initialized", {bubbles: true});
     document.documentElement.dispatchEvent(initEvent);
   },
 
   /**
@@ -181,24 +183,18 @@ let RLSidebar = {
   set activeItem(node) {
     if (node && node.parentNode != this.list) {
       log.error(`Unable to set activeItem to invalid node ${node}`);
       return;
     }
 
     log.debug(`Setting activeItem: ${node ? node.id : null}`);
 
-    if (node) {
-      if (!node.classList.contains("selected")) {
-        this.selectedItem = node;
-      }
-
-      if (node.classList.contains("active")) {
-        return;
-      }
+    if (node && node.classList.contains("active")) {
+      return;
     }
 
     let prevItem = document.querySelector("#list > .item.active");
     if (prevItem) {
       prevItem.classList.remove("active");
     }
 
     if (node) {
@@ -411,12 +407,32 @@ let RLSidebar = {
     } else if (event.keyCode == KeyEvent.DOM_VK_RETURN) {
       let selectedItem = this.selectedItem;
       if (selectedItem) {
         this.activeItem = this.selectedItem;
         this.openActiveItem(event);
       }
     }
   },
+
+  /**
+   * Handle a message, typically sent from browser-readinglist.js
+   * @param {Event} event - Triggering event.
+   */
+  onMessage(event) {
+    let msg = event.data;
+
+    if (msg.topic != "UpdateActiveItem") {
+      return;
+    }
+
+    if (!msg.url) {
+      this.activeItem = null;
+    } else {
+      ReadingList.getItemForURL(msg.url).then(item => {
+        this.activeItem = this.itemNodesById.get(item.id);
+      });
+    }
+  }
 };
 
 
 addEventListener("DOMContentLoaded", () => RLSidebar.init());