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-01-06 00:38:48 +03:00
|
|
|
populate: function(menulist, items, selectedIndex) {
|
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-04-21 01:11:16 +03:00
|
|
|
populateChildren(menulist, items, selectedIndex);
|
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) {
|
|
|
|
let popup = event.currentTarget;
|
2015-01-06 00:38:48 +03:00
|
|
|
let menulist = popup.parentNode;
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case "command":
|
|
|
|
if (event.target.hasAttribute("value")) {
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
|
|
|
|
value: event.target.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
popup.hidePopup();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "popuphidden":
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
|
|
|
|
currentBrowser = null;
|
|
|
|
this._unregisterListeners(popup);
|
2015-01-06 00:38:48 +03:00
|
|
|
menulist.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-05 15:33:28 +03:00
|
|
|
function populateChildren(menulist, options, selectedIndex, startIndex = 0, isInGroup = false) {
|
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
|
|
|
|
|
|
|
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;
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
element.appendChild(item);
|
|
|
|
|
2015-06-05 15:33:28 +03:00
|
|
|
if (isOptGroup) {
|
2015-04-21 01:11:16 +03:00
|
|
|
index = populateChildren(menulist, option.children, selectedIndex, index, true);
|
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;
|
|
|
|
}
|