Bug 1432016 - Part 3 - Use the PanelView class for knownViews and openViews. r=Gijs draft
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Mon, 22 Jan 2018 13:06:55 +0000
changeset 723061 25f0378c51b6d5a08fca22b561e239a245244e0a
parent 723060 e7c86579a97c8211089896af68e30f908ef3ba0e
child 723062 8595c2829999a3a1d5282de1a078f36c751d383c
child 723560 abeed534bd582af7618378aa44a6b7d407667ebd
push id96314
push userpaolo.mozmail@amadzone.org
push dateMon, 22 Jan 2018 13:20:54 +0000
reviewersGijs
bugs1432016
milestone59.0a1
Bug 1432016 - Part 3 - Use the PanelView class for knownViews and openViews. r=Gijs MozReview-Commit-ID: CQ0Ao3FlRRc
browser/components/customizableui/PanelMultiView.jsm
--- a/browser/components/customizableui/PanelMultiView.jsm
+++ b/browser/components/customizableui/PanelMultiView.jsm
@@ -180,33 +180,36 @@ this.PanelMultiView = class extends this
    *                     dispatched.
    */
   get current() {
     return this.node && (this._viewShowing || this._currentSubView);
   }
   get _currentSubView() {
     // Peek the top of the stack, but fall back to the main view if the list of
     // opened views is currently empty.
-    return this.openViews[this.openViews.length - 1] || this._mainView;
+    let panelView = this.openViews[this.openViews.length - 1];
+    return (panelView && panelView.node) || this._mainView;
   }
   /**
    * @return {Promise} showSubView() returns a promise, which is kept here for
    *                   random access.
    */
   get currentShowPromise() {
     return this._currentShowPromise || Promise.resolve();
   }
   get _keyNavigationMap() {
     if (!this.__keyNavigationMap)
       this.__keyNavigationMap = new Map();
     return this.__keyNavigationMap;
   }
 
   connect() {
-    this.knownViews = new Set(this.node.getElementsByTagName("panelview"));
+    this.knownViews = new Set(Array.from(
+      this.node.getElementsByTagName("panelview"),
+      node => PanelView.forNode(node)));
     this.openViews = [];
     this._mainViewHeight = 0;
     this.__transitioning = false;
     this.showingSubView = false;
 
     const {document, window} = this;
 
     this._viewContainer =
@@ -289,17 +292,17 @@ this.PanelMultiView = class extends this
     for (let subview of subviews) {
       // XBL lists the 'children' XBL element explicitly. :-(
       if (subview.nodeName != "children")
         this._panelViewCache.appendChild(subview);
     }
   }
 
   goBack() {
-    let previous = this.openViews.pop();
+    let previous = this.openViews.pop().node;
     let current = this._currentSubView;
     return this.showSubView(current, null, previous);
   }
 
   /**
    * Checks whether it is possible to navigate backwards currently. Returns
    * false if this is the panelmultiview's mainview, true otherwise.
    *
@@ -316,37 +319,37 @@ this.PanelMultiView = class extends this
 
     return this.showSubView(this._mainView);
   }
 
   /**
    * Ensures that all the panelviews, that are currently part of this instance,
    * are hidden, except one specifically.
    *
-   * @param {panelview} [theOne] The panelview DOM node to ensure is visible.
-   *                             Optional.
+   * @param {panelview} [nextPanelView]
+   *        The PanelView object to ensure is visible. Optional.
    */
-  hideAllViewsExcept(theOne = null) {
-    for (let panelview of this.knownViews) {
+  hideAllViewsExcept(nextPanelView = null) {
+    for (let panelView of this.knownViews) {
       // When the panelview was already reparented, don't interfere any more.
-      if (panelview == theOne || !this.node || panelview.panelMultiView != this.node)
+      if (panelView == nextPanelView || !this.node || panelView.node.panelMultiView != this.node)
         continue;
-      PanelView.forNode(panelview).current = false;
+      panelView.current = false;
     }
 
     this._viewShowing = null;
 
-    if (!this.node || !theOne)
+    if (!this.node || !nextPanelView)
       return;
 
-    if (!this.openViews.includes(theOne))
-      this.openViews.push(theOne);
+    if (!this.openViews.includes(nextPanelView))
+      this.openViews.push(nextPanelView);
 
-    PanelView.forNode(theOne).current = true;
-    this.showingSubView = theOne.id != this._mainViewId;
+    nextPanelView.current = true;
+    this.showingSubView = nextPanelView.node.id != this._mainViewId;
   }
 
   showSubView(aViewId, aAnchor, aPreviousView) {
     this._currentShowPromise = (async () => {
       // Support passing in the node directly.
       let viewNode = typeof aViewId == "string" ? this.node.querySelector("#" + aViewId) : aViewId;
       if (!viewNode) {
         viewNode = this.document.getElementById(aViewId);
@@ -355,17 +358,17 @@ this.PanelMultiView = class extends this
         } else {
           throw new Error(`Subview ${aViewId} doesn't exist!`);
         }
       } else if (viewNode.parentNode == this._panelViewCache) {
         this._viewStack.appendChild(viewNode);
       }
 
       let nextPanelView = PanelView.forNode(viewNode);
-      this.knownViews.add(viewNode);
+      this.knownViews.add(nextPanelView);
 
       viewNode.panelMultiView = this.node;
 
       let reverse = !!aPreviousView;
       if (!reverse) {
         nextPanelView.headerText = viewNode.getAttribute("title") ||
                                    (aAnchor && aAnchor.getAttribute("label"));
       }
@@ -431,17 +434,17 @@ this.PanelMultiView = class extends this
 
       // Now we have to transition the panel. If we've got an older transition
       // still running, make sure to clean it up.
       await this._cleanupTransitionPhase();
       if (playTransition) {
         await this._transitionViews(previousViewNode, viewNode, reverse, previousRect, aAnchor);
         this._updateKeyboardFocus(viewNode);
       } else {
-        this.hideAllViewsExcept(viewNode);
+        this.hideAllViewsExcept(nextPanelView);
       }
     })().catch(e => Cu.reportError(e));
     return this._currentShowPromise;
   }
 
   /**
    * Apply a transition to 'slide' from the currently active view to the next
    * one.
@@ -613,19 +616,21 @@ this.PanelMultiView = class extends this
   async _cleanupTransitionPhase(details = this._transitionDetails) {
     if (!details || !this.node)
       return;
 
     let {phase, previousViewNode, viewNode, reverse, resolve, listener, cancelListener, anchor} = details;
     if (details == this._transitionDetails)
       this._transitionDetails = null;
 
+    let nextPanelView = PanelView.forNode(viewNode);
+
     // Do the things we _always_ need to do whenever the transition ends or is
     // interrupted.
-    this.hideAllViewsExcept(viewNode);
+    this.hideAllViewsExcept(nextPanelView);
     previousViewNode.removeAttribute("in-transition");
     viewNode.removeAttribute("in-transition");
     if (reverse)
       this._resetKeyNavigation(previousViewNode);
 
     if (anchor)
       anchor.removeAttribute("open");