michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * Command Updater michael@0: */ michael@0: var CommandUpdater = { michael@0: /** michael@0: * Gets a controller that can handle a particular command. michael@0: * @param command michael@0: * A command to locate a controller for, preferring controllers that michael@0: * show the command as enabled. michael@0: * @returns In this order of precedence: michael@0: * - the first controller supporting the specified command michael@0: * associated with the focused element that advertises the michael@0: * command as ENABLED michael@0: * - the first controller supporting the specified command michael@0: * associated with the global window that advertises the michael@0: * command as ENABLED michael@0: * - the first controller supporting the specified command michael@0: * associated with the focused element michael@0: * - the first controller supporting the specified command michael@0: * associated with the global window michael@0: */ michael@0: _getControllerForCommand: function(command) { michael@0: try { michael@0: var controller = top.document.commandDispatcher.getControllerForCommand(command); michael@0: if (controller && controller.isCommandEnabled(command)) michael@0: return controller; michael@0: } michael@0: catch(e) { michael@0: } michael@0: var controllerCount = window.controllers.getControllerCount(); michael@0: for (var i = 0; i < controllerCount; ++i) { michael@0: var current = window.controllers.getControllerAt(i); michael@0: try { michael@0: if (current.supportsCommand(command) && current.isCommandEnabled(command)) michael@0: return current; michael@0: } michael@0: catch (e) { michael@0: } michael@0: } michael@0: return controller || window.controllers.getControllerForCommand(command); michael@0: }, michael@0: michael@0: /** michael@0: * Updates the state of a XUL element for the specified command michael@0: * depending on its state. michael@0: * @param command michael@0: * The name of the command to update the XUL element for michael@0: */ michael@0: updateCommand: function(command) { michael@0: var enabled = false; michael@0: try { michael@0: var controller = this._getControllerForCommand(command); michael@0: if (controller) { michael@0: enabled = controller.isCommandEnabled(command); michael@0: } michael@0: } michael@0: catch(ex) { } michael@0: michael@0: this.enableCommand(command, enabled); michael@0: }, michael@0: michael@0: /** michael@0: * Updates the state of a XUL element for the specified command michael@0: * depending on its state. michael@0: * @param command michael@0: * The name of the command to update the XUL element for michael@0: */ michael@0: updateCommands: function(_commands) { michael@0: var commands = _commands.split(","); michael@0: for (var command in commands) { michael@0: this.updateCommand(commands[command]); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Enables or disables a XUL element. michael@0: * @param command michael@0: * The name of the command to enable or disable michael@0: * @param enabled michael@0: * true if the command should be enabled, false otherwise. michael@0: */ michael@0: enableCommand: function(command, enabled) { michael@0: var element = document.getElementById(command); michael@0: if (!element) michael@0: return; michael@0: if (enabled) michael@0: element.removeAttribute("disabled"); michael@0: else michael@0: element.setAttribute("disabled", "true"); michael@0: }, michael@0: michael@0: /** michael@0: * Performs the action associated with a specified command using the most michael@0: * relevant controller. michael@0: * @param command michael@0: * The command to perform. michael@0: */ michael@0: doCommand: function(command) { michael@0: var controller = this._getControllerForCommand(command); michael@0: if (!controller) michael@0: return; michael@0: controller.doCommand(command); michael@0: }, michael@0: michael@0: /** michael@0: * Changes the label attribute for the specified command. michael@0: * @param command michael@0: * The command to update. michael@0: * @param labelAttribute michael@0: * The label value to use. michael@0: */ michael@0: setMenuValue: function(command, labelAttribute) { michael@0: var commandNode = top.document.getElementById(command); michael@0: if (commandNode) michael@0: { michael@0: var label = commandNode.getAttribute(labelAttribute); michael@0: if ( label ) michael@0: commandNode.setAttribute('label', label); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Changes the accesskey attribute for the specified command. michael@0: * @param command michael@0: * The command to update. michael@0: * @param valueAttribute michael@0: * The value attribute to use. michael@0: */ michael@0: setAccessKey: function(command, valueAttribute) { michael@0: var commandNode = top.document.getElementById(command); michael@0: if (commandNode) michael@0: { michael@0: var value = commandNode.getAttribute(valueAttribute); michael@0: if ( value ) michael@0: commandNode.setAttribute('accesskey', value); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Inform all the controllers attached to a node that an event has occurred michael@0: * (e.g. the tree controllers need to be informed of blur events so that they can change some of the michael@0: * menu items back to their default values) michael@0: * @param node michael@0: * The node receiving the event michael@0: * @param event michael@0: * The event. michael@0: */ michael@0: onEvent: function(node, event) { michael@0: var numControllers = node.controllers.getControllerCount(); michael@0: var controller; michael@0: michael@0: for ( var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++ ) michael@0: { michael@0: controller = node.controllers.getControllerAt(controllerIndex); michael@0: if ( controller ) michael@0: controller.onEvent(event); michael@0: } michael@0: } michael@0: };