Bug 1396855 - about:telemetry : Added search terms in the URL fragment. r=chutten draft bug_1396855
authorVedant Chakravadhanula <vedantc98@gmail.com>
Sat, 30 Sep 2017 09:07:50 +0530
branchbug_1396855
changeset 673010 c2cdb79e74faffa50bfd6636f0877f541f4f6ddf
parent 670404 bc56729898954e32d3a3731d03d178ed78924c33
child 733978 86e57cba2663d293db3a7aeb1418b264f3a73892
push id82435
push userbmo:vedantc98@gmail.com
push dateSat, 30 Sep 2017 03:45:40 +0000
reviewerschutten
bugs1396855
milestone58.0a1
Bug 1396855 - about:telemetry : Added search terms in the URL fragment. r=chutten Implementation of the search terms in the url fragment was done as concisely as possible(considering the various conditions required, with the underscore when other hash terms are present). To implement search through hashes in URL, the function urlStateRestore() was changed. To make the changes more aesthetic, another function urlSectionRestore() performs the tasks earlier performed by urlStateRestore(), and urlStateRestore() now implements the search parsing and a call to urlSectionRestore(). MozReview-Commit-ID: 9vgjNUpJkQG
toolkit/content/aboutTelemetry.js
--- a/toolkit/content/aboutTelemetry.js
+++ b/toolkit/content/aboutTelemetry.js
@@ -1270,16 +1270,18 @@ var Histogram = {
 
     return text.substr(EOL.length); // Trim the EOL before the first line
   },
 };
 
 
 var Search = {
 
+  HASH_SEARCH: "search=",
+
   // A list of ids of sections that do not support search.
   blacklist: [
     "raw-payload-section"
   ],
 
   // Pass if: all non-empty array items match (case-sensitive)
   isPassText(subject, filter) {
     for (let item of filter) {
@@ -1414,16 +1416,18 @@ var Search = {
       for (let table of tables) {
         noSearchResults = this.filterElements(table.rows, text);
         if (table.caption) {
           table.caption.hidden = noSearchResults;
         }
       }
     }
 
+    changeUrlSearch(text);
+
     if (!sectionParam) { // If we are not searching in all section.
       this.updateNoResults(text, noSearchResults);
     }
     return noSearchResults;
   },
 
   updateNoResults(text, noSearchResults) {
     document.getElementById("no-search-results").classList.toggle("hidden", !noSearchResults);
@@ -1447,16 +1451,17 @@ var Search = {
     document.getElementById("no-search-results").classList.add("hidden");
     adjustHeaderState();
     Array.from(document.querySelectorAll("section")).forEach((section) => {
       section.classList.toggle("active", section.id == "home-section");
     });
   },
 
   homeSearch(text) {
+    changeUrlSearch(text);
     if (text === "") {
       this.resetHome();
       return;
     }
     document.getElementById("main").classList.add("search");
     let title = bundle.formatStringFromName("resultsForSearch", [text], 1);
     adjustHeaderState(title);
     let noSearchResults = true;
@@ -1913,16 +1918,39 @@ function changeUrlPath(selectedSection, 
     let hash = window.location.hash.split("_")[0] + "_" + selectedSection;
     window.location.hash = hash;
   } else {
     window.location.hash = selectedSection.replace("-section", "-tab");
   }
 }
 
 /**
+ * Change the url according to the current search text
+ */
+function changeUrlSearch(searchText) {
+  let currentHash = window.location.hash;
+  let hashWithoutSearch = currentHash.split(Search.HASH_SEARCH)[0];
+  let hash = "";
+
+  if (!currentHash && !searchText) {
+    return;
+  }
+  if (!currentHash.includes(Search.HASH_SEARCH) && hashWithoutSearch) {
+    hashWithoutSearch += "_";
+  }
+  if (searchText) {
+    hash = hashWithoutSearch + Search.HASH_SEARCH + searchText.replace(/ /g, "+");
+  } else if (hashWithoutSearch) {
+    hash = hashWithoutSearch.slice(0, hashWithoutSearch.length - 1);
+  }
+
+  window.location.hash = hash;
+}
+
+/**
  * Change the section displayed
  */
 function show(selected) {
   let selectedValue = selected.getAttribute("value");
   if (selectedValue === "raw-json-viewer") {
     openJsonInFirefoxJsonViewer(JSON.stringify(gPingData, null, 2));
     return;
   }
@@ -2067,34 +2095,52 @@ function setupListeners() {
       if (!gPingData) {
         return;
       }
 
       LateWritesSingleton.renderLateWrites(gPingData.payload.lateWrites);
   });
 }
 
-// Restore sections states
-function urlStateRestore() {
-  if (window.location.hash) {
-    let section = window.location.hash.slice(1).replace("-tab", "-section");
+// Restores the sections states
+function urlSectionRestore(hash) {
+  if (hash) {
+    let section = hash.replace("-tab", "-section");
     let subsection = section.split("_")[1];
     section = section.split("_")[0];
     let category = document.querySelector(".category[value=" + section + "]");
     if (category) {
       show(category);
       if (subsection) {
         let selector = ".category-subsection[value=" + section + "-" + subsection + "]";
         let subcategory = document.querySelector(selector);
         showSubSection(subcategory);
       }
     }
   }
 }
 
+// Restore sections states and search terms
+function urlStateRestore() {
+  let hash = window.location.hash;
+  let searchQuery = "";
+  if (hash) {
+    hash = hash.slice(1);
+    if (hash.includes(Search.HASH_SEARCH)) {
+      searchQuery = hash.split(Search.HASH_SEARCH)[1].replace(/[+]/g, " ");
+      hash = hash.split(Search.HASH_SEARCH)[0];
+    }
+    urlSectionRestore(hash);
+  }
+  if (searchQuery) {
+    let search = document.getElementById("search");
+    search.value = searchQuery;
+  }
+}
+
 function openJsonInFirefoxJsonViewer(json) {
   json = unescape(encodeURIComponent(json));
   try {
     window.open("data:application/json;base64," + btoa(json));
   } catch (e) {
     show(document.querySelector(".category[value=raw-payload-section]"));
   }
 }