toolkit/modules/SelectParentHelper.jsm

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:f183be90c541
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/. */
4
5 "use strict";
6
7 this.EXPORTED_SYMBOLS = [
8 "SelectParentHelper"
9 ];
10
11 let currentBrowser = null;
12
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 },
19
20 open: function(browser, popup, rect) {
21 currentBrowser = browser;
22 this._registerListeners(popup);
23 popup.hidden = false;
24
25 let {x, y} = browser.mapScreenCoordinatesFromContent(rect.left, rect.top + rect.height);
26 popup.openPopupAtScreen(x, y);
27 },
28
29 hide: function(popup) {
30 popup.hidePopup();
31 },
32
33 handleEvent: function(event) {
34 let popup = event.currentTarget;
35
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;
45
46 case "popuphidden":
47 currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
48 currentBrowser = null;
49 this._unregisterListeners(popup);
50 break;
51 }
52 },
53
54 _registerListeners: function(popup) {
55 popup.addEventListener("command", this);
56 popup.addEventListener("popuphidden", this);
57 },
58
59 _unregisterListeners: function(popup) {
60 popup.removeEventListener("command", this);
61 popup.removeEventListener("popuphidden", this);
62 },
63
64 };
65
66 function populateChildren(element, options, selectedIndex, startIndex = 0, isGroup = false) {
67 let index = startIndex;
68
69 for (let option of options) {
70 let item = element.ownerDocument.createElement("menuitem");
71 item.setAttribute("label", option.textContent);
72 item.setAttribute("type", "radio");
73
74 if (index == selectedIndex) {
75 item.setAttribute("checked", "true");
76 }
77
78 element.appendChild(item);
79
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++);
86
87 if (isGroup) {
88 item.classList.add("contentSelectDropdown-ingroup")
89 }
90 }
91 }
92
93 return index;
94 }

mercurial