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");
|
|
|
|
|
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;
|
|
|
|
this.init();
|
|
|
|
this.showDropDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.SelectContentHelper.prototype = {
|
|
|
|
init: function() {
|
|
|
|
this.global.addMessageListener("Forms:SelectDropDownItem", this);
|
|
|
|
this.global.addMessageListener("Forms:DismissedDropDown", this);
|
|
|
|
this.global.addEventListener("pagehide", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
this.global.removeMessageListener("Forms:SelectDropDownItem", this);
|
|
|
|
this.global.removeMessageListener("Forms:DismissedDropDown", this);
|
|
|
|
this.global.removeEventListener("pagehide", this);
|
|
|
|
this.element = null;
|
|
|
|
this.global = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
showDropDown: function() {
|
|
|
|
let rect = this._getBoundingContentRect();
|
|
|
|
|
|
|
|
this.global.sendAsyncMessage("Forms:ShowDropDown", {
|
|
|
|
rect: rect,
|
|
|
|
options: this._buildOptionList(),
|
2015-06-08 21:41:17 +03:00
|
|
|
selectedIndex: this.element.selectedIndex
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Forms:SelectDropDownItem":
|
2015-06-26 19:32:25 +03:00
|
|
|
this.element.selectedIndex = message.data.value;
|
|
|
|
break;
|
2013-12-11 01:46:21 +04:00
|
|
|
|
2015-06-26 19:32:25 +03:00
|
|
|
case "Forms:DismissedDropDown":
|
|
|
|
if (this.initialSelection != this.element.item[this.element.selectedIndex]) {
|
2013-12-11 01:46:21 +04:00
|
|
|
let event = this.element.ownerDocument.createEvent("Events");
|
|
|
|
event.initEvent("change", true, true);
|
|
|
|
this.element.dispatchEvent(event);
|
|
|
|
}
|
2013-08-26 05:34:23 +04:00
|
|
|
|
|
|
|
this.uninit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case "pagehide":
|
|
|
|
this.global.sendAsyncMessage("Forms:HideDropDown", {});
|
|
|
|
this.uninit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildOptionListForChildren(node) {
|
|
|
|
let result = [];
|
2015-04-15 23:00:29 +03:00
|
|
|
let win = node.ownerDocument.defaultView;
|
|
|
|
|
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') {
|
2014-11-05 10:21:05 +03:00
|
|
|
let textContent =
|
2015-03-17 02:29:51 +03:00
|
|
|
tagName == 'OPTGROUP' ? child.getAttribute("label")
|
|
|
|
: child.textContent;
|
2014-11-05 10:21:05 +03:00
|
|
|
|
|
|
|
if (textContent != null) {
|
|
|
|
textContent = textContent.trim();
|
|
|
|
} else {
|
|
|
|
textContent = ""
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:34:23 +04:00
|
|
|
let info = {
|
|
|
|
tagName: child.tagName,
|
2014-11-05 10:21:05 +03:00
|
|
|
textContent: textContent,
|
2015-06-05 15:33:29 +03:00
|
|
|
disabled: child.disabled,
|
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
|
|
|
|
textDirection: win.getComputedStyle(child).getPropertyValue("direction"),
|
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;
|
|
|
|
}
|