browser/metro/base/content/commandUtil.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial