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

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

mercurial