2013-08-28 05:09:44 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"SelectParentHelper"
|
|
|
|
];
|
|
|
|
|
|
|
|
let currentBrowser = null;
|
|
|
|
|
|
|
|
this.SelectParentHelper = {
|
2015-06-08 21:41:17 +03:00
|
|
|
populate: function(menulist, items, selectedIndex, zoom) {
|
2013-08-26 05:34:23 +04:00
|
|
|
// Clear the current contents of the popup
|
2015-01-06 00:38:48 +03:00
|
|
|
menulist.menupopup.textContent = "";
|
2015-06-08 21:41:17 +03:00
|
|
|
populateChildren(menulist, items, selectedIndex, zoom);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2015-01-06 00:38:48 +03:00
|
|
|
open: function(browser, menulist, rect) {
|
|
|
|
menulist.hidden = false;
|
2013-08-26 05:34:23 +04:00
|
|
|
currentBrowser = browser;
|
2015-01-06 00:38:48 +03:00
|
|
|
this._registerListeners(menulist.menupopup);
|
2013-08-26 05:34:23 +04:00
|
|
|
|
2015-05-08 21:53:56 +03:00
|
|
|
menulist.menupopup.openPopupAtScreenRect("after_start", rect.left, rect.top, rect.width, rect.height, false, false);
|
2015-01-06 00:38:48 +03:00
|
|
|
menulist.selectedItem.scrollIntoView();
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2015-01-06 00:38:48 +03:00
|
|
|
hide: function(menulist) {
|
|
|
|
menulist.menupopup.hidePopup();
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case "command":
|
|
|
|
if (event.target.hasAttribute("value")) {
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
|
|
|
|
value: event.target.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "popuphidden":
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
|
|
|
|
currentBrowser = null;
|
2015-06-26 19:32:25 +03:00
|
|
|
let popup = event.target;
|
2013-08-26 05:34:23 +04:00
|
|
|
this._unregisterListeners(popup);
|
2015-06-26 19:32:25 +03:00
|
|
|
popup.parentNode.hidden = true;
|
2013-08-26 05:34:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_registerListeners: function(popup) {
|
|
|
|
popup.addEventListener("command", this);
|
|
|
|
popup.addEventListener("popuphidden", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
_unregisterListeners: function(popup) {
|
|
|
|
popup.removeEventListener("command", this);
|
|
|
|
popup.removeEventListener("popuphidden", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-06-08 21:41:17 +03:00
|
|
|
function populateChildren(menulist, options, selectedIndex, zoom, startIndex = 0,
|
|
|
|
isInGroup = false, isGroupDisabled = false, adjustedTextSize = -1) {
|
2013-08-26 05:34:23 +04:00
|
|
|
let index = startIndex;
|
2015-04-21 01:11:16 +03:00
|
|
|
let element = menulist.menupopup;
|
2013-08-26 05:34:23 +04:00
|
|
|
|
2015-06-08 21:41:17 +03:00
|
|
|
// -1 just means we haven't calculated it yet. When we recurse through this function
|
|
|
|
// we will pass in adjustedTextSize to save on recalculations.
|
|
|
|
if (adjustedTextSize == -1) {
|
|
|
|
let win = element.ownerDocument.defaultView;
|
|
|
|
|
|
|
|
// Grab the computed text size and multiply it by the remote browser's fullZoom to ensure
|
|
|
|
// the popup's text size is matched with the content's. We can't just apply a CSS transform
|
|
|
|
// here as the popup's preferred size is calculated pre-transform.
|
|
|
|
let textSize = win.getComputedStyle(element).getPropertyValue("font-size");
|
|
|
|
adjustedTextSize = (zoom * parseFloat(textSize, 10)) + "px";
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
for (let option of options) {
|
2015-06-05 15:33:28 +03:00
|
|
|
let isOptGroup = (option.tagName == 'OPTGROUP');
|
|
|
|
let item = element.ownerDocument.createElement(isOptGroup ? "menucaption" : "menuitem");
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
item.setAttribute("label", option.textContent);
|
2015-04-15 23:00:29 +03:00
|
|
|
item.style.direction = option.textDirection;
|
2015-06-08 21:41:17 +03:00
|
|
|
item.style.fontSize = adjustedTextSize;
|
2015-07-14 05:14:31 +03:00
|
|
|
item.setAttribute("tooltiptext", option.tooltip);
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
element.appendChild(item);
|
|
|
|
|
2015-06-05 15:33:29 +03:00
|
|
|
// A disabled optgroup disables all of its child options.
|
|
|
|
let isDisabled = isGroupDisabled || option.disabled;
|
|
|
|
if (isDisabled) {
|
|
|
|
item.setAttribute("disabled", "true");
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:33:28 +03:00
|
|
|
if (isOptGroup) {
|
2015-06-08 21:41:17 +03:00
|
|
|
index = populateChildren(menulist, option.children, selectedIndex, zoom,
|
|
|
|
index, true, isDisabled, adjustedTextSize);
|
2013-08-26 05:34:23 +04:00
|
|
|
} else {
|
2015-04-21 01:11:16 +03:00
|
|
|
if (index == selectedIndex) {
|
|
|
|
// We expect the parent element of the popup to be a <xul:menulist> that
|
|
|
|
// has the popuponly attribute set to "true". This is necessary in order
|
|
|
|
// for a <xul:menupopup> to act like a proper <html:select> dropdown, as
|
|
|
|
// the <xul:menulist> does things like remember state and set the
|
|
|
|
// _moz-menuactive attribute on the selected <xul:menuitem>.
|
|
|
|
menulist.selectedItem = item;
|
|
|
|
}
|
2015-06-05 15:33:28 +03:00
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
item.setAttribute("value", index++);
|
|
|
|
|
2015-06-05 15:33:28 +03:00
|
|
|
if (isInGroup) {
|
2013-08-26 05:34:23 +04:00
|
|
|
item.classList.add("contentSelectDropdown-ingroup")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|