gecko-dev/toolkit/actors/SelectChild.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

401 строка
13 KiB
JavaScript
Исходник Обычный вид История

/* vim: set ts=2 sw=2 sts=2 et tw=80: */
/* 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/. */
"use strict";
var EXPORTED_SYMBOLS = ["SelectChild"];
const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.defineModuleGetter(this, "BrowserUtils",
"resource://gre/modules/BrowserUtils.jsm");
ChromeUtils.defineModuleGetter(this, "DeferredTask",
"resource://gre/modules/DeferredTask.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["InspectorUtils"]);
const kStateActive = 0x00000001; // NS_EVENT_STATE_ACTIVE
const kStateHover = 0x00000004; // NS_EVENT_STATE_HOVER
const SUPPORTED_PROPERTIES = [
"direction",
"color",
"background-color",
"text-shadow",
"font-family",
"font-weight",
"font-size",
"font-style",
];
// A process global state for whether or not content thinks
// that a <select> dropdown is open or not. This is managed
// entirely within this module, and is read-only accessible
// via SelectContentHelper.open.
var gOpen = false;
var SelectContentHelper = function(aElement, aOptions, aGlobal) {
this.element = aElement;
this.initialSelection = aElement[aElement.selectedIndex] || null;
this.global = aGlobal;
this.closedWithClickOn = false;
this.isOpenedViaTouch = aOptions.isOpenedViaTouch;
this._closeAfterBlur = true;
this._pseudoStylesSetup = false;
this._lockedDescendants = null;
this.init();
this.showDropDown();
this._updateTimer = new DeferredTask(this._update.bind(this), 0);
};
Object.defineProperty(SelectContentHelper, "open", {
get() {
return gOpen;
},
});
this.SelectContentHelper.prototype = {
init() {
this.global.addMessageListener("Forms:SelectDropDownItem", this);
this.global.addMessageListener("Forms:DismissedDropDown", this);
this.global.addMessageListener("Forms:MouseOver", this);
this.global.addMessageListener("Forms:MouseOut", this);
this.global.addMessageListener("Forms:MouseUp", this);
this.global.addMessageListener("Forms:SearchFocused", this);
this.global.addMessageListener("Forms:BlurDropDown-Pong", this);
this.global.addEventListener("pagehide", this, { mozSystemGroup: true });
this.global.addEventListener("mozhidedropdown", this, { mozSystemGroup: true });
this.element.addEventListener("blur", this, { mozSystemGroup: true });
this.element.addEventListener("transitionend", this, { mozSystemGroup: true });
let MutationObserver = this.element.ownerGlobal.MutationObserver;
this.mut = new MutationObserver(mutations => {
// Something changed the <select> while it was open, so
// we'll poke a DeferredTask to update the parent sometime
// in the very near future.
this._updateTimer.arm();
});
this.mut.observe(this.element, {childList: true, subtree: true, attributes: true});
},
uninit() {
this.element.openInParentProcess = false;
this.global.removeMessageListener("Forms:SelectDropDownItem", this);
this.global.removeMessageListener("Forms:DismissedDropDown", this);
this.global.removeMessageListener("Forms:MouseOver", this);
this.global.removeMessageListener("Forms:MouseOut", this);
this.global.removeMessageListener("Forms:MouseUp", this);
this.global.removeMessageListener("Forms:SearchFocused", this);
this.global.removeMessageListener("Forms:BlurDropDown-Pong", this);
this.global.removeEventListener("pagehide", this, { mozSystemGroup: true });
this.global.removeEventListener("mozhidedropdown", this, { mozSystemGroup: true });
this.element.removeEventListener("blur", this, { mozSystemGroup: true });
this.element.removeEventListener("transitionend", this, { mozSystemGroup: true });
this.element = null;
this.global = null;
this.mut.disconnect();
this._updateTimer.disarm();
this._updateTimer = null;
gOpen = false;
},
showDropDown() {
this.element.openInParentProcess = true;
this._setupPseudoClassStyles();
let rect = this._getBoundingContentRect();
let computedStyles = getComputedStyles(this.element);
let options = this._buildOptionList();
let defaultStyles = this.element.ownerGlobal.getDefaultComputedStyle(this.element);
this.global.sendAsyncMessage("Forms:ShowDropDown", {
isOpenedViaTouch: this.isOpenedViaTouch,
options,
rect,
selectedIndex: this.element.selectedIndex,
style: supportedStyles(computedStyles),
defaultStyle: supportedStyles(defaultStyles),
});
this._clearPseudoClassStyles();
gOpen = true;
},
_setupPseudoClassStyles() {
if (this._pseudoStylesSetup) {
throw new Error("pseudo styles must not be set up yet");
}
// Do all of the things that change style at once, before we read
// any styles.
this._pseudoStylesSetup = true;
InspectorUtils.addPseudoClassLock(this.element, ":focus");
let lockedDescendants = this._lockedDescendants = this.element.querySelectorAll(":checked");
for (let child of lockedDescendants) {
// Selected options have the :checked pseudo-class, which
// we want to disable before calculating the computed
// styles since the user agent styles alter the styling
// based on :checked.
InspectorUtils.addPseudoClassLock(child, ":checked", false);
}
},
_clearPseudoClassStyles() {
if (!this._pseudoStylesSetup) {
throw new Error("pseudo styles must be set up already");
}
// Undo all of the things that change style at once, after we're
// done reading styles.
InspectorUtils.clearPseudoClassLocks(this.element);
let lockedDescendants = this._lockedDescendants;
for (let child of lockedDescendants) {
InspectorUtils.clearPseudoClassLocks(child);
}
this._lockedDescendants = null;
this._pseudoStylesSetup = false;
},
_getBoundingContentRect() {
return BrowserUtils.getElementBoundingScreenRect(this.element);
},
_buildOptionList() {
if (!this._pseudoStylesSetup) {
throw new Error("pseudo styles must be set up");
}
let uniqueStyles = [];
let options = buildOptionListForChildren(this.element, uniqueStyles);
return { options, uniqueStyles };
},
_update() {
// The <select> was updated while the dropdown was open.
// Let's send up a new list of options.
// Technically we might not need to set this pseudo-class
// during _update() since the element should organically
// have :focus, though it is here for belt-and-suspenders.
this._setupPseudoClassStyles();
let computedStyles = getComputedStyles(this.element);
let defaultStyles = this.element.ownerGlobal.getDefaultComputedStyle(this.element);
this.global.sendAsyncMessage("Forms:UpdateDropDown", {
options: this._buildOptionList(),
selectedIndex: this.element.selectedIndex,
style: supportedStyles(computedStyles),
defaultStyle: supportedStyles(defaultStyles),
});
this._clearPseudoClassStyles();
},
dispatchMouseEvent(win, target, eventName) {
let mouseEvent = new win.MouseEvent(eventName, {
view: win,
bubbles: true,
cancelable: true,
});
target.dispatchEvent(mouseEvent);
},
receiveMessage(message) {
switch (message.name) {
case "Forms:SelectDropDownItem":
this.element.selectedIndex = message.data.value;
this.closedWithClickOn = !message.data.closedWithEnter;
break;
case "Forms:DismissedDropDown": {
let win = this.element.ownerGlobal;
let selectedOption = this.element.item(this.element.selectedIndex);
// For ordering of events, we're using non-e10s as our guide here,
// since the spec isn't exactly clear. In non-e10s:
// - If the user clicks on an element in the dropdown, we fire
// mousedown, mouseup, input, change, and click events.
// - If the user uses the keyboard to select an element in the
// dropdown, we only fire input and change events.
// - If the user pressed ESC key or clicks outside the dropdown,
// we fire nothing as the selected option is unchanged.
if (this.closedWithClickOn) {
this.dispatchMouseEvent(win, selectedOption, "mousedown");
this.dispatchMouseEvent(win, selectedOption, "mouseup");
}
// Clear active document no matter user selects via keyboard or mouse
InspectorUtils.removeContentState(this.element, kStateActive,
/* aClearActiveDocument */ true);
// Fire input and change events when selected option changes
if (this.initialSelection !== selectedOption) {
let inputEvent = new win.Event("input", {
bubbles: true,
});
this.element.dispatchEvent(inputEvent);
let changeEvent = new win.Event("change", {
bubbles: true,
});
this.element.dispatchEvent(changeEvent);
}
// Fire click event
if (this.closedWithClickOn) {
this.dispatchMouseEvent(win, selectedOption, "click");
}
this.uninit();
break;
}
case "Forms:MouseOver":
InspectorUtils.setContentState(this.element, kStateHover);
break;
case "Forms:MouseOut":
InspectorUtils.removeContentState(this.element, kStateHover);
break;
case "Forms:MouseUp":
let win = this.element.ownerGlobal;
if (message.data.onAnchor) {
this.dispatchMouseEvent(win, this.element, "mouseup");
}
InspectorUtils.removeContentState(this.element, kStateActive);
if (message.data.onAnchor) {
this.dispatchMouseEvent(win, this.element, "click");
}
break;
case "Forms:SearchFocused":
this._closeAfterBlur = false;
break;
case "Forms:BlurDropDown-Pong":
if (!this._closeAfterBlur || !gOpen) {
return;
}
this.global.sendAsyncMessage("Forms:HideDropDown", {});
this.uninit();
break;
}
},
handleEvent(event) {
switch (event.type) {
case "pagehide":
if (this.element.ownerDocument === event.target) {
this.global.sendAsyncMessage("Forms:HideDropDown", {});
this.uninit();
}
break;
case "blur": {
if (this.element !== event.target) {
break;
}
this._closeAfterBlur = true;
// Send a ping-pong message to make sure that we wait for
// enough cycles to pass from the potential focusing of the
// search box to disable closing-after-blur.
this.global.sendAsyncMessage("Forms:BlurDropDown-Ping", {});
break;
}
case "mozhidedropdown":
if (this.element === event.target) {
this.global.sendAsyncMessage("Forms:HideDropDown", {});
this.uninit();
}
break;
case "transitionend":
if (SUPPORTED_PROPERTIES.includes(event.propertyName)) {
this._updateTimer.arm();
}
break;
}
},
};
function getComputedStyles(element) {
return element.ownerGlobal.getComputedStyle(element);
}
function supportedStyles(cs) {
let styles = {};
for (let property of SUPPORTED_PROPERTIES) {
styles[property] = cs.getPropertyValue(property);
}
return styles;
}
function supportedStylesEqual(styles, otherStyles) {
for (let property of SUPPORTED_PROPERTIES) {
if (styles[property] !== otherStyles[property]) {
return false;
}
}
return true;
}
function uniqueStylesIndex(cs, uniqueStyles) {
let styles = supportedStyles(cs);
for (let i = uniqueStyles.length; i--; ) {
if (supportedStylesEqual(uniqueStyles[i], styles)) {
return i;
}
}
uniqueStyles.push(styles);
return uniqueStyles.length - 1;
}
function buildOptionListForChildren(node, uniqueStyles) {
let result = [];
for (let child of node.children) {
let tagName = child.tagName.toUpperCase();
if (tagName == "OPTION" || tagName == "OPTGROUP") {
if (child.hidden) {
continue;
}
let textContent =
tagName == "OPTGROUP" ? child.getAttribute("label")
: child.text;
if (textContent == null) {
textContent = "";
}
let cs = getComputedStyles(child);
let info = {
index: child.index,
tagName,
textContent,
disabled: child.disabled,
display: cs.display,
tooltip: child.title,
children: tagName == "OPTGROUP"
? buildOptionListForChildren(child, uniqueStyles)
: [],
// Most options have the same style. In order to reduce the size of the
// IPC message, coalesce them in uniqueStyles.
styleIndex: uniqueStylesIndex(cs, uniqueStyles),
};
result.push(info);
}
}
return result;
}
class SelectChild extends ActorChild {
handleEvent(event) {
if (SelectContentHelper.open) {
return;
}
switch (event.type) {
case "mozshowdropdown":
new SelectContentHelper(event.target, {isOpenedViaTouch: false}, this.mm);
break;
case "mozshowdropdown-sourcetouch":
new SelectContentHelper(event.target, {isOpenedViaTouch: true}, this.mm);
break;
}
}
}