From bfaebdfb1ca023f70fdb13c512172bafce7fed17 Mon Sep 17 00:00:00 2001 From: Shane Caraveo Date: Fri, 3 Aug 2012 14:42:40 -0700 Subject: [PATCH] Bug 777177 - Keep the social sidebar loaded, and dispatch events when it is opened/closed, r=gavin --HG-- extra : rebase_source : 169f829e129cdabf64db8aea28201a7660fd0446 --- browser/base/content/browser-social.js | 39 +++++++++---- .../content/test/browser_social_sidebar.js | 57 +++++++++++++------ 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/browser/base/content/browser-social.js b/browser/base/content/browser-social.js index 078e607c8dc7..5fa88f5c3b1e 100644 --- a/browser/base/content/browser-social.js +++ b/browser/base/content/browser-social.js @@ -406,6 +406,13 @@ var SocialSidebar = { return Services.prefs.getBoolPref("social.sidebar.open"); }, + dispatchEvent: function(aType, aDetail) { + let sbrowser = document.getElementById("social-sidebar-browser"); + let evt = sbrowser.contentDocument.createEvent("CustomEvent"); + evt.initCustomEvent(aType, true, true, aDetail ? aDetail : {}); + sbrowser.contentDocument.documentElement.dispatchEvent(evt); + }, + updateSidebar: function SocialSidebar_updateSidebar() { // Hide the toggle menu item if the sidebar cannot appear let command = document.getElementById("Social:ToggleSidebar"); @@ -418,17 +425,29 @@ var SocialSidebar = { broadcaster.hidden = hideSidebar; command.setAttribute("checked", !hideSidebar); - // If the sidebar is hidden, unload its document - // XXX this results in a poor UX, we should revisit let sbrowser = document.getElementById("social-sidebar-browser"); - if (broadcaster.hidden) { - sbrowser.removeAttribute("origin"); - sbrowser.setAttribute("src", "about:blank"); - return; + if (hideSidebar) { + this.dispatchEvent("sidebarhide"); + // If we're disabled, unload the sidebar content + if (!this.canShow) { + sbrowser.removeAttribute("origin"); + sbrowser.setAttribute("src", "about:blank"); + } + } else { + // Make sure the right sidebar URL is loaded + if (sbrowser.getAttribute("origin") != Social.provider.origin) { + sbrowser.setAttribute("origin", Social.provider.origin); + sbrowser.setAttribute("src", Social.provider.sidebarURL); + sbrowser.addEventListener("load", function sidebarOnShow() { + sbrowser.removeEventListener("load", sidebarOnShow); + // let load finish, then fire our event + setTimeout(function () { + SocialSidebar.dispatchEvent("sidebarshow"); + }, 0); + }); + } else { + this.dispatchEvent("sidebarshow"); + } } - - // Load the sidebar document - sbrowser.setAttribute("origin", Social.provider.origin); - sbrowser.setAttribute("src", Social.provider.sidebarURL); } } diff --git a/browser/base/content/test/browser_social_sidebar.js b/browser/base/content/test/browser_social_sidebar.js index 052ba6a3bcc6..0da759b48c51 100644 --- a/browser/base/content/test/browser_social_sidebar.js +++ b/browser/base/content/test/browser_social_sidebar.js @@ -15,9 +15,9 @@ function test() { let manifest = { // normal provider name: "provider 1", - origin: "https://example1.com", - sidebarURL: "https://example1.com/sidebar.html", - workerURL: "https://example1.com/worker.js", + origin: "https://example.com", + sidebarURL: "https://example.com/browser/browser/base/content/test/social_sidebar.html", + workerURL: "https://example.com/browser/browser/base/content/test/social_worker.js", iconURL: "chrome://branding/content/icon48.png" }; runSocialTestWithProvider(manifest, doTest); @@ -29,24 +29,45 @@ function doTest() { let command = document.getElementById("Social:ToggleSidebar"); let sidebar = document.getElementById("social-sidebar-box"); + let browser = sidebar.firstChild; - // Check the the sidebar is initially visible, and loaded - ok(!command.hidden, "sidebar toggle command should be visible"); - is(command.getAttribute("checked"), "true", "sidebar toggle command should be checked"); - ok(!sidebar.hidden, "sidebar itself should be visible"); - ok(Services.prefs.getBoolPref("social.sidebar.open"), "sidebar open pref should be true"); - is(sidebar.firstChild.getAttribute('src'), "https://example1.com/sidebar.html", "sidebar url should be set"); + function checkShown(shouldBeShown) { + is(command.getAttribute("checked"), shouldBeShown ? "true" : "false", + "toggle command should be " + (shouldBeShown ? "checked" : "unchecked")); + is(sidebar.hidden, !shouldBeShown, + "sidebar should be " + (shouldBeShown ? "visible" : "hidden")); + is(Services.prefs.getBoolPref("social.sidebar.open"), shouldBeShown, + "sidebar open pref should be " + shouldBeShown); + if (shouldBeShown) + is(browser.getAttribute('src'), Social.provider.sidebarURL, "sidebar url should be set"); + } - // Now toggle it! - info("Toggling sidebar"); + // First check the the sidebar is initially visible, and loaded + ok(!command.hidden, "toggle command should be visible"); + checkShown(true); + + browser.addEventListener("sidebarhide", function sidebarhide() { + browser.removeEventListener("sidebarhide", sidebarhide); + + checkShown(false); + + browser.addEventListener("sidebarshow", function sidebarshow() { + browser.removeEventListener("sidebarshow", sidebarshow); + + checkShown(true); + + // Remove the test provider + SocialService.removeProvider(Social.provider.origin, finish); + }); + + // Toggle it back on + info("Toggling sidebar back on"); + Social.toggleSidebar(); + }); + + // Now toggle it off + info("Toggling sidebar off"); Social.toggleSidebar(); - is(command.getAttribute("checked"), "false", "sidebar toggle command should not be checked"); - ok(sidebar.hidden, "sidebar itself should not be visible"); - ok(!Services.prefs.getBoolPref("social.sidebar.open"), "sidebar open pref should be false"); - is(sidebar.firstChild.getAttribute('src'), "about:blank", "sidebar url should not be set"); - - // Remove the test provider - SocialService.removeProvider(Social.provider.origin, finish); } // XXX test sidebar in popup