browser/metro/base/content/commandUtil.js

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

     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/. */
     6 /**
     7  * Command Updater
     8  */
     9 var CommandUpdater = {
    10   /**
    11    * Gets a controller that can handle a particular command.
    12    * @param   command
    13    *          A command to locate a controller for, preferring controllers that
    14    *          show the command as enabled.
    15    * @returns In this order of precedence:
    16    *            - the first controller supporting the specified command
    17    *              associated with the focused element that advertises the
    18    *              command as ENABLED
    19    *            - the first controller supporting the specified command
    20    *              associated with the global window that advertises the
    21    *              command as ENABLED
    22    *            - the first controller supporting the specified command
    23    *              associated with the focused element
    24    *            - the first controller supporting the specified command
    25    *              associated with the global window
    26    */
    27   _getControllerForCommand: function(command) {
    28     try {
    29       var controller = top.document.commandDispatcher.getControllerForCommand(command);
    30       if (controller && controller.isCommandEnabled(command))
    31         return controller;
    32     }
    33     catch(e) {
    34     }
    35     var controllerCount = window.controllers.getControllerCount();
    36     for (var i = 0; i < controllerCount; ++i) {
    37       var current = window.controllers.getControllerAt(i);
    38       try {
    39         if (current.supportsCommand(command) && current.isCommandEnabled(command))
    40           return current;
    41       }
    42       catch (e) {
    43       }
    44     }
    45     return controller || window.controllers.getControllerForCommand(command);
    46   },
    48   /**
    49    * Updates the state of a XUL <command> element for the specified command
    50    * depending on its state.
    51    * @param   command
    52    *          The name of the command to update the XUL <command> element for
    53    */
    54   updateCommand: function(command) {
    55     var enabled = false;
    56     try {
    57       var controller = this._getControllerForCommand(command);
    58       if (controller) {
    59         enabled = controller.isCommandEnabled(command);
    60       }
    61     }
    62     catch(ex) { }
    64     this.enableCommand(command, enabled);
    65   },
    67   /**
    68    * Updates the state of a XUL <command> element for the specified command
    69    * depending on its state.
    70    * @param   command
    71    *          The name of the command to update the XUL <command> element for
    72    */
    73   updateCommands: function(_commands) {
    74     var commands = _commands.split(",");
    75     for (var command in commands) {
    76       this.updateCommand(commands[command]);
    77     }
    78   },
    80   /**
    81    * Enables or disables a XUL <command> element.
    82    * @param   command
    83    *          The name of the command to enable or disable
    84    * @param   enabled
    85    *          true if the command should be enabled, false otherwise.
    86    */
    87   enableCommand: function(command, enabled) {
    88     var element = document.getElementById(command);
    89     if (!element)
    90       return;
    91     if (enabled)
    92       element.removeAttribute("disabled");
    93     else
    94       element.setAttribute("disabled", "true");
    95   },
    97   /**
    98    * Performs the action associated with a specified command using the most
    99    * relevant controller.
   100    * @param   command
   101    *          The command to perform.
   102    */
   103   doCommand: function(command) {
   104     var controller = this._getControllerForCommand(command);
   105     if (!controller)
   106       return;
   107     controller.doCommand(command);
   108   },
   110   /**
   111    * Changes the label attribute for the specified command.
   112    * @param   command
   113    *          The command to update.
   114    * @param   labelAttribute
   115    *          The label value to use.
   116    */
   117   setMenuValue: function(command, labelAttribute) {
   118     var commandNode = top.document.getElementById(command);
   119     if (commandNode)
   120     {
   121       var label = commandNode.getAttribute(labelAttribute);
   122       if ( label )
   123         commandNode.setAttribute('label', label);
   124     }
   125   },
   127   /**
   128    * Changes the accesskey attribute for the specified command.
   129    * @param   command
   130    *          The command to update.
   131    * @param   valueAttribute
   132    *          The value attribute to use.
   133    */
   134   setAccessKey: function(command, valueAttribute) {
   135     var commandNode = top.document.getElementById(command);
   136     if (commandNode)
   137     {
   138       var value = commandNode.getAttribute(valueAttribute);
   139       if ( value )
   140         commandNode.setAttribute('accesskey', value);
   141     }
   142   },
   144   /**
   145    * Inform all the controllers attached to a node that an event has occurred
   146    * (e.g. the tree controllers need to be informed of blur events so that they can change some of the
   147    * menu items back to their default values)
   148    * @param   node
   149    *          The node receiving the event
   150    * @param   event
   151    *          The event.
   152    */
   153   onEvent: function(node, event) {
   154     var numControllers = node.controllers.getControllerCount();
   155     var controller;
   157     for ( var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++ )
   158     {
   159       controller = node.controllers.getControllerAt(controllerIndex);
   160       if ( controller )
   161         controller.onEvent(event);
   162     }
   163   }
   164 };

mercurial