toolkit/modules/SelectContentHelper.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "BrowserUtils",
michael@0 14 "resource://gre/modules/BrowserUtils.jsm");
michael@0 15
michael@0 16 this.EXPORTED_SYMBOLS = [
michael@0 17 "SelectContentHelper"
michael@0 18 ];
michael@0 19
michael@0 20 this.SelectContentHelper = function (aElement, aGlobal) {
michael@0 21 this.element = aElement;
michael@0 22 this.global = aGlobal;
michael@0 23 this.init();
michael@0 24 this.showDropDown();
michael@0 25 }
michael@0 26
michael@0 27 this.SelectContentHelper.prototype = {
michael@0 28 init: function() {
michael@0 29 this.global.addMessageListener("Forms:SelectDropDownItem", this);
michael@0 30 this.global.addMessageListener("Forms:DismissedDropDown", this);
michael@0 31 this.global.addEventListener("pagehide", this);
michael@0 32 },
michael@0 33
michael@0 34 uninit: function() {
michael@0 35 this.global.removeMessageListener("Forms:SelectDropDownItem", this);
michael@0 36 this.global.removeMessageListener("Forms:DismissedDropDown", this);
michael@0 37 this.global.removeEventListener("pagehide", this);
michael@0 38 this.element = null;
michael@0 39 this.global = null;
michael@0 40 },
michael@0 41
michael@0 42 showDropDown: function() {
michael@0 43 let rect = this._getBoundingContentRect();
michael@0 44
michael@0 45 this.global.sendAsyncMessage("Forms:ShowDropDown", {
michael@0 46 rect: rect,
michael@0 47 options: this._buildOptionList(),
michael@0 48 selectedIndex: this.element.selectedIndex,
michael@0 49 });
michael@0 50 },
michael@0 51
michael@0 52 _getBoundingContentRect: function() {
michael@0 53 return BrowserUtils.getElementBoundingScreenRect(this.element);
michael@0 54 },
michael@0 55
michael@0 56 _buildOptionList: function() {
michael@0 57 return buildOptionListForChildren(this.element);
michael@0 58 },
michael@0 59
michael@0 60 receiveMessage: function(message) {
michael@0 61 switch (message.name) {
michael@0 62 case "Forms:SelectDropDownItem":
michael@0 63 if (this.element.selectedIndex != message.data.value) {
michael@0 64 this.element.selectedIndex = message.data.value;
michael@0 65
michael@0 66 let event = this.element.ownerDocument.createEvent("Events");
michael@0 67 event.initEvent("change", true, true);
michael@0 68 this.element.dispatchEvent(event);
michael@0 69 }
michael@0 70
michael@0 71 //intentional fall-through
michael@0 72 case "Forms:DismissedDropDown":
michael@0 73 this.uninit();
michael@0 74 break;
michael@0 75 }
michael@0 76 },
michael@0 77
michael@0 78 handleEvent: function(event) {
michael@0 79 switch (event.type) {
michael@0 80 case "pagehide":
michael@0 81 this.global.sendAsyncMessage("Forms:HideDropDown", {});
michael@0 82 this.uninit();
michael@0 83 break;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 }
michael@0 88
michael@0 89 function buildOptionListForChildren(node) {
michael@0 90 let result = [];
michael@0 91 for (let child = node.firstChild; child; child = child.nextSibling) {
michael@0 92 if (child.tagName == 'OPTION' || child.tagName == 'OPTGROUP') {
michael@0 93 let info = {
michael@0 94 tagName: child.tagName,
michael@0 95 textContent: child.tagName == 'OPTGROUP' ? child.getAttribute("label")
michael@0 96 : child.textContent,
michael@0 97 // XXX this uses a highlight color when this is the selected element.
michael@0 98 // We need to suppress such highlighting in the content process to get
michael@0 99 // the option's correct unhighlighted color here.
michael@0 100 // We also need to detect default color vs. custom so that a standard
michael@0 101 // color does not override color: menutext in the parent.
michael@0 102 // backgroundColor: computedStyle.backgroundColor,
michael@0 103 // color: computedStyle.color,
michael@0 104 children: child.tagName == 'OPTGROUP' ? buildOptionListForChildren(child) : []
michael@0 105 };
michael@0 106 result.push(info);
michael@0 107 }
michael@0 108 }
michael@0 109 return result;
michael@0 110 }

mercurial