Writing the JavaScript code
The following example consists of two extension API functions,
commandButtons()
which is called from the
canAcceptCommand()
When a user clicks the Commands menu, Dreamweaver calls the
for each menu item to determine whether it should be enabled. If
the value
, Dreamweaver displays the menu item text as active or enabled. If
true
canAcceptCommand()
The first task in creating a command is to determine when the item should be active and when it
should be dimmed. In this example, the menu item is active when the user has selected text in
the document.
The first lines of
user's document and calling the
function retrieves the node that contains the selected text, followed by any children of the node,
as shown in the following code:
function canAcceptCommand(){
var theDOM = dw.getDocumentDOM(); // Get the DOM of the current document
var theSel = theDOM.getSelection(); // Get start and end of selection
var theSelNode = theDOM.getSelectedNode(); // Get the selected node
var theChildren = theSelNode.childNodes; // Get children of selected node
The last line checks to see if the selection or its first child is text and returns the result as a value of
or
:
true
false
return (theSel[0] != theSel[1] && (theSelNode.nodeType ==
Node.TEXT_NODE || theSelNode.hasChildNodes() && (theChildren[0].nodeType ==
Node.TEXT_NODE)));
The first part of the
anything in the document. The variable
ending offsets of the selection within the document. If the two values are not equal, content has
been selected. If the values in the two slots are equal, there is only an insertion point and nothing
has been selected.
140
Chapter 7: Commands
, which Dreamweaver calls, and one user-defined function,
commandButtons()
returns the value
canAcceptCommand()
getSelection()
statement (
return
function.
, Dreamweaver dims the menu item.
false
retrieve the selected text by retrieving the DOM for the
function on the document object. Next, the
theSel[0] != theSel[1]
is a two-slot array that holds the beginning and
theSel
canAcceptCommand()
changeCase()
canAcceptCommand()
canAcceptCommand()
) checks if the user has selected
and
function
returns
Need help?
Do you have a question about the DREAMWEAVER MX 2004-EXTENDING DREAMWEAVER and is the answer not in the manual?
Questions and answers