browser/metro/base/content/commandUtil.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/content/commandUtil.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +
     1.9 +/**
    1.10 + * Command Updater
    1.11 + */
    1.12 +var CommandUpdater = {
    1.13 +  /**
    1.14 +   * Gets a controller that can handle a particular command.
    1.15 +   * @param   command
    1.16 +   *          A command to locate a controller for, preferring controllers that
    1.17 +   *          show the command as enabled.
    1.18 +   * @returns In this order of precedence:
    1.19 +   *            - the first controller supporting the specified command
    1.20 +   *              associated with the focused element that advertises the
    1.21 +   *              command as ENABLED
    1.22 +   *            - the first controller supporting the specified command
    1.23 +   *              associated with the global window that advertises the
    1.24 +   *              command as ENABLED
    1.25 +   *            - the first controller supporting the specified command
    1.26 +   *              associated with the focused element
    1.27 +   *            - the first controller supporting the specified command
    1.28 +   *              associated with the global window
    1.29 +   */
    1.30 +  _getControllerForCommand: function(command) {
    1.31 +    try {
    1.32 +      var controller = top.document.commandDispatcher.getControllerForCommand(command);
    1.33 +      if (controller && controller.isCommandEnabled(command))
    1.34 +        return controller;
    1.35 +    }
    1.36 +    catch(e) {
    1.37 +    }
    1.38 +    var controllerCount = window.controllers.getControllerCount();
    1.39 +    for (var i = 0; i < controllerCount; ++i) {
    1.40 +      var current = window.controllers.getControllerAt(i);
    1.41 +      try {
    1.42 +        if (current.supportsCommand(command) && current.isCommandEnabled(command))
    1.43 +          return current;
    1.44 +      }
    1.45 +      catch (e) {
    1.46 +      }
    1.47 +    }
    1.48 +    return controller || window.controllers.getControllerForCommand(command);
    1.49 +  },
    1.50 +
    1.51 +  /**
    1.52 +   * Updates the state of a XUL <command> element for the specified command
    1.53 +   * depending on its state.
    1.54 +   * @param   command
    1.55 +   *          The name of the command to update the XUL <command> element for
    1.56 +   */
    1.57 +  updateCommand: function(command) {
    1.58 +    var enabled = false;
    1.59 +    try {
    1.60 +      var controller = this._getControllerForCommand(command);
    1.61 +      if (controller) {
    1.62 +        enabled = controller.isCommandEnabled(command);
    1.63 +      }
    1.64 +    }
    1.65 +    catch(ex) { }
    1.66 +
    1.67 +    this.enableCommand(command, enabled);
    1.68 +  },
    1.69 +
    1.70 +  /**
    1.71 +   * Updates the state of a XUL <command> element for the specified command
    1.72 +   * depending on its state.
    1.73 +   * @param   command
    1.74 +   *          The name of the command to update the XUL <command> element for
    1.75 +   */
    1.76 +  updateCommands: function(_commands) {
    1.77 +    var commands = _commands.split(",");
    1.78 +    for (var command in commands) {
    1.79 +      this.updateCommand(commands[command]);
    1.80 +    }
    1.81 +  },
    1.82 +
    1.83 +  /**
    1.84 +   * Enables or disables a XUL <command> element.
    1.85 +   * @param   command
    1.86 +   *          The name of the command to enable or disable
    1.87 +   * @param   enabled
    1.88 +   *          true if the command should be enabled, false otherwise.
    1.89 +   */
    1.90 +  enableCommand: function(command, enabled) {
    1.91 +    var element = document.getElementById(command);
    1.92 +    if (!element)
    1.93 +      return;
    1.94 +    if (enabled)
    1.95 +      element.removeAttribute("disabled");
    1.96 +    else
    1.97 +      element.setAttribute("disabled", "true");
    1.98 +  },
    1.99 +
   1.100 +  /**
   1.101 +   * Performs the action associated with a specified command using the most
   1.102 +   * relevant controller.
   1.103 +   * @param   command
   1.104 +   *          The command to perform.
   1.105 +   */
   1.106 +  doCommand: function(command) {
   1.107 +    var controller = this._getControllerForCommand(command);
   1.108 +    if (!controller)
   1.109 +      return;
   1.110 +    controller.doCommand(command);
   1.111 +  },
   1.112 +
   1.113 +  /**
   1.114 +   * Changes the label attribute for the specified command.
   1.115 +   * @param   command
   1.116 +   *          The command to update.
   1.117 +   * @param   labelAttribute
   1.118 +   *          The label value to use.
   1.119 +   */
   1.120 +  setMenuValue: function(command, labelAttribute) {
   1.121 +    var commandNode = top.document.getElementById(command);
   1.122 +    if (commandNode)
   1.123 +    {
   1.124 +      var label = commandNode.getAttribute(labelAttribute);
   1.125 +      if ( label )
   1.126 +        commandNode.setAttribute('label', label);
   1.127 +    }
   1.128 +  },
   1.129 +
   1.130 +  /**
   1.131 +   * Changes the accesskey attribute for the specified command.
   1.132 +   * @param   command
   1.133 +   *          The command to update.
   1.134 +   * @param   valueAttribute
   1.135 +   *          The value attribute to use.
   1.136 +   */
   1.137 +  setAccessKey: function(command, valueAttribute) {
   1.138 +    var commandNode = top.document.getElementById(command);
   1.139 +    if (commandNode)
   1.140 +    {
   1.141 +      var value = commandNode.getAttribute(valueAttribute);
   1.142 +      if ( value )
   1.143 +        commandNode.setAttribute('accesskey', value);
   1.144 +    }
   1.145 +  },
   1.146 +
   1.147 +  /**
   1.148 +   * Inform all the controllers attached to a node that an event has occurred
   1.149 +   * (e.g. the tree controllers need to be informed of blur events so that they can change some of the
   1.150 +   * menu items back to their default values)
   1.151 +   * @param   node
   1.152 +   *          The node receiving the event
   1.153 +   * @param   event
   1.154 +   *          The event.
   1.155 +   */
   1.156 +  onEvent: function(node, event) {
   1.157 +    var numControllers = node.controllers.getControllerCount();
   1.158 +    var controller;
   1.159 +
   1.160 +    for ( var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++ )
   1.161 +    {
   1.162 +      controller = node.controllers.getControllerAt(controllerIndex);
   1.163 +      if ( controller )
   1.164 +        controller.onEvent(event);
   1.165 +    }
   1.166 +  }
   1.167 +};

mercurial