CommandManager

Description

Manages global application commands that can be called from menu items, key bindings, or subparts of the application.

This module dispatches these event(s):

  • commandRegistered -- when a new command is registered
  • beforeExecuteCommand -- before dispatching a command

Variables Summary

Functions Summary

Classes Summary

Variables

_commands

Map of all registered global commands

_commandsOriginal

Temporary copy of commands map for restoring after testing TODO (issue #1039): implement separate require contexts for unit tests

Functions

Public API

_testReset

Clear all commands for unit testing, but first make copy of commands so that they can be restored afterward

Public API

_testRestore

Restore original commands after test and release copy

Public API

doNothing

Blank handler

Public API

execute

Looks up and runs a global command. Additional arguments are passed to the command.

id string
The ID of the command to run.
Returns: $.Promise
a jQuery promise that will be resolved when the command completes.
Public API

get

Retrieves a Command object by id

id string
Returns: Command
Public API

getAll

Returns the ids of all registered commands

Returns: Array.<string>
Public API

register

Registers a global command.

name string
- text that will be displayed in the UI to represent command
id string
- unique identifier for command. Core commands in app use a simple command title as an id, for example "open.file". Extensions should use the following format: "author.myextension.mycommandname". For example, "lschmitt.csswizard.format.css".
commandFn function(...)
- the function to call when the command is executed. Any arguments passed to execute() (after the id) are passed as arguments to the function. If the function is asynchronous, it must return a jQuery promise that is resolved when the command completes. Otherwise, the CommandManager will assume it is synchronous, and return a promise that is already resolved.
Returns: ?Command
Public API

registerInternal

Registers a global internal only command.

id string
- unique identifier for command. Core commands in app use a simple command title as an id, for example "app.abort_quit". Extensions should use the following format: "author.myextension.mycommandname". For example, "lschmitt.csswizard.format.css".
commandFn function(...)
- the function to call when the command is executed. Any arguments passed to execute() (after the id) are passed as arguments to the function. If the function is asynchronous, it must return a jQuery promise that is resolved when the command completes. Otherwise, the CommandManager will assume it is synchronous, and return a promise that is already resolved.
Returns: ?Command

Classes

Constructor

Command

name string
- text that will be displayed in the UI to represent command
id string
commandFn function
- the function that is called when the command is executed. TODO: where should this be triggered, The Command or Exports? Events: enabledStateChange checkedStateChange visibleStateChange keyBindingAdded keyBindingRemoved
Methods

Methods

Public API

execute

Executes the command. Additional arguments are passed to the executing function

Returns: $.Promise
a jQuery promise that will be resolved when the command completes.

getChecked

Returns: boolean

getEnabled

Returns: boolean

getID

Returns: Command

getName

Returns: string

getVisible

Returns: boolean

setChecked

Sets enabled state of Command and dispatches "checkedStateChange" when the enabled state changes.

checked boolean

setEnabled

Sets enabled state of Command and dispatches "enabledStateChange" when the enabled state changes.

enabled boolean

setName

Sets the name of the Command and dispatches "nameChange" so that UI that reflects the command name can update.

Note, a Command name can appear in either HTML or native UI so HTML tags should not be used. To add a Unicode character, use \uXXXX instead of an HTML entity.

name string

setVisible

Sets visible state of Command and dispatches "visibleStateChange" when the visible state changes.

visible boolean