toolkit/modules/SelectContentHelper.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial