Bug 1479320 - Lazily load calendar unifinder tree. r=philipp
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 11 Jun 2019 20:28:38 +1200
changeset 35825 4e10b0b8aa3fee9d57b986878c47306dea5d2b70
parent 35824 75267aa625a2ee5cb0581271dec6871c0b3b7fc7
child 35826 02a09c7cc1e384140f9675a9aa3048485f13c519
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersphilipp
bugs1479320
Bug 1479320 - Lazily load calendar unifinder tree. r=philipp
calendar/base/content/calendar-unifinder.js
calendar/lightning/content/messenger-overlay-sidebar.js
--- a/calendar/base/content/calendar-unifinder.js
+++ b/calendar/base/content/calendar-unifinder.js
@@ -1,14 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* exported gCalendarEventTreeClicked, unifinderDoubleClick, unifinderKeyPress,
- *          focusSearch, toggleUnifinder
+ *          focusSearch, ensureUnifinderLoaded, toggleUnifinder
  */
 
 /* import-globals-from ../../../mail/base/content/utilityOverlay.js */
 /* import-globals-from ../../resources/content/mouseoverPreviews.js */
 /* import-globals-from ../src/calFilter.js */
 /* import-globals-from calendar-common-sets.js */
 /* import-globals-from calendar-views-utils.js */
 
