toolkit/modules/SelectParentHelper.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/SelectParentHelper.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [
    1.11 +  "SelectParentHelper"
    1.12 +];
    1.13 +
    1.14 +let currentBrowser = null;
    1.15 +
    1.16 +this.SelectParentHelper = {
    1.17 +  populate: function(popup, items, selectedIndex) {
    1.18 +    // Clear the current contents of the popup
    1.19 +    popup.textContent = "";
    1.20 +    populateChildren(popup, items, selectedIndex);
    1.21 +  },
    1.22 +
    1.23 +  open: function(browser, popup, rect) {
    1.24 +    currentBrowser = browser;
    1.25 +    this._registerListeners(popup);
    1.26 +    popup.hidden = false;
    1.27 +
    1.28 +    let {x, y} = browser.mapScreenCoordinatesFromContent(rect.left, rect.top + rect.height);
    1.29 +    popup.openPopupAtScreen(x, y);
    1.30 +  },
    1.31 +
    1.32 +  hide: function(popup) {
    1.33 +    popup.hidePopup();
    1.34 +  },
    1.35 +
    1.36 +  handleEvent: function(event) {
    1.37 +    let popup = event.currentTarget;
    1.38 +
    1.39 +    switch (event.type) {
    1.40 +      case "command":
    1.41 +        if (event.target.hasAttribute("value")) {
    1.42 +          currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
    1.43 +            value: event.target.value
    1.44 +          });
    1.45 +        }
    1.46 +        popup.hidePopup();
    1.47 +        break;
    1.48 +
    1.49 +      case "popuphidden":
    1.50 +        currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
    1.51 +        currentBrowser = null;
    1.52 +        this._unregisterListeners(popup);
    1.53 +        break;
    1.54 +    }
    1.55 +  },
    1.56 +
    1.57 +  _registerListeners: function(popup) {
    1.58 +    popup.addEventListener("command", this);
    1.59 +    popup.addEventListener("popuphidden", this);
    1.60 +  },
    1.61 +
    1.62 +  _unregisterListeners: function(popup) {
    1.63 +    popup.removeEventListener("command", this);
    1.64 +    popup.removeEventListener("popuphidden", this);
    1.65 +  },
    1.66 +
    1.67 +};
    1.68 +
    1.69 +function populateChildren(element, options, selectedIndex, startIndex = 0, isGroup = false) {
    1.70 +  let index = startIndex;
    1.71 +
    1.72 +  for (let option of options) {
    1.73 +    let item = element.ownerDocument.createElement("menuitem");
    1.74 +    item.setAttribute("label", option.textContent);
    1.75 +    item.setAttribute("type", "radio");
    1.76 +
    1.77 +    if (index == selectedIndex) {
    1.78 +      item.setAttribute("checked", "true");
    1.79 +    }
    1.80 +
    1.81 +    element.appendChild(item);
    1.82 +
    1.83 +    if (option.children.length > 0) {
    1.84 +      item.classList.add("contentSelectDropdown-optgroup");
    1.85 +      item.setAttribute("disabled", "true");
    1.86 +      index = populateChildren(element, option.children, selectedIndex, index, true);
    1.87 +    } else {
    1.88 +      item.setAttribute("value", index++);
    1.89 +
    1.90 +      if (isGroup) {
    1.91 +        item.classList.add("contentSelectDropdown-ingroup")
    1.92 +      }
    1.93 +    }
    1.94 +  }
    1.95 +
    1.96 +  return index;
    1.97 +}

mercurial