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"
|
|
|
|
];
|
|
|
|
|
2016-11-08 00:19:19 +03:00
|
|
|
const {utils: Cu} = Components;
|
2017-01-09 13:12:26 +03:00
|
|
|
const {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm", {});
|
2017-01-17 23:40:15 +03:00
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
|
2016-11-08 00:19:19 +03:00
|
|
|
|
2016-07-25 16:08:34 +03:00
|
|
|
// Maximum number of rows to display in the select dropdown.
|
|
|
|
const MAX_ROWS = 20;
|
|
|
|
|
2017-01-17 23:40:15 +03:00
|
|
|
// Minimum elements required to show select search
|
|
|
|
const SEARCH_MINIMUM_ELEMENTS = 40;
|
|
|
|
|
2017-08-10 23:26:37 +03:00
|
|
|
// Make sure to clear these objects when the popup closes to avoid leaking.
|
2015-09-15 21:19:45 +03:00
|
|
|
var currentBrowser = null;
|
2016-04-16 05:00:36 +03:00
|
|
|
var currentMenulist = null;
|
2017-08-10 23:26:37 +03:00
|
|
|
var selectRect = null;
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
var currentZoom = 1;
|
2016-08-03 14:45:45 +03:00
|
|
|
var closedWithEnter = false;
|
2017-02-16 02:25:31 +03:00
|
|
|
var customStylingEnabled = Services.prefs.getBoolPref("dom.forms.select.customstyling");
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
this.SelectParentHelper = {
|
2017-08-12 03:38:14 +03:00
|
|
|
/**
|
|
|
|
* `populate` takes the `menulist` element and a list of `items` and generates
|
|
|
|
* a popup list of options.
|
|
|
|
*
|
|
|
|
* If `customStylingEnabled` is set to `true`, the function will alse
|
|
|
|
* style the select and its popup trying to prevent the text
|
|
|
|
* and background to end up in the same color.
|
|
|
|
*
|
|
|
|
* All `ua*` variables represent the color values for the default colors
|
|
|
|
* for their respective form elements used by the user agent.
|
|
|
|
* The `select*` variables represent the color values defined for the
|
|
|
|
* particular <select> element.
|
|
|
|
*
|
|
|
|
* The `customoptionstyling` attribute controls the application of
|
|
|
|
* `-moz-appearance` on the elements and is disabled if the element is
|
|
|
|
* defining its own background-color.
|
|
|
|
*
|
|
|
|
* @param {Element} menulist
|
|
|
|
* @param {Array<Element>} items
|
|
|
|
* @param {Number} selectedIndex
|
|
|
|
* @param {Number} zoom
|
|
|
|
* @param {String} uaBackgroundColor
|
|
|
|
* @param {String} uaColor
|
|
|
|
* @param {String} uaSelectBackgroundColor
|
|
|
|
* @param {String} uaSelectColor
|
|
|
|
* @param {String} selectBackgroundColor
|
|
|
|
* @param {String} selectColor
|
|
|
|
* @param {String} selectTextShadow
|
|
|
|
*/
|
2017-02-04 08:33:13 +03:00
|
|
|
populate(menulist, items, selectedIndex, zoom, uaBackgroundColor, uaColor,
|
2017-08-12 03:38:14 +03:00
|
|
|
uaSelectBackgroundColor, uaSelectColor, selectBackgroundColor,
|
|
|
|
selectColor, selectTextShadow) {
|
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 = "";
|
2017-06-21 22:31:31 +03:00
|
|
|
let stylesheet = menulist.querySelector("#ContentSelectDropdownStylesheet");
|
2017-02-03 20:46:45 +03:00
|
|
|
if (stylesheet) {
|
|
|
|
stylesheet.remove();
|
|
|
|
}
|
|
|
|
|
2017-02-04 08:33:13 +03:00
|
|
|
let doc = menulist.ownerDocument;
|
2017-02-16 02:25:31 +03:00
|
|
|
let sheet;
|
|
|
|
if (customStylingEnabled) {
|
|
|
|
stylesheet = doc.createElementNS("http://www.w3.org/1999/xhtml", "style");
|
2017-06-21 22:31:31 +03:00
|
|
|
stylesheet.setAttribute("id", "ContentSelectDropdownStylesheet");
|
2017-02-16 02:25:31 +03:00
|
|
|
stylesheet.hidden = true;
|
|
|
|
stylesheet = menulist.appendChild(stylesheet);
|
|
|
|
sheet = stylesheet.sheet;
|
|
|
|
}
|
2017-02-04 08:33:13 +03:00
|
|
|
|
2017-02-15 21:30:50 +03:00
|
|
|
let ruleBody = "";
|
2017-08-12 03:38:14 +03:00
|
|
|
let usedSelectBackgroundColor;
|
|
|
|
let usedSelectColor;
|
|
|
|
let selectBackgroundSet = false;
|
2017-02-15 21:30:50 +03:00
|
|
|
|
|
|
|
// Some webpages set the <select> backgroundColor to transparent,
|
|
|
|
// but they don't intend to change the popup to transparent.
|
2017-02-16 02:25:31 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
selectBackgroundColor != uaSelectBackgroundColor &&
|
2017-03-28 22:27:57 +03:00
|
|
|
selectBackgroundColor != "rgba(0, 0, 0, 0)") {
|
2017-03-21 17:51:40 +03:00
|
|
|
ruleBody = `background-image: linear-gradient(${selectBackgroundColor}, ${selectBackgroundColor});`;
|
2017-03-06 20:10:14 +03:00
|
|
|
usedSelectBackgroundColor = selectBackgroundColor;
|
2017-08-12 03:38:14 +03:00
|
|
|
selectBackgroundSet = true;
|
2017-03-06 20:10:14 +03:00
|
|
|
} else {
|
|
|
|
usedSelectBackgroundColor = uaSelectBackgroundColor;
|
2017-02-15 21:30:50 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 02:25:31 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
selectColor != uaSelectColor &&
|
2017-08-12 03:38:14 +03:00
|
|
|
selectColor != usedSelectBackgroundColor) {
|
2017-02-15 21:30:50 +03:00
|
|
|
ruleBody += `color: ${selectColor};`;
|
2017-08-12 03:38:14 +03:00
|
|
|
usedSelectColor = selectColor;
|
|
|
|
} else {
|
|
|
|
usedSelectColor = uaColor;
|
2017-02-15 21:30:50 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 23:03:03 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
selectTextShadow != "none") {
|
|
|
|
ruleBody += `text-shadow: ${selectTextShadow};`;
|
2017-08-12 03:38:14 +03:00
|
|
|
sheet.insertRule(`#ContentSelectDropdown > menupopup > [_moz-menuactive="true"] {
|
|
|
|
text-shadow: none;
|
|
|
|
}`, 0);
|
2017-04-10 23:03:03 +03:00
|
|
|
}
|
|
|
|
|
2017-02-15 21:30:50 +03:00
|
|
|
if (ruleBody) {
|
2017-06-21 22:31:31 +03:00
|
|
|
sheet.insertRule(`#ContentSelectDropdown > menupopup {
|
2017-02-15 21:30:50 +03:00
|
|
|
${ruleBody}
|
2017-02-04 08:33:13 +03:00
|
|
|
}`, 0);
|
2017-08-12 03:38:14 +03:00
|
|
|
sheet.insertRule(`#ContentSelectDropdown > menupopup > :not([_moz-menuactive="true"]) {
|
|
|
|
color: inherit;
|
|
|
|
}`, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only set the `customoptionstyling` if the background has been
|
|
|
|
// manually set. This prevents the overlap between moz-appearance and
|
|
|
|
// background-color. `color` and `text-shadow` do not interfere with it.
|
|
|
|
if (selectBackgroundSet) {
|
2017-02-04 08:33:13 +03:00
|
|
|
menulist.menupopup.setAttribute("customoptionstyling", "true");
|
|
|
|
} else {
|
|
|
|
menulist.menupopup.removeAttribute("customoptionstyling");
|
|
|
|
}
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
currentZoom = zoom;
|
|
|
|
currentMenulist = menulist;
|
2017-01-31 23:58:27 +03:00
|
|
|
populateChildren(menulist, items, selectedIndex, zoom,
|
2017-08-12 03:38:14 +03:00
|
|
|
usedSelectBackgroundColor, usedSelectColor, selectTextShadow, selectBackgroundSet, sheet);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
open(browser, menulist, rect, isOpenedViaTouch) {
|
2015-01-06 00:38:48 +03:00
|
|
|
menulist.hidden = false;
|
2013-08-26 05:34:23 +04:00
|
|
|
currentBrowser = browser;
|
2016-08-03 14:45:45 +03:00
|
|
|
closedWithEnter = false;
|
2017-01-19 18:03:56 +03:00
|
|
|
selectRect = rect;
|
2016-04-16 05:00:36 +03:00
|
|
|
this._registerListeners(browser, menulist.menupopup);
|
2013-08-26 05:34:23 +04:00
|
|
|
|
2017-01-27 12:51:03 +03:00
|
|
|
let win = browser.ownerGlobal;
|
2016-07-25 16:08:34 +03:00
|
|
|
|
|
|
|
// Set the maximum height to show exactly MAX_ROWS items.
|
2016-08-29 19:34:45 +03:00
|
|
|
let menupopup = menulist.menupopup;
|
|
|
|
let firstItem = menupopup.firstChild;
|
|
|
|
while (firstItem && firstItem.hidden) {
|
|
|
|
firstItem = firstItem.nextSibling;
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:08:34 +03:00
|
|
|
if (firstItem) {
|
|
|
|
let itemHeight = firstItem.getBoundingClientRect().height;
|
|
|
|
|
|
|
|
// Include the padding and border on the popup.
|
2016-08-29 19:34:45 +03:00
|
|
|
let cs = win.getComputedStyle(menupopup);
|
2016-07-25 16:08:34 +03:00
|
|
|
let bpHeight = parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth) +
|
|
|
|
parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
|
2016-08-29 19:34:45 +03:00
|
|
|
menupopup.style.maxHeight = (itemHeight * MAX_ROWS + bpHeight) + "px";
|
2016-07-25 16:08:34 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 19:45:39 +03:00
|
|
|
menupopup.classList.toggle("isOpenedViaTouch", isOpenedViaTouch);
|
|
|
|
|
2017-07-16 23:13:03 +03:00
|
|
|
if (browser.getAttribute("selectmenuconstrained") != "false") {
|
|
|
|
let constraintRect = browser.getBoundingClientRect();
|
|
|
|
constraintRect = new win.DOMRect(constraintRect.left + win.mozInnerScreenX,
|
|
|
|
constraintRect.top + win.mozInnerScreenY,
|
|
|
|
constraintRect.width, constraintRect.height);
|
|
|
|
menupopup.setConstraintRect(constraintRect);
|
|
|
|
} else {
|
|
|
|
menupopup.setConstraintRect(new win.DOMRect(0, 0, 0, 0));
|
|
|
|
}
|
2016-11-08 00:19:19 +03:00
|
|
|
menupopup.openPopupAtScreenRect(AppConstants.platform == "macosx" ? "selection" : "after_start", rect.left, rect.top, rect.width, rect.height, false, false);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
hide(menulist, browser) {
|
2016-04-13 20:15:55 +03:00
|
|
|
if (currentBrowser == browser) {
|
|
|
|
menulist.menupopup.hidePopup();
|
|
|
|
}
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleEvent(event) {
|
2013-08-26 05:34:23 +04:00
|
|
|
switch (event.type) {
|
2016-11-17 17:56:43 +03:00
|
|
|
case "mouseup":
|
2017-01-19 18:03:56 +03:00
|
|
|
function inRect(rect, x, y) {
|
|
|
|
return x >= rect.left && x <= rect.left + rect.width && y >= rect.top && y <= rect.top + rect.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = event.screenX, y = event.screenY;
|
|
|
|
let onAnchor = !inRect(currentMenulist.menupopup.getOuterScreenRect(), x, y) &&
|
|
|
|
inRect(selectRect, x, y) && currentMenulist.menupopup.state == "open";
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:MouseUp", { onAnchor });
|
2016-11-17 17:56:43 +03:00
|
|
|
break;
|
|
|
|
|
2016-03-30 12:54:31 +03:00
|
|
|
case "mouseover":
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:MouseOver", {});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "mouseout":
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:MouseOut", {});
|
|
|
|
break;
|
|
|
|
|
2016-08-03 14:45:45 +03:00
|
|
|
case "keydown":
|
|
|
|
if (event.keyCode == event.DOM_VK_RETURN) {
|
|
|
|
closedWithEnter = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
case "command":
|
|
|
|
if (event.target.hasAttribute("value")) {
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:SelectDropDownItem", {
|
2016-08-03 14:45:45 +03:00
|
|
|
value: event.target.value,
|
2016-12-30 02:34:54 +03:00
|
|
|
closedWithEnter
|
2013-08-26 05:34:23 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-11 17:37:00 +03:00
|
|
|
case "fullscreen":
|
|
|
|
if (currentMenulist) {
|
|
|
|
currentMenulist.menupopup.hidePopup();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
case "popuphidden":
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:DismissedDropDown", {});
|
2015-06-26 19:32:25 +03:00
|
|
|
let popup = event.target;
|
2016-04-16 05:00:36 +03:00
|
|
|
this._unregisterListeners(currentBrowser, popup);
|
2015-06-26 19:32:25 +03:00
|
|
|
popup.parentNode.hidden = true;
|
2016-04-16 05:00:36 +03:00
|
|
|
currentBrowser = null;
|
|
|
|
currentMenulist = null;
|
2017-08-10 23:26:37 +03:00
|
|
|
selectRect = null;
|
2016-04-16 05:00:36 +03:00
|
|
|
currentZoom = 1;
|
2013-08-26 05:34:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
receiveMessage(msg) {
|
2017-04-26 03:19:42 +03:00
|
|
|
if (!currentBrowser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
if (msg.name == "Forms:UpdateDropDown") {
|
|
|
|
// Sanity check - we'd better know what the currently
|
|
|
|
// opened menulist is, and what browser it belongs to...
|
2017-04-26 03:19:42 +03:00
|
|
|
if (!currentMenulist) {
|
2016-04-16 05:00:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-11 08:36:07 +03:00
|
|
|
let scrollBox = currentMenulist.menupopup.scrollBox;
|
|
|
|
let scrollTop = scrollBox.scrollTop;
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
let options = msg.data.options;
|
|
|
|
let selectedIndex = msg.data.selectedIndex;
|
2017-01-31 23:58:27 +03:00
|
|
|
let uaBackgroundColor = msg.data.uaBackgroundColor;
|
|
|
|
let uaColor = msg.data.uaColor;
|
2017-03-23 20:34:21 +03:00
|
|
|
let uaSelectBackgroundColor = msg.data.uaSelectBackgroundColor;
|
|
|
|
let uaSelectColor = msg.data.uaSelectColor;
|
2017-02-04 08:33:13 +03:00
|
|
|
let selectBackgroundColor = msg.data.selectBackgroundColor;
|
|
|
|
let selectColor = msg.data.selectColor;
|
2017-04-10 23:03:03 +03:00
|
|
|
let selectTextShadow = msg.data.selectTextShadow;
|
2017-01-31 23:58:27 +03:00
|
|
|
this.populate(currentMenulist, options, selectedIndex,
|
2017-02-04 08:33:13 +03:00
|
|
|
currentZoom, uaBackgroundColor, uaColor,
|
2017-03-23 20:34:21 +03:00
|
|
|
uaSelectBackgroundColor, uaSelectColor,
|
2017-04-10 23:03:03 +03:00
|
|
|
selectBackgroundColor, selectColor, selectTextShadow);
|
2017-04-11 08:36:07 +03:00
|
|
|
|
|
|
|
// Restore scroll position to what it was prior to the update.
|
|
|
|
scrollBox.scrollTop = scrollTop;
|
2017-04-26 03:19:42 +03:00
|
|
|
} else if (msg.name == "Forms:BlurDropDown-Ping") {
|
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:BlurDropDown-Pong", {});
|
2016-04-16 05:00:36 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_registerListeners(browser, popup) {
|
2013-08-26 05:34:23 +04:00
|
|
|
popup.addEventListener("command", this);
|
|
|
|
popup.addEventListener("popuphidden", this);
|
2016-03-30 12:54:31 +03:00
|
|
|
popup.addEventListener("mouseover", this);
|
|
|
|
popup.addEventListener("mouseout", this);
|
2017-01-27 12:51:03 +03:00
|
|
|
browser.ownerGlobal.addEventListener("mouseup", this, true);
|
|
|
|
browser.ownerGlobal.addEventListener("keydown", this, true);
|
|
|
|
browser.ownerGlobal.addEventListener("fullscreen", this, true);
|
2016-04-16 05:00:36 +03:00
|
|
|
browser.messageManager.addMessageListener("Forms:UpdateDropDown", this);
|
2017-04-26 03:19:42 +03:00
|
|
|
browser.messageManager.addMessageListener("Forms:BlurDropDown-Ping", this);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_unregisterListeners(browser, popup) {
|
2013-08-26 05:34:23 +04:00
|
|
|
popup.removeEventListener("command", this);
|
|
|
|
popup.removeEventListener("popuphidden", this);
|
2016-03-30 12:54:31 +03:00
|
|
|
popup.removeEventListener("mouseover", this);
|
|
|
|
popup.removeEventListener("mouseout", this);
|
2017-01-27 12:51:03 +03:00
|
|
|
browser.ownerGlobal.removeEventListener("mouseup", this, true);
|
|
|
|
browser.ownerGlobal.removeEventListener("keydown", this, true);
|
|
|
|
browser.ownerGlobal.removeEventListener("fullscreen", this, true);
|
2016-04-16 05:00:36 +03:00
|
|
|
browser.messageManager.removeMessageListener("Forms:UpdateDropDown", this);
|
2017-04-26 03:19:42 +03:00
|
|
|
browser.messageManager.removeMessageListener("Forms:BlurDropDown-Ping", this);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-12 03:38:14 +03:00
|
|
|
/**
|
|
|
|
* `populateChildren` creates all <menuitem> elements for the popup menu
|
|
|
|
* based on the list of <option> elements from the <select> element.
|
|
|
|
*
|
|
|
|
* It attempts to intelligently add per-item CSS rules if the single
|
|
|
|
* item values differ from the parent menu values and attempting to avoid
|
|
|
|
* ending up with the same color of text and background.
|
|
|
|
*
|
|
|
|
* @param {Element} menulist
|
|
|
|
* @param {Array<Element>} options
|
|
|
|
* @param {Number} selectedIndex
|
|
|
|
* @param {Number} zoom
|
|
|
|
* @param {String} usedSelectBackgroundColor
|
|
|
|
* @param {String} usedSelectColor
|
|
|
|
* @param {String} selectTextShadow
|
|
|
|
* @param {String} selectBackgroundSet
|
|
|
|
* @param {CSSStyleSheet} sheet
|
|
|
|
* @param {Element} parentElement
|
|
|
|
* @param {Boolean} isGroupDisabled
|
|
|
|
* @param {Number} adjustedTextSize
|
|
|
|
* @param {Boolean} addSearch
|
|
|
|
* @param {Number} nthChildIndex
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
2016-03-18 20:38:40 +03:00
|
|
|
function populateChildren(menulist, options, selectedIndex, zoom,
|
2017-08-12 03:38:14 +03:00
|
|
|
usedSelectBackgroundColor, usedSelectColor,
|
|
|
|
selectTextShadow, selectBackgroundSet, sheet,
|
2017-01-17 23:40:15 +03:00
|
|
|
parentElement = null, isGroupDisabled = false,
|
2017-08-12 03:38:14 +03:00
|
|
|
adjustedTextSize = -1, addSearch = true,
|
|
|
|
nthChildIndex = 1) {
|
2015-04-21 01:11:16 +03:00
|
|
|
let element = menulist.menupopup;
|
2017-01-31 23:58:27 +03:00
|
|
|
let win = element.ownerGlobal;
|
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) {
|
|
|
|
// 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) {
|
2017-01-17 18:48:17 +03:00
|
|
|
let isOptGroup = (option.tagName == "OPTGROUP");
|
2015-06-05 15:33:28 +03:00
|
|
|
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;
|
2016-08-29 19:34:45 +03:00
|
|
|
item.hidden = option.display == "none" || (parentElement && parentElement.hidden);
|
2017-01-17 23:40:15 +03:00
|
|
|
// Keep track of which options are hidden by page content, so we can avoid showing
|
|
|
|
// them on search input
|
|
|
|
item.hiddenByContent = item.hidden;
|
2015-07-14 05:14:31 +03:00
|
|
|
item.setAttribute("tooltiptext", option.tooltip);
|
2013-08-26 05:34:23 +04:00
|
|
|
|
2017-02-03 20:46:45 +03:00
|
|
|
let ruleBody = "";
|
2017-08-12 03:38:14 +03:00
|
|
|
let usedBackgroundColor;
|
|
|
|
let optionBackgroundSet = false;
|
|
|
|
|
2017-02-16 02:25:31 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
option.backgroundColor &&
|
2017-02-16 02:26:13 +03:00
|
|
|
option.backgroundColor != "rgba(0, 0, 0, 0)" &&
|
2017-03-06 20:10:14 +03:00
|
|
|
option.backgroundColor != usedSelectBackgroundColor) {
|
2017-02-03 20:46:45 +03:00
|
|
|
ruleBody = `background-color: ${option.backgroundColor};`;
|
2017-08-12 03:38:14 +03:00
|
|
|
usedBackgroundColor = option.backgroundColor;
|
|
|
|
optionBackgroundSet = true;
|
|
|
|
} else {
|
|
|
|
usedBackgroundColor = usedSelectBackgroundColor;
|
2017-01-31 01:35:14 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 02:25:31 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
option.color &&
|
2017-08-12 03:38:14 +03:00
|
|
|
option.color != usedBackgroundColor &&
|
|
|
|
option.color != usedSelectColor) {
|
2017-02-03 20:46:45 +03:00
|
|
|
ruleBody += `color: ${option.color};`;
|
2017-01-31 01:35:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 23:03:03 +03:00
|
|
|
if (customStylingEnabled &&
|
2017-08-12 03:38:14 +03:00
|
|
|
option.textShadow &&
|
|
|
|
option.textShadow != selectTextShadow) {
|
2017-04-10 23:03:03 +03:00
|
|
|
ruleBody += `text-shadow: ${option.textShadow};`;
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:46:45 +03:00
|
|
|
if (ruleBody) {
|
2017-06-21 22:31:31 +03:00
|
|
|
sheet.insertRule(`#ContentSelectDropdown > menupopup > :nth-child(${nthChildIndex}):not([_moz-menuactive="true"]) {
|
2017-02-03 20:46:45 +03:00
|
|
|
${ruleBody}
|
|
|
|
}`, 0);
|
2017-04-10 23:03:03 +03:00
|
|
|
|
2017-08-12 03:38:14 +03:00
|
|
|
if (option.textShadow && option.textShadow != selectTextShadow) {
|
2017-04-10 23:03:03 +03:00
|
|
|
// Need to explicitly disable the possibly inherited
|
|
|
|
// text-shadow rule when _moz-menuactive=true since
|
|
|
|
// _moz-menuactive=true disables custom option styling.
|
2017-06-21 22:31:31 +03:00
|
|
|
sheet.insertRule(`#ContentSelectDropdown > menupopup > :nth-child(${nthChildIndex})[_moz-menuactive="true"] {
|
2017-04-10 23:03:03 +03:00
|
|
|
text-shadow: none;
|
|
|
|
}`, 0);
|
|
|
|
}
|
2017-08-12 03:38:14 +03:00
|
|
|
}
|
2017-04-10 23:03:03 +03:00
|
|
|
|
2017-08-12 03:38:14 +03:00
|
|
|
if (customStylingEnabled &&
|
|
|
|
(optionBackgroundSet || selectBackgroundSet)) {
|
2017-01-31 01:35:14 +03:00
|
|
|
item.setAttribute("customoptionstyling", "true");
|
|
|
|
} else {
|
|
|
|
item.removeAttribute("customoptionstyling");
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
element.appendChild(item);
|
2017-02-03 20:46:45 +03:00
|
|
|
nthChildIndex++;
|
2013-08-26 05:34:23 +04:00
|
|
|
|
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) {
|
2017-02-03 20:46:45 +03:00
|
|
|
nthChildIndex =
|
|
|
|
populateChildren(menulist, option.children, selectedIndex, zoom,
|
2017-08-12 03:38:14 +03:00
|
|
|
usedSelectBackgroundColor, usedSelectColor,
|
|
|
|
selectTextShadow, selectBackgroundSet, sheet,
|
2017-03-15 02:06:57 +03:00
|
|
|
item, isDisabled, adjustedTextSize, false, nthChildIndex);
|
2013-08-26 05:34:23 +04:00
|
|
|
} else {
|
2016-03-18 20:38:40 +03:00
|
|
|
if (option.index == selectedIndex) {
|
2015-04-21 01:11:16 +03:00
|
|
|
// 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;
|
2016-04-16 05:00:36 +03:00
|
|
|
|
|
|
|
// It's hack time. In the event that we've re-populated the menulist due
|
|
|
|
// to a mutation in the <select> in content, that means that the -moz_activemenu
|
|
|
|
// may have been removed from the selected item. Since that's normally only
|
|
|
|
// set for the initially selected on popupshowing for the menulist, and we
|
|
|
|
// don't want to close and re-open the popup, we manually set it here.
|
|
|
|
menulist.menuBoxObject.activeChild = item;
|
2015-04-21 01:11:16 +03:00
|
|
|
}
|
2015-06-05 15:33:28 +03:00
|
|
|
|
2016-03-18 20:38:40 +03:00
|
|
|
item.setAttribute("value", option.index);
|
2013-08-26 05:34:23 +04:00
|
|
|
|
2016-08-29 19:34:45 +03:00
|
|
|
if (parentElement) {
|
2013-08-26 05:34:23 +04:00
|
|
|
item.classList.add("contentSelectDropdown-ingroup")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 23:40:15 +03:00
|
|
|
|
|
|
|
// Check if search pref is enabled, if this is the first time iterating through
|
|
|
|
// the dropdown, and if the list is long enough for a search element to be added.
|
|
|
|
if (Services.prefs.getBoolPref("dom.forms.selectSearch") && addSearch
|
|
|
|
&& element.childElementCount > SEARCH_MINIMUM_ELEMENTS) {
|
|
|
|
|
|
|
|
// Add a search text field as the first element of the dropdown
|
|
|
|
let searchbox = element.ownerDocument.createElement("textbox");
|
|
|
|
searchbox.setAttribute("type", "search");
|
|
|
|
searchbox.addEventListener("input", onSearchInput);
|
|
|
|
searchbox.addEventListener("focus", onSearchFocus);
|
|
|
|
searchbox.addEventListener("blur", onSearchBlur);
|
2017-03-17 01:58:31 +03:00
|
|
|
searchbox.addEventListener("command", onSearchInput);
|
2017-01-17 23:40:15 +03:00
|
|
|
|
|
|
|
// Handle special keys for exiting search
|
|
|
|
searchbox.addEventListener("keydown", function(event) {
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (event.key) {
|
|
|
|
case "Escape":
|
|
|
|
searchbox.parentElement.hidePopup();
|
|
|
|
break;
|
|
|
|
case "ArrowDown":
|
|
|
|
case "Enter":
|
|
|
|
case "Tab":
|
|
|
|
searchbox.blur();
|
|
|
|
if (searchbox.nextSibling.localName == "menuitem" &&
|
|
|
|
!searchbox.nextSibling.hidden) {
|
|
|
|
menulist.menuBoxObject.activeChild = searchbox.nextSibling;
|
|
|
|
} else {
|
|
|
|
var currentOption = searchbox.nextSibling;
|
|
|
|
while (currentOption && (currentOption.localName != "menuitem" ||
|
|
|
|
currentOption.hidden)) {
|
|
|
|
currentOption = currentOption.nextSibling;
|
|
|
|
}
|
|
|
|
if (currentOption) {
|
|
|
|
menulist.menuBoxObject.activeChild = currentOption;
|
|
|
|
} else {
|
|
|
|
searchbox.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
element.insertBefore(searchbox, element.childNodes[0]);
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:46:45 +03:00
|
|
|
return nthChildIndex;
|
2017-01-17 23:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSearchInput() {
|
|
|
|
let searchObj = this;
|
|
|
|
|
|
|
|
// Get input from search field, set to all lower case for comparison
|
|
|
|
let input = searchObj.value.toLowerCase();
|
|
|
|
// Get all items in dropdown (could be options or optgroups)
|
|
|
|
let menupopup = searchObj.parentElement;
|
|
|
|
let menuItems = menupopup.querySelectorAll("menuitem, menucaption");
|
|
|
|
|
|
|
|
// Flag used to detect any group headers with no visible options.
|
|
|
|
// These group headers should be hidden.
|
|
|
|
let allHidden = true;
|
|
|
|
// Keep a reference to the previous group header (menucaption) to go back
|
|
|
|
// and set to hidden if all options within are hidden.
|
|
|
|
let prevCaption = null;
|
|
|
|
|
|
|
|
for (let currentItem of menuItems) {
|
|
|
|
// Make sure we don't show any options that were hidden by page content
|
|
|
|
if (!currentItem.hiddenByContent) {
|
|
|
|
// Get label and tooltip (title) from option and change to
|
|
|
|
// lower case for comparison
|
|
|
|
let itemLabel = currentItem.getAttribute("label").toLowerCase();
|
|
|
|
let itemTooltip = currentItem.getAttribute("title").toLowerCase();
|
|
|
|
|
|
|
|
// If search input is empty, all options should be shown
|
|
|
|
if (!input) {
|
|
|
|
currentItem.hidden = false;
|
|
|
|
} else if (currentItem.localName == "menucaption") {
|
|
|
|
if (prevCaption != null) {
|
|
|
|
prevCaption.hidden = allHidden;
|
|
|
|
}
|
|
|
|
prevCaption = currentItem;
|
|
|
|
allHidden = true;
|
|
|
|
} else {
|
|
|
|
if (!currentItem.classList.contains("contentSelectDropdown-ingroup") &&
|
|
|
|
currentItem.previousSibling.classList.contains("contentSelectDropdown-ingroup")) {
|
|
|
|
if (prevCaption != null) {
|
|
|
|
prevCaption.hidden = allHidden;
|
|
|
|
}
|
|
|
|
prevCaption = null;
|
|
|
|
allHidden = true;
|
|
|
|
}
|
|
|
|
if (itemLabel.includes(input) || itemTooltip.includes(input)) {
|
|
|
|
currentItem.hidden = false;
|
|
|
|
allHidden = false;
|
|
|
|
} else {
|
|
|
|
currentItem.hidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prevCaption != null) {
|
|
|
|
prevCaption.hidden = allHidden;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSearchFocus() {
|
|
|
|
let searchObj = this;
|
|
|
|
let menupopup = searchObj.parentElement;
|
|
|
|
menupopup.parentElement.menuBoxObject.activeChild = null;
|
|
|
|
menupopup.setAttribute("ignorekeys", "true");
|
2017-04-26 03:19:42 +03:00
|
|
|
currentBrowser.messageManager.sendAsyncMessage("Forms:SearchFocused", {});
|
2017-01-17 23:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSearchBlur() {
|
|
|
|
let searchObj = this;
|
|
|
|
let menupopup = searchObj.parentElement;
|
|
|
|
menupopup.setAttribute("ignorekeys", "false");
|
2013-08-26 05:34:23 +04:00
|
|
|
}
|