toolkit/modules/SelectParentHelper.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 this.EXPORTED_SYMBOLS = [
     8   "SelectParentHelper"
     9 ];
    11 let currentBrowser = null;
    13 this.SelectParentHelper = {
    14   populate: function(popup, items, selectedIndex) {
    15     // Clear the current contents of the popup
    16     popup.textContent = "";
    17     populateChildren(popup, items, selectedIndex);
    18   },
    20   open: function(browser, popup, rect) {
    21     currentBrowser = browser;
    22     this._registerListeners(popup);
    23     popup.hidden = false;
    25     let {x, y} = browser.mapScreenCoordinatesFromContent(rect.left, rect.top + rect.height);
    26     popup.openPopupAtScreen(x, y);
    27   },
    29   hide: function(popup) {
    30     popup.hidePopup();
    31   },
    33   handleEvent: function(event) {
    34     let popup = event.currentTarget;
    36     switch (event.type) {
    37       case "command":
    38         if (event.target.hasAttribute("value")) {
    39           currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
    40             value: event.target.value
    41           });
    42         }
    43         popup.hidePopup();
    44         break;
    46       case "popuphidden":
    47         currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
    48         currentBrowser = null;
    49         this._unregisterListeners(popup);
    50         break;
    51     }
    52   },
    54   _registerListeners: function(popup) {
    55     popup.addEventListener("command", this);
    56     popup.addEventListener("popuphidden", this);
    57   },
    59   _unregisterListeners: function(popup) {
    60     popup.removeEventListener("command", this);
    61     popup.removeEventListener("popuphidden", this);
    62   },
    64 };
    66 function populateChildren(element, options, selectedIndex, startIndex = 0, isGroup = false) {
    67   let index = startIndex;
    69   for (let option of options) {
    70     let item = element.ownerDocument.createElement("menuitem");
    71     item.setAttribute("label", option.textContent);
    72     item.setAttribute("type", "radio");
    74     if (index == selectedIndex) {
    75       item.setAttribute("checked", "true");
    76     }
    78     element.appendChild(item);
    80     if (option.children.length > 0) {
    81       item.classList.add("contentSelectDropdown-optgroup");
    82       item.setAttribute("disabled", "true");
    83       index = populateChildren(element, option.children, selectedIndex, index, true);
    84     } else {
    85       item.setAttribute("value", index++);
    87       if (isGroup) {
    88         item.classList.add("contentSelectDropdown-ingroup")
    89       }
    90     }
    91   }
    93   return index;
    94 }

mercurial