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/. */
7 this.EXPORTED_SYMBOLS = [
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);
20 open: function(browser, popup, rect) {
21 currentBrowser = browser;
22 this._registerListeners(popup);
25 let {x, y} = browser.mapScreenCoordinatesFromContent(rect.left, rect.top + rect.height);
26 popup.openPopupAtScreen(x, y);
29 hide: function(popup) {
33 handleEvent: function(event) {
34 let popup = event.currentTarget;
38 if (event.target.hasAttribute("value")) {
39 currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
40 value: event.target.value
47 currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
48 currentBrowser = null;
49 this._unregisterListeners(popup);
54 _registerListeners: function(popup) {
55 popup.addEventListener("command", this);
56 popup.addEventListener("popuphidden", this);
59 _unregisterListeners: function(popup) {
60 popup.removeEventListener("command", this);
61 popup.removeEventListener("popuphidden", this);
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");
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);
85 item.setAttribute("value", index++);
88 item.classList.add("contentSelectDropdown-ingroup")