Bug 1469696 - Part 3: Allow searching for languages from dialog r=jaws
authorMark Striemer <mstriemer@mozilla.com>
Tue, 02 Oct 2018 17:57:32 +0000
changeset 495132 10b8ab80417dafbf54c53b7916b3493e015d2bc2
parent 495131 31c94d7845b709c7a6d8ff8cf8e35b9b918091fd
child 495133 4973d5d699c5f6dc29d3c7f512431d3ae49b11f9
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws
bugs1469696
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1469696 - Part 3: Allow searching for languages from dialog r=jaws Differential Revision: https://phabricator.services.mozilla.com/D6313
browser/components/preferences/browserLanguages.js
--- a/browser/components/preferences/browserLanguages.js
+++ b/browser/components/preferences/browserLanguages.js
@@ -130,24 +130,27 @@ class OrderedListBox {
     labelEl.textContent = label;
     listitem.appendChild(labelEl);
 
     return listitem;
   }
 }
 
 class SortedItemSelectList {
-  constructor({menulist, button, onSelect}) {
+  constructor({menulist, button, onSelect, onChange}) {
     this.menulist = menulist;
     this.popup = menulist.firstElementChild;
     this.button = button;
     this.items = [];
 
     menulist.addEventListener("command", () => {
       button.disabled = !menulist.selectedItem;
+      if (menulist.selectedItem) {
+        onChange(this.items[menulist.selectedIndex]);
+      }
     });
     button.addEventListener("command", () => {
       if (!menulist.selectedItem) return;
 
       let [item] = this.items.splice(menulist.selectedIndex, 1);
       menulist.selectedItem.remove();
       menulist.setAttribute("label", menulist.getAttribute("placeholder"));
       button.disabled = true;
@@ -158,27 +161,29 @@ class SortedItemSelectList {
   }
 
   setItems(items) {
     this.items = items.sort((a, b) => a.label > b.label);
     this.populate();
   }
 
   populate() {
-    let {items, menulist, popup} = this;
+    let {button, items, menulist, popup} = this;
     popup.textContent = "";
 
     let frag = document.createDocumentFragment();
     for (let item of items) {
       frag.appendChild(this.createItem(item));
     }
     popup.appendChild(frag);
 
     menulist.setAttribute("label", menulist.getAttribute("placeholder"));
     menulist.disabled = menulist.itemCount == 0;
+    menulist.selectedIndex = -1;
+    button.disabled = true;
   }
 
   /**
    * Add an item to the list sorted by the label.
    *
    * @param {object} item The item to insert.
    */
   addItem(item) {
@@ -273,16 +278,22 @@ var gBrowserLanguagesDialog = {
     this._requestedLocales.setItems(getLocaleDisplayInfo(requested));
   },
 
   async initAvailableLocales(available, search) {
     this._availableLocales = new SortedItemSelectList({
       menulist: document.getElementById("availableLocales"),
       button: document.getElementById("add"),
       onSelect: (item) => this.availableLanguageSelected(item),
+      onChange: (item) => {
+        this.hideError();
+        if (item.value == "search") {
+          this.loadLocalesFromAMO();
+        }
+      },
     });
 
     // Populate the list with the installed locales even if the user is
     // searching in case the download fails.
     await this.loadLocalesFromInstalled(available);
 
     // If the user opened this from the "Search for more languages" option,
     // search AMO for available locales.
@@ -318,30 +329,36 @@ var gBrowserLanguagesDialog = {
       ...Services.locale.availableLocales,
     ]);
 
     let availableLocales = availableLangpacks
       .filter(({target_locale}) => !installedLocales.has(target_locale))
       .map(lang => lang.target_locale);
     let availableItems = getLocaleDisplayInfo(availableLocales);
     let items = this._availableLocales.items;
+    // Drop the search item.
+    items.pop();
     items = items.concat(availableItems);
 
     // Update the dropdown and enable it again.
     this._availableLocales.setItems(items);
     this._availableLocales.enableWithMessageId("browser-languages-select-language");
   },
 
   async loadLocalesFromInstalled(available) {
     let items;
     if (available.length > 0) {
       items = getLocaleDisplayInfo(available);
     } else {
       items = [];
     }
+    items.push({
+      label: await document.l10n.formatValue("browser-languages-search"),
+      value: "search",
+    });
     this._availableLocales.setItems(items);
   },
 
   async availableLanguageSelected(item) {
     let available = new Set(Services.locale.availableLocales);
 
     if (available.has(item.value)) {
       this._requestedLocales.addItem(item);