A simple menu command example, Create a menu command, Javascript code for the menu – Adobe Extending Dreamweaver CS4 User Manual
Page 159
153
EXTENDING DREAMWEAVER CS4
Menus and menu commands
12
The dialog box remains visible until one of the scripts in the menu commands file calls the
window.close()
function.
A simple menu command example
This simple menu command example shows how Undo and Redo menu commands might work. The Undo menu
command reverses the effect of a user’s editing operation, and the Redo item reverses an Undo operation and restores
the effect of the user’s last editing operation.
You implement this example by creating the menu commands, writing the JavaScript code, and placing the command
file in the Menu folder.
Create a menu command
To create a menu called MyMenu that contains the Undo and Redo menu commands, add the following HTML menu
tags. Add the tags after the last closing
in menus.xml.
The
key
attribute defines keyboard shortcuts that the user can type to load the menu command. The
file
attribute
specifies the name of the menu command file that Dreamweaver executes when Dreamweaver loads the command.
The value of the
arguments
attribute defines the arguments that Dreamweaver passes when it calls the
receiveArguments()
function.
The following figure shows these menu commands:
JavaScript code for the menu
When the user selects either Undo or Redo on the MyMenu menu, Dreamweaver calls the MyMenu.htm command
file, which is specified by the
file
attribute of the
menuitem
tag. Create the MyMenu.htm command file in the
Dreamweaver Configuration/Menus folder and add the three menu command API functions,
canAcceptCommand()
,
receiveArguments()
, and
setMenuText()
, to implement the logic associated with the Undo and Redo menu items.
The following sections describe these functions.
canAcceptCommand()
Dreamweaver calls the
canAcceptCommand()
function for each menu item in the MyMenu menu to determine
whether it should be enabled or disabled. In the MyMenu.htm file, the
canAcceptCommand()
function checks the
value of
arguments[0]
to determine whether Dreamweaver is processing a Redo menu item or an Undo menu item.
If the argument is
"undo"
, the
canAcceptCommand()
function calls the enabler function
dw.canUndo()
and returns
the returned value, which is either
true
or
false
. Likewise, if the argument is
"redo"
, the
canAcceptCommand()
function calls the enabler function
dw.canRedo()
and returns its value to Dreamweaver. If the
canAcceptCommand()
function returns the value
false
, Dreamweaver dims the menu item for which it called the function. The following
example shows the code for the
canAcceptCommand()
function: