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";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2014-03-01 05:00:17 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "BrowserUtils",
|
|
|
|
"resource://gre/modules/BrowserUtils.jsm");
|
2016-03-30 12:54:31 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "DOMUtils",
|
|
|
|
"@mozilla.org/inspector/dom-utils;1", "inIDOMUtils");
|
2016-04-16 05:00:36 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask",
|
|
|
|
"resource://gre/modules/DeferredTask.jsm");
|
2016-03-30 12:54:31 +03:00
|
|
|
|
2016-07-25 16:09:11 +03:00
|
|
|
const kStateActive = 0x00000001; // NS_EVENT_STATE_ACTIVE
|
2016-03-30 12:54:31 +03:00
|
|
|
const kStateHover = 0x00000004; // NS_EVENT_STATE_HOVER
|
2014-03-01 05:00:17 +04:00
|
|
|
|
2016-05-13 17:40:24 +03:00
|
|
|
// 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;
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"SelectContentHelper"
|
|
|
|
];
|
|
|
|
|
|
|
|
this.SelectContentHelper = function (aElement, aGlobal) {
|
|
|
|
this.element = aElement;
|
2015-06-26 19:32:25 +03:00
|
|
|
this.initialSelection = aElement[aElement.selectedIndex] || null;
|
2013-08-26 05:34:23 +04:00
|
|
|
this.global = aGlobal;
|
2016-08-03 14:45:45 +03:00
|
|
|
this.closedWithEnter = false;
|
2013-08-26 05:34:23 +04:00
|
|
|
this.init();
|
|
|
|
this.showDropDown();
|
2016-04-16 05:00:36 +03:00
|
|
|
this._updateTimer = new DeferredTask(this._update.bind(this), 0);
|
2013-08-26 05:34:23 +04:00
|
|
|
}
|
|
|
|
|
2016-05-13 17:40:24 +03:00
|
|
|
Object.defineProperty(SelectContentHelper, "open", {
|
|
|
|
get: function() {
|
|
|
|
return gOpen;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
this.SelectContentHelper.prototype = {
|
|
|
|
init: function() {
|
|
|
|
this.global.addMessageListener("Forms:SelectDropDownItem", this);
|
|
|
|
this.global.addMessageListener("Forms:DismissedDropDown", this);
|
2016-03-30 12:54:31 +03:00
|
|
|
this.global.addMessageListener("Forms:MouseOver", this);
|
|
|
|
this.global.addMessageListener("Forms:MouseOut", this);
|
2013-08-26 05:34:23 +04:00
|
|
|
this.global.addEventListener("pagehide", this);
|
2016-04-22 23:13:10 +03:00
|
|
|
this.global.addEventListener("mozhidedropdown", this);
|
2016-04-16 05:00:36 +03:00
|
|
|
let MutationObserver = this.element.ownerDocument.defaultView.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});
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
this.global.removeMessageListener("Forms:SelectDropDownItem", this);
|
|
|
|
this.global.removeMessageListener("Forms:DismissedDropDown", this);
|
2016-03-30 12:54:31 +03:00
|
|
|
this.global.removeMessageListener("Forms:MouseOver", this);
|
|
|
|
this.global.removeMessageListener("Forms:MouseOut", this);
|
2013-08-26 05:34:23 +04:00
|
|
|
this.global.removeEventListener("pagehide", this);
|
2016-04-22 23:13:10 +03:00
|
|
|
this.global.removeEventListener("mozhidedropdown", this);
|
2013-08-26 05:34:23 +04:00
|
|
|
this.element = null;
|
|
|
|
this.global = null;
|
2016-04-16 05:00:36 +03:00
|
|
|
this.mut.disconnect();
|
|
|
|
this._updateTimer.disarm();
|
|
|
|
this._updateTimer = null;
|
2016-05-13 17:40:24 +03:00
|
|
|
gOpen = false;
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showDropDown: function() {
|
|
|
|
let rect = this._getBoundingContentRect();
|
|
|
|
this.global.sendAsyncMessage("Forms:ShowDropDown", {
|
|
|
|
rect: rect,
|
|
|
|
options: this._buildOptionList(),
|
2015-07-14 05:03:51 +03:00
|
|
|
selectedIndex: this.element.selectedIndex,
|
|
|
|
direction: getComputedDirection(this.element)
|
2013-08-26 05:34:23 +04:00
|
|
|
});
|
2016-05-13 17:40:24 +03:00
|
|
|
gOpen = true;
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_getBoundingContentRect: function() {
|
2014-03-01 05:00:17 +04:00
|
|
|
return BrowserUtils.getElementBoundingScreenRect(this.element);
|
2013-08-26 05:34:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildOptionList: function() {
|
|
|
|
return buildOptionListForChildren(this.element);
|
|
|
|
},
|
|
|
|
|
2016-04-16 05:00:36 +03:00
|
|
|
_update() {
|
|
|
|
// The <select> was updated while the dropdown was open.
|
|
|
|
// Let's send up a new list of options.
|
|
|
|
this.global.sendAsyncMessage("Forms:UpdateDropDown", {
|
|
|
|
options: this._buildOptionList(),
|
|
|
|
selectedIndex: this.element.selectedIndex,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
receiveMessage: function(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Forms:SelectDropDownItem":
|
2015-06-26 19:32:25 +03:00
|
|
|
this.element.selectedIndex = message.data.value;
|
2016-08-03 14:45:45 +03:00
|
|
|
this.closedWithEnter = message.data.closedWithEnter;
|
2015-06-26 19:32:25 +03:00
|
|
|
break;
|
2013-12-11 01:46:21 +04:00
|
|
|
|
2015-06-26 19:32:25 +03:00
|
|
|
case "Forms:DismissedDropDown":
|
2016-08-03 14:45:45 +03:00
|
|
|
let selectedOption = this.element.item(this.element.selectedIndex);
|
|
|
|
if (this.initialSelection != selectedOption) {
|
2016-05-13 17:40:24 +03:00
|
|
|
let win = this.element.ownerDocument.defaultView;
|
|
|
|
let inputEvent = new win.UIEvent("input", {
|
|
|
|
bubbles: true,
|
|
|
|
});
|
|
|
|
this.element.dispatchEvent(inputEvent);
|
|
|
|
|
|
|
|
let changeEvent = new win.Event("change", {
|
|
|
|
bubbles: true,
|
|
|
|
});
|
|
|
|
this.element.dispatchEvent(changeEvent);
|
|
|
|
|
2016-08-03 14:45:45 +03:00
|
|
|
if (!this.closedWithEnter) {
|
|
|
|
// Going for mostly-Blink parity here, which (at least on Windows)
|
|
|
|
// fires a mouseup and click event after each selection -
|
|
|
|
// even by keyboard. We're firing a mousedown too, since that
|
|
|
|
// seems to make more sense. Unfortunately, the spec on form
|
|
|
|
// control behaviours for these events is really not clear.
|
|
|
|
const MOUSE_EVENTS = ["mousedown", "mouseup", "click"];
|
|
|
|
for (let eventName of MOUSE_EVENTS) {
|
|
|
|
let mouseEvent = new win.MouseEvent(eventName, {
|
|
|
|
view: win,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
});
|
|
|
|
selectedOption.dispatchEvent(mouseEvent);
|
|
|
|
if (eventName == "mouseup") {
|
|
|
|
DOMUtils.removeContentState(this.element, kStateActive);
|
|
|
|
}
|
2016-07-25 16:09:11 +03:00
|
|
|
}
|
2016-05-25 20:21:23 +03:00
|
|
|
}
|
2013-12-11 01:46:21 +04:00
|
|
|
}
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
this.uninit();
|
|
|
|
break;
|
2016-03-30 12:54:31 +03:00
|
|
|
|
|
|
|
case "Forms:MouseOver":
|
|
|
|
DOMUtils.setContentState(this.element, kStateHover);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Forms:MouseOut":
|
|
|
|
DOMUtils.removeContentState(this.element, kStateHover);
|
|
|
|
break;
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case "pagehide":
|
2016-03-18 10:12:46 +03:00
|
|
|
if (this.element.ownerDocument === event.target) {
|
|
|
|
this.global.sendAsyncMessage("Forms:HideDropDown", {});
|
|
|
|
this.uninit();
|
|
|
|
}
|
2013-08-26 05:34:23 +04:00
|
|
|
break;
|
2016-04-13 20:15:55 +03:00
|
|
|
case "mozhidedropdown":
|
|
|
|
if (this.element === event.target) {
|
|
|
|
this.global.sendAsyncMessage("Forms:HideDropDown", {});
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
break;
|
2013-08-26 05:34:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-14 05:03:51 +03:00
|
|
|
function getComputedDirection(element) {
|
|
|
|
return element.ownerDocument.defaultView.getComputedStyle(element).getPropertyValue("direction");
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
function buildOptionListForChildren(node) {
|
|
|
|
let result = [];
|
2015-04-15 23:00:29 +03:00
|
|
|
|
2015-03-17 02:29:51 +03:00
|
|
|
for (let child of node.children) {
|
|
|
|
let tagName = child.tagName.toUpperCase();
|
2015-04-15 23:00:29 +03:00
|
|
|
|
2015-03-17 02:29:51 +03:00
|
|
|
if (tagName == 'OPTION' || tagName == 'OPTGROUP') {
|
2016-01-27 02:12:17 +03:00
|
|
|
if (child.hidden) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:21:05 +03:00
|
|
|
let textContent =
|
2015-03-17 02:29:51 +03:00
|
|
|
tagName == 'OPTGROUP' ? child.getAttribute("label")
|
2015-08-21 20:30:04 +03:00
|
|
|
: child.text;
|
|
|
|
if (textContent == null) {
|
|
|
|
textContent = "";
|
2014-11-05 10:21:05 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
let info = {
|
2016-03-18 20:38:40 +03:00
|
|
|
index: child.index,
|
2015-08-10 15:31:37 +03:00
|
|
|
tagName: tagName,
|
2014-11-05 10:21:05 +03:00
|
|
|
textContent: textContent,
|
2015-06-05 15:33:29 +03:00
|
|
|
disabled: child.disabled,
|
2015-10-15 21:29:25 +03:00
|
|
|
display: child.style.display,
|
2015-04-15 23:00:29 +03:00
|
|
|
// We need to do this for every option element as each one can have
|
|
|
|
// an individual style set for direction
|
2015-07-14 05:03:51 +03:00
|
|
|
textDirection: getComputedDirection(child),
|
2015-07-14 05:14:31 +03:00
|
|
|
tooltip: child.title,
|
2013-08-26 05:34:23 +04:00
|
|
|
// XXX this uses a highlight color when this is the selected element.
|
|
|
|
// We need to suppress such highlighting in the content process to get
|
|
|
|
// the option's correct unhighlighted color here.
|
|
|
|
// We also need to detect default color vs. custom so that a standard
|
|
|
|
// color does not override color: menutext in the parent.
|
|
|
|
// backgroundColor: computedStyle.backgroundColor,
|
|
|
|
// color: computedStyle.color,
|
2015-03-17 02:29:51 +03:00
|
|
|
children: tagName == 'OPTGROUP' ? buildOptionListForChildren(child) : []
|
2013-08-26 05:34:23 +04:00
|
|
|
};
|
|
|
|
result.push(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|