Bug 1545199 - Refactor restoring calendar-task-tree column state after restart. r=darktrojan
authorPaul Morris <paul@paulwmorris.com>
Tue, 02 Jul 2019 17:05:45 -0400
changeset 36038 63616633fa865cdf7ce749bc003a0e01f0eb0f31
parent 36037 091a75e4e8ccd8396deb5f9c1aef85d54e43d57e
child 36039 83dd4afa641fe1128d23b004fc59d97d2829f974
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersdarktrojan
bugs1545199
Bug 1545199 - Refactor restoring calendar-task-tree column state after restart. r=darktrojan
calendar/base/content/calendar-task-tree.js
--- a/calendar/base/content/calendar-task-tree.js
+++ b/calendar/base/content/calendar-task-tree.js
@@ -267,45 +267,17 @@
             };
 
             // Set up the tree filter.
             this.mFilter = new calFilter();
 
             // This refresh call sets up the tree view and observers.
             this.refresh();
 
-            // We want to make several attributes on the column
-            // elements persistent, but unfortunately there's no
-            // reliable way with the 'persist' feature.
-            // That's why we need to store the necessary bits and
-            // pieces on the calendar-task-tree element.
-            let visibleColumns = this.getAttribute("visible-columns").split(" ");
-            let ordinals = this.getAttribute("ordinals").split(" ");
-            let widths = this.getAttribute("widths").split(" ");
-            let sorted = this.getAttribute("sortActive");
-            let sortDirection = this.getAttribute("sortDirection") || "ascending";
-
-            this.querySelectorAll("treecol").forEach((col) => {
-                const itemProperty = col.getAttribute("itemproperty");
-                if (visibleColumns.some(visCol => visCol == itemProperty)) {
-                    col.removeAttribute("hidden");
-                } else {
-                    col.setAttribute("hidden", "true");
-                }
-                if (ordinals && ordinals.length > 0) {
-                    col.ordinal = Number(ordinals.shift());
-                }
-                if (widths && widths.length > 0) {
-                    col.width = Number(widths.shift());
-                }
-                if (sorted && sorted == itemProperty) {
-                    this.mTreeView.sortDirection = sortDirection;
-                    this.mTreeView.selectedColumn = col;
-                }
-            });
+            this.restoreColumnState();
 
             window.addEventListener("unload", this.persistColumnState.bind(this));
 
             this.dispatchEvent(new CustomEvent("bindingattached", { bubbles: false }));
         }
 
         get currentTask() {
             const index = this.currentIndex;
@@ -384,16 +356,48 @@
                 this.setAttribute("sort-direction", this.mTreeView.sortDirection);
             } else {
                 this.removeAttribute("sort-active");
                 this.removeAttribute("sort-direction");
             }
         }
 
         /**
+         * Reads data from several attributes on the calendar-task-tree element and sets it on the
+         * attributes of the columns of the tree. Called on Thunderbird startup to persist the
+         * state of the columns across restarts. Used with `persistTaskTreeColumnState` function.
+         */
+        restoreColumnState() {
+            let visibleColumns = this.getAttribute("visible-columns").split(" ");
+            let ordinals = this.getAttribute("ordinals").split(" ");
+            let widths = this.getAttribute("widths").split(" ");
+            let sorted = this.getAttribute("sortActive");
+            let sortDirection = this.getAttribute("sortDirection") || "ascending";
+
+            this.querySelectorAll("treecol").forEach((col) => {
+                const itemProperty = col.getAttribute("itemproperty");
+                if (visibleColumns.includes(itemProperty)) {
+                    col.removeAttribute("hidden");
+                } else {
+                    col.setAttribute("hidden", "true");
+                }
+                if (ordinals && ordinals.length > 0) {
+                    col.ordinal = Number(ordinals.shift());
+                }
+                if (widths && widths.length > 0) {
+                    col.width = Number(widths.shift());
+                }
+                if (sorted && sorted == itemProperty) {
+                    this.mTreeView.sortDirection = sortDirection;
+                    this.mTreeView.selectedColumn = col;
+                }
+            });
+        }
+
+        /**
          * Calculates the text to display in the "Due In" column for the given task,
          * the amount of time between now and when the task is due.
          *
          * @param {Object} task    A task object.
          * @return {string}        A formatted string for the "Due In" column for the task.
          */
         duration(task) {
             const noValidDueDate = !(task && task.dueDate && task.dueDate.isValid);