@@ -35,17 +35,19 @@ var gCalendarEventTreeClicked = false;
 var gUnifinderNeedsRefresh = true;
 
 /**
  * Checks if the unifinder is hidden
  *
  * @return      Returns true if the unifinder is hidden.
  */
 function isUnifinderHidden() {
-    return document.getElementById("bottom-events-box").hidden;
+    let tabmail = document.getElementById("tabmail");
+    return tabmail.currentTabInfo.mode.type != "calendar" ||
+        document.getElementById("bottom-events-box").hidden;
 }
 
 /**
  * Returns the current filter applied to the unifinder.
  *
  * @return      The string name of the applied filter.
  */
 function getCurrentUnifinderFilter() {
@@ -178,69 +180,67 @@ var unifinderObserver = {
     }
 };
 
 /**
  * Called when the window is loaded to prepare the unifinder. This function is
  * used to add observers, event listeners, etc.
  */
 function prepareCalendarUnifinder() {
-    // Only load once
-    window.removeEventListener("load", prepareCalendarUnifinder);
     let unifinderTree = document.getElementById("unifinder-search-results-tree");
+    // Check that this is not the hidden window, which has no UI elements
+    if (!unifinderTree) {
+        return;
+    }
 
     // Add pref observer
     Services.prefs.addObserver("calendar.date.format", unifinderObserver);
     Services.obs.addObserver(unifinderObserver, "defaultTimezoneChanged");
 
-    // Check if this is not the hidden window, which has no UI elements
-    if (unifinderTree) {
-        // set up our calendar event observer
-        let ccalendar = cal.view.getCompositeCalendar(window);
-        ccalendar.addObserver(unifinderObserver);
+    // set up our calendar event observer
+    let ccalendar = cal.view.getCompositeCalendar(window);
+    ccalendar.addObserver(unifinderObserver);
 
-        // Set up the filter
-        unifinderTreeView.mFilter = new calFilter();
+    // Set up the filter
+    unifinderTreeView.mFilter = new calFilter();
 
-        // Set up the unifinder views.
-        unifinderTreeView.treeElement = unifinderTree;
-        unifinderTree.view = unifinderTreeView;
+    // Set up the unifinder views.
+    unifinderTreeView.treeElement = unifinderTree;
+    unifinderTree.view = unifinderTreeView;
 
-        // Listen for changes in the selected day, so we can update if need be
-        let viewDeck = getViewDeck();
-        viewDeck.addEventListener("dayselect", unifinderDaySelect);
-        viewDeck.addEventListener("itemselect", unifinderItemSelect, true);
+    // Listen for changes in the selected day, so we can update if need be
+    let viewDeck = getViewDeck();
+    viewDeck.addEventListener("dayselect", unifinderDaySelect);
+    viewDeck.addEventListener("itemselect", unifinderItemSelect, true);
 
-        // Set up sortDirection and sortActive, in case it persisted
-        let sorted = unifinderTree.getAttribute("sort-active");
-        let sortDirection = unifinderTree.getAttribute("sort-direction");
-        if (!sortDirection || sortDirection == "undefined") {
-            sortDirection = "ascending";
-        }
-        let tree = document.getElementById("unifinder-search-results-tree");
-        let treecols = tree.getElementsByTagName("treecol");
-        for (let i = 0; i < treecols.length; i++) {
-            let col = treecols[i];
-            let content = col.getAttribute("itemproperty");
-            if (sorted && sorted.length > 0) {
-                if (sorted == content) {
-                    unifinderTreeView.sortDirection = sortDirection;
-                    unifinderTreeView.selectedColumn = col;
-                }
+    // Set up sortDirection and sortActive, in case it persisted
+    let sorted = unifinderTree.getAttribute("sort-active");
+    let sortDirection = unifinderTree.getAttribute("sort-direction");
+    if (!sortDirection || sortDirection == "undefined") {
+        sortDirection = "ascending";
+    }
+    let tree = document.getElementById("unifinder-search-results-tree");
+    let treecols = tree.getElementsByTagName("treecol");
+    for (let col of treecols) {
+        let content = col.getAttribute("itemproperty");
+        if (sorted && sorted.length > 0) {
+            if (sorted == content) {
+                unifinderTreeView.sortDirection = sortDirection;
+                unifinderTreeView.selectedColumn = col;
             }
         }
+    }
 
-        unifinderTreeView.ready = true;
+    unifinderTreeView.ready = true;
 
-        // Display something upon first load. onLoad doesn't work properly for
-        // observers
-        if (!isUnifinderHidden()) {
-            gUnifinderNeedsRefresh = false;
-            refreshEventTree();
-        }
+    // Display something upon first load. onLoad doesn't work properly for
+    // observers
+    if (!isUnifinderHidden()) {
+        gUnifinderNeedsRefresh = false;
+        refreshEventTree();
     }
 }
 
 /**
  * Called when the window is unloaded to clean up any observers and listeners
  * added.
  */
 function finishCalendarUnifinder() {
@@ -925,16 +925,32 @@ function deleteItemsFromCalendar(aCalend
 /**
  * Focuses the unifinder search field
  */
 function focusSearch() {
     document.getElementById("unifinder-search-field").focus();
 }
 
 /**
+ * The unifinder is hidden if the calendar tab is not selected. When the tab
+ * is selected, this function is called so that unifinder setup completes.
+ */
+function ensureUnifinderLoaded() {
+    if (ensureUnifinderLoaded.done) {
+        return;
+    }
+    ensureUnifinderLoaded.done = true;
+
+    if (!isUnifinderHidden() && gUnifinderNeedsRefresh) {
+        gUnifinderNeedsRefresh = false;
+        refreshEventTree();
+    }
+}
+
+/**
  * Toggles the hidden state of the unifinder.
  */
 function toggleUnifinder() {
     // Toggle the elements
     goToggleToolbar("bottom-events-box", "calendar_show_unifinder_command");
     goToggleToolbar("calendar-view-splitter");
 
     unifinderTreeView.treeElement.view = unifinderTreeView;
@@ -948,10 +964,10 @@ function toggleUnifinder() {
 
     // Make sure the selection is correct
     if (unifinderTreeView.doingSelection) {
         unifinderTreeView.resetAllowSelection();
     }
     unifinderTreeView.setSelectedItems();
 }
 
-window.addEventListener("load", prepareCalendarUnifinder);
+window.addEventListener("load", prepareCalendarUnifinder, { once: true });
 window.addEventListener("unload", finishCalendarUnifinder);
--- a/calendar/lightning/content/messenger-overlay-sidebar.js
+++ b/calendar/lightning/content/messenger-overlay-sidebar.js
@@ -676,16 +676,19 @@ function ltnSwitch2Calendar() {
         // show the last displayed type of calendar view
         switchToView(gLastShownCalendarView);
 
         document.commandDispatcher.updateCommands("calendar_commands");
         window.setCursor("auto");
 
         // make sure the view is sized correctly
         onCalendarViewResize();
+
+        // Load the unifinder if it isn't already loaded.
+        ensureUnifinderLoaded();
     }
 }
 
 /**
  * ltnSwitch2Task() switches to the task mode
  */
 
 function ltnSwitch2Task() {